Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: split whiteSpace into multiple tokens to fix findCaretTokenIndex… #385

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/grammar/flink/FlinkSqlLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ options {

// SKIP

SPACE : [ \t\r\n]+ -> channel(HIDDEN);
SPACE : (' ' | '\t' | '\r' | '\n') -> channel(HIDDEN);
COMMENT_INPUT : '/*' .*? '*/' -> channel(HIDDEN);
LINE_COMMENT: (('--' | '#') ~[\r\n]* ('\r'? '\n' | EOF) | '--' ('\r'? '\n' | EOF)) -> channel(HIDDEN);

Expand Down
2 changes: 1 addition & 1 deletion src/grammar/impala/ImpalaSqlLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,4 @@ SIMPLE_COMMENT: '--' ~[\r\n]* '\r'? '\n'? -> channel(HIDDEN);

BRACKETED_COMMENT: '/*' .*? '*/' -> channel(HIDDEN);

WS: [ \r\n\t]+ -> channel(HIDDEN);
WS: (' ' | '\t' | '\r' | '\n') -> channel(HIDDEN);
2 changes: 1 addition & 1 deletion src/grammar/mysql/MySqlLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ channels {
ERRORCHANNEL
}

SPACE : [ \t\r\n]+ -> channel(HIDDEN);
SPACE : (' ' | '\t' | '\r' | '\n') -> channel(HIDDEN);
SPEC_MYSQL_COMMENT : '/*!' .+? '*/' -> channel(MYSQLCOMMENT);
COMMENT_INPUT : '/*' .*? '*/' -> channel(HIDDEN);
LINE_COMMENT: (('--' [ \t]* | '#') ~[\r\n]* ('\r'? '\n' | EOF) | '--' ('\r'? '\n' | EOF)) -> channel(HIDDEN);
Expand Down
2 changes: 1 addition & 1 deletion src/grammar/spark/SparkSqlLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ SIMPLE_COMMENT: '--' ('\\\n' | ~[\r\n])* '\r'? '\n'? -> channel(HIDDEN);
BRACKETED_COMMENT:
'/*' (BRACKETED_COMMENT | .)*? ('*/' | {this.markUnclosedComment();} EOF) -> channel(HIDDEN);

WS: [ \r\n\t]+ -> channel(HIDDEN);
WS: (' ' | '\t' | '\r' | '\n') -> channel(HIDDEN);

// Catch-all for anything we can't recognize.
// We use this to be able to ignore and recover all the text
Expand Down
2 changes: 1 addition & 1 deletion src/grammar/trino/TrinoSql.g4
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ SIMPLE_COMMENT: '--' ~[\r\n]* '\r'? '\n'? -> channel(HIDDEN);

BRACKETED_COMMENT: '/*' .*? '*/' -> channel(HIDDEN);

WS: [ \r\n\t]+ -> channel(HIDDEN);
WS: (' ' | '\t' | '\r' | '\n') -> channel(HIDDEN);

// Catch-all for anything we can't recognize.
// We use this to be able to ignore and recover all the text
Expand Down
2 changes: 1 addition & 1 deletion src/lib/flink/FlinkSqlLexer.interp

Large diffs are not rendered by default.

3,923 changes: 1,961 additions & 1,962 deletions src/lib/flink/FlinkSqlLexer.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/lib/impala/ImpalaSqlLexer.interp

Large diffs are not rendered by default.

1,165 changes: 582 additions & 583 deletions src/lib/impala/ImpalaSqlLexer.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/lib/mysql/MySqlLexer.interp

Large diffs are not rendered by default.

6,945 changes: 3,472 additions & 3,473 deletions src/lib/mysql/MySqlLexer.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/lib/spark/SparkSqlLexer.interp

Large diffs are not rendered by default.

1,887 changes: 943 additions & 944 deletions src/lib/spark/SparkSqlLexer.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/lib/trino/TrinoSqlLexer.interp

Large diffs are not rendered by default.

630 changes: 314 additions & 316 deletions src/lib/trino/TrinoSqlLexer.ts

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ export const readSQL = (dirname: string, fileName: string) => {
return result;
};

export function commentOtherLine(sqlContent: string, line: number) {
export function commentOtherLine(
sqlContent: string,
line: number | [startLine: number, endLine: number]
) {
const slices = sqlContent.split('\n').map((item, index) => {
if (index !== line - 1) {
if (
(Array.isArray(line) && (index + 1 < line[0] || index + 1 > line[1])) ||
(typeof line === 'number' && index + 1 !== line)
) {
return '-- ' + item;
} else {
return item;
Expand Down
6 changes: 5 additions & 1 deletion test/parser/flink/suggestion/fixtures/tokenSuggestion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ USE
;
CREATE
;
SHOW
SHOW

CREATE TABLE tb (id

);
12 changes: 12 additions & 0 deletions test/parser/flink/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ describe('Flink SQL Token Suggestion', () => {
'JARS',
]);
});

test('Suggestion in new line', () => {
const pos: CaretPosition = {
lineNumber: 10,
column: 2,
};
const suggestion = flink.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, [9, 11]),
pos
)?.keywords;
expect(suggestion.length).not.toBe(0);
});
});
4 changes: 4 additions & 0 deletions test/parser/hive/suggestion/fixtures/tokenSuggestion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ LOAD
;
SHOW
;

CREATE TABLE tb (id

);
12 changes: 12 additions & 0 deletions test/parser/hive/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,16 @@ describe('Hive SQL Token Suggestion', () => {
'SCHEMAS',
]);
});

