+178
@@ -0,0 +1,178 @@
|
||||
/******************************************************************************
|
||||
* Copyright 2021 TypeFox GmbH
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the MIT License, which is available in the project root.
|
||||
******************************************************************************/
|
||||
import type { CompletionItem, CompletionParams, TextEdit } from 'vscode-languageserver-protocol';
|
||||
import type { LangiumCompletionParser } from '../../parser/langium-parser.js';
|
||||
import type { NameProvider } from '../../references/name-provider.js';
|
||||
import type { ScopeProvider } from '../../references/scope-provider.js';
|
||||
import type { LangiumServices } from '../lsp-services.js';
|
||||
import type { AstNode, AstNodeDescription, AstReflection, CstNode, ReferenceInfo } from '../../syntax-tree.js';
|
||||
import type { CancellationToken } from '../../utils/cancellation.js';
|
||||
import type { MaybePromise } from '../../utils/promise-utils.js';
|
||||
import type { LangiumDocument, TextDocument } from '../../workspace/documents.js';
|
||||
import type { NextFeature } from './follow-element-computation.js';
|
||||
import type { NodeKindProvider } from '../node-kind-provider.js';
|
||||
import type { FuzzyMatcher } from '../fuzzy-matcher.js';
|
||||
import type { GrammarConfig } from '../../languages/grammar-config.js';
|
||||
import type { Lexer } from '../../parser/lexer.js';
|
||||
import type { DocumentationProvider } from '../../documentation/documentation-provider.js';
|
||||
import type { MarkupContent } from 'vscode-languageserver';
|
||||
import { CompletionItemKind, CompletionList, Position } from 'vscode-languageserver';
|
||||
import * as ast from '../../languages/generated/ast.js';
|
||||
import { type Stream } from '../../utils/stream.js';
|
||||
export type CompletionAcceptor = (context: CompletionContext, value: CompletionValueItem) => void;
|
||||
export type CompletionValueItem = ({
|
||||
label?: string;
|
||||
} | {
|
||||
node: AstNode;
|
||||
} | {
|
||||
nodeDescription: AstNodeDescription;
|
||||
}) & Partial<CompletionItem>;
|
||||
export interface CompletionContext {
|
||||
node?: AstNode;
|
||||
document: LangiumDocument;
|
||||
textDocument: TextDocument;
|
||||
features: NextFeature[];
|
||||
/**
|
||||
* Index at the start of the token related to this context.
|
||||
* If the context performs completion for a token that doesn't exist yet, it is equal to the `offset`.
|
||||
*/
|
||||
tokenOffset: number;
|
||||
/**
|
||||
* Index at the end of the token related to this context, even if it is behind the cursor position.
|
||||
* Points at the first character after the last token.
|
||||
* If the context performs completion for a token that doesn't exist yet, it is equal to the `offset`.
|
||||
*/
|
||||
tokenEndOffset: number;
|
||||
/**
|
||||
* Index of the requested completed position.
|
||||
*/
|
||||
offset: number;
|
||||
position: Position;
|
||||
}
|
||||
export interface CompletionProviderOptions {
|
||||
/**
|
||||
* Most tools trigger completion request automatically without explicitly requesting
|
||||
* it using a keyboard shortcut (e.g. Ctrl+Space). Typically they do so when the user
|
||||
* starts to type an identifier. For example if the user types `c` in a JavaScript file
|
||||
* code complete will automatically pop up present `console` besides others as a
|
||||
* completion item. Characters that make up identifiers don't need to be listed here.
|
||||
*
|
||||
* If code complete should automatically be trigger on characters not being valid inside
|
||||
* an identifier (for example `.` in JavaScript) list them in `triggerCharacters`.
|
||||
*/
|
||||
triggerCharacters?: string[];
|
||||
/**
|
||||
* The list of all possible characters that commit a completion. This field can be used
|
||||
* if clients don't support individual commit characters per completion item.
|
||||
*
|
||||
* If a server provides both `allCommitCharacters` and commit characters on an individual
|
||||
* completion item the ones on the completion item win.
|
||||
*/
|
||||
allCommitCharacters?: string[];
|
||||
}
|
||||
export interface CompletionBacktrackingInformation {
|
||||
previousTokenStart?: number;
|
||||
previousTokenEnd?: number;
|
||||
nextTokenStart: number;
|
||||
nextTokenEnd: number;
|
||||
}
|
||||
export declare function mergeCompletionProviderOptions(options: Array<CompletionProviderOptions | undefined>): CompletionProviderOptions;
|
||||
/**
|
||||
* Language-specific service for handling completion requests.
|
||||
*/
|
||||
export interface CompletionProvider {
|
||||
/**
|
||||
* Handle a completion request.
|
||||
*
|
||||
* @param document - the document for which the completion request was triggered
|
||||
* @param params - the completion parameters
|
||||
* @param cancelToken - a token that can be used to cancel the request
|
||||
*
|
||||
* @throws `OperationCancelled` if cancellation is detected during execution
|
||||
* @throws `ResponseError` if an error is detected that should be sent as response to the client
|
||||
*/
|
||||
getCompletion(document: LangiumDocument, params: CompletionParams, cancelToken?: CancellationToken): MaybePromise<CompletionList | undefined>;
|
||||
/**
|
||||
* Contains the completion options for this completion provider.
|
||||
*
|
||||
* If multiple languages return different options, they are merged before being sent to the language client.
|
||||
*/
|
||||
readonly completionOptions?: CompletionProviderOptions;
|
||||
}
|
||||
export declare class DefaultCompletionProvider implements CompletionProvider {
|
||||
protected readonly completionParser: LangiumCompletionParser;
|
||||
protected readonly documentationProvider: DocumentationProvider;
|
||||
protected readonly scopeProvider: ScopeProvider;
|
||||
protected readonly grammar: ast.Grammar;
|
||||
protected readonly nameProvider: NameProvider;
|
||||
protected readonly lexer: Lexer;
|
||||
protected readonly nodeKindProvider: NodeKindProvider;
|
||||
protected readonly fuzzyMatcher: FuzzyMatcher;
|
||||
protected readonly grammarConfig: GrammarConfig;
|
||||
protected readonly astReflection: AstReflection;
|
||||
readonly completionOptions?: CompletionProviderOptions;
|
||||
constructor(services: LangiumServices);
|
||||
getCompletion(document: LangiumDocument, params: CompletionParams, _cancelToken?: CancellationToken): Promise<CompletionList | undefined>;
|
||||
/**
|
||||
* The completion algorithm could yield the same reference/keyword multiple times.
|
||||
*
|
||||
* This methods deduplicates these items afterwards before returning to the client.
|
||||
* Unique items are identified as a combination of `kind`, `label` and `detail`.
|
||||
*/
|
||||
protected deduplicateItems(items: CompletionItem[]): CompletionItem[];
|
||||
protected findFeaturesAt(document: TextDocument, offset: number): NextFeature[];
|
||||
protected buildSyntheticEntryRuleCall(rule: ast.ParserRule): NextFeature;
|
||||
protected buildContexts(document: LangiumDocument, position: Position): IterableIterator<CompletionContext>;
|
||||
protected performNextTokenCompletion(document: LangiumDocument, text: string, _offset: number, _end: number): boolean;
|
||||
protected findDataTypeRuleStart(cst: CstNode, offset: number): [number, number] | undefined;
|
||||
/**
|
||||
* Indicates whether the completion should continue to process the next completion context.
|
||||
*
|
||||
* The default implementation continues the completion only if there are currently no proposed completion items.
|
||||
*/
|
||||
protected continueCompletion(items: CompletionItem[]): boolean;
|
||||
/**
|
||||
* This method returns two sets of token offset information.
|
||||
*
|
||||
* The `nextToken*` offsets are related to the token at the cursor position.
|
||||
* If there is none, both offsets are simply set to `offset`.
|
||||
*
|
||||
* The `previousToken*` offsets are related to the last token before the current token at the cursor position.
|
||||
* They are `undefined`, if there is no token before the cursor position.
|
||||
*/
|
||||
protected backtrackToAnyToken(text: string, offset: number): CompletionBacktrackingInformation;
|
||||
protected completionFor(context: CompletionContext, next: NextFeature, acceptor: CompletionAcceptor): MaybePromise<void>;
|
||||
protected completionForCrossReference(context: CompletionContext, next: NextFeature<ast.CrossReference>, acceptor: CompletionAcceptor): MaybePromise<void>;
|
||||
/**
|
||||
* Override this method to change how the stream of candidates is determined for a reference.
|
||||
* This way completion-specific modifications and refinements can be added to the proposals computation
|
||||
* beyond the rules being implemented in the scope provider, e.g. filtering.
|
||||
*
|
||||
* @param refInfo Information about the reference for which the candidates are requested.
|
||||
* @param _context Information about the completion request including document, cursor position, token under cursor, etc.
|
||||
* @returns A stream of all elements being valid for the given reference.
|
||||
*/
|
||||
protected getReferenceCandidates(refInfo: ReferenceInfo, _context: CompletionContext): Stream<AstNodeDescription>;
|
||||
/**
|
||||
* Override this method to change how reference completion items are created.
|
||||
*
|
||||
* To change the `kind` of a completion item, override the `NodeKindProvider` service instead.
|
||||
* To change the `documentation`, override the `DocumentationProvider` service instead.
|
||||
*
|
||||
* @param nodeDescription The description of a reference candidate
|
||||
* @param _refInfo Information about the reference for which the candidate is proposed
|
||||
* @param _context The completion context
|
||||
* @returns A partial completion item
|
||||
*/
|
||||
protected createReferenceCompletionItem(nodeDescription: AstNodeDescription, _refInfo: ReferenceInfo, _context: CompletionContext): CompletionValueItem;
|
||||
protected getReferenceDocumentation(nodeDescription: AstNodeDescription): MarkupContent | string | undefined;
|
||||
protected completionForKeyword(context: CompletionContext, keyword: ast.Keyword, acceptor: CompletionAcceptor): MaybePromise<void>;
|
||||
protected getKeywordCompletionItemKind(_keyword: ast.Keyword): CompletionItemKind;
|
||||
protected filterKeyword(context: CompletionContext, keyword: ast.Keyword): boolean;
|
||||
protected fillCompletionItem(context: CompletionContext, item: CompletionValueItem): CompletionItem | undefined;
|
||||
protected buildCompletionTextEdit(context: CompletionContext, label: string, newText: string): TextEdit | undefined;
|
||||
}
|
||||
//# sourceMappingURL=completion-provider.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"completion-provider.d.ts","sourceRoot":"","sources":["../../../src/lsp/completion/completion-provider.ts"],"names":[],"mappings":"AAAA;;;;gFAIgF;AAEhF,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AACjG,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAC9E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AACtE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAC;AACxE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,KAAK,EAAE,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,OAAO,EAAsC,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACnJ,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAClF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AACnE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mCAAmC,CAAC;AACvE,OAAO,KAAK,EAAE,KAAK,EA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,CAAC,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,iBAAiB,GAAG,MAAM,CAAC,kBAAkB,CAAC;IAIjH;;;;;;;;;;OAUG;IACH,SAAS,CAAC,6BAA6B,CAAC,eAAe,EAAE,kBAAkB,EAAE,QAAQ,EAAE,aAAa,EAAE,QAAQ,EAAE,iBAAiB,GAAG,mBAAmB;IAYvJ,SAAS,CAAC,yBAAyB,CAAC,eAAe,EAAE,kBAAkB,GAAG,aAAa,GAAG,MAAM,GAAG,SAAS;IAW5G,SAAS,CAAC,oBAAoB,CAAC,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,QAAQ,EAAE,kBAAkB,GAAG,YAAY,CAAC,IAAI,CAAC;IAYlI,SAAS,CAAC,4BAA4B,CAAC,QAAQ,EAAE,GAAG,CAAC,OAAO,GAAG,kBAAkB;IAIjF,SAAS,CAAC,aAAa,CAAC,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,GAAG,OAAO;IAKlF,SAAS,CAAC,kBAAkB,CAAC,OAAO,EAAE,iBAAiB,EAAE,IAAI,EAAE,mBAAmB,GAAG,cAAc,GAAG,SAAS;IAmD/G,SAAS,CAAC,uBAAuB,CAAC,OAAO,EAAE,iBAAiB,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,QAAQ,GAAG,SAAS;CAiBtH"}
|
||||
+478
@@ -0,0 +1,478 @@
|
||||
/******************************************************************************
|
||||
* Copyright 2021 TypeFox GmbH
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the MIT License, which is available in the project root.
|
||||
******************************************************************************/
|
||||
import { CompletionItemKind, CompletionList, Position } from 'vscode-languageserver';
|
||||
import * as ast from '../../languages/generated/ast.js';
|
||||
import { assignMandatoryProperties, getContainerOfType } from '../../utils/ast-utils.js';
|
||||
import { findDeclarationNodeAtOffset, findLeafNodeBeforeOffset, getDatatypeNode } from '../../utils/cst-utils.js';
|
||||
import { getEntryRule } from '../../utils/grammar-utils.js';
|
||||
import { stream } from '../../utils/stream.js';
|
||||
import { findFirstFeatures, findNextFeatures } from './follow-element-computation.js';
|
||||
export function mergeCompletionProviderOptions(options) {
|
||||
const triggerCharacters = Array.from(new Set(options.flatMap(option => option?.triggerCharacters ?? [])));
|
||||
const allCommitCharacters = Array.from(new Set(options.flatMap(option => option?.allCommitCharacters ?? [])));
|
||||
return {
|
||||
triggerCharacters: triggerCharacters.length > 0 ? triggerCharacters : undefined,
|
||||
allCommitCharacters: allCommitCharacters.length > 0 ? allCommitCharacters : undefined
|
||||
};
|
||||
}
|
||||
export class DefaultCompletionProvider {
|
||||
constructor(services) {
|
||||
this.scopeProvider = services.references.ScopeProvider;
|
||||
this.grammar = services.Grammar;
|
||||
this.completionParser = services.parser.CompletionParser;
|
||||
this.nameProvider = services.references.NameProvider;
|
||||
this.lexer = services.parser.Lexer;
|
||||
this.nodeKindProvider = services.shared.lsp.NodeKindProvider;
|
||||
this.fuzzyMatcher = services.shared.lsp.FuzzyMatcher;
|
||||
this.grammarConfig = services.parser.GrammarConfig;
|
||||
this.astReflection = services.shared.AstReflection;
|
||||
this.documentationProvider = services.documentation.DocumentationProvider;
|
||||
}
|
||||
async getCompletion(document, params, _cancelToken) {
|
||||
const items = [];
|
||||
const contexts = this.buildContexts(document, params.position);
|
||||
const acceptor = (context, value) => {
|
||||
const completionItem = this.fillCompletionItem(context, value);
|
||||
if (completionItem) {
|
||||
items.push(completionItem);
|
||||
}
|
||||
};
|
||||
const distinctionFunction = (element) => {
|
||||
if (ast.isKeyword(element.feature)) {
|
||||
return element.feature.value;
|
||||
}
|
||||
else {
|
||||
return element.feature;
|
||||
}
|
||||
};
|
||||
const completedFeatures = [];
|
||||
for (const context of contexts) {
|
||||
await Promise.all(stream(context.features)
|
||||
.distinct(distinctionFunction)
|
||||
.exclude(completedFeatures)
|
||||
.map(e => this.completionFor(context, e, acceptor)));
|
||||
// Do not try to complete the same feature multiple times
|
||||
completedFeatures.push(...context.features);
|
||||
// We might want to stop computing completion results
|
||||
if (!this.continueCompletion(items)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return CompletionList.create(this.deduplicateItems(items), true);
|
||||
}
|
||||
/**
|
||||
* The completion algorithm could yield the same reference/keyword multiple times.
|
||||
*
|
||||
* This methods deduplicates these items afterwards before returning to the client.
|
||||
* Unique items are identified as a combination of `kind`, `label` and `detail`.
|
||||
*/
|
||||
deduplicateItems(items) {
|
||||
return stream(items).distinct(item => `${item.kind}_${item.label}_${item.detail}`).toArray();
|
||||
}
|
||||
findFeaturesAt(document, offset) {
|
||||
const text = document.getText({
|
||||
start: Position.create(0, 0),
|
||||
end: document.positionAt(offset)
|
||||
});
|
||||
const parserResult = this.completionParser.parse(text);
|
||||
const tokens = parserResult.tokens;
|
||||
// If the parser didn't parse any tokens, return the next features of the entry rule
|
||||
if (parserResult.tokenIndex === 0) {
|
||||
const parserRule = getEntryRule(this.grammar);
|
||||
// Generate a synthetic RuleCall to the entry rule
|
||||
const syntheticEntryRuleCall = this.buildSyntheticEntryRuleCall(parserRule);
|
||||
return findNextFeatures([[syntheticEntryRuleCall]], tokens);
|
||||
}
|
||||
const leftoverTokens = [...tokens].splice(parserResult.tokenIndex);
|
||||
const features = findNextFeatures([parserResult.elementStack.map(feature => ({ feature }))], leftoverTokens);
|
||||
return features;
|
||||
}
|
||||
buildSyntheticEntryRuleCall(rule) {
|
||||
// The "start" node is simply an empty group that is followed by the rule call
|
||||
const start = {
|
||||
$type: 'Group',
|
||||
$container: undefined,
|
||||
elements: []
|
||||
};
|
||||
const startNext = {
|
||||
feature: start
|
||||
};
|
||||
// This is the element that we want to complete
|
||||
const ruleCall = {
|
||||
$type: 'RuleCall',
|
||||
$container: undefined,
|
||||
rule: {
|
||||
ref: rule,
|
||||
$refText: rule.name
|
||||
},
|
||||
arguments: []
|
||||
};
|
||||
const group = {
|
||||
$type: 'Group',
|
||||
$container: undefined,
|
||||
elements: [
|
||||
start,
|
||||
ruleCall
|
||||
]
|
||||
};
|
||||
start.$container = group;
|
||||
ruleCall.$container = group;
|
||||
return startNext;
|
||||
}
|
||||
*buildContexts(document, position) {
|
||||
const cst = document.parseResult.value.$cstNode;
|
||||
if (!cst) {
|
||||
return;
|
||||
}
|
||||
const textDocument = document.textDocument;
|
||||
const text = textDocument.getText();
|
||||
const offset = textDocument.offsetAt(position);
|
||||
const partialContext = {
|
||||
document,
|
||||
textDocument,
|
||||
offset,
|
||||
position
|
||||
};
|
||||
// Data type rules need special handling, as their tokens are irrelevant for completion purposes.
|
||||
// If we encounter a data type rule at the current offset, we jump to the start of the data type rule.
|
||||
const dataTypeRuleOffsets = this.findDataTypeRuleStart(cst, offset);
|
||||
if (dataTypeRuleOffsets) {
|
||||
const [ruleStart, ruleEnd] = dataTypeRuleOffsets;
|
||||
const parentNode = findLeafNodeBeforeOffset(cst, ruleStart)?.astNode;
|
||||
yield {
|
||||
...partialContext,
|
||||
node: parentNode,
|
||||
tokenOffset: ruleStart,
|
||||
tokenEndOffset: ruleEnd,
|
||||
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 astNodeOffset = nextTokenStart;
|
||||
if (offset <= nextTokenStart && previousTokenStart !== undefined) {
|
||||
// This check indicates that the cursor is still before the next token, so we should use the previous AST node (if it exists)
|
||||
astNodeOffset = previousTokenStart;
|
||||
}
|
||||
const astNode = findLeafNodeBeforeOffset(cst, astNodeOffset)?.astNode;
|
||||
let performNextCompletion = true;
|
||||
if (previousTokenStart !== undefined && previousTokenEnd !== undefined && previousTokenEnd === offset) {
|
||||
// This context aims to complete the current feature
|
||||
yield {
|
||||
...partialContext,
|
||||
node: astNode,
|
||||
tokenOffset: previousTokenStart,
|
||||
tokenEndOffset: previousTokenEnd,
|
||||
features: this.findFeaturesAt(textDocument, previousTokenStart),
|
||||
};
|
||||
// The completion after the current token should be prevented in case we find out that the current token definitely isn't completed yet
|
||||
// This is usually the case when the current token ends on a letter.
|
||||
performNextCompletion = this.performNextTokenCompletion(document, text.substring(previousTokenStart, previousTokenEnd), previousTokenStart, previousTokenEnd);
|
||||
if (performNextCompletion) {
|
||||
// This context aims to complete the immediate next feature (if one exists at the current cursor position)
|
||||
// It uses the previous cst start/offset for that.
|
||||
yield {
|
||||
...partialContext,
|
||||
node: astNode,
|
||||
tokenOffset: previousTokenEnd,
|
||||
tokenEndOffset: previousTokenEnd,
|
||||
features: this.findFeaturesAt(textDocument, previousTokenEnd),
|
||||
};
|
||||
}
|
||||
}
|
||||
if (!astNode) {
|
||||
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: findFirstFeatures(parserRule.definition).map(f => f[f.length - 1]),
|
||||
};
|
||||
}
|
||||
else if (performNextCompletion) {
|
||||
// This context aims to complete the next feature, using the next cst start/end
|
||||
yield {
|
||||
...partialContext,
|
||||
node: astNode,
|
||||
tokenOffset: nextTokenStart,
|
||||
tokenEndOffset: nextTokenEnd,
|
||||
features: this.findFeaturesAt(textDocument, nextTokenStart),
|
||||
};
|
||||
}
|
||||
}
|
||||
performNextTokenCompletion(document, text, _offset, _end) {
|
||||
// This regex returns false if the text ends with a letter.
|
||||
// We don't want to complete new text immediately after a keyword, ID etc.
|
||||
// We only care about the last character in the text, so we use $ here.
|
||||
// The \P{L} used here is a Unicode category that matches any character that is not a letter
|
||||
return /\P{L}$/u.test(text);
|
||||
}
|
||||
findDataTypeRuleStart(cst, offset) {
|
||||
const containerNode = findDeclarationNodeAtOffset(cst, offset, this.grammarConfig.nameRegexp);
|
||||
if (!containerNode) {
|
||||
return undefined;
|
||||
}
|
||||
// Identify whether the element was parsed as part of a data type rule
|
||||
const fullNode = getDatatypeNode(containerNode);
|
||||
if (fullNode) {
|
||||
return [fullNode.offset, fullNode.end];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
/**
|
||||
* Indicates whether the completion should continue to process the next completion context.
|
||||
*
|
||||
* The default implementation continues the completion only if there are currently no proposed completion items.
|
||||
*/
|
||||
continueCompletion(items) {
|
||||
return items.length === 0;
|
||||
}
|
||||
/**
|
||||
* This method returns two sets of token offset information.
|
||||
*
|
||||
* The `nextToken*` offsets are related to the token at the cursor position.
|
||||
* If there is none, both offsets are simply set to `offset`.
|
||||
*
|
||||
* The `previousToken*` offsets are related to the last token before the current token at the cursor position.
|
||||
* They are `undefined`, if there is no token before the cursor position.
|
||||
*/
|
||||
backtrackToAnyToken(text, offset) {
|
||||
const tokens = this.lexer.tokenize(text).tokens;
|
||||
if (tokens.length === 0) {
|
||||
// If we don't have any tokens in our document, just return the offset position
|
||||
return {
|
||||
nextTokenStart: offset,
|
||||
nextTokenEnd: offset
|
||||
};
|
||||
}
|
||||
let previousToken;
|
||||
for (const token of tokens) {
|
||||
if (token.startOffset >= offset) {
|
||||
// We are between two tokens
|
||||
// Return the current offset as the next token index
|
||||
return {
|
||||
nextTokenStart: offset,
|
||||
nextTokenEnd: offset,
|
||||
previousTokenStart: previousToken ? previousToken.startOffset : undefined,
|
||||
previousTokenEnd: previousToken ? previousToken.endOffset + 1 : undefined
|
||||
};
|
||||
}
|
||||
if (token.endOffset >= offset) {
|
||||
// We are within a token
|
||||
// Return the current and previous token offsets as normal
|
||||
return {
|
||||
nextTokenStart: token.startOffset,
|
||||
nextTokenEnd: token.endOffset + 1,
|
||||
previousTokenStart: previousToken ? previousToken.startOffset : undefined,
|
||||
previousTokenEnd: previousToken ? previousToken.endOffset + 1 : undefined
|
||||
};
|
||||
}
|
||||
previousToken = token;
|
||||
}
|
||||
// We have run into the end of the file
|
||||
// Return the current offset as the next token index
|
||||
return {
|
||||
nextTokenStart: offset,
|
||||
nextTokenEnd: offset,
|
||||
previousTokenStart: previousToken ? previousToken.startOffset : undefined,
|
||||
previousTokenEnd: previousToken ? previousToken.endOffset + 1 : undefined
|
||||
};
|
||||
}
|
||||
completionFor(context, next, acceptor) {
|
||||
if (ast.isKeyword(next.feature)) {
|
||||
return this.completionForKeyword(context, next.feature, acceptor);
|
||||
}
|
||||
else if (ast.isCrossReference(next.feature) && context.node) {
|
||||
return this.completionForCrossReference(context, next, acceptor);
|
||||
}
|
||||
// Don't offer any completion for other elements (i.e. terminals, datatype rules)
|
||||
// We - from a framework level - cannot reasonably assume their contents.
|
||||
// Adopters can just override `completionFor` if they want to do that anyway.
|
||||
}
|
||||
completionForCrossReference(context, next, acceptor) {
|
||||
const assignment = getContainerOfType(next.feature, ast.isAssignment);
|
||||
let node = context.node;
|
||||
if (assignment && node) {
|
||||
if (next.type) {
|
||||
// When `type` is set, it indicates that we have just entered a new parser rule.
|
||||
// The cross reference that we're trying to complete is on a new element that doesn't exist yet.
|
||||
// So we create a new synthetic element with the correct type information.
|
||||
node = {
|
||||
$type: next.type,
|
||||
$container: node,
|
||||
$containerProperty: next.property
|
||||
};
|
||||
assignMandatoryProperties(this.astReflection, node);
|
||||
}
|
||||
let reference;
|
||||
if (next.feature.isMulti) {
|
||||
reference = {
|
||||
$refText: '',
|
||||
items: []
|
||||
};
|
||||
}
|
||||
else {
|
||||
reference = {
|
||||
$refText: '',
|
||||
ref: undefined
|
||||
};
|
||||
}
|
||||
const refInfo = {
|
||||
reference,
|
||||
container: node,
|
||||
property: assignment.feature
|
||||
};
|
||||
try {
|
||||
for (const candidate of this.getReferenceCandidates(refInfo, context)) {
|
||||
acceptor(context, this.createReferenceCompletionItem(candidate, refInfo, context));
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Override this method to change how the stream of candidates is determined for a reference.
|
||||
* This way completion-specific modifications and refinements can be added to the proposals computation
|
||||
* beyond the rules being implemented in the scope provider, e.g. filtering.
|
||||
*
|
||||
* @param refInfo Information about the reference for which the candidates are requested.
|
||||
* @param _context Information about the completion request including document, cursor position, token under cursor, etc.
|
||||
* @returns A stream of all elements being valid for the given reference.
|
||||
*/
|
||||
getReferenceCandidates(refInfo, _context) {
|
||||
return this.scopeProvider.getScope(refInfo).getAllElements();
|
||||
}
|
||||
/**
|
||||
* Override this method to change how reference completion items are created.
|
||||
*
|
||||
* To change the `kind` of a completion item, override the `NodeKindProvider` service instead.
|
||||
* To change the `documentation`, override the `DocumentationProvider` service instead.
|
||||
*
|
||||
* @param nodeDescription The description of a reference candidate
|
||||
* @param _refInfo Information about the reference for which the candidate is proposed
|
||||
* @param _context The completion context
|
||||
* @returns A partial completion item
|
||||
*/
|
||||
createReferenceCompletionItem(nodeDescription, _refInfo, _context) {
|
||||
const kind = this.nodeKindProvider.getCompletionItemKind(nodeDescription);
|
||||
const documentation = this.getReferenceDocumentation(nodeDescription);
|
||||
return {
|
||||
nodeDescription,
|
||||
kind,
|
||||
documentation,
|
||||
detail: nodeDescription.type,
|
||||
sortText: '0'
|
||||
};
|
||||
}
|
||||
getReferenceDocumentation(nodeDescription) {
|
||||
if (!nodeDescription.node) {
|
||||
return undefined;
|
||||
}
|
||||
const documentationText = this.documentationProvider.getDocumentation(nodeDescription.node);
|
||||
if (!documentationText) {
|
||||
return undefined;
|
||||
}
|
||||
return { kind: 'markdown', value: documentationText };
|
||||
}
|
||||
completionForKeyword(context, keyword, acceptor) {
|
||||
if (!this.filterKeyword(context, keyword)) {
|
||||
return;
|
||||
}
|
||||
acceptor(context, {
|
||||
label: keyword.value,
|
||||
kind: this.getKeywordCompletionItemKind(keyword),
|
||||
detail: 'Keyword',
|
||||
sortText: '1'
|
||||
});
|
||||
}
|
||||
getKeywordCompletionItemKind(_keyword) {
|
||||
return CompletionItemKind.Keyword;
|
||||
}
|
||||
filterKeyword(context, keyword) {
|
||||
// Filter out keywords that do not contain any word character
|
||||
return /\p{L}/u.test(keyword.value);
|
||||
}
|
||||
fillCompletionItem(context, item) {
|
||||
let label;
|
||||
if (typeof item.label === 'string') {
|
||||
label = item.label;
|
||||
}
|
||||
else if ('node' in item) {
|
||||
const name = this.nameProvider.getName(item.node);
|
||||
if (!name) {
|
||||
return undefined;
|
||||
}
|
||||
label = name;
|
||||
}
|
||||
else if ('nodeDescription' in item) {
|
||||
label = item.nodeDescription.name;
|
||||
}
|
||||
else {
|
||||
return undefined;
|
||||
}
|
||||
let insertText;
|
||||
if (typeof item.textEdit?.newText === 'string') {
|
||||
insertText = item.textEdit.newText;
|
||||
}
|
||||
else if (typeof item.insertText === 'string') {
|
||||
insertText = item.insertText;
|
||||
}
|
||||
else {
|
||||
insertText = label;
|
||||
}
|
||||
const textEdit = item.textEdit ?? this.buildCompletionTextEdit(context, label, insertText);
|
||||
if (!textEdit) {
|
||||
return undefined;
|
||||
}
|
||||
// Copy all valid properties of `CompletionItem`
|
||||
const completionItem = {
|
||||
additionalTextEdits: item.additionalTextEdits,
|
||||
command: item.command,
|
||||
commitCharacters: item.commitCharacters,
|
||||
data: item.data,
|
||||
detail: item.detail,
|
||||
documentation: item.documentation,
|
||||
filterText: item.filterText,
|
||||
insertText: item.insertText,
|
||||
insertTextFormat: item.insertTextFormat,
|
||||
insertTextMode: item.insertTextMode,
|
||||
kind: item.kind,
|
||||
labelDetails: item.labelDetails,
|
||||
preselect: item.preselect,
|
||||
sortText: item.sortText,
|
||||
tags: item.tags,
|
||||
textEditText: item.textEditText,
|
||||
textEdit,
|
||||
label
|
||||
};
|
||||
return completionItem;
|
||||
}
|
||||
buildCompletionTextEdit(context, label, newText) {
|
||||
const content = context.textDocument.getText();
|
||||
const identifier = content.substring(context.tokenOffset, context.offset);
|
||||
if (this.fuzzyMatcher.match(identifier, label)) {
|
||||
const start = context.textDocument.positionAt(context.tokenOffset);
|
||||
const end = context.position;
|
||||
return {
|
||||
newText,
|
||||
range: {
|
||||
start,
|
||||
end
|
||||
}
|
||||
};
|
||||
}
|
||||
else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=completion-provider.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+39
@@ -0,0 +1,39 @@
|
||||
/******************************************************************************
|
||||
* Copyright 2021 TypeFox GmbH
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the MIT License, which is available in the project root.
|
||||
******************************************************************************/
|
||||
import type { IToken } from 'chevrotain';
|
||||
import * as ast from '../../languages/generated/ast.js';
|
||||
export interface NextFeature<T extends ast.AbstractElement = ast.AbstractElement> {
|
||||
/**
|
||||
* A feature that could appear during completion.
|
||||
*/
|
||||
feature: T;
|
||||
/**
|
||||
* The type that carries this `feature`. Only set if we encounter a new type.
|
||||
*/
|
||||
type?: string;
|
||||
/**
|
||||
* The container property for the new `type`
|
||||
*/
|
||||
property?: string;
|
||||
}
|
||||
/**
|
||||
* Calculates any features that can follow the given feature stack.
|
||||
* This also includes features following optional features and features from previously called rules that could follow the last feature.
|
||||
* @param featureStack A stack of features starting at the entry rule and ending at the feature of the current cursor position.
|
||||
* @param unparsedTokens All tokens which haven't been parsed successfully yet. This is the case when we call this function inside an alternative or before parsing any tokens.
|
||||
* @returns Any `AbstractElement` that could be following the given feature stack.
|
||||
*/
|
||||
export declare function findNextFeatures(featureStack: NextFeature[][], unparsedTokens: IToken[]): NextFeature[];
|
||||
/**
|
||||
* Calculates the first child feature of any `AbstractElement`.
|
||||
* @param next The `AbstractElement` whose first child features should be calculated.
|
||||
*/
|
||||
export declare function findFirstFeatures(next: ast.AbstractElement): NextFeature[][];
|
||||
export declare function findGroupAndChild(feature: ast.AbstractElement): {
|
||||
group: ast.Group | undefined;
|
||||
child: ast.AbstractElement;
|
||||
};
|
||||
//# sourceMappingURL=follow-element-computation.d.ts.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"follow-element-computation.d.ts","sourceRoot":"","sources":["../../../src/lsp/completion/follow-element-computation.ts"],"names":[],"mappings":"AAAA;;;;gFAIgF;AAEhF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,KAAK,GAAG,MAAM,kCAAkC,CAAC;AAIxD,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,GAAG,CAAC,eAAe,GAAG,GAAG,CAAC,eAAe;IAC5E;;OAEG;IACH,OAAO,EAAE,CAAC,CAAA;IACV;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;IACb;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;CACpB;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,YAAY,EAAE,WAAW,EAAE,EAAE,EAAE,cAAc,EAAE,MAAM,EAAE,GAAG,WAAW,EAAE,CAWvG;AAyED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,GAAG,CAAC,eAAe,GAAG,WAAW,EAAE,EAAE,CAE5E;AA4JD,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,GAAG,CAAC,eAAe,GAAG;IAAE,KAAK,EAAE,GAAG,CAAC,KAAK,GAAG,SAAS,CAAC;IAAC,KAAK,EAAE,GAAG,CAAC,eAAe,CAAA;CAAE,CAsB5H"}
|
||||
+312
@@ -0,0 +1,312 @@
|
||||
/******************************************************************************
|
||||
* Copyright 2021 TypeFox GmbH
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the MIT License, which is available in the project root.
|
||||
******************************************************************************/
|
||||
import * as ast from '../../languages/generated/ast.js';
|
||||
import { getCrossReferenceTerminal, getExplicitRuleType, getTypeName, isArrayCardinality, isOptionalCardinality, terminalRegex } from '../../utils/grammar-utils.js';
|
||||
import { getContainerOfType } from '../../utils/ast-utils.js';
|
||||
/**
|
||||
* Calculates any features that can follow the given feature stack.
|
||||
* This also includes features following optional features and features from previously called rules that could follow the last feature.
|
||||
* @param featureStack A stack of features starting at the entry rule and ending at the feature of the current cursor position.
|
||||
* @param unparsedTokens All tokens which haven't been parsed successfully yet. This is the case when we call this function inside an alternative or before parsing any tokens.
|
||||
* @returns Any `AbstractElement` that could be following the given feature stack.
|
||||
*/
|
||||
export function findNextFeatures(featureStack, unparsedTokens) {
|
||||
const context = {
|
||||
stacks: featureStack,
|
||||
tokens: unparsedTokens
|
||||
};
|
||||
interpretTokens(context);
|
||||
// Reset the container property
|
||||
context.stacks.flat().forEach(feature => { feature.property = undefined; });
|
||||
const nextStacks = findNextFeatureStacks(context.stacks);
|
||||
// We only need the last element of each stack
|
||||
return nextStacks.map(e => e[e.length - 1]);
|
||||
}
|
||||
function findNextFeaturesInternal(stack) {
|
||||
const features = [];
|
||||
if (stack.length === 0) {
|
||||
return features;
|
||||
}
|
||||
const top = stack[stack.length - 1];
|
||||
const { group, child } = findGroupAndChild(top.feature);
|
||||
if (isArrayCardinality(child.cardinality)) {
|
||||
// The feature can appear again, so we try to find its first features again
|
||||
const repeatingFeatures = findFirstFeaturesInternal({
|
||||
feature: child,
|
||||
type: top.type,
|
||||
property: top.property
|
||||
});
|
||||
features.push(...repeatingFeatures);
|
||||
}
|
||||
let groupIndex = -1;
|
||||
if (group) {
|
||||
groupIndex = group.elements.indexOf(child);
|
||||
}
|
||||
const parentStack = stack.slice(0, -1);
|
||||
if (!group) {
|
||||
const parent = getAbstractElementParent(child);
|
||||
if (parent) {
|
||||
// The feature is not part of a group
|
||||
// But the parent might be
|
||||
features.push(...findNextFeaturesInternal([
|
||||
...parentStack,
|
||||
{
|
||||
feature: parent,
|
||||
}
|
||||
]));
|
||||
}
|
||||
else {
|
||||
// The feature is "standalone", meaning it is the top-level feature of a rule
|
||||
// The next elements are defined by the previous stack elements
|
||||
features.push(...findNextFeaturesInternal(parentStack));
|
||||
}
|
||||
}
|
||||
else {
|
||||
// The feature is somewhere within the group
|
||||
const nextIndex = groupIndex + 1;
|
||||
const { stacks, end } = findNextFeaturesInGroup({
|
||||
feature: group,
|
||||
type: top.type
|
||||
}, nextIndex);
|
||||
for (const newStack of stacks) {
|
||||
features.push([...parentStack, ...newStack]);
|
||||
}
|
||||
// If we reached the end of the group, continue searching for following features
|
||||
if (end) {
|
||||
// Set groupIndex to the last element index to indicate that we are at the end of the group
|
||||
// This will trigger the parent search below
|
||||
groupIndex = group.elements.length - 1;
|
||||
}
|
||||
}
|
||||
if (group && groupIndex === group.elements.length - 1) {
|
||||
// The feature is the last element of a group
|
||||
// The next elements are defined by the parent group
|
||||
features.push(...findNextFeaturesInternal([
|
||||
...parentStack,
|
||||
{
|
||||
feature: group,
|
||||
}
|
||||
]));
|
||||
}
|
||||
return features;
|
||||
}
|
||||
/**
|
||||
* Calculates the first child feature of any `AbstractElement`.
|
||||
* @param next The `AbstractElement` whose first child features should be calculated.
|
||||
*/
|
||||
export function findFirstFeatures(next) {
|
||||
return findFirstFeaturesInternal({ feature: next });
|
||||
}
|
||||
function findFirstFeaturesInternal(next) {
|
||||
const { feature, type } = next;
|
||||
if (ast.isGroup(feature)) {
|
||||
return findNextFeaturesInGroup(next, 0).stacks;
|
||||
}
|
||||
else if (ast.isAlternatives(feature) || ast.isUnorderedGroup(feature)) {
|
||||
return feature.elements.flatMap(e => findFirstFeaturesInternal({
|
||||
feature: e,
|
||||
type,
|
||||
property: next.property
|
||||
}));
|
||||
}
|
||||
else if (ast.isAssignment(feature)) {
|
||||
return findFirstFeaturesInternal({
|
||||
feature: feature.terminal,
|
||||
type,
|
||||
property: next.property ?? feature.feature
|
||||
});
|
||||
}
|
||||
else if (ast.isAction(feature)) {
|
||||
return findNextFeaturesInternal([{
|
||||
feature,
|
||||
type: getTypeName(feature),
|
||||
property: next.property ?? feature.feature
|
||||
}]);
|
||||
}
|
||||
else if (ast.isRuleCall(feature) && ast.isParserRule(feature.rule.ref)) {
|
||||
const rule = feature.rule.ref;
|
||||
const stacks = findFirstFeaturesInternal({
|
||||
feature: rule.definition,
|
||||
type: rule.fragment || rule.dataType ? undefined : (getExplicitRuleType(rule) ?? rule.name),
|
||||
property: next.property
|
||||
});
|
||||
for (const stack of stacks) {
|
||||
stack.unshift(next);
|
||||
}
|
||||
return stacks;
|
||||
}
|
||||
else if (ast.isRuleCall(feature) && ast.isInfixRule(feature.rule.ref)) {
|
||||
const rule = feature.rule.ref;
|
||||
const call = rule.call.rule.ref;
|
||||
if (!ast.isParserRule(call)) {
|
||||
console.error('Failed to resolve reference to ' + rule.call.rule.$refText);
|
||||
return [];
|
||||
}
|
||||
const stacks = findFirstFeaturesInternal({
|
||||
feature: call.definition,
|
||||
type: getExplicitRuleType(call) ?? call.name,
|
||||
property: 'parts'
|
||||
});
|
||||
for (const stack of stacks) {
|
||||
stack.unshift(next);
|
||||
}
|
||||
return stacks;
|
||||
}
|
||||
else {
|
||||
return [[next]];
|
||||
}
|
||||
}
|
||||
function findNextFeaturesInGroup(next, index) {
|
||||
const features = [];
|
||||
let firstFeature;
|
||||
while (index < next.feature.elements.length) {
|
||||
const feature = next.feature.elements[index];
|
||||
firstFeature = {
|
||||
feature,
|
||||
type: next.type
|
||||
};
|
||||
const stacks = findFirstFeaturesInternal(firstFeature);
|
||||
features.push(...stacks);
|
||||
if (isElementOptional(feature, new Map())) {
|
||||
// Continue with the next element
|
||||
index++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return {
|
||||
stacks: features,
|
||||
end: index >= next.feature.elements.length
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Determines recursively whether an element is optional. It is not sufficient to check only the cardinality of the element itself,
|
||||
* because it could a group or alternative that contains only optional elements.
|
||||
*/
|
||||
function isElementOptional(feature, visited) {
|
||||
const visitedResult = visited.get(feature);
|
||||
if (visitedResult !== undefined) {
|
||||
return visitedResult;
|
||||
}
|
||||
visited.set(feature, false);
|
||||
if (isOptionalCardinality(feature.cardinality, feature)) {
|
||||
visited.set(feature, true);
|
||||
return true;
|
||||
}
|
||||
if (ast.isGroup(feature)) {
|
||||
for (const element of feature.elements) {
|
||||
if (!isElementOptional(element, visited)) {
|
||||
visited.set(feature, false);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
visited.set(feature, true);
|
||||
return true;
|
||||
}
|
||||
else if (ast.isAlternatives(feature) || ast.isUnorderedGroup(feature)) {
|
||||
for (const element of feature.elements) {
|
||||
if (isElementOptional(element, visited)) {
|
||||
visited.set(feature, true);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
visited.set(feature, false);
|
||||
return false;
|
||||
}
|
||||
else if (ast.isRuleCall(feature) && ast.isParserRule(feature.rule.ref)) {
|
||||
const rule = feature.rule.ref;
|
||||
const result = isElementOptional(rule.definition, visited);
|
||||
visited.set(feature, result);
|
||||
return result;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function interpretTokens(context) {
|
||||
for (const token of context.tokens) {
|
||||
const nextFeatureStacks = findNextFeatureStacks(context.stacks, token);
|
||||
context.stacks = nextFeatureStacks;
|
||||
}
|
||||
}
|
||||
function findNextFeatureStacks(stacks, token) {
|
||||
const newStacks = [];
|
||||
for (const stack of stacks) {
|
||||
newStacks.push(...interpretStackToken(stack, token));
|
||||
}
|
||||
return newStacks;
|
||||
}
|
||||
function interpretStackToken(stack, token) {
|
||||
const allNextFeatures = findNextFeaturesInternal(stack);
|
||||
const matchingNextFeatures = allNextFeatures.filter(next => token ? featureMatches(next[next.length - 1].feature, token) : true);
|
||||
return matchingNextFeatures;
|
||||
}
|
||||
export function findGroupAndChild(feature) {
|
||||
let parent;
|
||||
let item = feature;
|
||||
while (item.$container) {
|
||||
if (ast.isGroup(item.$container)) {
|
||||
parent = item.$container;
|
||||
break;
|
||||
}
|
||||
else if (ast.isAbstractElement(item.$container)) {
|
||||
item = item.$container;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (parent) {
|
||||
return { group: parent, child: item };
|
||||
}
|
||||
return {
|
||||
group: undefined,
|
||||
// Even if there is no group, return the feature parent if it is an assignment
|
||||
// We need this later to handle the cardinality of the feature correctly
|
||||
child: getContainerOfType(feature, ast.isAssignment) ?? feature
|
||||
};
|
||||
}
|
||||
function getAbstractElementParent(element) {
|
||||
const parent = element.$container;
|
||||
const assignment = getContainerOfType(parent, ast.isAssignment);
|
||||
if (assignment) {
|
||||
return getAbstractElementParent(assignment);
|
||||
}
|
||||
else {
|
||||
if (parent && ast.isAbstractElement(parent)) {
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
function featureMatches(feature, token) {
|
||||
if (ast.isKeyword(feature)) {
|
||||
const content = feature.value;
|
||||
return content === token.tokenType.name;
|
||||
}
|
||||
else if (ast.isRuleCall(feature)) {
|
||||
return ruleMatches(feature.rule.ref, token);
|
||||
}
|
||||
else if (ast.isCrossReference(feature)) {
|
||||
const crossRefTerminal = getCrossReferenceTerminal(feature);
|
||||
if (crossRefTerminal) {
|
||||
return featureMatches(crossRefTerminal, token);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function ruleMatches(rule, token) {
|
||||
if (ast.isParserRule(rule)) {
|
||||
const ruleFeatures = findFirstFeatures(rule.definition);
|
||||
return ruleFeatures.some(e => featureMatches(e[e.length - 1].feature, token));
|
||||
}
|
||||
else if (ast.isTerminalRule(rule)) {
|
||||
return terminalRegex(rule).test(token.image);
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=follow-element-computation.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user