@@ -5,6 +5,26 @@ import { EditorState } from "@codemirror/state";
5
5
import { SyntaxNode } from "@lezer/common" ;
6
6
import { getExpressionLanguageConfig , keywords , resolveTypes } from "./utils" ;
7
7
import { syntaxTree } from "@codemirror/language" ;
8
+ import {
9
+ Arguments ,
10
+ BinaryExpression ,
11
+ Expression ,
12
+ Function ,
13
+ MethodAccess ,
14
+ OperatorKeyword ,
15
+ PropertyAccess ,
16
+ TernaryExpression ,
17
+ UnaryExpression ,
18
+ Variable ,
19
+ String ,
20
+ BlockComment ,
21
+ ArrayAccess ,
22
+ Call ,
23
+ Application ,
24
+ Property ,
25
+ Method ,
26
+ Array ,
27
+ } from "./syntax.grammar.terms" ;
8
28
9
29
const autocompleteFunction = ( x : ELFunction ) : Completion => ( {
10
30
label : `${ x . name } (${ x . args ?. map ( x => x . name ) ?. join ( ',' ) || '' } )` ,
@@ -56,11 +76,11 @@ function completeIdentifier(state: EditorState, config: ExpressionLanguageConfig
56
76
}
57
77
58
78
function completeMember ( state : EditorState , config : ExpressionLanguageConfig , tree : SyntaxNode , from : number , to : number , explicit : boolean ) : CompletionResult | null {
59
- if ( tree . parent ?. name != 'ObjectAccess' || ! tree . parent . firstChild ) {
79
+ if ( ! ( tree . parent ?. type . is ( PropertyAccess ) || tree . parent ?. type . is ( MethodAccess ) ) || ! tree . parent ? .firstChild ) {
60
80
return null ;
61
81
}
62
82
63
- const types = resolveTypes ( state , tree . parent . firstChild . node , config , false ) ;
83
+ const types = resolveTypes ( state , tree . parent . firstChild . node , config ) ;
64
84
if ( ! types ?. size ) {
65
85
return null ;
66
86
}
@@ -89,31 +109,31 @@ export function expressionLanguageCompletion(context: CompletionContext): Comple
89
109
const prevNode = tree . resolveInner ( pos , lastChar === ')' ? 0 : - 1 ) ;
90
110
const config = getExpressionLanguageConfig ( state ) ;
91
111
92
- const isIdentifier = ( node : SyntaxNode | undefined ) => [ 'Variable' , 'Function' ] . includes ( node ?. name ?? '' ) ;
93
- const isMember = ( node : SyntaxNode | undefined ) => [ 'Property' , 'Method' ] . includes ( node ?. name ?? '' ) ;
112
+ const isIdentifier = ( node : SyntaxNode | undefined ) => node ?. type . is ( Variable ) || node ?. type . is ( Function ) ;
113
+ const isMember = ( node : SyntaxNode | undefined ) => node ?. type . is ( Property ) || node ?. type . is ( Method ) ;
94
114
95
- if ( prevNode . name == ' String' || prevNode . name == ' BlockComment' ) {
115
+ if ( prevNode . type . is ( String ) || prevNode . type . is ( BlockComment ) ) {
96
116
return null ;
97
117
}
98
118
99
- if ( prevNode . parent ?. name == 'ObjectAccess' && [ 'ObjectAccess' , ' ArrayAccess' , ' Variable' , ' Call' , ' Application' ] . includes ( prevNode . parent . firstChild ?. name || '' ) ) {
119
+ if ( ( prevNode . parent ?. type . is ( PropertyAccess ) || prevNode . parent ?. type . is ( MethodAccess ) ) && [ PropertyAccess , MethodAccess , ArrayAccess , Variable , Call , Application ] . includes ( prevNode . parent . firstChild ?. type . id ) ) {
100
120
return completeMember ( state , config , prevNode , isIdentifier ( prevNode ) || isMember ( prevNode ) ? prevNode . from : pos , pos , explicit ) ;
101
121
}
102
122
103
123
if (
104
- [ ' Expression' , ' UnaryExpression' , ' BinaryExpression' , ' TernaryExpression' ] . includes ( prevNode . name ) && prevNode . lastChild && ! prevNode . lastChild ?. type . isError
105
- || [ ' Arguments' , ' Array' ] . includes ( prevNode . name ) && prevNode . lastChild && ! prevNode . lastChild ?. type . isError
106
- || [ ' Expression' , ' UnaryExpression' , ' BinaryExpression' , ' TernaryExpression' ] . includes ( prevNode . parent ?. name ?? '' ) && prevNode . type . isError
107
- || [ ' Variable' , ' Function' ] . includes ( prevNode . parent ?. name ?? '' ) && prevNode . type . isError
124
+ [ Expression , UnaryExpression , BinaryExpression , TernaryExpression ] . includes ( prevNode . type . id ) && prevNode . lastChild && ! prevNode . lastChild ?. type . isError
125
+ || [ Arguments , Array ] . includes ( prevNode . type . id ) && prevNode . lastChild && ! prevNode . lastChild ?. type . isError
126
+ || [ Expression , UnaryExpression , BinaryExpression , TernaryExpression ] . includes ( prevNode . parent ?. type . id ) && prevNode . type . isError
127
+ || [ Variable , Function ] . includes ( prevNode . parent ?. type . id ) && prevNode . type . isError
108
128
) {
109
- return completeOperatorKeyword ( state , config , prevNode , ! [ ' Expression' , ' UnaryExpression' , ' BinaryExpression' , ' TernaryExpression' , ' Arguments' ] . includes ( prevNode . name ) ? prevNode . from : pos , pos , explicit ) ;
129
+ return completeOperatorKeyword ( state , config , prevNode , ! [ Expression , UnaryExpression , BinaryExpression , TernaryExpression , Arguments ] . includes ( prevNode . type . id ) ? prevNode . from : pos , pos , explicit ) ;
110
130
}
111
131
112
132
if (
113
- ! / [ 0 - 9 ] / . test ( lastChar ) && ! [ 'OperatorKeyword' ] . includes ( prevNode . name ?? '' ) && (
114
- [ ' Expression' , ' UnaryExpression' , ' BinaryExpression' , ' TernaryExpression' ] . includes ( prevNode . name ) && prevNode . lastChild ?. type . isError
115
- || [ ' Expression' , ' UnaryExpression' , ' BinaryExpression' , ' TernaryExpression' , ' Arguments' ] . includes ( prevNode . parent ?. name ?? '' ) && ! prevNode . type . isError
116
- || [ 'Arguments' , 'Array' ] . includes ( prevNode . name ?? '' )
133
+ ! / [ 0 - 9 ] / . test ( lastChar ) && ! prevNode . type . is ( OperatorKeyword ) && (
134
+ [ Expression , UnaryExpression , BinaryExpression , TernaryExpression ] . includes ( prevNode . type . id ) && prevNode . lastChild ?. type . isError
135
+ || [ Expression , UnaryExpression , BinaryExpression , TernaryExpression , Arguments ] . includes ( prevNode . parent ?. type . id ?? - 1 ) && ! prevNode . type . isError
136
+ || prevNode . type . is ( Arguments ) || prevNode . type . is ( Array )
117
137
)
118
138
) {
119
139
return completeIdentifier ( state , config , prevNode , isIdentifier ( prevNode ) ? prevNode . from : pos , pos ) ;
0 commit comments