1
- import { CompletionItemKind , DefinitionParams } from "vscode-languageserver" ;
1
+ import { CompletionItemKind , DefinitionParams , Position } from "vscode-languageserver" ;
2
+ import { TextDocument } from "vscode-languageserver-textdocument" ;
2
3
3
4
import type { OwnedComplexTokens , OwnedStructComplexTokens } from "../Documents/Document" ;
4
5
import type { ServerManager } from "../ServerManager" ;
5
6
import type { ComplexToken } from "../Tokenizer/types" ;
6
7
import { TokenizedScope } from "../Tokenizer/Tokenizer" ;
8
+ import { Document } from "../Documents" ;
7
9
import Provider from "./Provider" ;
8
10
9
11
export default class GotoDefinitionProvider extends Provider {
@@ -22,98 +24,91 @@ export default class GotoDefinitionProvider extends Provider {
22
24
23
25
const liveDocument = this . server . liveDocumentsManager . get ( uri ) ;
24
26
const document = this . server . documentsCollection . getFromUri ( uri ) ;
27
+ if ( ! liveDocument || ! document ) return ;
25
28
26
- if ( liveDocument && this . server . tokenizer ) {
27
- let token : ComplexToken | undefined ;
28
- let ref : OwnedComplexTokens | OwnedStructComplexTokens | undefined ;
29
- const { tokenType, structVariableIdentifier, identifier } = this . server . tokenizer . findActionTargetAtPosition (
30
- liveDocument . getText ( ) ,
31
- position ,
32
- ) ;
29
+ const [ token , ref ] = this . resolveTokenAndRef ( position , document , liveDocument ) ;
33
30
34
- const localScope = this . server . tokenizer ?. tokenizeContent ( liveDocument . getText ( ) , TokenizedScope . local , 0 , position . line ) ;
35
-
36
- if ( ! tokenType ) {
37
- token = localScope ?. functionVariablesComplexTokens . find ( ( token ) => token . identifier === identifier ) ;
38
- }
39
-
40
- if ( ! token && tokenType === CompletionItemKind . Function ) {
41
- token = localScope ?. functionsComplexTokens . find ( ( token ) => token . identifier === identifier ) ;
31
+ if ( token ) {
32
+ if ( ref && ! ref . owner ) {
33
+ return {
34
+ uri,
35
+ range : {
36
+ start : { line : position . line , character : position . character } ,
37
+ end : { line : position . line , character : position . character } ,
38
+ } ,
39
+ } ;
42
40
}
43
41
44
- if ( document ) {
45
- if ( tokenType === CompletionItemKind . Property && structVariableIdentifier ) {
46
- const structIdentifer = localScope ?. functionVariablesComplexTokens . find (
47
- ( token ) => token . identifier === structVariableIdentifier ,
48
- ) ?. valueType ;
49
-
50
- const tokensWithRef = document . getGlobalStructComplexTokensWithRef ( ) ;
51
- for ( let i = 0 ; i < tokensWithRef . length ; i ++ ) {
52
- ref = tokensWithRef [ i ] ;
53
-
54
- token = ( ref as OwnedStructComplexTokens ) . tokens
55
- . find ( ( token ) => token . identifier === structIdentifer )
56
- ?. properties . find ( ( property ) => property . identifier === identifier ) ;
42
+ return {
43
+ uri : ref ? ref . owner || "" : uri ,
44
+ range : {
45
+ start : { line : token . position . line , character : token . position . character } ,
46
+ end : { line : token . position . line , character : token . position . character } ,
47
+ } ,
48
+ } ;
49
+ }
50
+ } ;
51
+ }
57
52
58
- if ( token ) {
59
- break ;
60
- }
61
- }
62
- }
53
+ private resolveTokenAndRef ( position : Position , document : Document , liveDocument : TextDocument ) : [ token : ComplexToken | undefined , ref : OwnedComplexTokens | OwnedStructComplexTokens | undefined ] {
54
+ let tokensWithRef ;
55
+ let token : ComplexToken | undefined ;
56
+ let ref : OwnedComplexTokens | OwnedStructComplexTokens | undefined ;
63
57
64
- if ( ! token && tokenType === CompletionItemKind . Struct ) {
65
- const tokensWithRef = document . getGlobalStructComplexTokensWithRef ( ) ;
66
- for ( let i = 0 ; i < tokensWithRef . length ; i ++ ) {
67
- ref = tokensWithRef [ i ] ;
58
+ const { tokenType, structVariableIdentifier, identifier } = this . server . tokenizer . findActionTargetAtPosition ( liveDocument . getText ( ) , position ) ;
59
+ const localScope = this . server . tokenizer . tokenizeContent ( liveDocument . getText ( ) , TokenizedScope . local , 0 , position . line ) ;
68
60
69
- token = ref . tokens . find ( ( token ) => token . identifier === identifier ) ;
70
- if ( token ) {
71
- break ;
72
- }
73
- }
74
- }
61
+ switch ( tokenType ) {
62
+ case CompletionItemKind . Function :
63
+ case CompletionItemKind . Constant :
64
+ token = localScope . functionsComplexTokens . find ( ( candidate ) => candidate . identifier === identifier ) ;
65
+ if ( token ) break ;
75
66
76
- if ( ! token && ( tokenType === CompletionItemKind . Constant || tokenType === CompletionItemKind . Function ) ) {
77
- const localStandardLibDefinitions = this . server . documentsCollection . get ( "nwscript" ) ;
78
- const tokensWithRef = document . getGlobalComplexTokensWithRef ( ) ;
67
+ const localStandardLibDefinitions = this . server . documentsCollection . get ( "nwscript" ) ;
68
+ tokensWithRef = document . getGlobalComplexTokensWithRef ( ) ;
79
69
80
- if ( localStandardLibDefinitions ) {
81
- tokensWithRef . push ( { owner : localStandardLibDefinitions ?. uri , tokens : localStandardLibDefinitions ?. complexTokens } ) ;
82
- }
70
+ if ( localStandardLibDefinitions ) {
71
+ tokensWithRef . push ( { owner : localStandardLibDefinitions ?. uri , tokens : localStandardLibDefinitions ?. complexTokens } ) ;
72
+ }
83
73
84
- for ( let i = 0 ; i < tokensWithRef . length ; i ++ ) {
85
- ref = tokensWithRef [ i ] ;
74
+ loop: for ( let i = 0 ; i < tokensWithRef . length ; i ++ ) {
75
+ ref = tokensWithRef [ i ] ;
86
76
87
- token = ref . tokens . find ( ( token ) => token . identifier === identifier ) ;
88
- if ( token ) {
89
- break ;
90
- }
91
- }
77
+ token = ref ?. tokens . find ( ( candidate ) => candidate . identifier === identifier ) ;
78
+ if ( token ) {
79
+ break loop;
92
80
}
93
81
}
94
-
95
- if ( token ) {
96
- if ( ref && ! ref . owner ) {
97
- return {
98
- uri,
99
- range : {
100
- start : { line : position . line , character : position . character } ,
101
- end : { line : position . line , character : position . character } ,
102
- } ,
103
- } ;
82
+ break ;
83
+ case CompletionItemKind . Struct :
84
+ tokensWithRef = document . getGlobalStructComplexTokensWithRef ( ) ;
85
+ loop: for ( let i = 0 ; i < tokensWithRef . length ; i ++ ) {
86
+ ref = tokensWithRef [ i ] ;
87
+
88
+ token = ref ?. tokens . find ( ( candidate ) => candidate . identifier === identifier ) ;
89
+ if ( token ) {
90
+ break loop;
104
91
}
92
+ }
93
+ break ;
94
+ case CompletionItemKind . Property :
95
+ const structIdentifer = localScope . functionVariablesComplexTokens . find ( ( candidate ) => candidate . identifier === structVariableIdentifier ) ?. valueType ;
105
96
106
- return {
107
- uri : ref ? ref . owner || "" : uri ,
108
- range : {
109
- start : { line : token . position . line , character : token . position . character } ,
110
- end : { line : token . position . line , character : token . position . character } ,
111
- } ,
112
- } ;
97
+ tokensWithRef = document . getGlobalStructComplexTokensWithRef ( ) ;
98
+ loop: for ( let i = 0 ; i < tokensWithRef . length ; i ++ ) {
99
+ ref = tokensWithRef [ i ] ;
100
+
101
+ token = ( ref as OwnedStructComplexTokens ) . tokens . find ( ( candidate ) => candidate . identifier === structIdentifer ) ?. properties . find ( ( property ) => property . identifier === identifier ) ;
102
+
103
+ if ( token ) {
104
+ break loop;
105
+ }
113
106
}
114
- }
107
+ break ;
108
+ default :
109
+ token = localScope . functionVariablesComplexTokens . find ( ( candidate ) => candidate . identifier === identifier ) ;
110
+ }
115
111
116
- return undefined ;
117
- } ;
112
+ return [ token , ref ] ;
118
113
}
119
114
}
0 commit comments