Skip to content

Commit

Permalink
fix: files with extensions not being auto-created. closes #2923.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoon committed Aug 2, 2018
1 parent 6a21390 commit dc49c05
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions src/cmd_line/commands/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class FileCommand extends node.CommandBase {
await vscode.commands.executeCommand('workbench.action.files.revert');
return;
}

if (this._arguments.name === undefined) {
// Open an empty file
if (this._arguments.position === FilePosition.CurrentWindow) {
Expand All @@ -75,11 +76,14 @@ export class FileCommand extends node.CommandBase {
await vscode.commands.executeCommand('workbench.action.closeOtherEditors');
}
return;
} else if (this._arguments.name === '') {
}

if (this._arguments.name === '') {
if (this._arguments.position === FilePosition.NewWindow) {
await vscode.commands.executeCommand('workbench.action.splitEditor');
return;
}

const fileList = await vscode.window.showOpenDialog({});
if (fileList) {
const doc = await vscode.workspace.openTextDocument(fileList[0]);
Expand All @@ -88,49 +92,49 @@ export class FileCommand extends node.CommandBase {
return;
}

let editorFilePath = vscode.window.activeTextEditor!.document.uri.fsPath;
this._arguments.name = <string>untildify(this._arguments.name);
let filePath = path.isAbsolute(this._arguments.name)
? this._arguments.name
: path.join(path.dirname(editorFilePath), this._arguments.name);

if (filePath !== editorFilePath) {
let filePath = this._arguments.name;
if (!path.isAbsolute(this._arguments.name)) {
let curFilePath = vscode.window.activeTextEditor!.document.uri.fsPath;
filePath = path.join(path.dirname(curFilePath), this._arguments.name);

if (!fs.existsSync(filePath)) {
// if file does not exist and does not have an extension
// try to find it with the same extension
if (path.extname(filePath) === '') {
const pathWithExt = filePath + path.extname(editorFilePath);
const pathWithExt = filePath + path.extname(curFilePath);
if (fs.existsSync(pathWithExt)) {
filePath = pathWithExt;
} else {
// create file
if (this.arguments.createFileIfNotExists) {
fs.closeSync(fs.openSync(filePath, 'w'));
} else {
Message.ShowError('The file ' + filePath + ' does not exist.');
return;
}
}
}
}
}

let folder = vscode.Uri.file(filePath);
await vscode.commands.executeCommand(
'vscode.open',
folder,
this._arguments.position === FilePosition.NewWindow
? this.getViewColumnToRight()
: this.getActiveViewColumn()
);
// create file
if (this.arguments.createFileIfNotExists) {
fs.closeSync(fs.openSync(filePath, 'w'));
} else {
Message.ShowError('The file ' + filePath + ' does not exist.');
return;
}

if (this.arguments.lineNumber) {
vscode.window.activeTextEditor!.revealRange(
new vscode.Range(
new vscode.Position(this.arguments.lineNumber, 0),
new vscode.Position(this.arguments.lineNumber, 0)
)
);
}
let folder = vscode.Uri.file(filePath);
await vscode.commands.executeCommand(
'vscode.open',
folder,
this._arguments.position === FilePosition.NewWindow
? this.getViewColumnToRight()
: this.getActiveViewColumn()
);

if (this.arguments.lineNumber) {
vscode.window.activeTextEditor!.revealRange(
new vscode.Range(
new vscode.Position(this.arguments.lineNumber, 0),
new vscode.Position(this.arguments.lineNumber, 0)
)
);
}
}
}

0 comments on commit dc49c05

Please sign in to comment.