@@ -2,68 +2,41 @@ import * as tokens from "./generated/typeql.grammar.generated.terms";
2
2
import { CompletionContext , Completion } from "@codemirror/autocomplete" ;
3
3
import { SyntaxNode , NodeType , Tree } from "@lezer/common"
4
4
import { SuggestionMap , SuffixOfPrefixSuggestion , suggest } from "./complete" ;
5
- import { Schema } from "./schema " ;
5
+ import { TypeQLAutocompleteSchema } from "./typeQLAutocompleteSchema " ;
6
6
7
- // The actual suggestions
8
- // TODO: See if we can make this declarative based on token sequences expected as prefixes of a given node.
9
- function suggestAttributeTypeLabels ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : Schema ) : Completion [ ] {
10
- // TODO: We could do better by climbing up the tree using `atNode.parentNode` to predict based on position as well.
11
- // We could also refine the suggestions by creating datastructures based on the declarations in the schema, rather than blindly suggesting every label.
7
+ function suggestAttributeTypeLabels ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : TypeQLAutocompleteSchema ) : Completion [ ] {
12
8
var options : Completion [ ] = [ ] ;
13
9
schema . attributeTypes ( ) . forEach ( ( label ) => { options . push ( suggest ( "AttributeType" , label ) ) ; } )
14
10
return options ;
15
11
}
16
12
17
- function suggestObjectTypeLabels ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : Schema ) : Completion [ ] {
18
- // TODO: We could do better by climbing up the tree using `atNode.parentNode` to predict based on position as well.
19
- // We could also refine the suggestions by creating datastructures based on the declarations in the schema, rather than blindly suggesting every label.
13
+ function suggestObjectTypeLabels ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : TypeQLAutocompleteSchema ) : Completion [ ] {
20
14
var options : Completion [ ] = [ ] ;
21
15
schema . objectTypes ( ) . forEach ( ( label ) => { options . push ( suggest ( "ObjectType" , label ) ) ; } )
22
16
return options ;
23
17
}
24
18
25
- function suggestRoleTypeLabelsScoped ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : Schema ) : Completion [ ] {
19
+ function suggestRoleTypeLabelsScoped ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : TypeQLAutocompleteSchema ) : Completion [ ] {
26
20
var options : Completion [ ] = [ ] ;
27
21
schema . objectTypes ( )
28
22
. flatMap ( ( label ) => schema . objectType ( label ) . relates )
29
23
. forEach ( ( label ) => { options . push ( suggest ( "RoleType" , label ) ) ; } ) ;
30
24
return options ;
31
25
}
32
26
33
- function suggestRoleTypeLabelsUnscoped ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : Schema ) : Completion [ ] {
27
+ function suggestRoleTypeLabelsUnscoped ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : TypeQLAutocompleteSchema ) : Completion [ ] {
34
28
var options : Completion [ ] = [ ] ;
35
29
schema . objectTypes ( )
36
30
. flatMap ( ( label ) => schema . objectType ( label ) . relates )
37
31
. forEach ( ( label ) => { options . push ( suggest ( "RoleType" , label . split ( ":" ) [ 1 ] ) ) ; } ) ; return options ;
38
32
}
39
33
40
-
41
- // The actual suggestions
42
- // TODO: See if we can make this declarative based on token sequences expected as prefixes of a given node.
43
- function suggestThingTypeLabels ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : Schema ) : Completion [ ] {
34
+ function suggestThingTypeLabels ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : TypeQLAutocompleteSchema ) : Completion [ ] {
44
35
return suggestAttributeTypeLabels ( context , tree , parseAt , climbedTo , prefix , schema ) . concat (
45
36
suggestObjectTypeLabels ( context , tree , parseAt , climbedTo , prefix , schema )
46
37
) ;
47
38
}
48
39
49
-
50
- // // TODO: See if we can make this declarative based on token sequences expected as prefixes of a given node.
51
- // function suggestLabels(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: Schema): Completion[] {
52
- // // TODO: We could do better by climbing up the tree using `atNode.parentNode` to predict based on position as well.
53
- // // We could also refine the suggestions by creating datastructures based on the declarations in the schema, rather than blindly suggesting every label.
54
- // var options: Completion[] = [];
55
- // tree.iterate({
56
- // enter: (other: SyntaxNode) => {
57
- // if (other.type.id == tokens.LABEL) {
58
- // let label = context.state.sliceDoc(other.from, other.to);
59
- // options.push(suggest("type", label));
60
- // }
61
- // }
62
- // });
63
- // return options;
64
-
65
- // }
66
-
67
40
function suggestVariables ( context : CompletionContext , tree : Tree , boost = 0 ) : Completion [ ] {
68
41
var options : Completion [ ] = [ ] ;
69
42
tree . iterate ( {
@@ -85,7 +58,6 @@ function suggestVariablesAtMinus10(context: CompletionContext, tree: Tree): Comp
85
58
return suggestVariables ( context , tree , - 10 ) ;
86
59
}
87
60
88
-
89
61
function suggestThingConstraintKeywords ( ) : Completion [ ] {
90
62
return [ "isa" , "has" , "links" ] . map ( ( constraintName ) => {
91
63
return {
@@ -107,37 +79,34 @@ function suggestTypeConstraintKeywords(): Completion[] {
107
79
} ) ;
108
80
}
109
81
110
- function suggestDefinedKeywords ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : Schema ) : Completion [ ] {
82
+ function suggestDefinedKeywords ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : TypeQLAutocompleteSchema ) : Completion [ ] {
111
83
return [ "define" , "redefine" , "undefine" ] . map ( ( keyword ) => suggest ( "keyword" , keyword , 1 ) ) ;
112
84
}
113
85
114
- function suggestPipelineStages ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : Schema ) : Completion [ ] {
86
+ function suggestPipelineStages ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : TypeQLAutocompleteSchema ) : Completion [ ] {
115
87
return [ "match" , "insert" , "delete" , "update" , "put" , "select" , "reduce" , "sort" , "limit" , "offset" , "end" ] . map ( ( keyword ) => suggest ( "keyword" , keyword , 1 ) )
116
88
}
117
89
118
- function suggestKinds ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : Schema ) : Completion [ ] {
90
+ function suggestKinds ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : TypeQLAutocompleteSchema ) : Completion [ ] {
119
91
return [ "entity" , "attribute" , "relation" ] . map ( ( keyword ) => suggest ( "kind" , keyword , 2 ) ) ;
120
92
}
121
93
122
- function suggestNestedPatterns ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : Schema ) : Completion [ ] {
94
+ function suggestNestedPatterns ( context : CompletionContext , tree : Tree , parseAt : SyntaxNode , climbedTo : SyntaxNode , prefix : NodeType [ ] , schema : TypeQLAutocompleteSchema ) : Completion [ ] {
123
95
return [ "not {};" , "{} or {};" , "try {};" ] . map ( ( keyword ) => suggest ( "method" , keyword , 2 ) ) ;
124
96
}
125
97
126
- // Hopefully you only have to touch this.
127
-
128
98
const SUFFIX_VAR_OR_COMMA = [ [ tokens . COMMA ] , [ tokens . VAR ] ] ;
129
99
130
100
131
- // Will pick the first matching suffix. If you want to handle things manually, use an empty suffix, duh.
132
-
133
- const SUGGESTION_GROUP_FOR_THING_STATEMENTS : SuffixOfPrefixSuggestion < Schema > [ ] = [
101
+ // Will pick the first matching suffix. If you want to handle things manually, use an empty suffix.
102
+ const SUGGESTION_GROUP_FOR_THING_STATEMENTS : SuffixOfPrefixSuggestion < TypeQLAutocompleteSchema > [ ] = [
134
103
{ suffixes : SUFFIX_VAR_OR_COMMA , suggestions : [ suggestThingConstraintKeywords ] } ,
135
104
{ suffixes : [ [ tokens . HAS ] ] , suggestions : [ suggestAttributeTypeLabels , suggestVariablesAtMinus10 ] } ,
136
105
{ suffixes : [ [ tokens . ISA ] ] , suggestions : [ suggestThingTypeLabels , suggestVariablesAtMinus10 ] } ,
137
106
{ suffixes : [ [ tokens . HAS , tokens . TypeRef ] , [ tokens . ISA , tokens . TypeRef ] ] , suggestions : [ suggestVariablesAtMinus10 ] } ,
138
107
] ;
139
108
140
- export const SUGGESTION_MAP : SuggestionMap < Schema > = {
109
+ export const SUGGESTION_MAP : SuggestionMap < TypeQLAutocompleteSchema > = {
141
110
[ tokens . LABEL ] : [ { suffixes : [ [ ] ] , suggestions : [ suggestThingTypeLabels ] } ] ,
142
111
[ tokens . VAR ] : [ { suffixes : [ [ ] ] , suggestions : [ suggestVariablesAt10 ] } ] ,
143
112
0 commit comments