Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: files with extensions not being auto-created. closes #2923. #2928

Merged
merged 3 commits into from
Aug 3, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: files with extensions not being auto-created. closes #2923.
  • Loading branch information
jpoon committed Aug 2, 2018
commit dc49c05f5bf636d42341d82f8f56036d6b79f8b8
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)
)
);
}
}
}