Skip to content

Commit c57ad41

Browse files
langium/lsp: Added 'getReferenceCandidates(...)' hook to 'DefaultCompletionProvider' (#1385)
* allows to add completion-specific scope computation refinements * removed the 'filterCrossReference' hook method, as its functionality is covered by the new hook
1 parent fa5cc39 commit c57ad41

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

packages/langium/src/lsp/completion/completion-provider.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import * as ast from '../../languages/generated/ast.js';
2424
import { assignMandatoryProperties, getContainerOfType } from '../../utils/ast-utils.js';
2525
import { findDeclarationNodeAtOffset, findLeafNodeBeforeOffset } from '../../utils/cst-utils.js';
2626
import { getEntryRule, getExplicitRuleType } from '../../utils/grammar-utils.js';
27-
import { stream } from '../../utils/stream.js';
27+
import { stream, type Stream } from '../../utils/stream.js';
2828
import { findFirstFeatures, findNextFeatures } from './follow-element-computation.js';
2929

3030
export type CompletionAcceptor = (context: CompletionContext, value: CompletionValueItem) => void
@@ -428,18 +428,28 @@ export class DefaultCompletionProvider implements CompletionProvider {
428428
property: assignment.feature
429429
};
430430
try {
431-
const scope = this.scopeProvider.getScope(refInfo);
432-
scope.getAllElements().forEach(e => {
433-
if (this.filterCrossReference(context, e)) {
434-
acceptor(context, this.createReferenceCompletionItem(e));
435-
}
436-
});
431+
this.getReferenceCandidates(refInfo, context).forEach(
432+
c => acceptor(context, this.createReferenceCompletionItem(c))
433+
);
437434
} catch (err) {
438435
console.error(err);
439436
}
440437
}
441438
}
442439

440+
/**
441+
* Override this method to change how the stream of candidates is determined for a reference.
442+
* This way completion-specific modifications and refinements can be added to the proposals computation
443+
* beyond the rules being implemented in the scope provider, e.g. filtering.
444+
*
445+
* @param refInfo Information about the reference for which the candidates are requested.
446+
* @param _context Information about the completion request including document, cursor position, token under cursor, etc.
447+
* @returns A stream of all elements being valid for the given reference.
448+
*/
449+
protected getReferenceCandidates(refInfo: ReferenceInfo, _context: CompletionContext): Stream<AstNodeDescription> {
450+
return this.scopeProvider.getScope(refInfo).getAllElements();
451+
}
452+
443453
/**
444454
* Override this method to change how reference completion items are created.
445455
* To change the `kind` of a completion item, override the `NodeKindProvider` service instead.
@@ -456,10 +466,6 @@ export class DefaultCompletionProvider implements CompletionProvider {
456466
};
457467
}
458468

459-
protected filterCrossReference(_context: CompletionContext, _nodeDescription: AstNodeDescription): boolean {
460-
return true;
461-
}
462-
463469
protected completionForKeyword(context: CompletionContext, keyword: ast.Keyword, acceptor: CompletionAcceptor): MaybePromise<void> {
464470
if (!this.filterKeyword(context, keyword)) {
465471
return;

0 commit comments

Comments
 (0)