Skip to content

Commit

Permalink
fix: address 'gf' bug. closes #3310
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoon committed Jan 20, 2019
1 parent f57e7fa commit 3ee835b
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions src/cmd_line/commands/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as vscode from 'vscode';
import { logger } from '../../util/logger';

const untildify = require('untildify');
const doesFileExist = util.promisify(fs.exists);

export enum FilePosition {
NewWindowVerticalSplit,
Expand All @@ -27,11 +28,13 @@ export class FileCommand extends node.CommandBase {
super();
this._name = 'file';
this._arguments = args;
this._arguments.name = <string>untildify(this.arguments.name);
}

get arguments(): IFileCommandArguments {
return this._arguments;
}

async execute(): Promise<void> {
if (this._arguments.bang) {
await vscode.commands.executeCommand('workbench.action.files.revert');
Expand Down Expand Up @@ -83,32 +86,38 @@ export class FileCommand extends node.CommandBase {
}
} else {
// Using a filename, open or create the file
this._arguments.name =
this._arguments.name.indexOf('file://') === 0
? this._arguments.name.slice(7)
: <string>untildify(this._arguments.name);
this._arguments.name = this._arguments.name.replace('^file://', '');

filePath = path.isAbsolute(this._arguments.name)
? this._arguments.name
? path.normalize(this._arguments.name)
: path.join(path.dirname(editorFilePath), this._arguments.name);

if (filePath !== editorFilePath && !(await util.promisify(fs.exists)(filePath))) {
// if file does not exist try to find it with the same extension
const pathWithExt = filePath + path.extname(editorFilePath);
if (await util.promisify(fs.exists)(pathWithExt)) {
filePath = pathWithExt;
// if there is no file with the same extension
// and the createFileIfNotExists is true then create one
} else if (this._arguments.createFileIfNotExists) {
await util.promisify(fs.close)(await util.promisify(fs.open)(filePath, 'w'));
} else {
logger.error(`file: ${filePath} does not exist.`);
return;
if (filePath !== editorFilePath) {
let fileExists = await doesFileExist(filePath);
if (!fileExists) {
// if file does not exist and does not have a file extension
// try to find it with the same extension as the current file
if (path.extname(filePath) === '') {
const pathWithExt = filePath + path.extname(editorFilePath);
fileExists = await doesFileExist(pathWithExt);
if (fileExists) {
filePath = pathWithExt;
}
}
}

if (!fileExists) {
if (this._arguments.createFileIfNotExists) {
await util.promisify(fs.close)(await util.promisify(fs.open)(filePath, 'w'));
} else {
logger.error(`file: ${filePath} does not exist.`);
return;
}
}
}
}

const doc = await vscode.workspace.openTextDocument(filePath);

vscode.window.showTextDocument(doc);

if (this.arguments.lineNumber) {
Expand Down

0 comments on commit 3ee835b

Please sign in to comment.