test('Suggestion in new line', () => {
const pos: CaretPosition = {
lineNumber: 23,
column: 2,
};
const suggestion = hive.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, [21, 23]),
pos
)?.keywords;
expect(suggestion.length).not.toBe(0);
});
});
4 changes: 4 additions & 0 deletions test/parser/impala/suggestion/fixtures/tokenSuggestion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ INSERT ;
SHOW ;

CREATE TABLE t1 (id );

CREATE TABLE tb (id

);
12 changes: 12 additions & 0 deletions test/parser/impala/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,16 @@ describe('Impala SQL Token Suggestion', () => {

expect(dataTypes.every((dataType) => suggestion.includes(dataType))).toBe(true);
});

test('Suggestion in new line', () => {
const pos: CaretPosition = {
lineNumber: 14,
column: 2,
};
const suggestion = impala.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, [13, 15]),
pos
)?.keywords;
expect(suggestion.length).not.toBe(0);
});
});
4 changes: 4 additions & 0 deletions test/parser/mysql/suggestion/fixtures/tokenSuggestion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ LOAD
;
SHOW
;

CREATE TABLE tb (id

);
12 changes: 12 additions & 0 deletions test/parser/mysql/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,16 @@ describe('MySQL Token Suggestion', () => {
'BINARY',
]);
});

test('Suggestion in new line', () => {
const pos: CaretPosition = {
lineNumber: 19,
column: 2,
};
const suggestion = mysql.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, [18, 20]),
pos
)?.keywords;
expect(suggestion.length).not.toBe(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ DELETE ;

CREATE ;



CREATE TABLE tb (id

);
12 changes: 12 additions & 0 deletions test/parser/postgresql/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,16 @@ describe('Postgres SQL Token Suggestion', () => {
)?.keywords;
expect(suggestion).toMatchUnorderedArray(['INTO']);
});

test('Suggestion in new line', () => {
const pos: CaretPosition = {
lineNumber: 12,
column: 2,
};
const suggestion = postgresql.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, [11, 13]),
pos
)?.keywords;
expect(suggestion.length).not.toBe(0);
});
});
4 changes: 4 additions & 0 deletions test/parser/spark/suggestion/fixtures/tokenSuggestion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ SHOW
;
EXPORT
;

CREATE TABLE tb (id

);
12 changes: 12 additions & 0 deletions test/parser/spark/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,16 @@ describe('Spark SQL Token Suggestion', () => {

expect(suggestion).toMatchUnorderedArray(['TABLE']);
});

test('Suggestion in new line', () => {
const pos: CaretPosition = {
lineNumber: 21,
column: 2,
};
const suggestion = spark.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, [20, 22]),
pos
)?.keywords;
expect(suggestion.length).not.toBe(0);
});
});
4 changes: 4 additions & 0 deletions test/parser/trino/suggestion/fixtures/tokenSuggestion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ DESCRIBE ;
DROP ;

INSERT ;

CREATE TABLE tb (id

);
12 changes: 12 additions & 0 deletions test/parser/trino/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,16 @@ describe('Trino SQL Token Suggestion', () => {

expect(suggestion).toMatchUnorderedArray(['INTO']);
});

test('Suggestion in new line', () => {
const pos: CaretPosition = {
lineNumber: 16,
column: 2,
};
const suggestion = trino.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, [15, 17]),
pos
)?.keywords;
expect(suggestion.length).not.toBe(0);
});
});