-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: scoping of annotation calls, type parameter constraints & yields (
#561) Closes partially #540 ### Summary of Changes * Implement scoping for * annotation calls (_annotation_) * type parameter constraints (_left operand_) * yields (_result_)
- Loading branch information
1 parent
4bde898
commit a510f2b
Showing
30 changed files
with
537 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
AstNode, | ||
AstNodeDescription, | ||
DefaultScopeComputation, | ||
getContainerOfType, | ||
LangiumDocument, | ||
PrecomputedScopes, | ||
} from 'langium'; | ||
import { | ||
isSdsClass, | ||
isSdsEnumVariant, | ||
isSdsFunction, | ||
isSdsTypeParameter, | ||
isSdsTypeParameterList, | ||
SdsTypeParameter, | ||
} from '../generated/ast.js'; | ||
|
||
export class SafeDsScopeComputation extends DefaultScopeComputation { | ||
override processNode(node: AstNode, document: LangiumDocument, scopes: PrecomputedScopes): void { | ||
if (isSdsTypeParameter(node)) { | ||
this.processSdsTypeParameter(node, document, scopes); | ||
} else { | ||
super.processNode(node, document, scopes); | ||
} | ||
} | ||
|
||
private processSdsTypeParameter( | ||
node: SdsTypeParameter, | ||
document: LangiumDocument, | ||
scopes: PrecomputedScopes, | ||
): void { | ||
const containingDeclaration = getContainerOfType(node, isSdsTypeParameterList)?.$container; | ||
if (!containingDeclaration) { | ||
return; | ||
} | ||
|
||
const name = this.nameProvider.getName(node); | ||
const description = this.descriptions.createDescription(node, name, document); | ||
|
||
if (isSdsClass(containingDeclaration)) { | ||
this.addToScopesIfKeyIsDefined(scopes, containingDeclaration.parameterList, description); | ||
this.addToScopesIfKeyIsDefined(scopes, containingDeclaration.constraintList, description); | ||
this.addToScopesIfKeyIsDefined(scopes, containingDeclaration.body, description); | ||
} else if (isSdsEnumVariant(containingDeclaration)) { | ||
this.addToScopesIfKeyIsDefined(scopes, containingDeclaration.parameterList, description); | ||
this.addToScopesIfKeyIsDefined(scopes, containingDeclaration.constraintList, description); | ||
} else if (isSdsFunction(containingDeclaration)) { | ||
this.addToScopesIfKeyIsDefined(scopes, containingDeclaration.parameterList, description); | ||
this.addToScopesIfKeyIsDefined(scopes, containingDeclaration.resultList, description); | ||
this.addToScopesIfKeyIsDefined(scopes, containingDeclaration.constraintList, description); | ||
} | ||
} | ||
|
||
/** | ||
* Adds the key/value pair to the scopes if the key is defined. | ||
* | ||
* @param scopes The scopes to add the key/value pair to. | ||
* @param key The key. | ||
* @param value The value. | ||
*/ | ||
addToScopesIfKeyIsDefined(scopes: PrecomputedScopes, key: AstNode | undefined, value: AstNodeDescription): void { | ||
if (key) { | ||
scopes.add(key, value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { DefaultScopeProvider, EMPTY_SCOPE, getContainerOfType, ReferenceInfo, Scope } from 'langium'; | ||
import { isSdsSegment, isSdsYield } from '../generated/ast.js'; | ||
import { resultsOrEmpty } from '../helpers/astShortcuts.js'; | ||
|
||
export class SafeDsScopeProvider extends DefaultScopeProvider { | ||
override getScope(context: ReferenceInfo): Scope { | ||
if (isSdsYield(context.container) && context.property === 'result') { | ||
return this.getScopeForYieldResult(context); | ||
} else { | ||
return super.getScope(context); | ||
} | ||
} | ||
|
||
getScopeForYieldResult(context: ReferenceInfo): Scope { | ||
const containingSegment = getContainerOfType(context.container, isSdsSegment); | ||
if (!containingSegment) { | ||
return EMPTY_SCOPE; | ||
} | ||
|
||
return this.createScopeForNodes(resultsOrEmpty(containingSegment.resultList)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
tests/resources/scoping/annotation calls/on annotation/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package test.scoping.annotationCalls.onAnnotation | ||
|
||
// $TEST$ target before | ||
annotation »Before« | ||
|
||
// $TEST$ references before | ||
@»Before« | ||
// $TEST$ references after | ||
@»After« | ||
// $TEST$ unresolved | ||
@»Unresolved« | ||
annotation MyAnnotation | ||
|
||
// $TEST$ target after | ||
annotation »After« |
17 changes: 17 additions & 0 deletions
17
tests/resources/scoping/annotation calls/on attribute/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package test.scoping.annotationCalls.onAttribute | ||
|
||
// $TEST$ target before | ||
annotation »Before« | ||
|
||
class MyClass { | ||
// $TEST$ references before | ||
@»Before« | ||
// $TEST$ references after | ||
@»After« | ||
// $TEST$ unresolved | ||
@»Unresolved« | ||
attr myAttribute: Int | ||
} | ||
|
||
// $TEST$ target after | ||
annotation »After« |
15 changes: 15 additions & 0 deletions
15
tests/resources/scoping/annotation calls/on class/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package test.scoping.annotationCalls.onClass | ||
|
||
// $TEST$ target before | ||
annotation »Before« | ||
|
||
// $TEST$ references before | ||
@»Before« | ||
// $TEST$ references after | ||
@»After« | ||
// $TEST$ unresolved | ||
@»Unresolved« | ||
class MyClass | ||
|
||
// $TEST$ target after | ||
annotation »After« |
17 changes: 17 additions & 0 deletions
17
tests/resources/scoping/annotation calls/on enum variant/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package test.scoping.annotationCalls.onEnumVariant | ||
|
||
// $TEST$ target before | ||
annotation »Before« | ||
|
||
enum MyEnum { | ||
// $TEST$ references before | ||
@»Before« | ||
// $TEST$ references after | ||
@»After« | ||
// $TEST$ unresolved | ||
@»Unresolved« | ||
MyEnumVariant | ||
} | ||
|
||
// $TEST$ target after | ||
annotation »After« |
15 changes: 15 additions & 0 deletions
15
tests/resources/scoping/annotation calls/on enum/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package test.scoping.annotationCalls.onEnum | ||
|
||
// $TEST$ target before | ||
annotation »Before« | ||
|
||
// $TEST$ references before | ||
@»Before« | ||
// $TEST$ references after | ||
@»After« | ||
// $TEST$ unresolved | ||
@»Unresolved« | ||
enum MyEnum | ||
|
||
// $TEST$ target after | ||
annotation »After« |
15 changes: 15 additions & 0 deletions
15
tests/resources/scoping/annotation calls/on function/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package test.scoping.annotationCalls.onFunction | ||
|
||
// $TEST$ target before | ||
annotation »Before« | ||
|
||
// $TEST$ references before | ||
@»Before« | ||
// $TEST$ references after | ||
@»After« | ||
// $TEST$ unresolved | ||
@»Unresolved« | ||
fun myFunction() | ||
|
||
// $TEST$ target after | ||
annotation »After« |
9 changes: 9 additions & 0 deletions
9
tests/resources/scoping/annotation calls/on module/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// $TEST$ references annotation | ||
@»MyAnnotation« | ||
// $TEST$ unresolved | ||
@»Unresolved« | ||
|
||
package test.scoping.annotationCalls.onModule | ||
|
||
// $TEST$ target annotation | ||
annotation »MyAnnotation« |
17 changes: 17 additions & 0 deletions
17
tests/resources/scoping/annotation calls/on parameter/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package test.scoping.annotationCalls.onParameter | ||
|
||
// $TEST$ target before | ||
annotation »Before« | ||
|
||
fun myFunction( | ||
// $TEST$ references before | ||
@»Before« | ||
// $TEST$ references after | ||
@»After« | ||
// $TEST$ unresolved | ||
@»Unresolved« | ||
myParameter: Int | ||
) | ||
|
||
// $TEST$ target after | ||
annotation »After« |
15 changes: 15 additions & 0 deletions
15
tests/resources/scoping/annotation calls/on pipeline/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package test.scoping.annotationCalls.onPipeline | ||
|
||
// $TEST$ target before | ||
annotation »Before« | ||
|
||
// $TEST$ references before | ||
@»Before« | ||
// $TEST$ references after | ||
@»After« | ||
// $TEST$ unresolved | ||
@»Unresolved« | ||
pipeline myPipeline {} | ||
|
||
// $TEST$ target after | ||
annotation »After« |
17 changes: 17 additions & 0 deletions
17
tests/resources/scoping/annotation calls/on result/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package test.scoping.annotationCalls.onResult | ||
|
||
// $TEST$ target before | ||
annotation »Before« | ||
|
||
fun myFunction() -> ( | ||
// $TEST$ references before | ||
@»Before« | ||
// $TEST$ references after | ||
@»After« | ||
// $TEST$ unresolved | ||
@»Unresolved« | ||
myResult: Int | ||
) | ||
|
||
// $TEST$ target after | ||
annotation »After« |
15 changes: 15 additions & 0 deletions
15
tests/resources/scoping/annotation calls/on segment/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package test.scoping.annotationCalls.onSegment | ||
|
||
// $TEST$ target before | ||
annotation »Before« | ||
|
||
// $TEST$ references before | ||
@»Before« | ||
// $TEST$ references after | ||
@»After« | ||
// $TEST$ unresolved | ||
@»Unresolved« | ||
segment mySegment() {} | ||
|
||
// $TEST$ target after | ||
annotation »After« |
17 changes: 17 additions & 0 deletions
17
tests/resources/scoping/annotation calls/on type parameter/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package test.scoping.annotationCalls.onTypeParameter | ||
|
||
// $TEST$ target before | ||
annotation »Before« | ||
|
||
fun myFunction< | ||
// $TEST$ references before | ||
@»Before« | ||
// $TEST$ references after | ||
@»After« | ||
// $TEST$ unresolved | ||
@»Unresolved« | ||
MyTypeParameter | ||
>() | ||
|
||
// $TEST$ target after | ||
annotation »After« |
Oops, something went wrong.