You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+71-1Lines changed: 71 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -290,7 +290,7 @@ console.log(sqlSlices)
290
290
Obtaining code completion information at a specified position in SQL.
291
291
We can refer to the example of using `FlinkSQL`.
292
292
293
-
Invoke the `getSuggestionAtCaretPosition` method, pass the SQL content and the row and column numbers indicating the position where code completion is desired.
293
+
Invoke the `getSuggestionAtCaretPosition` method, pass the SQL content and the row and column numbers indicating the position where code completion is desired. The following are some additional explanations about [CaretPosition](#caretposition-of-code-completion).
294
294
+**keyword candidates list**
295
295
296
296
```javascript
@@ -358,6 +358,76 @@ The grammar-related code completion information returns an array, where each ite
358
358
359
359
<br/>
360
360
361
+
## Position and Range
362
+
Some return results of the APIs provided by `dt-sql-parser` contain text information, among which the range and start value of line number, column number and index may cause some confusion.
363
+
364
+
### Index
365
+
The index starts at 0. In the programming field, it is more intuitive.
366
+
367
+

368
+
369
+
For an index range, the start index starts from 0 and ends with n-1, as shown in the figure above, an index range of blue text should be represented as follows:
370
+
371
+
```javascript
372
+
{
373
+
startIndex:0,
374
+
endIndex:3
375
+
}
376
+
```
377
+
378
+
### Line
379
+
The line starts at 1.
380
+
381
+

382
+
383
+
For a range of multiple lines, the line number starts from 1 and ends with n. A range of the first and second lines is represented as follows:
384
+
385
+
```javascript
386
+
{
387
+
startLine:1,
388
+
endLine:2
389
+
}
390
+
```
391
+
392
+
### Column
393
+
The column also starts at 1.
394
+
395
+

396
+
397
+
It is easier to understand by comparing the column number with the cursor position of the editor. For a range of multiple columns, the column number starts from 1 and ends with n+1, as shown in the figure above, a range of blue text columns is represented as follows:
398
+
399
+
```javascript
400
+
{
401
+
startColumn:1,
402
+
endColumn:5
403
+
}
404
+
```
405
+
406
+
### CaretPosition Of Code Completion
407
+
The code completion of `dt-sql-parser` was designed to be used in the editor, so the format of the second parameter (CaretPosition) of the `getSuggestionAtCaretPosition` method is line and column number instead of character position index. This makes it easier to integrate the code completion into the editor. For the editor, it only needs to get the text content and cursor position in the editor at a specific time to call the code completion of `dt-sql-parser`, without any additional calculation.
408
+
409
+
But in some other scenarios, you may need to get the caret position required by the code completion through conversion or calculation. Then, there are some precautions that you may need to care about before that.
410
+
411
+
The code completion of `dt-sql-parser` depends on [antlr4-c3](https://github.com/mike-lischke/antlr4-c3), which is a great library. The code completion of `dt-sql-parser` is just encapsulated and converted based on antlr4-c3, including converting the line and column number information into the token index required by antlr4-c3, as shown in the figure below:
412
+
413
+

414
+
415
+
Regard the column in the figure as the cursor position, and put this text into the editor, you will get 13 possible cursor positions, while for dt-sql-parser, this text will generate 4 Tokens after being parsed. An important strategy of the code completion is: **When the cursor (CaretPosition) has not completely left a Token, dt-sql-parser thinks that this Token has not been completed, and the code completion will infer what can be filled in the position of this Token.**
416
+
417
+
For example, if you want to know what to fill in after `SHOW` through the code completion, the caret position should be:
418
+
419
+
```javascript
420
+
{
421
+
lineNumber:1,
422
+
column:6
423
+
}
424
+
```
425
+
426
+
At this time, dt-sql-parser will think that `SHOW` is already a complete Token, and it should infer what can be filled in after `SHOW`. If the column in the passed-in caret position is 5, then dt-sql-parser will think that `SHOW` has not been completed, and then infer what can be filled in the position of `SHOW`. In other words, in the figure above, `column:5` belongs to `token:0`, and `column:6` belongs to `token:1`.
427
+
428
+
For the editor, this strategy is also more intuitive. After the user enters `SHOW`, before pressing the space key, the user probably has not finished entering, maybe the user wants to enter something like `SHOWS`. When the user presses the space key, the editor thinks that the user wants to enter the next Token, and it is time to ask dt-sql-parser what can be filled in the next Token position.
0 commit comments