5
5
isArray ,
6
6
isObject ,
7
7
uniqueMergeArrays ,
8
- asyncExec ,
8
+ asyncExec
9
9
} from './util'
10
10
11
11
/**
@@ -15,50 +15,50 @@ import {
15
15
*/
16
16
export class SchemaTextCompleter {
17
17
constructor ( schema , schemaRefs ) {
18
- this . schema = schema ;
19
- this . schemaRefs = schemaRefs || { } ;
20
- this . suggestions = { } ;
21
- this . suggestionsRefs = { } ;
22
- this . _buildSuggestions ( ) ;
18
+ this . schema = schema
19
+ this . schemaRefs = schemaRefs || { }
20
+ this . suggestions = { }
21
+ this . suggestionsRefs = { }
22
+ this . _buildSuggestions ( )
23
23
}
24
24
25
25
_buildSuggestions ( ) {
26
- this . _handleSchemaEntry ( "" , this . schema , this . suggestions ) ;
27
- for ( let refName in this . schemaRefs ) {
28
- this . suggestionsRefs [ refName ] = { } ;
29
- this . _handleSchemaEntry ( "" , this . schemaRefs [ refName ] , this . suggestionsRefs [ refName ] ) ;
26
+ this . _handleSchemaEntry ( '' , this . schema , this . suggestions )
27
+ for ( const refName in this . schemaRefs ) {
28
+ this . suggestionsRefs [ refName ] = { }
29
+ this . _handleSchemaEntry ( '' , this . schemaRefs [ refName ] , this . suggestionsRefs [ refName ] )
30
30
}
31
31
}
32
32
33
- _handleRef ( currectPath , refName , suggestionsObj ) {
33
+ _handleRef ( currectPath , refName , suggestionsObj ) {
34
34
suggestionsObj [ currectPath ] = suggestionsObj [ currectPath ] || { }
35
35
suggestionsObj [ currectPath ] . refs = suggestionsObj [ currectPath ] . refs || [ ]
36
- suggestionsObj [ currectPath ] . refs = uniqueMergeArrays ( suggestionsObj [ currectPath ] . refs , [ refName ] ) ;
36
+ suggestionsObj [ currectPath ] . refs = uniqueMergeArrays ( suggestionsObj [ currectPath ] . refs , [ refName ] )
37
37
}
38
38
39
- _handleSchemaEntry ( currectPath , schemaNode , suggestionsObj ) {
40
- if ( ! schemaNode ) {
41
- console . error ( 'SchemaTextCompleter: schema node is missing for path' , currectPath ) ;
42
- return ;
39
+ _handleSchemaEntry ( currectPath , schemaNode , suggestionsObj ) {
40
+ if ( ! schemaNode ) {
41
+ console . error ( 'SchemaTextCompleter: schema node is missing for path' , currectPath )
42
+ return
43
43
}
44
44
if ( schemaNode . $ref ) {
45
- this . _handleRef ( currectPath , schemaNode . $ref , suggestionsObj ) ;
46
- return ;
45
+ this . _handleRef ( currectPath , schemaNode . $ref , suggestionsObj )
46
+ return
47
47
}
48
- const ofConditionEntry = this . _checkOfConditon ( schemaNode ) ;
48
+ const ofConditionEntry = this . _checkOfConditon ( schemaNode )
49
49
if ( ofConditionEntry ) {
50
- this . _handleOfCondition ( currectPath , schemaNode [ ofConditionEntry ] , suggestionsObj ) ;
51
- return ;
50
+ this . _handleOfCondition ( currectPath , schemaNode [ ofConditionEntry ] , suggestionsObj )
51
+ return
52
52
}
53
- switch ( schemaNode . type ) {
53
+ switch ( schemaNode . type ) {
54
54
case 'object' :
55
55
this . _handleObject ( currectPath , schemaNode , suggestionsObj )
56
- break ;
56
+ break
57
57
case 'string' :
58
58
case 'number' :
59
59
case 'integer' :
60
60
this . _handlePrimitive ( currectPath , schemaNode , suggestionsObj )
61
- break ;
61
+ break
62
62
case 'boolean' :
63
63
this . _handleBoolean ( currectPath , schemaNode , suggestionsObj )
64
64
break
@@ -67,81 +67,80 @@ export class SchemaTextCompleter {
67
67
}
68
68
}
69
69
70
- _handleObject ( currectPath , schemaNode , suggestionsObj ) {
70
+ _handleObject ( currectPath , schemaNode , suggestionsObj ) {
71
71
if ( isObject ( schemaNode . properties ) ) {
72
- const props = Object . keys ( schemaNode . properties ) ;
73
- suggestionsObj [ currectPath ] = suggestionsObj [ currectPath ] || { } ;
74
- suggestionsObj [ currectPath ] . props = suggestionsObj [ currectPath ] . props || [ ] ;
75
- suggestionsObj [ currectPath ] . props = uniqueMergeArrays ( suggestionsObj [ currectPath ] . props , props ) ;
72
+ const props = Object . keys ( schemaNode . properties )
73
+ suggestionsObj [ currectPath ] = suggestionsObj [ currectPath ] || { }
74
+ suggestionsObj [ currectPath ] . props = suggestionsObj [ currectPath ] . props || [ ]
75
+ suggestionsObj [ currectPath ] . props = uniqueMergeArrays ( suggestionsObj [ currectPath ] . props , props )
76
76
props . forEach ( ( prop ) => {
77
77
asyncExec ( ( ) => {
78
- this . _handleSchemaEntry ( `${ currectPath } /${ prop } ` , schemaNode . properties [ prop ] , suggestionsObj ) ;
78
+ this . _handleSchemaEntry ( `${ currectPath } /${ prop } ` , schemaNode . properties [ prop ] , suggestionsObj )
79
79
} )
80
80
} )
81
81
}
82
82
}
83
83
84
- _handlePrimitive ( currectPath , schemaNode , suggestionsObj ) {
85
- suggestionsObj [ currectPath ] = suggestionsObj [ currectPath ] || { } ;
84
+ _handlePrimitive ( currectPath , schemaNode , suggestionsObj ) {
85
+ suggestionsObj [ currectPath ] = suggestionsObj [ currectPath ] || { }
86
86
if ( isArray ( schemaNode . examples ) ) {
87
- suggestionsObj [ currectPath ] . examples = suggestionsObj [ currectPath ] . examples || [ ] ;
88
- suggestionsObj [ currectPath ] . examples = uniqueMergeArrays ( suggestionsObj [ currectPath ] . examples , schemaNode . examples ) ;
87
+ suggestionsObj [ currectPath ] . examples = suggestionsObj [ currectPath ] . examples || [ ]
88
+ suggestionsObj [ currectPath ] . examples = uniqueMergeArrays ( suggestionsObj [ currectPath ] . examples , schemaNode . examples )
89
89
}
90
90
if ( isArray ( schemaNode . enum ) ) {
91
- suggestionsObj [ currectPath ] . enum = suggestionsObj [ currectPath ] . enum || [ ] ;
92
- suggestionsObj [ currectPath ] . enum = uniqueMergeArrays ( suggestionsObj [ currectPath ] . enum , schemaNode . enum ) ;
91
+ suggestionsObj [ currectPath ] . enum = suggestionsObj [ currectPath ] . enum || [ ]
92
+ suggestionsObj [ currectPath ] . enum = uniqueMergeArrays ( suggestionsObj [ currectPath ] . enum , schemaNode . enum )
93
93
}
94
94
}
95
95
96
- _handleBoolean ( currectPath , schemaNode , suggestionsObj ) {
96
+ _handleBoolean ( currectPath , schemaNode , suggestionsObj ) {
97
97
if ( ! suggestionsObj [ currectPath ] ) {
98
98
suggestionsObj [ currectPath ] = {
99
- bool : [ true , false ]
99
+ bool : [ true , false ]
100
100
}
101
101
}
102
102
}
103
103
104
- _handleArray ( currectPath , schemaNode , suggestionsObj ) {
104
+ _handleArray ( currectPath , schemaNode , suggestionsObj ) {
105
105
if ( schemaNode . items ) {
106
106
asyncExec ( ( ) => {
107
- this . _handleSchemaEntry ( `${ currectPath } /\\d+` , schemaNode . items , suggestionsObj ) ;
107
+ this . _handleSchemaEntry ( `${ currectPath } /\\d+` , schemaNode . items , suggestionsObj )
108
108
} )
109
109
}
110
110
}
111
111
112
- _handleOfCondition ( currectPath , schemaNode , suggestionsObj ) {
112
+ _handleOfCondition ( currectPath , schemaNode , suggestionsObj ) {
113
113
if ( schemaNode && schemaNode . length ) {
114
114
schemaNode . forEach ( schemaEntry => {
115
115
asyncExec ( ( ) => {
116
- this . _handleSchemaEntry ( currectPath , schemaEntry , suggestionsObj ) ;
116
+ this . _handleSchemaEntry ( currectPath , schemaEntry , suggestionsObj )
117
117
} )
118
118
} )
119
119
}
120
120
}
121
121
122
- _checkOfConditon ( entry ) {
122
+ _checkOfConditon ( entry ) {
123
123
if ( ! entry ) {
124
- return ;
124
+ return
125
125
}
126
126
if ( entry . oneOf ) {
127
- return 'oneOf' ;
127
+ return 'oneOf'
128
128
}
129
129
if ( entry . anyOf ) {
130
- return 'anyOf' ;
130
+ return 'anyOf'
131
131
}
132
132
if ( entry . allOf ) {
133
- return 'allOf' ;
133
+ return 'allOf'
134
134
}
135
135
}
136
-
137
136
138
137
getCompletions ( editor , session , pos , prefix , callback ) {
139
138
try {
140
139
const map = jsonMap . parse ( session . getValue ( ) )
141
- const pointers = map . pointers || { } ;
140
+ const pointers = map . pointers || { }
142
141
const processCompletionsCallback = ( suggestions ) => {
143
- let completions = [ ] ;
144
- let score = 0 ;
142
+ let completions = [ ]
143
+ let score = 0
145
144
const appendSuggesions = ( type ) => {
146
145
const typeTitle = {
147
146
props : 'property' ,
@@ -155,7 +154,7 @@ export class SchemaTextCompleter {
155
154
caption : term + '' ,
156
155
meta : `schema [${ typeTitle [ type ] } ]` ,
157
156
score : score ++ ,
158
- value : term + '' ,
157
+ value : term + ''
159
158
}
160
159
} ) )
161
160
}
@@ -164,73 +163,70 @@ export class SchemaTextCompleter {
164
163
appendSuggesions ( 'enum' )
165
164
appendSuggesions ( 'bool' )
166
165
appendSuggesions ( 'examples' )
167
-
166
+
168
167
if ( completions . length ) {
169
- callback ( null , completions ) ;
168
+ callback ( null , completions )
170
169
}
171
-
172
170
}
173
171
Object . keys ( pointers ) . forEach ( ( ptr ) => {
174
172
asyncExec ( ( ) => {
175
173
const matchPointersToPath = ( pointer , currentSuggestions , path ) => {
176
174
const option = Object . keys ( currentSuggestions ) . reduce ( ( last , key ) => {
177
- if ( new RegExp ( `\ ^${ path } ${ key } ` ) . test ( pointer ) ) {
175
+ if ( new RegExp ( `^${ path } ${ key } ` ) . test ( pointer ) ) {
178
176
if ( ! last || last . length < key . length ) {
179
- return key ;
177
+ return key
180
178
}
181
179
}
182
- return last ;
183
- } ) ;
180
+ return last
181
+ } )
184
182
if ( typeof option === 'string' ) {
185
183
if ( currentSuggestions [ option ] ?. refs ?. length ) {
186
- const mergedSuggestions = { } ;
187
- for ( let idx in currentSuggestions [ option ] . refs ) {
188
- const refName = currentSuggestions [ option ] . refs [ idx ]
184
+ const mergedSuggestions = { }
185
+ for ( const idx in currentSuggestions [ option ] . refs ) {
186
+ const refName = currentSuggestions [ option ] . refs [ idx ]
189
187
if ( this . suggestionsRefs [ refName ] ) {
190
- const refSuggestion = matchPointersToPath ( pointer , this . suggestionsRefs [ refName ] , `${ path } ${ option } ` ) ;
191
- if ( refSuggestion ?. enum ) {
192
- mergedSuggestions . enum = uniqueMergeArrays ( mergedSuggestions . enum , refSuggestion . enum ) ;
188
+ const refSuggestion = matchPointersToPath ( pointer , this . suggestionsRefs [ refName ] , `${ path } ${ option } ` )
189
+ if ( refSuggestion ?. enum ) {
190
+ mergedSuggestions . enum = uniqueMergeArrays ( mergedSuggestions . enum , refSuggestion . enum )
193
191
}
194
- if ( refSuggestion ?. examples ) {
195
- mergedSuggestions . examples = uniqueMergeArrays ( mergedSuggestions . examples , refSuggestion . examples ) ;
192
+ if ( refSuggestion ?. examples ) {
193
+ mergedSuggestions . examples = uniqueMergeArrays ( mergedSuggestions . examples , refSuggestion . examples )
196
194
}
197
- if ( refSuggestion ?. bool ) {
198
- mergedSuggestions . bool = uniqueMergeArrays ( mergedSuggestions . bool , refSuggestion . bool ) ;
195
+ if ( refSuggestion ?. bool ) {
196
+ mergedSuggestions . bool = uniqueMergeArrays ( mergedSuggestions . bool , refSuggestion . bool )
199
197
}
200
- if ( refSuggestion ?. props ) {
201
- mergedSuggestions . props = uniqueMergeArrays ( mergedSuggestions . props , refSuggestion . props ) ;
198
+ if ( refSuggestion ?. props ) {
199
+ mergedSuggestions . props = uniqueMergeArrays ( mergedSuggestions . props , refSuggestion . props )
202
200
}
203
201
}
204
202
}
205
- return mergedSuggestions ;
206
- } else if ( new RegExp ( `\ ^${ path } ${ option } $` ) . test ( pointer ) ) {
207
- console . log ( 'SchemaTextCompleter: Text suggestion match' , { path : pointer , schemaPath : `${ path } ${ option } ` , suggestions : currentSuggestions [ option ] } ) ;
208
- return currentSuggestions [ option ] ;
203
+ return mergedSuggestions
204
+ } else if ( new RegExp ( `^${ path } ${ option } $` ) . test ( pointer ) ) {
205
+ console . log ( 'SchemaTextCompleter: Text suggestion match' , { path : pointer , schemaPath : `${ path } ${ option } ` , suggestions : currentSuggestions [ option ] } )
206
+ return currentSuggestions [ option ]
209
207
}
210
208
}
211
209
}
212
- let selectedPtr ;
210
+ let selectedPtr
213
211
if ( pointers [ ptr ] . key ?. line === pos . row ) {
214
212
if ( pos . column >= pointers [ ptr ] . key . column && pos . column <= pointers [ ptr ] . keyEnd . column ) {
215
- selectedPtr = ptr . slice ( 0 , ptr . lastIndexOf ( '/' ) ) ;
213
+ selectedPtr = ptr . slice ( 0 , ptr . lastIndexOf ( '/' ) )
216
214
}
217
- }
218
- if ( pointers [ ptr ] . value ?. line === pos . row &&
215
+ }
216
+ if ( pointers [ ptr ] . value ?. line === pos . row &&
219
217
pointers [ ptr ] . value ?. line === pointers [ ptr ] . valueEnd ?. line ) { // multiline values are objects
220
218
if ( pos . column >= pointers [ ptr ] . value . column && pos . column <= pointers [ ptr ] . valueEnd . column ) {
221
- selectedPtr = ptr ;
219
+ selectedPtr = ptr
222
220
}
223
221
}
224
222
if ( selectedPtr ) {
225
- const chosenCompletions = matchPointersToPath ( selectedPtr , this . suggestions , '' ) ;
226
- processCompletionsCallback ( chosenCompletions ) ;
223
+ const chosenCompletions = matchPointersToPath ( selectedPtr , this . suggestions , '' )
224
+ processCompletionsCallback ( chosenCompletions )
227
225
}
228
-
229
226
} )
230
227
} )
231
228
} catch ( e ) {
232
229
// probably not valid json, ignore.
233
230
}
234
231
}
235
-
236
232
}
0 commit comments