-
Notifications
You must be signed in to change notification settings - Fork 27
Map LSP documents by normalised FilePath
#1252
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
Open
lionel-
wants to merge
1
commit into
oak/salsa-15-file-path-refactor
Choose a base branch
from
oak/salsa-16-document-wire-url
base: oak/salsa-15-file-path-refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| use std::collections::HashMap; | ||
| use std::path::Path; | ||
|
|
||
| use aether_path::FilePath; | ||
| use anyhow::anyhow; | ||
| use oak_db::OakDatabase; | ||
| use oak_semantic::library::Library; | ||
|
|
@@ -18,8 +19,10 @@ pub(crate) struct WorldState { | |
| /// Salsa input tree for Oak queries. | ||
| pub(crate) db: OakDatabase, | ||
|
|
||
| /// Watched documents | ||
| pub(crate) documents: HashMap<Url, Document>, | ||
| /// Watched documents, keyed on the normalised [`FilePath`] form. | ||
| /// The verbatim editor URL is preserved on each [`Document::url`] | ||
| /// for wire output. | ||
| pub(crate) documents: HashMap<FilePath, Document>, | ||
|
|
||
| /// Watched folders | ||
| pub(crate) workspace: Workspace, | ||
|
|
@@ -79,15 +82,17 @@ impl WorldState { | |
| } | ||
|
|
||
| pub(crate) fn get_document(&self, uri: &Url) -> anyhow::Result<&Document> { | ||
| if let Some(doc) = self.documents.get(uri) { | ||
| let key = FilePath::from_url(uri); | ||
| if let Some(doc) = self.documents.get(&key) { | ||
| Ok(doc) | ||
| } else { | ||
| Err(anyhow!("Can't find document for URI {uri}")) | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn get_document_mut(&mut self, uri: &Url) -> anyhow::Result<&mut Document> { | ||
|
Comment on lines
84
to
93
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like these should take |
||
| if let Some(doc) = self.documents.get_mut(uri) { | ||
| let key = FilePath::from_url(uri); | ||
| if let Some(doc) = self.documents.get_mut(&key) { | ||
| Ok(doc) | ||
| } else { | ||
| Err(anyhow!("Can't find document for URI {uri}")) | ||
|
|
@@ -115,6 +120,15 @@ impl WorldState { | |
| ..self.clone() | ||
| } | ||
| } | ||
|
|
||
| /// Insert a document, keying on the normalised [`FilePath`] and | ||
| /// stashing the verbatim editor URL on [`Document::url`] for wire | ||
| /// output. | ||
| pub(crate) fn insert_document(&mut self, uri: Url, mut doc: Document) { | ||
| let key = FilePath::from_url(&uri); | ||
| doc.url = uri; | ||
| self.documents.insert(key, doc); | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn with_document<T, F>( | ||
|
|
@@ -151,8 +165,11 @@ where | |
| } | ||
|
|
||
| pub(crate) fn workspace_uris(state: &WorldState) -> Vec<Url> { | ||
| let uris: Vec<Url> = state.documents.iter().map(|elt| elt.0.clone()).collect(); | ||
| uris | ||
| state | ||
| .documents | ||
| .values() | ||
| .map(|doc| doc.url.clone()) | ||
| .collect() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to advocate for the following strategy
Documentno longer knows abouturl. I think this is correct, as you should be able to create aDocumentwithout a correspondingurl.Documentno longer has that weirdPLACEHOLDER_URLhack, which i really do not likeinsert_document()would be removed since we only.insert()in 1 place in production code, and then in a few tests, so the abstraction doesn't feel worth itWorldState::get_document()andWorldState::get_document_mut()would then:path: FilePathas I mentioned elsewhere(Url, Document)enum and just pull out theDocumentand return itWe would need to go update 2 direct usages of
state.document.get()to bestate.get_document(), but I think we should do that anyways.This feels much much better to me, and I looked at all call sites of
state.documents.and think it would work quite wellI am quite strongly against
PLACEHOLDER_URL, so am going to block this pr until that is resolvedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops sorry, my fault. I asked Claude to get rid of
Optionand didn't closely review that one. The placeholder would leak to the user if we made a mistake somewhere.I did consider hoisting url out of there but it conflicted with existing code IIRC, and I wanted to avoid making too many changes to legacy Ark handlers. Let me take another look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh wait I forgot it's already gone in an ulterior PR!