Skip to content

Commit 82b398f

Browse files
authored
Merge pull request #293 from evidolob/fix-symbols-for-complex-mapping
Fix document symbols computation if yaml has complex mappings
2 parents 6df2d61 + 80875c6 commit 82b398f

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/languageservice/services/documentSymbols.ts

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ export class YAMLDocumentSymbols {
1717

1818
constructor (schemaService: YAMLSchemaService) {
1919
this.jsonDocumentSymbols = new JSONDocumentSymbols(schemaService);
20+
const origKeyLabel = this.jsonDocumentSymbols.getKeyLabel;
21+
22+
// override 'getKeyLabel' to handle complex mapping
23+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
24+
this.jsonDocumentSymbols.getKeyLabel = (property: any) => {
25+
if (typeof property.keyNode.value === 'object') {
26+
return property.keyNode.value.value;
27+
} else {
28+
return origKeyLabel.call(this.jsonDocumentSymbols, property);
29+
}
30+
};
2031
}
2132

2233
public findDocumentSymbols (document: TextDocument): SymbolInformation[] {

test/documentSymbols.test.ts

+43-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Red Hat. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5-
import { setupTextDocument, TEST_URI, configureLanguageService } from './utils/testHelper';
5+
import { setupTextDocument, TEST_URI, configureLanguageService } from './utils/testHelper';
66
import { createExpectedSymbolInformation, createExpectedDocumentSymbol } from './utils/verifyError';
77
import { DocumentSymbol, SymbolKind } from 'vscode-languageserver-types';
88
import assert = require('assert');
@@ -16,7 +16,7 @@ suite('Document Symbols Tests', () => {
1616

1717
function parseNonHierarchicalSetup (content: string) {
1818
const testTextDocument = setupTextDocument(content);
19-
return languageService.findDocumentSymbols(testTextDocument );
19+
return languageService.findDocumentSymbols(testTextDocument);
2020
}
2121

2222
it('Document is empty', done => {
@@ -298,6 +298,47 @@ suite('Document Symbols Tests', () => {
298298
);
299299
});
300300

301+
it('Document Symbols with complex mapping and aliases', () => {
302+
const content = `
303+
version: 0.0.1
304+
structure:
305+
? &root root
306+
:
307+
element: div
308+
conditions:
309+
? *root
310+
:
311+
style:
312+
height: 41
313+
`;
314+
315+
const symbols = parseHierarchicalSetup(content);
316+
317+
assert.equal(symbols.length, 3);
318+
assert.deepEqual(
319+
symbols[0],
320+
createExpectedDocumentSymbol('version', SymbolKind.String, 1, 12, 1, 26, 1, 12, 1, 19)
321+
);
322+
323+
const element = createExpectedDocumentSymbol('element', SymbolKind.String, 5, 16, 5, 28, 5, 16, 5, 23, );
324+
const root1 = createExpectedDocumentSymbol('root', SymbolKind.Module, 3, 22, 5, 28, 3, 22, 3, 26,[element]);
325+
326+
const height = createExpectedDocumentSymbol('height', SymbolKind.Number, 10, 18, 10, 28, 10, 18, 10, 24);
327+
const style = createExpectedDocumentSymbol('style', SymbolKind.Module, 9, 16, 10, 28, 9, 16, 9, 21, [height]);
328+
const root2 = createExpectedDocumentSymbol('root', SymbolKind.Module, 7, 17, 10, 28, 7, 17, 7, 21, [style]);
329+
330+
assert.deepEqual(
331+
symbols[1],
332+
createExpectedDocumentSymbol('structure', SymbolKind.Module, 2, 12, 5, 28, 2, 12, 2, 21, [root1])
333+
);
334+
335+
assert.deepEqual(
336+
symbols[2],
337+
createExpectedDocumentSymbol('conditions', SymbolKind.Module, 6, 12, 10, 28, 6, 12, 6, 22, [root2])
338+
);
339+
340+
});
341+
301342
});
302343

303344
});

0 commit comments

Comments
 (0)