-
Notifications
You must be signed in to change notification settings - Fork 48
Fix autocomplete suggesting erratically & add more suggestions #866
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
Merged
alexjpwalker
merged 19 commits into
typedb:development
from
krishnangovindraj:update-typeql-autocomplete
Jun 23, 2025
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
321227a
Update autocomplete
krishnangovindraj 288a0e8
Cleanup some comments
krishnangovindraj 8c34fda
Eeps. Why can't i select autocomplete stuff
krishnangovindraj 1227834
OOf, works somewhat ok
krishnangovindraj 1f4abe9
Rearrange
krishnangovindraj 52acba1
More clenaups, esp with navigation helpers
krishnangovindraj cdbbc8a
Suggestions for has and links constraints are nicely scoped
krishnangovindraj 80c0101
Fix boost in variables not being used
krishnangovindraj 5fde472
Minor refactors
krishnangovindraj 150f613
Add TODO to remove window.OC_lastQueryAnswers
krishnangovindraj 8d0bc18
Retry CI
krishnangovindraj 7297973
Post rebase working in
krishnangovindraj 79d49e5
Update deriving partial schema from text editor state
krishnangovindraj 0c7cd20
Plug in schema from schema state to autocomplete
krishnangovindraj edca4e1
Small cleanup of todos and unused imports
krishnangovindraj a7ce29c
Move lezer to the appropriate line in devDependencies
krishnangovindraj 0f6b708
Update names for rebase
krishnangovindraj 6b76ebb
run pnpm install
krishnangovindraj d9c6388
Cleanup comment from index.ts
krishnangovindraj 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
|
|
||
| import { CompletionContext, Completion, CompletionResult } from "@codemirror/autocomplete"; | ||
| import { syntaxTree } from "@codemirror/language" | ||
| import { SyntaxNode, NodeType, Tree } from "@lezer/common" | ||
|
|
||
| type TokenID = number; | ||
| export interface SuggestionMap<STATE> { | ||
| [key: TokenID]: SuffixOfPrefixSuggestion<STATE>[] | ||
| } | ||
|
|
||
|
|
||
| export type SuffixCandidate = TokenID[]; // A SuffixCandidate 's' "matches" a prefix if prefix[-s.length:] == s | ||
| export interface SuffixOfPrefixSuggestion<STATE> { | ||
| suffixes: SuffixCandidate[], // If any of the suffix candidates match, the suggestions will be used. | ||
| suggestions: SuggestionFunction<STATE>[] | ||
| } | ||
|
|
||
| export type SuggestionFunction<STATE> = (context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], state: STATE) => Completion[] | null; | ||
|
|
||
| export function suggest(type: string, label: string, boost: number = 0): Completion { | ||
| // type (docs): used to pick an icon to show for the completion. Icons are styled with a CSS class created by appending the type name to "cm-completionIcon-". | ||
| return { | ||
| label: label, | ||
| type: type, | ||
| apply: label, | ||
| info: type, | ||
| boost: boost, | ||
| }; | ||
| } | ||
|
|
||
| interface NodePrefixAutoCompleteState { | ||
| mayUpdateFromEditorState(context: CompletionContext, tree: Tree): void; | ||
| } | ||
|
|
||
| // See: https://codemirror.net/examples/autocompletion/ and maybe the SQL / HTML Example there. | ||
| export class NodePrefixAutoComplete<STATE extends NodePrefixAutoCompleteState> { | ||
| suggestionMap: SuggestionMap<STATE>; | ||
| suggestorState: STATE; | ||
|
|
||
| constructor(suggestionMap: SuggestionMap<STATE>, suggestorState: STATE) { | ||
| // This is where we would set up the autocompletion, but we do it in the index.ts file. | ||
| // See: https://codemirror.net/docs/ref/#autocomplete.autocompletion | ||
| this.suggestionMap = suggestionMap; | ||
| this.suggestorState = suggestorState; | ||
| } | ||
|
|
||
| getState(): STATE { | ||
| return this.suggestorState; | ||
| } | ||
|
|
||
| autocomplete(context: CompletionContext): CompletionResult | null { | ||
| let tree: Tree = syntaxTree(context.state); | ||
| this.suggestorState.mayUpdateFromEditorState(context, tree); | ||
| let currentNode: SyntaxNode = tree.resolveInner(context.pos, -1); // https://lezer.codemirror.net/docs/ref/#common.SyntaxNode | ||
| let options = this.getSuggestions(context, tree, currentNode); | ||
| if (options != null) { | ||
| // And once we figure out, we have to create a list of completion objects | ||
| // It may be worth changing the grammar to be able to do this more easily, rather than replicate the original TypeQL grammar. | ||
| // https://codemirror.net/docs/ref/#autocomplete.Completion | ||
| let from = findStartOfCompletion(context) + 1; | ||
| return { | ||
| from: from, | ||
| options: options, | ||
| // Docs: "regular expression that tells the extension that, as long as the updated input (the range between the result's from property and the completion point) matches that value, it can continue to use the list of completions." | ||
| validFor: /^([\w\$]+)?$/ | ||
| } | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| getSuggestions(context: CompletionContext, tree: Tree, parseAt: SyntaxNode): Completion[] | null { | ||
| return this.climbTillWeRecogniseSomething(context, tree, parseAt, parseAt, collectPrecedingChildrenOf(context, parseAt)); | ||
| } | ||
|
|
||
|
|
||
| climbTillWeRecogniseSomething(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode | null, prefix: NodeType[]): Completion[] | null { | ||
| if (climbedTo == null) { | ||
| // Uncomment this if you don't see suggestions | ||
| // this.logInterestingStuff(context, tree, parseAt, climbedTo, prefix); | ||
| return null; | ||
| } | ||
| let suggestionEither = this.suggestionMap[climbedTo.type.id]; | ||
| if (suggestionEither != null) { | ||
| for (let sops of (suggestionEither as SuffixOfPrefixSuggestion<STATE>[])) { | ||
| if (prefixHasAnyOfSuffixes(prefix, sops.suffixes)) { | ||
| return this.combineSuggestions(context, tree, parseAt, climbedTo, prefix, sops.suggestions); | ||
| } | ||
| } | ||
| // None match? Fall through. | ||
| // console.log("Fell through!!!: ", climbedTo.type.name, "with prefix", prefix); | ||
| } | ||
| let newPrefix = collectSiblingsOf(climbedTo).concat(prefix); | ||
| return this.climbTillWeRecogniseSomething(context, tree, parseAt, climbedTo.parent, newPrefix); | ||
| } | ||
|
|
||
|
|
||
| combineSuggestions(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], suggestionFunctions: SuggestionFunction<STATE>[]): Completion[] { | ||
| let suggestions = suggestionFunctions.map((f) => { | ||
| return f(context, tree, parseAt, climbedTo, prefix, this.suggestorState); | ||
| }).reduce((acc, curr) => { | ||
| return (curr == null) ? acc : acc!.concat(curr); | ||
| }, []); | ||
| // console.log("Matched:", climbedTo.type.name, "with prefix", prefix, ". Suggestions:", suggestions); | ||
| return suggestions!; | ||
| } | ||
|
|
||
| logInterestingStuff(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode | null, prefix: NodeType[]) { | ||
| console.log("Current Node:", parseAt.name); | ||
| console.log("ClimbedTo Node:", climbedTo?.name); | ||
|
|
||
| let at: SyntaxNode | null = parseAt; | ||
| let climbThrough = []; | ||
| while (at != null && at.name != climbedTo?.name) { | ||
| climbThrough.push(at.name); | ||
| at = at.parent; | ||
| } | ||
| climbThrough.push(at?.name); | ||
| console.log("Climbed through", climbThrough); | ||
| console.log("Prefix:", prefix); | ||
| } | ||
| } | ||
|
|
||
| function isPartOfWord(s: string): boolean { | ||
| let matches = s.match(/^[A-Za-z0-9_\-\$]+/); | ||
| return matches != null && matches.length > 0; | ||
| } | ||
|
|
||
| function findStartOfCompletion(context: CompletionContext): number { | ||
| let str = context.state.doc.sliceString(0, context.pos); | ||
| let at = context.pos - 1; | ||
| while (at >= 0 && isPartOfWord(str.charAt(at))) { | ||
| at -= 1; | ||
| } | ||
| return at; | ||
| } | ||
|
|
||
| function collectSiblingsOf(node: SyntaxNode): NodeType[] { | ||
| let siblings = []; | ||
| let prev: SyntaxNode | null = node; | ||
| while (null != (prev = prev.prevSibling)) { | ||
| siblings.push(prev.type); | ||
| } | ||
| return siblings.reverse(); | ||
| } | ||
|
|
||
| function collectPrecedingChildrenOf(context: CompletionContext, node: SyntaxNode): NodeType[] { | ||
| let lastChild = node.childBefore(context.pos); | ||
| if (lastChild == null) { | ||
| return []; | ||
| } | ||
| let precedingChildren = collectSiblingsOf(lastChild); | ||
| precedingChildren.push(lastChild.type); | ||
| return precedingChildren; | ||
| } | ||
|
|
||
| function prefixHasAnyOfSuffixes(prefix: NodeType[], suffixes: SuffixCandidate[]): boolean { | ||
| for (let i = 0; i < suffixes.length; i++) { | ||
| if (prefixHasSuffix(prefix, suffixes[i])) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| function prefixHasSuffix(prefix: NodeType[], suffix: TokenID[]): boolean { | ||
| if (prefix.length < suffix.length) { | ||
| return false; | ||
| } | ||
| for (let i = 0; i < suffix.length; i++) { | ||
| if (prefix[prefix.length - suffix.length + i].id != suffix[i]) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.