Skip to content

Commit 330fc4c

Browse files
committed
feat(vscode): added "File > New File" menu support and context menu option for creating Robot files in the file explorer
closes #259
1 parent b3c1ce2 commit 330fc4c

File tree

5 files changed

+102
-8
lines changed

5 files changed

+102
-8
lines changed

package.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
}
6262
},
6363
"activationEvents": [
64-
"workspaceContains:**/*.{robot,resource}",
64+
"workspaceContains:**/*.{robot,resource,robotrepl,robotscript}",
65+
"workspaceContains:robot.toml",
66+
"workspaceContains:.robot.toml",
6567
"onDebug",
6668
"onDebugResolve:robotcode",
6769
"onDebugInitialConfigurations",
@@ -1333,19 +1335,26 @@
13331335
"icon": "$(book)"
13341336
},
13351337
{
1336-
"title": "Create New File",
1338+
"title": "New Robot Framework File",
13371339
"shortTitle": "Robot Framework File",
13381340
"category": "RobotCode",
13391341
"command": "robotcode.createNewFile"
13401342
},
13411343
{
1342-
"title": "Create New Robot Framework Notebook",
1344+
"title": "New Robot Framework Notebook",
13431345
"shortTitle": "Robot Framework Notebook",
13441346
"category": "RobotCode",
13451347
"command": "robotcode.createNewNotebook"
13461348
}
13471349
],
13481350
"menus": {
1351+
"explorer/context": [
1352+
{
1353+
"command": "robotcode.createNewFile",
1354+
"group": "1_robotcode@1",
1355+
"when": "!virtualWorkspace && explorerResourceIsFolder"
1356+
}
1357+
],
13491358
"editor/title/run": [
13501359
{
13511360
"command": "robotcode.runCurrentFile",
@@ -1925,4 +1934,4 @@
19251934
"workspaces": [
19261935
"docs"
19271936
]
1928-
}
1937+
}

packages/core/src/robotcode/core/uri.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def _to_path_str(self) -> str:
121121
netloc = parse.unquote(self.netloc)
122122
path = parse.unquote(self.path)
123123

124-
if self._parts.scheme != "file":
124+
if self._parts.scheme not in ["file", "untitled"]:
125125
raise InvalidUriError(f"Invalid URI scheme '{self!s}'.")
126126

127127
if netloc and self._parts.scheme == "file":

packages/robot/src/robotcode/robot/diagnostics/document_cache_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def get_model(self, document: TextDocument, data_only: bool = True) -> ast.AST:
338338
if document_type == DocumentType.RESOURCE:
339339
return self.get_resource_model(document, data_only)
340340

341-
raise UnknownFileTypeError(f"Unknown file type '{document.uri}'.")
341+
return self.get_general_model(document, data_only)
342342

343343
def __get_model(
344344
self,

vscode-client/extension/languageToolsManager.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ export class LanguageToolsManager {
212212
this.pythonVersion,
213213
this.robocopVersion,
214214
this.tidyVersion,
215+
vscode.commands.registerCommand("robotcode.createNewFile", LanguageToolsManager.createNewFile),
215216
vscode.commands.registerCommand(
216217
"robotcode.startTerminalRepl",
217218
async (folder: vscode.WorkspaceFolder | undefined): Promise<void> => {
@@ -443,6 +444,84 @@ export class LanguageToolsManager {
443444
}, 500);
444445
}
445446

447+
static async createNewFile(destinationFolder?: vscode.Uri): Promise<vscode.TextDocument | undefined> {
448+
let showSaveDialog = false;
449+
450+
if (destinationFolder === undefined) {
451+
let folder: vscode.WorkspaceFolder | undefined = undefined;
452+
if (vscode.workspace.workspaceFolders?.length == 1) {
453+
folder = vscode.workspace.workspaceFolders[0];
454+
} else {
455+
folder = await vscode.window.showWorkspaceFolderPick({
456+
placeHolder: "Select a workspace folder to create the file in",
457+
});
458+
}
459+
if (folder === undefined) {
460+
return undefined;
461+
}
462+
463+
destinationFolder = folder.uri;
464+
showSaveDialog = true;
465+
}
466+
467+
const type = (
468+
await vscode.window.showQuickPick(
469+
[
470+
{ description: "Robot Framework suite file", label: ".robot", picked: true, type: "robot" },
471+
{ description: "Robot Framework resource file", label: ".resource", type: "resource" },
472+
{
473+
description: "Robot Framework suite init file",
474+
label: "__init__.robot",
475+
type: "init",
476+
},
477+
],
478+
{ title: "Select a Robot Framework file type", placeHolder: "Select a file type" },
479+
)
480+
)?.type;
481+
482+
if (type === undefined) {
483+
return undefined;
484+
}
485+
486+
let uri: vscode.Uri | undefined = undefined;
487+
488+
if (type === "init") {
489+
uri = vscode.Uri.joinPath(destinationFolder, `__init__.robot`);
490+
} else {
491+
uri = vscode.Uri.joinPath(destinationFolder, `newfile.${type}`);
492+
let i = 1;
493+
while (await fs.pathExists(uri.fsPath)) {
494+
uri = vscode.Uri.joinPath(destinationFolder, `newfile-${i++}.${type}`);
495+
}
496+
}
497+
498+
if (showSaveDialog) {
499+
uri = await vscode.window.showSaveDialog({
500+
defaultUri: uri,
501+
filters: { "Robot Framework": ["robot", "resource"] },
502+
title: "Create new Robot Framework file",
503+
});
504+
505+
if (uri === undefined) {
506+
return;
507+
}
508+
}
509+
510+
await fs.ensureFile(uri.fsPath);
511+
512+
const doc = await vscode.workspace.openTextDocument(uri);
513+
514+
await vscode.window.showTextDocument(doc);
515+
516+
if (!showSaveDialog && type !== "init") {
517+
await vscode.commands.executeCommand("workbench.files.action.focusFilesExplorer");
518+
await vscode.commands.executeCommand("revealInExplorer", uri);
519+
await vscode.commands.executeCommand("renameFile", uri);
520+
}
521+
522+
return doc;
523+
}
524+
446525
private removeFolder(folder: vscode.WorkspaceFolder) {
447526
if (this._workspaceFolderLanguageStatuses.has(folder)) {
448527
this._workspaceFolderLanguageStatuses.delete(folder);

vscode-client/extension/languageclientsmanger.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,14 @@ export class LanguageClientsManager {
547547
documentSelector:
548548
// TODO: use SUPPORTED_LANGUAGES here
549549
vscode.workspace.workspaceFolders?.length === 1
550-
? [{ scheme: "file", language: "robotframework" }]
551-
: [{ scheme: "file", language: "robotframework", pattern: `${workspaceFolder.uri.fsPath}/**/*` }],
550+
? [
551+
{ scheme: "file", language: "robotframework" },
552+
{ scheme: "untitled", language: "robotframework" },
553+
]
554+
: [
555+
{ scheme: "file", language: "robotframework", pattern: `${workspaceFolder.uri.fsPath}/**/*` },
556+
{ scheme: "untitled", language: "robotframework", pattern: `${workspaceFolder.uri.fsPath}/**/*` },
557+
],
552558
synchronize: {
553559
configurationSection: [CONFIG_SECTION],
554560
},

0 commit comments

Comments
 (0)