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:
		
							
								
								
									
										21
									
								
								test/parser/mysql/suggestion/fixtures/multipleStatement.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								test/parser/mysql/suggestion/fixtures/multipleStatement.sql
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
			
		||||
SELECT * FROM  -- unfinished
 | 
			
		||||
 | 
			
		||||
CREATE SCHEMA IF NOT EXISTS db_name DEFAULT ENCRYPTION 'Y';
 | 
			
		||||
 | 
			
		||||
CREATE TABLE test (a INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (a), KEY(b)) ENGINE=InnoDB SELECT b,c FROM test2;
 | 
			
		||||
 | 
			
		||||
ALTER LOGFILE GROUP lg_3 ADD UNDOFILE 'undo_10.dat' INITIAL_SIZE=32M ENGINE=NDBCLUSTER;
 | 
			
		||||
 | 
			
		||||
CREATE TABLE db. LIKE orig_tbl; -- unfinished
 | 
			
		||||
 | 
			
		||||
INSERT HIGH_PRIORITY IGNORE INTO tbl_temp2 (fld_id) VALUES ROW(1,-2,3), ROW(5,7,9), ROW(4,6,8);
 | 
			
		||||
 | 
			
		||||
CHANGE REPLICATION FILTER REPLICATE_DO_DB = (db3, db4);
 | 
			
		||||
 | 
			
		||||
INSERT INTO  VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b); -- unfinished
 | 
			
		||||
 | 
			
		||||
ALTER FUNCTION function_name LANGUAGE SQL;
 | 
			
		||||
 | 
			
		||||
ALTER FUNCTION function_name NOT DETERMINISTIC;
 | 
			
		||||
 | 
			
		||||
INSERT LOW_PRIORITY IGNORE INTO tbl_temp2 (fld_id) SELECT tbl_temp1.fld_order_id FROM  -- unfinished
 | 
			
		||||
							
								
								
									
										69
									
								
								test/parser/mysql/suggestion/multipleStatement.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								test/parser/mysql/suggestion/multipleStatement.test.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,69 @@
 | 
			
		||||
import fs from 'fs';
 | 
			
		||||
import path from 'path';
 | 
			
		||||
import { CaretPosition, SyntaxContextType } from '../../../../src/parser/common/basic-parser-types';
 | 
			
		||||
import MySQL from '../../../../src/parser/mysql';
 | 
			
		||||
 | 
			
		||||
const syntaxSql = fs.readFileSync(
 | 
			
		||||
    path.join(__dirname, 'fixtures', 'multipleStatement.sql'),
 | 
			
		||||
    'utf-8'
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
describe('MySQL Multiple Statements Syntax Suggestion', () => {
 | 
			
		||||
    const parser = new MySQL();
 | 
			
		||||
 | 
			
		||||
    test('Select from table ', () => {
 | 
			
		||||
        const pos: CaretPosition = {
 | 
			
		||||
            lineNumber: 1,
 | 
			
		||||
            column: 15,
 | 
			
		||||
        };
 | 
			
		||||
        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('Create table ', () => {
 | 
			
		||||
        const pos: CaretPosition = {
 | 
			
		||||
            lineNumber: 9,
 | 
			
		||||
            column: 17,
 | 
			
		||||
        };
 | 
			
		||||
        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(['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: 87,
 | 
			
		||||
        };
 | 
			
		||||
        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([]);
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
@ -50,23 +50,22 @@ describe('MySQL Syntax Suggestion', () => {
 | 
			
		||||
        expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    // TODO: fix bug of basic parser and decomment following test
 | 
			
		||||
    // test('Create table ', () => {
 | 
			
		||||
    //     const pos: CaretPosition = {
 | 
			
		||||
    //         lineNumber: 5,
 | 
			
		||||
    //         column: 17,
 | 
			
		||||
    //     };
 | 
			
		||||
    //     const syntaxes = parser.getSuggestionAtCaretPosition(
 | 
			
		||||
    //         commentOtherLine(syntaxSql, pos.lineNumber),
 | 
			
		||||
    //         pos
 | 
			
		||||
    //     )?.syntax;
 | 
			
		||||
    //     const suggestion = syntaxes?.find(
 | 
			
		||||
    //         (syn) => syn.syntaxContextType === SyntaxContextType.TABLE_CREATE
 | 
			
		||||
    //     );
 | 
			
		||||
    test('Create table ', () => {
 | 
			
		||||
        const pos: CaretPosition = {
 | 
			
		||||
            lineNumber: 5,
 | 
			
		||||
            column: 17,
 | 
			
		||||
        };
 | 
			
		||||
        const syntaxes = parser.getSuggestionAtCaretPosition(
 | 
			
		||||
            commentOtherLine(syntaxSql, pos.lineNumber),
 | 
			
		||||
            pos
 | 
			
		||||
        )?.syntax;
 | 
			
		||||
        const suggestion = syntaxes?.find(
 | 
			
		||||
            (syn) => syn.syntaxContextType === SyntaxContextType.TABLE_CREATE
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
    //     expect(suggestion).not.toBeUndefined();
 | 
			
		||||
    //     expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
 | 
			
		||||
    // });
 | 
			
		||||
        expect(suggestion).not.toBeUndefined();
 | 
			
		||||
        expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('DROP table ', () => {
 | 
			
		||||
        const pos: CaretPosition = {
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user