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

introduced dedicated interface PrecomputedScopes, updated implementations of DefaultScopeComputation and DefaultScopeProvider #1788

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
introduced dedicated interface PrecomputedScopes, updated implement…
…ations of `DefaultScopeComputation` and `DefaultScopeProvider`

* added factory method to `DefaultScopeComputation`
* added `getStream()` to new interface in addition to existing `add`, `addAll`, and `get` resembling the methods `MultiMap` in collection.ts
  • Loading branch information
sailingKieler committed Jan 15, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 324fa41c4cb91b330565cf6d5af092324c9975df
10 changes: 5 additions & 5 deletions examples/domainmodel/src/language-server/domain-model-scope.ts
Original file line number Diff line number Diff line change
@@ -4,11 +4,11 @@
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import type { AstNode, AstNodeDescription, LangiumDocument, PrecomputedScopes } from 'langium';
import type { AstNodeDescription, LangiumDocument, PrecomputedScopes } from 'langium';
import type { DomainModelServices } from './domain-model-module.js';
import type { QualifiedNameProvider } from './domain-model-naming.js';
import type { Domainmodel, PackageDeclaration } from './generated/ast.js';
import { AstUtils, Cancellation, DefaultScopeComputation, interruptAndCheck, MultiMap } from 'langium';
import { AstUtils, Cancellation, DefaultScopeComputation, interruptAndCheck } from 'langium';
import { isType, isPackageDeclaration } from './generated/ast.js';

export class DomainModelScopeComputation extends DefaultScopeComputation {
@@ -40,9 +40,9 @@ export class DomainModelScopeComputation extends DefaultScopeComputation {
return descr;
}

override async computeLocalScopes(document: LangiumDocument, cancelToken = Cancellation.CancellationToken.None): Promise<PrecomputedScopes> {
const model = document.parseResult.value as Domainmodel;
const scopes = new MultiMap<AstNode, AstNodeDescription>();
override async computeLocalScopes(document: LangiumDocument<Domainmodel>, cancelToken = Cancellation.CancellationToken.None): Promise<PrecomputedScopes> {
const model = document.parseResult.value;
const scopes = this.newPrecomputedScopes(document);
await this.processContainer(model, scopes, document, cancelToken);
return scopes;
}
16 changes: 15 additions & 1 deletion packages/langium/src/references/scope-computation.ts
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ import { CancellationToken } from '../utils/cancellation.js';
import { streamAllContents, streamContents } from '../utils/ast-utils.js';
import { MultiMap } from '../utils/collections.js';
import { interruptAndCheck } from '../utils/promise-utils.js';
import { stream } from '../utils/stream.js';

/**
* Language-specific service for precomputing global and local scopes. The service methods are executed
@@ -113,7 +114,7 @@ export class DefaultScopeComputation implements ScopeComputation {

async computeLocalScopes(document: LangiumDocument, cancelToken = CancellationToken.None): Promise<PrecomputedScopes> {
const rootNode = document.parseResult.value;
const scopes = new MultiMap<AstNode, AstNodeDescription>();
const scopes = this.newPrecomputedScopes(document);
// Here we navigate the full AST - local scopes shall be available in the whole document
for (const node of streamAllContents(rootNode)) {
await interruptAndCheck(cancelToken);
@@ -122,6 +123,19 @@ export class DefaultScopeComputation implements ScopeComputation {
return scopes;
}

/**
* @returns A new precomputed scopes container instance for the given document.
*/
protected newPrecomputedScopes(_document: LangiumDocument): PrecomputedScopes {
const map = new MultiMap<AstNode, AstNodeDescription>();
return {
add: map.add.bind(map),
addAll: map.addAll.bind(map),
get: map.get.bind(map),
getStream: (key: AstNode) => stream(map.get(key))
};
}
Comment on lines +129 to +137
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: I don't think that the "default implementation" of PrecomputedScopes should be exclusively available as a method inside of the default implementation of ScopeComputation. Instead, I would much rather have a MultiMapPrecomputedScopes class, that can be used by adopters that don't want to override DefaultScopeComputation.


/**
* Process a single node during scopes computation. The default implementation makes the node visible
* in the subtree of its container (if the node has a name). Override this method to change this,
10 changes: 5 additions & 5 deletions packages/langium/src/references/scope-provider.ts
Original file line number Diff line number Diff line change
@@ -56,11 +56,11 @@ export class DefaultScopeProvider implements ScopeProvider {
if (precomputed) {
let currentNode: AstNode | undefined = context.container;
do {
const allDescriptions = precomputed.get(currentNode);
if (allDescriptions.length > 0) {
scopes.push(stream(allDescriptions).filter(
desc => this.reflection.isSubtype(desc.type, referenceType)));
}
scopes.push(
precomputed.getStream(currentNode).filter(
desc => this.reflection.isSubtype(desc.type, referenceType)
)
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates empty scopes, which was avoided in the previous version. The change might have a performance impact for larger documents. I suggest keeping a check for an empty stream.

currentNode = currentNode.$container;
} while (currentNode);
}
10 changes: 7 additions & 3 deletions packages/langium/src/workspace/documents.ts
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@ import type { ParseResult, ParserOptions } from '../parser/langium-parser.js';
import type { ServiceRegistry } from '../service-registry.js';
import type { LangiumSharedCoreServices } from '../services.js';
import type { AstNode, AstNodeDescription, Mutable, Reference } from '../syntax-tree.js';
import type { MultiMap } from '../utils/collections.js';
import type { Stream } from '../utils/stream.js';
import { TextDocument } from './documents.js';
import { CancellationToken } from '../utils/cancellation.js';
@@ -95,10 +94,15 @@ export enum DocumentState {
}

/**
* Result of the scope precomputation phase (`ScopeComputation` service).
* Result of the scope pre-computation phase (`ScopeComputation` service).
* It maps every AST node to the set of symbols that are visible in the subtree of that node.
*/
export type PrecomputedScopes = MultiMap<AstNode, AstNodeDescription>
export interface PrecomputedScopes {
add(key: AstNode, value: AstNodeDescription): void
addAll(key: AstNode, values: AstNodeDescription[]): void
get(key: AstNode): readonly AstNodeDescription[]
getStream(key: AstNode): Stream<AstNodeDescription>
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change; we should defer the PR to v4.0.0.


export interface DocumentSegment {
readonly range: Range
Loading