diff --git a/src/parser/common/basicSQL.ts b/src/parser/common/basicSQL.ts index 686df544..db0e4f88 100644 --- a/src/parser/common/basicSQL.ts +++ b/src/parser/common/basicSQL.ts @@ -73,15 +73,13 @@ export abstract class BasicSQL< /** * Convert candidates to suggestions * @param candidates candidate list - * @param allTokens all tokens from input + * @param allTokens slice all tokens from input by tokenIndexOffset * @param caretTokenIndex tokenIndex of caretPosition - * @param tokenIndexOffset offset of the tokenIndex in the candidates compared to the tokenIndex in allTokens */ protected abstract processCandidates( candidates: CandidatesCollection, allTokens: Token[], - caretTokenIndex: number, - tokenIndexOffset: number + caretTokenIndex: number ): Suggestions; /** @@ -539,13 +537,7 @@ export abstract class BasicSQL< core.preferredRules = this.preferredRules; const candidates = core.collectCandidates(caretTokenIndex, parseTree); - const originalSuggestions = this.processCandidates( - candidates, - allTokens, - caretTokenIndex, - 0 - // tokenIndexOffset - ); + const originalSuggestions = this.processCandidates(candidates, allTokens, caretTokenIndex); const syntaxSuggestions: SyntaxSuggestion[] = originalSuggestions.syntax.map( (syntaxCtx) => { diff --git a/src/parser/flink/index.ts b/src/parser/flink/index.ts index a5dd432e..da089fa8 100644 --- a/src/parser/flink/index.ts +++ b/src/parser/flink/index.ts @@ -68,19 +68,14 @@ export class FlinkSQL extends BasicSQL { const originalSyntaxSuggestions: SyntaxSuggestion[] = []; const keywords: string[] = []; for (let candidate of candidates.rules) { const [ruleType, candidateRule] = candidate; - const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset; - const tokenRanges = allTokens.slice( - startTokenIndex, - caretTokenIndex + tokenIndexOffset + 1 - ); + const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1); let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0; switch (ruleType) { diff --git a/src/parser/hive/index.ts b/src/parser/hive/index.ts index 17d21c5b..67e65084 100644 --- a/src/parser/hive/index.ts +++ b/src/parser/hive/index.ts @@ -69,18 +69,13 @@ export class HiveSQL extends BasicSQL { const originalSyntaxSuggestions: SyntaxSuggestion[] = []; const keywords: string[] = []; for (let candidate of candidates.rules) { const [ruleType, candidateRule] = candidate; - const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset; - const tokenRanges = allTokens.slice( - startTokenIndex, - caretTokenIndex + tokenIndexOffset + 1 - ); + const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1); let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0; switch (ruleType) { diff --git a/src/parser/impala/index.ts b/src/parser/impala/index.ts index 77cb3290..2bff4bbe 100644 --- a/src/parser/impala/index.ts +++ b/src/parser/impala/index.ts @@ -67,18 +67,13 @@ export class ImpalaSQL extends BasicSQL { const originalSyntaxSuggestions: SyntaxSuggestion[] = []; const keywords: string[] = []; for (let candidate of candidates.rules) { const [ruleType, candidateRule] = candidate; - const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset; - const tokenRanges = allTokens.slice( - startTokenIndex, - caretTokenIndex + tokenIndexOffset + 1 - ); + const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1); let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0; switch (ruleType) { diff --git a/src/parser/mysql/index.ts b/src/parser/mysql/index.ts index c98be3d2..b2868b0c 100644 --- a/src/parser/mysql/index.ts +++ b/src/parser/mysql/index.ts @@ -66,19 +66,14 @@ export class MySQL extends BasicSQL { protected processCandidates( candidates: CandidatesCollection, allTokens: Token[], - caretTokenIndex: number, - tokenIndexOffset: number + caretTokenIndex: number ): Suggestions { const originalSyntaxSuggestions: SyntaxSuggestion[] = []; const keywords: string[] = []; for (const candidate of candidates.rules) { const [ruleType, candidateRule] = candidate; - const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset; - const tokenRanges = allTokens.slice( - startTokenIndex, - caretTokenIndex + tokenIndexOffset + 1 - ); + const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1); let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0; switch (ruleType) { diff --git a/src/parser/postgresql/index.ts b/src/parser/postgresql/index.ts index 3c3b6593..254a3c26 100644 --- a/src/parser/postgresql/index.ts +++ b/src/parser/postgresql/index.ts @@ -72,18 +72,13 @@ export class PostgreSQL extends BasicSQL { const originalSyntaxSuggestions: SyntaxSuggestion[] = []; const keywords: string[] = []; for (let candidate of candidates.rules) { const [ruleType, candidateRule] = candidate; - const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset; - const tokenRanges = allTokens.slice( - startTokenIndex, - caretTokenIndex + tokenIndexOffset + 1 - ); + const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1); let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0; switch (ruleType) { diff --git a/src/parser/spark/index.ts b/src/parser/spark/index.ts index f9e2dc43..f0125fc3 100644 --- a/src/parser/spark/index.ts +++ b/src/parser/spark/index.ts @@ -67,19 +67,14 @@ export class SparkSQL extends BasicSQL { const originalSyntaxSuggestions: SyntaxSuggestion[] = []; const keywords: string[] = []; for (const candidate of candidates.rules) { const [ruleType, candidateRule] = candidate; - const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset; - const tokenRanges = allTokens.slice( - startTokenIndex, - caretTokenIndex + tokenIndexOffset + 1 - ); + const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1); let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0; switch (ruleType) { diff --git a/src/parser/trino/index.ts b/src/parser/trino/index.ts index 95953736..8b70c1e0 100644 --- a/src/parser/trino/index.ts +++ b/src/parser/trino/index.ts @@ -69,19 +69,14 @@ export class TrinoSQL extends BasicSQL { const originalSyntaxSuggestions: SyntaxSuggestion[] = []; const keywords: string[] = []; for (let candidate of candidates.rules) { const [ruleType, candidateRule] = candidate; - const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset; - const tokenRanges = allTokens.slice( - startTokenIndex, - caretTokenIndex + tokenIndexOffset + 1 - ); + const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1); let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0; switch (ruleType) { diff --git a/test/parser/flink/suggestion/multipleStatement.test.ts b/test/parser/flink/suggestion/multipleStatement.test.ts index e89e4bdf..e0ecd308 100644 --- a/test/parser/flink/suggestion/multipleStatement.test.ts +++ b/test/parser/flink/suggestion/multipleStatement.test.ts @@ -36,7 +36,25 @@ describe('FlinkSQL Multiple Statements Syntax Suggestion', () => { ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']); + expect(suggestion?.wordRanges?.length).toBe(2); + expect(suggestion?.wordRanges).toEqual([ + { + text: 'db', + line: 16, + startIndex: 306, + endIndex: 307, + startColumn: 14, + endColumn: 16, + }, + { + text: '.', + line: 16, + startIndex: 308, + endIndex: 308, + startColumn: 16, + endColumn: 17, + }, + ]); }); test('Insert into table ', () => { diff --git a/test/parser/flink/suggestion/syntaxSuggestion.test.ts b/test/parser/flink/suggestion/syntaxSuggestion.test.ts index 9f6eb5cd..ebe47c7f 100644 --- a/test/parser/flink/suggestion/syntaxSuggestion.test.ts +++ b/test/parser/flink/suggestion/syntaxSuggestion.test.ts @@ -32,7 +32,17 @@ describe('Flink SQL Syntax Suggestion', () => { ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['cat']); + expect(suggestion?.wordRanges?.length).toBe(1); + expect(suggestion?.wordRanges).toEqual([ + { + text: 'cat', + line: 1, + startIndex: 13, + endIndex: 15, + startColumn: 14, + endColumn: 17, + }, + ]); }); test('Select table', () => { diff --git a/test/parser/hive/suggestion/multipleStatement.test.ts b/test/parser/hive/suggestion/multipleStatement.test.ts index 0c1d36fe..223ab846 100644 --- a/test/parser/hive/suggestion/multipleStatement.test.ts +++ b/test/parser/hive/suggestion/multipleStatement.test.ts @@ -36,7 +36,25 @@ describe('HiveSQL Multiple Statements Syntax Suggestion', () => { ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']); + expect(suggestion?.wordRanges?.length).toBe(2); + expect(suggestion?.wordRanges).toEqual([ + { + text: 'db', + line: 9, + startIndex: 272, + endIndex: 273, + startColumn: 14, + endColumn: 16, + }, + { + text: '.', + line: 9, + startIndex: 274, + endIndex: 274, + startColumn: 16, + endColumn: 17, + }, + ]); }); test('Insert into table ', () => { diff --git a/test/parser/hive/suggestion/syntaxSuggestion.test.ts b/test/parser/hive/suggestion/syntaxSuggestion.test.ts index 90ee89af..2013d578 100644 --- a/test/parser/hive/suggestion/syntaxSuggestion.test.ts +++ b/test/parser/hive/suggestion/syntaxSuggestion.test.ts @@ -32,7 +32,33 @@ describe('Hive SQL Syntax Suggestion', () => { ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'tb']); + expect(suggestion?.wordRanges?.length).toBe(3); + expect(suggestion?.wordRanges).toEqual([ + { + text: 'db', + line: 1, + startIndex: 12, + endIndex: 13, + startColumn: 13, + endColumn: 15, + }, + { + text: '.', + line: 1, + startIndex: 14, + endIndex: 14, + startColumn: 15, + endColumn: 16, + }, + { + text: 'tb', + line: 1, + startIndex: 15, + endIndex: 16, + startColumn: 16, + endColumn: 18, + }, + ]); }); test('Select table ', () => { diff --git a/test/parser/impala/suggestion/multipleStatement.test.ts b/test/parser/impala/suggestion/multipleStatement.test.ts index 115d2012..d9a00b02 100644 --- a/test/parser/impala/suggestion/multipleStatement.test.ts +++ b/test/parser/impala/suggestion/multipleStatement.test.ts @@ -36,7 +36,25 @@ describe('ImpalaSQL Multiple Statements Syntax Suggestion', () => { ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']); + expect(suggestion?.wordRanges?.length).toBe(2); + expect(suggestion?.wordRanges).toEqual([ + { + text: 'db', + line: 9, + startIndex: 203, + endIndex: 204, + startColumn: 14, + endColumn: 16, + }, + { + text: '.', + line: 9, + startIndex: 205, + endIndex: 205, + startColumn: 16, + endColumn: 17, + }, + ]); }); test('Insert into table ', () => { diff --git a/test/parser/impala/suggestion/syntaxSuggestion.test.ts b/test/parser/impala/suggestion/syntaxSuggestion.test.ts index 08629187..02cf8d4d 100644 --- a/test/parser/impala/suggestion/syntaxSuggestion.test.ts +++ b/test/parser/impala/suggestion/syntaxSuggestion.test.ts @@ -26,7 +26,33 @@ describe('Impala SQL Syntax Suggestion', () => { ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['cat', '.', 'a']); + expect(suggestion?.wordRanges?.length).toBe(3); + expect(suggestion?.wordRanges).toEqual([ + { + text: 'cat', + line: 1, + startIndex: 14, + endIndex: 16, + startColumn: 15, + endColumn: 18, + }, + { + text: '.', + line: 1, + startIndex: 17, + endIndex: 17, + startColumn: 18, + endColumn: 19, + }, + { + text: 'a', + line: 1, + startIndex: 18, + endIndex: 18, + startColumn: 19, + endColumn: 20, + }, + ]); }); test('Function call', () => { diff --git a/test/parser/mysql/suggestion/multipleStatement.test.ts b/test/parser/mysql/suggestion/multipleStatement.test.ts index 48efa713..d08099b8 100644 --- a/test/parser/mysql/suggestion/multipleStatement.test.ts +++ b/test/parser/mysql/suggestion/multipleStatement.test.ts @@ -36,7 +36,25 @@ describe('MySQL Multiple Statements Syntax Suggestion', () => { ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']); + expect(suggestion?.wordRanges?.length).toBe(2); + expect(suggestion?.wordRanges).toEqual([ + { + text: 'db', + line: 9, + startIndex: 306, + endIndex: 307, + startColumn: 14, + endColumn: 16, + }, + { + text: '.', + line: 9, + startIndex: 308, + endIndex: 308, + startColumn: 16, + endColumn: 17, + }, + ]); }); test('Insert into table ', () => { diff --git a/test/parser/mysql/suggestion/syntaxSuggestion.test.ts b/test/parser/mysql/suggestion/syntaxSuggestion.test.ts index b8bf0569..98bb4852 100644 --- a/test/parser/mysql/suggestion/syntaxSuggestion.test.ts +++ b/test/parser/mysql/suggestion/syntaxSuggestion.test.ts @@ -30,7 +30,33 @@ describe('MySQL Syntax Suggestion', () => { ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'tb']); + expect(suggestion?.wordRanges?.length).toBe(3); + expect(suggestion?.wordRanges).toEqual([ + { + text: 'db', + line: 1, + startIndex: 12, + endIndex: 13, + startColumn: 13, + endColumn: 15, + }, + { + text: '.', + line: 1, + startIndex: 14, + endIndex: 14, + startColumn: 15, + endColumn: 16, + }, + { + text: 'tb', + line: 1, + startIndex: 15, + endIndex: 16, + startColumn: 16, + endColumn: 18, + }, + ]); }); test('Select table ', () => { diff --git a/test/parser/postgresql/suggestion/multipleStatement.test.ts b/test/parser/postgresql/suggestion/multipleStatement.test.ts index 48e199a8..a688db41 100644 --- a/test/parser/postgresql/suggestion/multipleStatement.test.ts +++ b/test/parser/postgresql/suggestion/multipleStatement.test.ts @@ -69,6 +69,24 @@ describe('PgSQL Multiple Statements Syntax Suggestion', () => { ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']); + expect(suggestion?.wordRanges?.length).toBe(2); + expect(suggestion?.wordRanges).toEqual([ + { + text: 'db', + line: 21, + startIndex: 682, + endIndex: 683, + startColumn: 62, + endColumn: 64, + }, + { + text: '.', + line: 21, + startIndex: 684, + endIndex: 684, + startColumn: 64, + endColumn: 65, + }, + ]); }); }); diff --git a/test/parser/postgresql/suggestion/suggestionWithEntity.test.ts b/test/parser/postgresql/suggestion/suggestionWithEntity.test.ts index 89ae26a6..a80adff3 100644 --- a/test/parser/postgresql/suggestion/suggestionWithEntity.test.ts +++ b/test/parser/postgresql/suggestion/suggestionWithEntity.test.ts @@ -166,7 +166,17 @@ describe('PostgreSql Syntax Suggestion with collect entity', () => { (syn) => syn.syntaxContextType === EntityContextType.COLUMN ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['a_column']); + expect(suggestion?.wordRanges?.length).toBe(1); + expect(suggestion?.wordRanges).toEqual([ + { + text: 'a_column', + line: 13, + startIndex: 399, + endIndex: 406, + startColumn: 27, + endColumn: 35, + }, + ]); const entities = postgre.getAllEntities(sql, pos); expect(entities.length).toBe(1); diff --git a/test/parser/postgresql/suggestion/syntaxSuggestion.test.ts b/test/parser/postgresql/suggestion/syntaxSuggestion.test.ts index 67dc2084..e52dd25f 100644 --- a/test/parser/postgresql/suggestion/syntaxSuggestion.test.ts +++ b/test/parser/postgresql/suggestion/syntaxSuggestion.test.ts @@ -32,7 +32,33 @@ describe('Postgre SQL Syntax Suggestion', () => { ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'tb']); + expect(suggestion?.wordRanges?.length).toBe(3); + expect(suggestion?.wordRanges).toEqual([ + { + text: 'db', + line: 3, + startIndex: 88, + endIndex: 89, + startColumn: 13, + endColumn: 15, + }, + { + text: '.', + line: 3, + startIndex: 90, + endIndex: 90, + startColumn: 15, + endColumn: 16, + }, + { + text: 'tb', + line: 3, + startIndex: 91, + endIndex: 92, + startColumn: 16, + endColumn: 18, + }, + ]); }); test('Alter table ', () => { diff --git a/test/parser/spark/suggestion/multipleStatement.test.ts b/test/parser/spark/suggestion/multipleStatement.test.ts index a65b020e..04ee292f 100644 --- a/test/parser/spark/suggestion/multipleStatement.test.ts +++ b/test/parser/spark/suggestion/multipleStatement.test.ts @@ -36,7 +36,25 @@ describe('SparkSQL Multiple Statements Syntax Suggestion', () => { ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']); + expect(suggestion?.wordRanges?.length).toBe(2); + expect(suggestion?.wordRanges).toEqual([ + { + endColumn: 17, + endIndex: 258, + line: 9, + startColumn: 15, + startIndex: 257, + text: 'db', + }, + { + endColumn: 18, + endIndex: 259, + line: 9, + startColumn: 17, + startIndex: 259, + text: '.', + }, + ]); }); test('Insert into table ', () => { diff --git a/test/parser/spark/suggestion/syntaxSuggestion.test.ts b/test/parser/spark/suggestion/syntaxSuggestion.test.ts index 8e5cdb36..4bb8245f 100644 --- a/test/parser/spark/suggestion/syntaxSuggestion.test.ts +++ b/test/parser/spark/suggestion/syntaxSuggestion.test.ts @@ -32,7 +32,33 @@ describe('Spark SQL Syntax Suggestion', () => { ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'tb']); + expect(suggestion?.wordRanges?.length).toBe(3); + expect(suggestion?.wordRanges).toEqual([ + { + text: 'db', + line: 1, + startIndex: 12, + endIndex: 13, + startColumn: 13, + endColumn: 15, + }, + { + text: '.', + line: 1, + startIndex: 14, + endIndex: 14, + startColumn: 15, + endColumn: 16, + }, + { + text: 'tb', + line: 1, + startIndex: 15, + endIndex: 16, + startColumn: 16, + endColumn: 18, + }, + ]); }); test('Select table ', () => { diff --git a/test/parser/trino/suggestion/multipleStatement.test.ts b/test/parser/trino/suggestion/multipleStatement.test.ts index a224030d..1d8c1fb9 100644 --- a/test/parser/trino/suggestion/multipleStatement.test.ts +++ b/test/parser/trino/suggestion/multipleStatement.test.ts @@ -36,7 +36,25 @@ describe('TrinoSQL Multiple Statements Syntax Suggestion', () => { ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']); + expect(suggestion?.wordRanges?.length).toBe(2); + expect(suggestion?.wordRanges).toEqual([ + { + text: 'db', + line: 9, + startIndex: 137, + endIndex: 138, + startColumn: 17, + endColumn: 19, + }, + { + text: '.', + line: 9, + startIndex: 139, + endIndex: 139, + startColumn: 19, + endColumn: 20, + }, + ]); }); test('Insert into table ', () => { diff --git a/test/parser/trino/suggestion/syntaxSuggestion.test.ts b/test/parser/trino/suggestion/syntaxSuggestion.test.ts index 78bcb193..864ba751 100644 --- a/test/parser/trino/suggestion/syntaxSuggestion.test.ts +++ b/test/parser/trino/suggestion/syntaxSuggestion.test.ts @@ -32,7 +32,33 @@ describe('Trino SQL Syntax Suggestion', () => { (syn) => syn.syntaxContextType === EntityContextType.TABLE ); expect(suggestion).not.toBeUndefined(); - expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'tb']); + expect(suggestion?.wordRanges?.length).toBe(3); + expect(suggestion?.wordRanges).toEqual([ + { + text: 'db', + line: 1, + startIndex: 12, + endIndex: 13, + startColumn: 13, + endColumn: 15, + }, + { + text: '.', + line: 1, + startIndex: 14, + endIndex: 14, + startColumn: 15, + endColumn: 16, + }, + { + text: 'tb', + line: 1, + startIndex: 15, + endIndex: 16, + startColumn: 16, + endColumn: 18, + }, + ]); }); test('Select table ', () => {