Skip to content

Commit bb44aec

Browse files
Fix 'Add subprogram box' command
We were not using the right SymbolKind enumeration. A test for this command has also been added.
1 parent 83ce4fe commit bb44aec

File tree

5 files changed

+105
-39
lines changed

5 files changed

+105
-39
lines changed

integration/vscode/ada/src/extension.ts

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import {
2424
LanguageClientOptions,
2525
Middleware,
2626
ServerOptions,
27-
SymbolKind,
2827
} from 'vscode-languageclient/node';
28+
import { SymbolKind } from 'vscode';
2929
import { ALSClientFeatures } from './alsClientFeatures';
3030
import { alsCommandExecutor } from './alsExecuteCommand';
3131
import GnatTaskProvider, { getEnclosingSymbol } from './gnatTaskProvider';
@@ -274,44 +274,45 @@ function createClient(
274274
async function addSupbrogramBox() {
275275
const activeEditor = vscode.window.activeTextEditor;
276276

277-
await getEnclosingSymbol(activeEditor, [SymbolKind.Function, SymbolKind.Module]).then(
278-
async (symbol) => {
279-
if (symbol !== null) {
280-
const name: string = symbol.name ?? '';
281-
const insertPos = new vscode.Position(symbol.range.start.line, 0);
282-
const indentationRange = new vscode.Range(insertPos, symbol.range.start);
283-
const indentation: string = activeEditor?.document.getText(indentationRange) ?? '';
284-
const eol: string =
285-
activeEditor?.document.eol == vscode.EndOfLine.CRLF ? '\r\n' : '\n';
286-
287-
// Generate the subprogram box after retrieving the indentation of the line of
288-
// the subprogram's body declaration.
289-
const text: string =
290-
indentation +
291-
'---' +
292-
'-'.repeat(name.length) +
293-
'---' +
294-
eol +
295-
indentation +
296-
'-- ' +
297-
name +
298-
' --' +
299-
eol +
300-
indentation +
301-
'---' +
302-
'-'.repeat(name.length) +
303-
'---' +
304-
eol +
305-
eol;
306-
307-
if (activeEditor) {
308-
await activeEditor.edit((editBuilder) => {
309-
editBuilder.insert(insertPos, text);
310-
});
311-
}
277+
await getEnclosingSymbol(activeEditor, [
278+
SymbolKind.Function,
279+
SymbolKind.Package,
280+
SymbolKind.Module,
281+
]).then(async (symbol) => {
282+
if (symbol !== null) {
283+
const name: string = symbol.name ?? '';
284+
const insertPos = new vscode.Position(symbol.range.start.line, 0);
285+
const indentationRange = new vscode.Range(insertPos, symbol.range.start);
286+
const indentation: string = activeEditor?.document.getText(indentationRange) ?? '';
287+
const eol: string = activeEditor?.document.eol == vscode.EndOfLine.CRLF ? '\r\n' : '\n';
288+
289+
// Generate the subprogram box after retrieving the indentation of the line of
290+
// the subprogram's body declaration.
291+
const text: string =
292+
indentation +
293+
'---' +
294+
'-'.repeat(name.length) +
295+
'---' +
296+
eol +
297+
indentation +
298+
'-- ' +
299+
name +
300+
' --' +
301+
eol +
302+
indentation +
303+
'---' +
304+
'-'.repeat(name.length) +
305+
'---' +
306+
eol +
307+
eol;
308+
309+
if (activeEditor) {
310+
await activeEditor.edit((editBuilder) => {
311+
editBuilder.insert(insertPos, text);
312+
});
312313
}
313314
}
314-
);
315+
});
315316
}
316317

317318
type ALSSourceDirDescription = {

integration/vscode/ada/src/gnatTaskProvider.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ export async function getEnclosingSymbol(
309309
const getAllSymbols = (symbols: vscode.DocumentSymbol[]) => {
310310
let sym;
311311
for (sym of symbols) {
312-
if (sym.kind in kinds) {
312+
if (kinds.includes(sym.kind)) {
313313
filtered_symbols.push(sym);
314314
}
315315
if (sym.kind == SymbolKind.Function || sym.kind == SymbolKind.Module) {
@@ -324,11 +324,13 @@ export async function getEnclosingSymbol(
324324
const scopeSymbols = filtered_symbols.filter(
325325
(sym) => line >= sym.range.start.line && line <= sym.range.end.line
326326
);
327+
327328
if (scopeSymbols.length > 0) {
328329
scopeSymbols.sort(
329330
(a, b) =>
330331
a.range.end.line - a.range.start.line - (b.range.end.line - b.range.start.line)
331332
);
333+
332334
return scopeSymbols[0];
333335
}
334336
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
procedure Test_Subprogram_Box is
2+
3+
procedure Do_Nothing is
4+
begin
5+
null;
6+
end Do_Nothing;
7+
8+
task body T is
9+
begin
10+
null;
11+
end T;
12+
begin
13+
null;
14+
end Test_Subprogram_Box;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-------------------------
2+
-- Test_Subprogram_Box --
3+
-------------------------
4+
5+
procedure Test_Subprogram_Box is
6+
7+
----------------
8+
-- Do_Nothing --
9+
----------------
10+
11+
procedure Do_Nothing is
12+
begin
13+
null;
14+
end Do_Nothing;
15+
16+
-------
17+
-- T --
18+
-------
19+
20+
task body T is
21+
begin
22+
null;
23+
end T;
24+
begin
25+
null;
26+
end Test_Subprogram_Box;

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as assert from 'assert';
22
import { before } from 'mocha';
33
import { contextClients } from '../../src/extension';
44
import { GlsMainResult, GlsExecutableResult } from '../../src/gprTaskProvider';
5-
5+
import { assertEqualToFileContent } from './utils';
66
import * as vscode from 'vscode';
77

88
suite('Extension Tasks Test Suite', () => {
@@ -45,4 +45,27 @@ suite('Extension Tasks Test Suite', () => {
4545
throw new Error('No workspace folder found for the specified URI');
4646
}
4747
});
48+
test('Test Add Subprogram Box', async () => {
49+
if (vscode.workspace.workspaceFolders !== undefined) {
50+
await contextClients.adaClient.onReady();
51+
const cursorPositions : vscode.Position[] =
52+
[new vscode.Position (9, 1), new vscode.Position (4, 1), new vscode.Position (1, 1)];
53+
const folder = vscode.workspace.workspaceFolders[0].uri;
54+
const fileUri = vscode.Uri.joinPath(folder, 'src', 'test_subprogram_box.adb');
55+
const expectedUri = vscode.Uri.joinPath(
56+
folder, 'src', 'test_subprogram_box.expected');
57+
58+
for (let cursorPos of cursorPositions) {
59+
await vscode.window.showTextDocument(fileUri,
60+
{selection: new vscode.Range(cursorPos, cursorPos)});
61+
await vscode.commands.executeCommand('ada.subprogramBox');
62+
}
63+
64+
const editorText = vscode.window.activeTextEditor?.document.getText()?? "";
65+
66+
assertEqualToFileContent(editorText, expectedUri);
67+
} else {
68+
throw new Error('No workspace folder found for the specified URI');
69+
}
70+
});
4871
});

0 commit comments

Comments
 (0)