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

update qualified-name scoping with an alias for AstDescription #247

Closed
wants to merge 2 commits into from
Closed
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
54 changes: 48 additions & 6 deletions hugo/content/docs/recipes/scoping/qualified-name.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class CppScopeComputation extends DefaultScopeComputation {
for (const description of nestedDescriptions) {
// Add qualified names to the container
// This could also be a partial qualified name
const qualified = this.createQualifiedDescription(element, description, document);
const qualified = this.createQualifiedDescription(element, description);
localDescriptions.push(qualified);
}
}
Expand All @@ -143,14 +143,35 @@ export class CppScopeComputation extends DefaultScopeComputation {

private createQualifiedDescription(
container: Namespace,
description: AstNodeDescription,
document: LangiumDocument
description: AstNodeDescription
): AstNodeDescription {
// `getQualifiedName` has been implemented in the previous section
const name = this.getQualifiedName(container.name, description.name);
return this.descriptions.createDescription(description.node!, name, document);
return new AstNodeDescriptionAlias(description, name);
}
}
// provide an alias for AstNodeDescription to speed-up computation and reduce memory
export function isAstNodeDescriptionAlias(obj: unknown): obj is AstNodeDescriptionAlias {
return isAstNodeDescription(obj) && isAstNodeDescription(typeof (obj as AstNodeDescriptionAlias).alias);
}

export class AstNodeDescriptionAlias implements AstNodeDescription {
public readonly alias: AstNodeDescription;
private readonly alias_name: string;

constructor(description: AstNodeDescription, name: string) {
this.alias = isAstNodeDescriptionAlias(description) ? description.alias : description;
this.alias_name = name
}
get node(): AstNode | undefined { return this.alias.node; }
get nameSegment(): DocumentSegment | undefined { return this.alias.nameSegment; }
get selectionSegment(): DocumentSegment | undefined { return this.alias.selectionSegment; }
get type(): string { return this.alias.type; }
get name(): string { return this.alias_name; }
get documentUri(): URI { return this.alias.documentUri; }
get path(): string { return this.alias.path; }
}

```

This new change now allows us to use local names of functions in the local scope, while they are still exported using their fully qualified name to the global scope.
Expand Down Expand Up @@ -203,7 +224,7 @@ export class CppScopeComputation extends DefaultScopeComputation {
for (const description of nestedDescriptions) {
// Add qualified names to the container
// This could also be a partially qualified name
const qualified = this.createQualifiedDescription(element, description, document);
const qualified = this.createQualifiedDescription(element, description);
localDescriptions.push(qualified);
}
}
Expand All @@ -218,7 +239,7 @@ export class CppScopeComputation extends DefaultScopeComputation {
document: LangiumDocument
): AstNodeDescription {
const name = this.getQualifiedName(container.name, description.name);
return this.descriptions.createDescription(description.node!, name, document);
return new AstNodeDescriptionAlias(description, name);
}

/**
Expand All @@ -235,6 +256,27 @@ export class CppScopeComputation extends DefaultScopeComputation {
return name;
}
}
// provide an alias for AstNodeDescription to speed-up computation and reduce memory
export function isAstNodeDescriptionAlias(obj: unknown): obj is AstNodeDescriptionAlias {
return isAstNodeDescription(obj) && isAstNodeDescription(typeof (obj as AstNodeDescriptionAlias).alias);
}

export class AstNodeDescriptionAlias implements AstNodeDescription {
public readonly alias: AstNodeDescription;
private readonly alias_name: string;

constructor(description: AstNodeDescription, name: string) {
this.alias = isAstNodeDescriptionAlias(description) ? description.alias : description;
this.alias_name = name
}
get node(): AstNode | undefined { return this.alias.node; }
get nameSegment(): DocumentSegment | undefined { return this.alias.nameSegment; }
get selectionSegment(): DocumentSegment | undefined { return this.alias.selectionSegment; }
get type(): string { return this.alias.type; }
get name(): string { return this.alias_name; }
get documentUri(): URI { return this.alias.documentUri; }
get path(): string { return this.alias.path; }
}
```

</details>
Loading