eclipse-langium / langium

Next-gen language engineering / DSL framework
https://langium.org/
MIT License
663 stars 61 forks source link

Async Scope Computation #1554

Closed georg-schwarz closed 2 weeks ago

georg-schwarz commented 2 weeks ago

At Jayvee, we were evaluating whether we could implement dynamic document loading on imports (https://github.com/jvalue/jayvee/issues/593).

However, we ran into a roadblock as the ScopeProvider has a synchronous interface for the getScope method where we ultimately would need to await a promise the loads a document via services.shared.workspace.LangiumDocuments.getOrCreateDocument(documentUri).

If there is a better way to do it in a different place, I'd appreciate your expertise :)

Otherwise, would it be possible to make the interface of the ScopeProvider asynchronous by returning promises?

/**
 * Language-specific service for determining the scope of target elements visible in a specific cross-reference context.
 */
export interface ScopeProvider {

    /**
     * Return a scope describing what elements are visible for the given AST node and cross-reference
     * identifier.
     *
     * @param context Information about the reference for which a scope is requested.
     */
    getScope(context: ReferenceInfo): Promise<Scope>;

}

This would also affect some methods of the Linker and its clients.

Let me know if I can contribute anything to that change!

msujew commented 2 weeks ago

Hey @georg-schwarz,

The decision to make the getScope method sync was very deliberate, as otherwise every reference resolution, i.e. every call to ref becomes async.

There are other ways of solving this, see https://github.com/eclipse-langium/langium/discussions/1308. The main idea is to hook into the document builder and to load referenced documents before performing the linking.