@@ -7,29 +7,34 @@ import { DefinitionParams, LocationLink, Range } from 'vscode-languageserver-pro
7
7
import { TextDocument } from 'vscode-languageserver-textdocument' ;
8
8
import { DefinitionLink } from 'vscode-languageserver-types' ;
9
9
import { isAlias } from 'yaml' ;
10
+ import { Telemetry } from '../../languageserver/telemetry' ;
10
11
import { yamlDocumentsCache } from '../parser/yaml-documents' ;
11
12
import { matchOffsetToDocument } from '../utils/arrUtils' ;
12
13
import { TextBuffer } from '../utils/textBuffer' ;
13
14
14
- export function getDefinition ( document : TextDocument , params : DefinitionParams ) : DefinitionLink [ ] | undefined {
15
- try {
16
- const yamlDocument = yamlDocumentsCache . getYamlDocument ( document ) ;
17
- const offset = document . offsetAt ( params . position ) ;
18
- const currentDoc = matchOffsetToDocument ( offset , yamlDocument ) ;
19
- if ( currentDoc ) {
20
- const [ node ] = currentDoc . getNodeFromPosition ( offset , new TextBuffer ( document ) ) ;
21
- if ( node && isAlias ( node ) ) {
22
- const defNode = node . resolve ( currentDoc . internalDocument ) ;
23
- if ( defNode && defNode . range ) {
24
- const targetRange = Range . create ( document . positionAt ( defNode . range [ 0 ] ) , document . positionAt ( defNode . range [ 2 ] ) ) ;
25
- const selectionRange = Range . create ( document . positionAt ( defNode . range [ 0 ] ) , document . positionAt ( defNode . range [ 1 ] ) ) ;
26
- return [ LocationLink . create ( document . uri , targetRange , selectionRange ) ] ;
15
+ export class YamlDefinition {
16
+ constructor ( private readonly telemetry : Telemetry ) { }
17
+
18
+ getDefinition ( document : TextDocument , params : DefinitionParams ) : DefinitionLink [ ] | undefined {
19
+ try {
20
+ const yamlDocument = yamlDocumentsCache . getYamlDocument ( document ) ;
21
+ const offset = document . offsetAt ( params . position ) ;
22
+ const currentDoc = matchOffsetToDocument ( offset , yamlDocument ) ;
23
+ if ( currentDoc ) {
24
+ const [ node ] = currentDoc . getNodeFromPosition ( offset , new TextBuffer ( document ) ) ;
25
+ if ( node && isAlias ( node ) ) {
26
+ const defNode = node . resolve ( currentDoc . internalDocument ) ;
27
+ if ( defNode && defNode . range ) {
28
+ const targetRange = Range . create ( document . positionAt ( defNode . range [ 0 ] ) , document . positionAt ( defNode . range [ 2 ] ) ) ;
29
+ const selectionRange = Range . create ( document . positionAt ( defNode . range [ 0 ] ) , document . positionAt ( defNode . range [ 1 ] ) ) ;
30
+ return [ LocationLink . create ( document . uri , targetRange , selectionRange ) ] ;
31
+ }
27
32
}
28
33
}
34
+ } catch ( err ) {
35
+ this . telemetry . sendError ( 'yaml.definition.error' , { error : err . toString ( ) } ) ;
29
36
}
30
- } catch ( err ) {
31
- this . telemetry . sendError ( 'yaml.definition.error' , { error : err . toString ( ) } ) ;
32
- }
33
37
34
- return undefined ;
38
+ return undefined ;
39
+ }
35
40
}
0 commit comments