Skip to content

Commit b8f39c9

Browse files
Add commands to create new Ada files
These commands are displayed under VS Code's 'File->New File...' toplevel menu, providing file templates based on snippets for main and package units. For eng/ide/ada_language_server#1515
1 parent 5eabb82 commit b8f39c9

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed

integration/vscode/ada/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,16 @@
768768
}
769769
],
770770
"commands": [
771+
{
772+
"command": "ada.createNewAdaMainUnit",
773+
"title": "Ada: Create Main Unit",
774+
"shortTitle": "Ada Main Unit"
775+
},
776+
{
777+
"command": "ada.createNewAdaPackage",
778+
"title": "Ada: Create Package",
779+
"shortTitle": "Ada Package"
780+
},
771781
{
772782
"command": "ada.otherFile",
773783
"title": "Ada: Go to other file"
@@ -908,6 +918,16 @@
908918
"when": "editorLangId == ada",
909919
"group": "navigation@1"
910920
}
921+
],
922+
"file/newFile": [
923+
{
924+
"command": "ada.createNewAdaMainUnit",
925+
"group": "ada"
926+
},
927+
{
928+
"command": "ada.createNewAdaPackage",
929+
"group": "ada"
930+
}
911931
]
912932
},
913933
"configurationDefaults": {

integration/vscode/ada/src/ExtensionState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ export class ExtensionState {
393393

394394
/**
395395
*
396-
* @returns the list of source directorues defined in the project loaded by the ALS
396+
* @returns the list of source directories defined in the project loaded by the ALS
397397
*/
398398
public async getSourceDirs(): Promise<ALSSourceDirDescription[]> {
399399
if (this.cachedSourceDirs === undefined) {

integration/vscode/ada/src/commands.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ export function registerCommands(context: vscode.ExtensionContext, clients: Exte
8080
vscode.commands.registerCommand('ada.walkthroughStartDebugging', walkthroughStartDebugging),
8181
);
8282
context.subscriptions.push(vscode.commands.registerCommand('ada.otherFile', otherFileHandler));
83+
context.subscriptions.push(
84+
vscode.commands.registerCommand('ada.createNewAdaMainUnit', () =>
85+
createNewAdaFile('Main Procedure'),
86+
),
87+
);
88+
context.subscriptions.push(
89+
vscode.commands.registerCommand('ada.createNewAdaPackage', () =>
90+
createNewAdaFile('Package Declaration or Body'),
91+
),
92+
);
8393
context.subscriptions.push(
8494
vscode.commands.registerCommand('ada.subprogramBox', addSubprogramBoxCommand),
8595
);
@@ -429,6 +439,22 @@ async function buildAndRunMainAsk() {
429439
}
430440
}
431441

442+
/**
443+
* Handler for commands that create new Ada files.
444+
* This function creates a new Ada editor, focus it, and insert the specified snippet.
445+
* Used to proivide Ada file templates.
446+
*
447+
* @param snippetName - the name of the snippet to insert in the newly created editor.
448+
*/
449+
async function createNewAdaFile(snippetName: string) {
450+
const doc = await vscode.workspace.openTextDocument({ language: 'ada' });
451+
await vscode.window.showTextDocument(doc);
452+
await vscode.commands.executeCommand('editor.action.insertSnippet', {
453+
langId: 'ada',
454+
name: snippetName,
455+
});
456+
}
457+
432458
// Take active editor URI and call execute 'als-other-file' command in LSP
433459
const otherFileHandler = () => {
434460
const activeEditor = vscode.window.activeTextEditor;
@@ -458,11 +484,6 @@ const otherFileHandler = () => {
458484
* when missing directories
459485
*/
460486
export async function checkSrcDirectories(atStartup = false, displayYesNoPopup = true) {
461-
type ALSSourceDirDescription = {
462-
name: string;
463-
uri: string;
464-
};
465-
466487
const foldersInSettings = vscode.workspace.getConfiguration().get('folders');
467488
const doNotShowAgainKey = 'ada.addMissingDirsToWorkspace.doNotShowAgain';
468489
const doNotShowAgain = adaExtState.context.workspaceState.get(doNotShowAgainKey);

integration/vscode/ada/test/general/extension.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,45 @@ suite('Extensions Test Suite', function () {
6868
throw new Error('No workspace folder found for the specified URI');
6969
}
7070
});
71+
72+
test('New File - Ada Main', async () => {
73+
if (vscode.workspace.workspaceFolders !== undefined) {
74+
await vscode.commands.executeCommand('ada.createNewAdaMainUnit');
75+
const activeEditor = vscode.window.activeTextEditor;
76+
assert.strictEqual(
77+
activeEditor?.document.languageId,
78+
'ada',
79+
'We should have a newly created Ada editor',
80+
);
81+
const text = activeEditor.document.getText() ?? '';
82+
assert.strictEqual(
83+
text.startsWith('procedure'),
84+
true,
85+
`We should have a main procedure snippet at the beginning ` +
86+
`of the new editor, but instead we have:\n\n${text}`,
87+
);
88+
}
89+
});
90+
91+
test('New File - Ada Package', async () => {
92+
if (vscode.workspace.workspaceFolders !== undefined) {
93+
await vscode.commands.executeCommand('ada.createNewAdaPackage');
94+
const activeEditor = vscode.window.activeTextEditor;
95+
assert.strictEqual(
96+
activeEditor?.document.languageId,
97+
'ada',
98+
'We should have a newly created Ada editor',
99+
);
100+
const text = activeEditor.document.getText() ?? '';
101+
assert.strictEqual(
102+
text.startsWith('package'),
103+
true,
104+
`We should have a package declaration snippet at the beginning ` +
105+
`of the new editor, but instead we have:\n\n${text}`,
106+
);
107+
}
108+
});
109+
71110
test('Clear Cache On Project Reload', async () => {
72111
if (vscode.workspace.workspaceFolders !== undefined) {
73112
// Get the workspace root folder

0 commit comments

Comments
 (0)