-
Notifications
You must be signed in to change notification settings - Fork 108
fix: #436 improve SQL keyword suggestions #485
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
Open
liuxy0551
wants to merge
1
commit into
DTStack:next
Choose a base branch
from
liuxy0551:fix_436
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+318
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ import { | |
| CaretPosition, | ||
| LOCALE_TYPE, | ||
| SemanticCollectOptions, | ||
| SuggestionOptions, | ||
| Suggestions, | ||
| SyntaxSuggestion, | ||
| } from './types'; | ||
|
|
@@ -48,6 +49,7 @@ export abstract class BasicSQL< | |
| protected _parseTree: PRC | null; | ||
| protected _parsedInput: string; | ||
| protected _parseErrors: ParseError[] = []; | ||
| private _statementStartTokenTypes: Set<number> | null = null; | ||
| /** members for cache end */ | ||
|
|
||
| private _errorListener: ErrorListener = (error) => { | ||
|
|
@@ -555,15 +557,95 @@ export abstract class BasicSQL< | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Get the minimum statement tree for collecting completion candidates | ||
| */ | ||
| private getSuggestionParseTree( | ||
| parseTree: ParserRuleContext, | ||
| caretTokenIndex: number | ||
| ): ParserRuleContext { | ||
| const children = parseTree.children; | ||
| if (!children?.length) return parseTree; | ||
|
|
||
| for (let index = children.length - 1; index >= 0; index--) { | ||
| const child = children[index]; | ||
| if (!(child instanceof ParserRuleContext)) continue; | ||
|
|
||
| const startTokenIndex = child.start?.tokenIndex; | ||
| const stopTokenIndex = child.stop?.tokenIndex; | ||
| if ( | ||
| startTokenIndex === undefined || | ||
| stopTokenIndex === undefined || | ||
| startTokenIndex > caretTokenIndex | ||
| ) | ||
| continue; | ||
|
|
||
| // Use the current statement tree when the caret is inside it | ||
| if (stopTokenIndex >= caretTokenIndex) return child; | ||
|
|
||
| // Keep using the current statement until it ends with a semicolon | ||
| return child.stop?.text === SQL_SPLIT_SYMBOL_TEXT ? parseTree : child; | ||
| } | ||
|
|
||
| return parseTree; | ||
| } | ||
|
|
||
| /** | ||
| * Collect candidates for the current statement and remove new-statement-only keywords | ||
| */ | ||
| private collectSuggestionCandidates( | ||
| parser: Parser, | ||
| parseTree: ParserRuleContext, | ||
| caretTokenIndex: number | ||
| ): CandidatesCollection { | ||
| const core = new CodeCompletionCore(parser); | ||
| core.preferredRules = this.preferredRules; | ||
| const candidates = core.collectCandidates(caretTokenIndex, parseTree); | ||
| const suggestionParseTree = this.getSuggestionParseTree(parseTree, caretTokenIndex); | ||
|
|
||
| if (suggestionParseTree === parseTree) return candidates; | ||
|
|
||
| const statementCore = new CodeCompletionCore(parser); | ||
| statementCore.preferredRules = this.preferredRules; | ||
| const statementCandidates = statementCore.collectCandidates( | ||
| caretTokenIndex, | ||
| suggestionParseTree | ||
| ); | ||
|
|
||
| if (this._statementStartTokenTypes === null) { | ||
| const statementStartCore = new CodeCompletionCore(parser); | ||
| statementStartCore.preferredRules = this.preferredRules; | ||
| const statementStartCandidates = statementStartCore.collectCandidates(0, parseTree); | ||
| this._statementStartTokenTypes = new Set(statementStartCandidates.tokens.keys()); | ||
| } | ||
|
|
||
| const tokens = new Map(candidates.tokens); | ||
| for (const tokenType of this._statementStartTokenTypes) { | ||
| if (!statementCandidates.tokens.has(tokenType)) { | ||
| tokens.delete(tokenType); | ||
| } else if (tokens.has(tokenType)) { | ||
| // Use the current statement follow-list to preserve valid combined keywords | ||
| tokens.set(tokenType, statementCandidates.tokens.get(tokenType)!); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| rules: candidates.rules, | ||
| tokens, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Get suggestions of syntax and token at caretPosition | ||
| * @param input source string | ||
| * @param caretPosition caret position, such as cursor position | ||
| * @param options suggestion options | ||
| * @returns suggestion | ||
| */ | ||
| public getSuggestionAtCaretPosition( | ||
| input: string, | ||
| caretPosition: CaretPosition | ||
| caretPosition: CaretPosition, | ||
| options?: SuggestionOptions | ||
| ): Suggestions | null { | ||
| this.parseWithCache(input); | ||
| if (!this._parseTree) return null; | ||
|
|
@@ -614,12 +696,11 @@ export abstract class BasicSQL< | |
| parseTree = sqlParserIns.program(); | ||
| } | ||
|
|
||
| const core = new CodeCompletionCore(sqlParserIns); | ||
| core.preferredRules = this.preferredRules; | ||
| // core.showRuleStack = true; | ||
| // core.showResult = true; | ||
|
|
||
| const candidates = core.collectCandidates(caretTokenIndex, parseTree); | ||
| const candidates = this.collectSuggestionCandidates( | ||
| sqlParserIns, | ||
| parseTree, | ||
| caretTokenIndex | ||
| ); | ||
|
Comment on lines
+699
to
+703
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. collectSuggestionCandidates 在最坏情况下会跑 3 次 collectCandidates(整棵树 + 语句树 + 仅在首次缓存的语句起始 token 集)。常见未切片场景是 2 次。对大 SQL 输入这是可感知的额外开销。建议补充一个针对大输入的 benchmark,确认回归在可接受范围。 |
||
| const originalSuggestions = this.processCandidates(candidates, allTokens, caretTokenIndex); | ||
|
|
||
| const syntaxSuggestions: SyntaxSuggestion<WordRange>[] = originalSuggestions.syntax.map( | ||
|
|
@@ -633,9 +714,14 @@ export abstract class BasicSQL< | |
| }; | ||
| } | ||
| ); | ||
| const keywordFilter = options?.keywordFilter; | ||
| const keywords = keywordFilter | ||
| ? originalSuggestions.keywords.filter((keyword) => keywordFilter(keyword)) | ||
| : originalSuggestions.keywords; | ||
|
|
||
| return { | ||
| syntax: syntaxSuggestions, | ||
| keywords: originalSuggestions.keywords, | ||
| keywords, | ||
| }; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你这里是只向下看一层,它假设 program 的直接子节点就是语句。一旦文法把语句包了一层中间规则(如 program → batch → statement,或 list 规则),收窄会静默回退到整棵树,修复悄悄失效且无报错,确认下这个链路在这种情况下是否有问题?是否需要加一条断言/测试守护该假设?