-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathdocumentSymbols.ts
More file actions
85 lines (76 loc) · 2.94 KB
/
documentSymbols.ts
File metadata and controls
85 lines (76 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { SymbolInformation, DocumentSymbol } from 'vscode-languageserver-types';
import { YAMLSchemaService } from './yamlSchemaService';
import { JSONDocumentSymbols } from 'vscode-json-languageservice/lib/umd/services/jsonDocumentSymbols';
import { DocumentSymbolsContext } from 'vscode-json-languageservice/lib/umd/jsonLanguageTypes';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { yamlDocumentsCache } from '../parser/yaml-documents';
import { Telemetry } from '../yamlLanguageService';
import { isMap, isSeq, Node } from 'yaml';
export class YAMLDocumentSymbols {
private jsonDocumentSymbols;
constructor(
schemaService: YAMLSchemaService,
private readonly telemetry?: Telemetry
) {
this.jsonDocumentSymbols = new JSONDocumentSymbols(schemaService);
// override 'getKeyLabel' to handle complex mapping
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.jsonDocumentSymbols.getKeyLabel = (property: any) => {
const keyNode: Node = property.keyNode.internalNode;
let name = '';
if (isMap(keyNode)) {
name = '{}';
} else if (isSeq(keyNode)) {
name = '[]';
} else {
name = keyNode.source;
}
return name;
};
}
public findDocumentSymbols(
document: TextDocument,
context: DocumentSymbolsContext = { resultLimit: Number.MAX_VALUE }
): SymbolInformation[] {
let results = [];
try {
const doc = yamlDocumentsCache.getYamlDocument(document);
if (!doc || doc['documents'].length === 0) {
return null;
}
for (const yamlDoc of doc['documents']) {
if (yamlDoc.root) {
results = results.concat(this.jsonDocumentSymbols.findDocumentSymbols(document, yamlDoc, context));
}
}
} catch (err) {
this.telemetry?.sendError('yaml.documentSymbols.error', err);
}
return results;
}
public findHierarchicalDocumentSymbols(
document: TextDocument,
context: DocumentSymbolsContext = { resultLimit: Number.MAX_VALUE }
): DocumentSymbol[] {
let results = [];
try {
const doc = yamlDocumentsCache.getYamlDocument(document);
if (!doc || doc['documents'].length === 0) {
return null;
}
for (const yamlDoc of doc['documents']) {
if (yamlDoc.root) {
results = results.concat(this.jsonDocumentSymbols.findDocumentSymbols2(document, yamlDoc, context));
}
}
} catch (err) {
this.telemetry?.sendError('yaml.hierarchicalDocumentSymbols.error', err);
}
return results;
}
}