-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility function for lsp macro completion
- Loading branch information
Showing
6 changed files
with
97 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
58 changes: 58 additions & 0 deletions
58
packages/lsp-tools/src/auto-completer/methods/get-completion-context.ts
This file contains 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,58 @@ | ||
import { RowCol } from "../../doenet-source-object"; | ||
import { DastMacro, showCursor } from "@doenet/parser"; | ||
import { AutoCompleter } from ".."; | ||
|
||
export type CompletionContext = | ||
| { cursorPos: "body" } | ||
| { cursorPos: "element"; complete: boolean; } | ||
| { cursorPos: "macro"; complete: boolean;node: DastMacro | null }; | ||
|
||
/** | ||
* Get context about the current cursor position to determine whether completions should be offered or not, | ||
* and what type of completions should be offered. | ||
*/ | ||
export function getCompletionContext( | ||
this: AutoCompleter, | ||
offset: number | RowCol, | ||
): CompletionContext { | ||
if (typeof offset !== "number") { | ||
offset = this.sourceObj.rowColToOffset(offset); | ||
} | ||
|
||
const prevChar = this.sourceObj.source.charAt(offset - 1); | ||
const prevPrevChar = this.sourceObj.source.charAt(offset - 2); | ||
const nextChar = this.sourceObj.source.charAt(offset + 1); | ||
let prevNonWhitespaceCharOffset = offset - 1; | ||
while ( | ||
this.sourceObj.source | ||
.charAt(prevNonWhitespaceCharOffset) | ||
.match(/(\s|\n)/) | ||
) { | ||
prevNonWhitespaceCharOffset--; | ||
} | ||
const prevNonWhitespaceChar = this.sourceObj.source.charAt( | ||
prevNonWhitespaceCharOffset, | ||
); | ||
|
||
const leftNode = this.sourceObj.nodeAtOffset(offset, {side:"left"}); | ||
|
||
// Check for status inside a macro | ||
let macro = this.sourceObj.nodeAtOffset(offset, { | ||
type: "macro", | ||
side: "left", | ||
}); | ||
if (!macro && (prevChar === "." || prevChar === "[") && prevPrevChar !== ")") { | ||
macro = this.sourceObj.nodeAtOffset(offset - 1, { | ||
type: "macro", | ||
side: "left", | ||
}); | ||
} | ||
if (macro) { | ||
// Since macros are terminal, if the node to our immediate left is a macro, | ||
// the macro is complete. | ||
const complete = leftNode?.type === "macro"; | ||
return { cursorPos: "macro", complete, node: macro }; | ||
} | ||
|
||
return { cursorPos: "body" }; | ||
} |
This file contains 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 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 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 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