Access to external state from Computable impls #12
-
Maybe I missed it in the book, but what's the suggested approach for providing a reference to or accessing external state from within a
Global state is always an answer, but it's not a good answer. While it works fine for CLI tools/batch compilers, it is not fun to manage for unit tests and servers. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
One way of doing it might be:
Given how document names are stored in a global table, I'm assuming that document IDs are globally unique and not re-used. |
Beta Was this translation helpful? Give feedback.
-
@chklauser Well, the Analyzer object does not have built-in capabilities to enumerate or index documents because the file system mapping to documents is a job external to the analyzer's functional scope. The current proposed way to reveal the mapping between the file system and the analyzer's documents is to manage a separate Inside the computable function of the attribute, you can look up the document by, let's say, the name of the file (taken from, for example, a This approach should work fine for both compilers and language servers (recall that the LSP server, despite its name, typically manages just a single client), but binding the computable functions to the global static state is not good for testability. I was thinking about introducing a more proper API to handle the external state but hesitated to include it in this release because the API is already quite complex. I wanted to hear your feedback first; maybe the global state is sufficient. Alternatively, I could suggest the following API improvement. In the language Node definition, you would define a type describing the external state's type for this language: #[derive(Node)]
#[external(MyLangExtState)]
enum MyNode {
//...
} Then, in the mutation task, I will introduce a function to mutably access this state, and for the computable's context, a function to read this state (or a part of it) with automatic subscriptions to changes in the state. The MyLangExtState object would merely be an analyzer-wide "attribute" with a value that you can read or write externally, or maybe a set of such attributes.
Yes, that's correct. You can rely on the uniqueness of document IDs within the current process. |
Beta Was this translation helpful? Give feedback.
-
I wouldn't rush the API design here. Especially not just on my behalf. I think the API for the analysis framework is pretty cool (though I don't have enough experience with it to tell whether the automatic dependency inference works out in practice). Things I like about your proposed API
Downsides I can see
I'm still wondering if there should be a global |
Beta Was this translation helpful? Give feedback.
-
Following our conversation, I opened Issue #15 with the proposed API changes. |
Beta Was this translation helpful? Give feedback.
-
The feature is fully implemented. For implementation details, see the Multi-File Analysis chapter of the User Guide, and issue #15. |
Beta Was this translation helpful? Give feedback.
The feature is fully implemented.
For implementation details, see the Multi-File Analysis chapter of the User Guide, and issue #15.