Skip to content
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

Add new completion context after previous token #1239

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions packages/langium/src/lsp/completion/completion-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,49 +237,60 @@ export class DefaultCompletionProvider implements CompletionProvider {
if (dataTypeRuleOffsets) {
const [ruleStart, ruleEnd] = dataTypeRuleOffsets;
const parentNode = findLeafNodeAtOffset(cst, ruleStart)?.astNode;
const previousTokenFeatures = this.findFeaturesAt(textDocument, ruleStart);
yield {
...partialContext,
node: parentNode,
tokenOffset: ruleStart,
tokenEndOffset: ruleEnd,
features: previousTokenFeatures,
features: this.findFeaturesAt(textDocument, ruleStart),
};
}
// For all other purposes, it's enough to jump to the start of the current/previous token
const { nextTokenStart, nextTokenEnd, previousTokenStart, previousTokenEnd } = this.backtrackToAnyToken(text, offset);
let astNode: AstNode | undefined;
if (previousTokenStart !== undefined && previousTokenEnd !== undefined && previousTokenEnd === offset) {
astNode = findLeafNodeAtOffset(cst, previousTokenStart)?.astNode;
const previousTokenFeatures = this.findFeaturesAt(textDocument, previousTokenStart);
// This context aims to complete the current feature
yield {
...partialContext,
node: astNode,
tokenOffset: previousTokenStart,
tokenEndOffset: previousTokenEnd,
features: previousTokenFeatures,
features: this.findFeaturesAt(textDocument, previousTokenStart),
};
// This context aims to complete the immediate next feature (if one exists at the current cursor position)
// It uses the previous AST node for that.
yield {
...partialContext,
node: astNode,
tokenOffset: previousTokenEnd,
tokenEndOffset: previousTokenEnd,
features: this.findFeaturesAt(textDocument, previousTokenEnd),
};
}
astNode = findLeafNodeAtOffset(cst, nextTokenStart)?.astNode
?? (previousTokenStart === undefined ? undefined : findLeafNodeAtOffset(cst, previousTokenStart)?.astNode);

if (!astNode) {
const parserRule = getEntryRule(this.grammar)!;
const firstFeatures = findFirstFeatures(parserRule.definition);
const parserRule = getEntryRule(this.grammar);
if (!parserRule) {
throw new Error('Missing entry parser rule');
}
// This context aims to perform completion for the grammar start (usually when the document is empty)
yield {
...partialContext,
tokenOffset: nextTokenStart,
tokenEndOffset: nextTokenEnd,
features: firstFeatures
features: findFirstFeatures(parserRule.definition)
};
} else {
const nextTokenFeatures = this.findFeaturesAt(textDocument, nextTokenStart);
// This context aims to complete the next feature, using the next ast node
yield {
...partialContext,
node: astNode,
tokenOffset: nextTokenStart,
tokenEndOffset: nextTokenEnd,
features: nextTokenFeatures,
features: this.findFeaturesAt(textDocument, nextTokenStart),
};
}
}
Expand Down