feat: optimize suggestion (#231)

* feat: optimize the strategy of finding the right range

* test: apply commentOtherLine util to all suggestion tests

* test: decomment suggestion test cases

* test: add suggestion test cases in multiple statements

* chore: improve comments

* test: update log info in test
This commit is contained in:
Hayden
2023-12-13 11:33:47 +08:00
committed by GitHub
parent fd50c09a86
commit 3c7c59fb70
29 changed files with 930 additions and 155 deletions

View File

@ -0,0 +1,22 @@
CREATE TABLE VALUES -- unfinished
ALTER SCHEMA foo RENAME TO bar;
DELETE FROM t;
DENY SELECT ON SCHEMA s TO USER u;
SELECT ids FROM db. ; -- unfinished
INSERT INTO weather (date, city, temp_hi, temp_lo) VALUES ('1994-11-29', 'Hayward', 54, 37);
EXPLAIN ANALYZE VERBOSE SELECT * FROM t;
INSERT INTO weather (date, city, temp_hi, temp_lo) VALUES ('1994-11-29', 'Hayward', 54, 37); -- unfinished
DENY SELECT ON SCHEMA s TO USER u;
CALL catalog.schema.test();
INSERT INTO products (product_no, name, price) SELECT * FROM db. ; -- unfinished

View File

@ -0,0 +1,69 @@
import fs from 'fs';
import path from 'path';
import { CaretPosition, SyntaxContextType } from '../../../../src/parser/common/basic-parser-types';
import TrinoSQL from '../../../../src/parser/trinosql';
const syntaxSql = fs.readFileSync(
path.join(__dirname, 'fixtures', 'multipleStatement.sql'),
'utf-8'
);
describe('TrinoSQL Multiple Statements Syntax Suggestion', () => {
const parser = new TrinoSQL();
test('Create table ', () => {
const pos: CaretPosition = {
lineNumber: 1,
column: 14,
};
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE_CREATE
);
expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
});
test('Select from table', () => {
const pos: CaretPosition = {
lineNumber: 9,
column: 20,
};
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
);
expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
});
test('Insert into table ', () => {
const pos: CaretPosition = {
lineNumber: 15,
column: 13,
};
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
);
expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
});
test('Insert into select from table ', () => {
const pos: CaretPosition = {
lineNumber: 21,
column: 65,
};
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
);
expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
});
});

View File

@ -14,7 +14,10 @@ describe('Trino SQL Token Suggestion', () => {
lineNumber: 1,
column: 7,
};
const suggestion = parser.getSuggestionAtCaretPosition(tokenSql, pos)?.keywords;
const suggestion = parser.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual(['VIEW', 'MATERIALIZED', 'TABLE', 'SCHEMA']);
});