@@ -36,11 +36,11 @@ languageService.configure(languageSettings);
36
36
37
37
suite ( "Auto Completion Tests" , ( ) => {
38
38
39
-
39
+
40
40
describe ( 'yamlCompletion with bowerrc' , function ( ) {
41
-
41
+
42
42
describe ( 'doComplete' , function ( ) {
43
-
43
+
44
44
function setup ( content : string ) {
45
45
return TextDocument . create ( "file://~/Desktop/vscode-k8s/test.yaml" , "yaml" , 0 , content ) ;
46
46
}
@@ -55,7 +55,7 @@ suite("Auto Completion Tests", () => {
55
55
let content = "" ;
56
56
let completion = parseSetup ( content , 0 ) ;
57
57
completion . then ( function ( result ) {
58
- assert . notEqual ( result . items . length , 0 ) ;
58
+ assert . notEqual ( result . items . length , 0 ) ;
59
59
} ) . then ( done , done ) ;
60
60
} ) ;
61
61
@@ -123,39 +123,39 @@ suite("Auto Completion Tests", () => {
123
123
} ) . then ( done , done ) ;
124
124
} ) ;
125
125
126
- it ( 'Autocomplete key in middle of file 2' , ( done ) => {
126
+ it ( 'Autocomplete key in middle of file 2' , ( done ) => {
127
127
let content = "scripts:\n postinstall: /test\n preinsta" ;
128
128
let completion = parseSetup ( content , 31 ) ;
129
129
completion . then ( function ( result ) {
130
130
assert . notEqual ( result . items . length , 0 ) ;
131
131
} ) . then ( done , done ) ;
132
132
} ) ;
133
133
134
- it ( 'Autocomplete does not happen right after :' , ( done ) => {
134
+ it ( 'Autocomplete does not happen right after :' , ( done ) => {
135
135
let content = "analytics:" ;
136
136
let completion = parseSetup ( content , 9 ) ;
137
137
completion . then ( function ( result ) {
138
138
assert . notEqual ( result . items . length , 0 ) ;
139
139
} ) . then ( done , done ) ;
140
140
} ) ;
141
141
142
- it ( 'Autocomplete does not happen right after : under an object' , ( done ) => {
142
+ it ( 'Autocomplete does not happen right after : under an object' , ( done ) => {
143
143
let content = "scripts:\n postinstall:" ;
144
144
let completion = parseSetup ( content , 21 ) ;
145
145
completion . then ( function ( result ) {
146
146
assert . notEqual ( result . items . length , 0 ) ;
147
147
} ) . then ( done , done ) ;
148
148
} ) ;
149
149
150
- it ( 'Autocomplete on multi yaml documents in a single file on root' , ( done ) => {
150
+ it ( 'Autocomplete on multi yaml documents in a single file on root' , ( done ) => {
151
151
let content = `---\nanalytics: true\n...\n---\n...` ;
152
152
let completion = parseSetup ( content , 28 ) ;
153
153
completion . then ( function ( result ) {
154
154
assert . notEqual ( result . items . length , 0 ) ;
155
155
} ) . then ( done , done ) ;
156
156
} ) ;
157
157
158
- it ( 'Autocomplete on multi yaml documents in a single file on scalar' , ( done ) => {
158
+ it ( 'Autocomplete on multi yaml documents in a single file on scalar' , ( done ) => {
159
159
let content = `---\nanalytics: true\n...\n---\njson: \n...` ;
160
160
let completion = parseSetup ( content , 34 ) ;
161
161
completion . then ( function ( result ) {
@@ -166,55 +166,54 @@ suite("Auto Completion Tests", () => {
166
166
} ) ;
167
167
} ) ;
168
168
169
+ function is_EOL ( c ) {
170
+ return ( c === 0x0A /* LF */ ) || ( c === 0x0D /* CR */ ) ;
171
+ }
172
+
169
173
function completionHelper ( document : TextDocument , textDocumentPosition ) {
170
-
171
- //Get the string we are looking at via a substring
172
- let linePos = textDocumentPosition . line ;
173
- let position = textDocumentPosition ;
174
- let lineOffset = getLineOffsets ( document . getText ( ) ) ;
175
- let start = lineOffset [ linePos ] ; //Start of where the autocompletion is happening
176
- let end = 0 ; //End of where the autocompletion is happening
177
- if ( lineOffset [ linePos + 1 ] ) {
178
- end = lineOffset [ linePos + 1 ] ;
179
- } else {
180
- end = document . getText ( ) . length ;
181
- }
182
- let textLine = document . getText ( ) . substring ( start , end ) ;
183
-
184
- //Check if the string we are looking at is a node
185
- if ( textLine . indexOf ( ":" ) === - 1 ) {
186
- //We need to add the ":" to load the nodes
187
-
188
- let newText = "" ;
189
-
190
- //This is for the empty line case
191
- let trimmedText = textLine . trim ( ) ;
192
- if ( trimmedText . length === 0 || ( trimmedText . length === 1 && trimmedText [ 0 ] === '-' ) ) {
193
-
194
- //Add a temp node that is in the document but we don't use at all.
195
- if ( lineOffset [ linePos + 1 ] ) {
196
- newText = document . getText ( ) . substring ( 0 , start + ( textLine . length - 1 ) ) + "holder:\r\n" + document . getText ( ) . substr ( end + 2 ) ;
197
- } else {
198
- newText = document . getText ( ) . substring ( 0 , start + ( textLine . length ) ) + "holder:\r\n" + document . getText ( ) . substr ( end + 2 ) ;
199
- }
200
-
174
+
175
+ //Get the string we are looking at via a substring
176
+ let linePos = textDocumentPosition . line ;
177
+ let position = textDocumentPosition ;
178
+ let lineOffset = getLineOffsets ( document . getText ( ) ) ;
179
+ let start = lineOffset [ linePos ] ; //Start of where the autocompletion is happening
180
+ let end = 0 ; //End of where the autocompletion is happening
181
+ if ( lineOffset [ linePos + 1 ] ) {
182
+ end = lineOffset [ linePos + 1 ] ;
183
+ } else {
184
+ end = document . getText ( ) . length ;
185
+ }
186
+
187
+ while ( end - 1 >= 0 && is_EOL ( document . getText ( ) . charCodeAt ( end - 1 ) ) ) {
188
+ end -- ;
189
+ }
190
+
191
+ let textLine = document . getText ( ) . substring ( start , end ) ;
192
+
193
+ //Check if the string we are looking at is a node
194
+ if ( textLine . indexOf ( ":" ) === - 1 ) {
195
+ //We need to add the ":" to load the nodes
196
+
197
+ let newText = "" ;
198
+
199
+ //This is for the empty line case
200
+ let trimmedText = textLine . trim ( ) ;
201
+ if ( trimmedText . length === 0 || ( trimmedText . length === 1 && trimmedText [ 0 ] === '-' ) ) {
202
+ //Add a temp node that is in the document but we don't use at all.
203
+ newText = document . getText ( ) . substring ( 0 , start + textLine . length ) + "holder:\r\n" + document . getText ( ) . substr ( lineOffset [ linePos + 1 ] || document . getText ( ) . length ) ;
201
204
//For when missing semi colon case
202
- } else {
203
- //Add a semicolon to the end of the current line so we can validate the node
204
- if ( lineOffset [ linePos + 1 ] ) {
205
- newText = document . getText ( ) . substring ( 0 , start + ( textLine . length - 1 ) ) + ":\r\n" + document . getText ( ) . substr ( end + 2 ) ;
206
- } else {
207
- newText = document . getText ( ) . substring ( 0 , start + ( textLine . length ) ) + ":\r\n" + document . getText ( ) . substr ( end + 2 ) ;
208
- }
209
- }
210
- let jsonDocument = parseYAML ( newText ) ;
211
- return languageService . doComplete ( document , position , jsonDocument ) ;
212
205
} else {
213
-
214
- //All the nodes are loaded
215
- position . character = position . character - 1 ;
216
- let jsonDocument = parseYAML ( document . getText ( ) ) ;
217
- return languageService . doComplete ( document , position , jsonDocument ) ;
206
+ //Add a semicolon to the end of the current line so we can validate the node
207
+ newText = document . getText ( ) . substring ( 0 , start + textLine . length ) + ":\r\n" + document . getText ( ) . substr ( lineOffset [ linePos + 1 ] || document . getText ( ) . length ) ;
218
208
}
209
+ let jsonDocument = parseYAML ( newText ) ;
210
+ return languageService . doComplete ( document , position , jsonDocument ) ;
211
+ } else {
212
+
213
+ //All the nodes are loaded
214
+ position . character = position . character - 1 ;
215
+ let jsonDocument = parseYAML ( document . getText ( ) ) ;
216
+ return languageService . doComplete ( document , position , jsonDocument ) ;
217
+ }
219
218
220
219
}
0 commit comments