@@ -97,6 +97,19 @@ pub struct RumdlLanguageServer {
9797 pub ( crate ) rumdl_sourced : Arc < RwLock < Option < Arc < SourcedConfig < ConfigValidated > > > > > ,
9898 /// Document store for open files and cached disk files
9999 pub ( crate ) documents : Arc < RwLock < HashMap < Url , DocumentEntry > > > ,
100+ /// Maps a document's resolved URI to every open spelling that names it.
101+ ///
102+ /// The store is keyed by the editor's spelling, because that is the spelling
103+ /// diagnostics must be published against. Navigation asks for a document by
104+ /// its resolved spelling, which differs only when the editor reached the file
105+ /// through a symlinked ancestor, so this stays empty for most workspaces.
106+ /// Without it such a request would read the file on disk and miss the buffer.
107+ ///
108+ /// One resolved path can have several spellings open at once (two symlinks
109+ /// to the same directory, each opened), so this holds all of them rather
110+ /// than the latest. A single slot would let the second open displace the
111+ /// first and the first close strand the second.
112+ pub ( crate ) document_aliases : Arc < RwLock < HashMap < Url , Vec < Url > > > > ,
100113 /// Workspace root folders from the client
101114 pub ( crate ) workspace_roots : Arc < RwLock < Vec < PathBuf > > > ,
102115 /// Configuration cache: maps directory path to resolved config
@@ -158,6 +171,7 @@ impl RumdlLanguageServer {
158171 rumdl_config,
159172 rumdl_sourced : Arc :: new ( RwLock :: new ( None ) ) ,
160173 documents : Arc :: new ( RwLock :: new ( HashMap :: new ( ) ) ) ,
174+ document_aliases : Arc :: new ( RwLock :: new ( HashMap :: new ( ) ) ) ,
161175 workspace_roots,
162176 config_cache : Arc :: new ( RwLock :: new ( HashMap :: new ( ) ) ) ,
163177 workspace_index,
@@ -175,6 +189,8 @@ impl RumdlLanguageServer {
175189 /// If not found, it attempts to read the file from disk and caches it for
176190 /// future requests.
177191 pub ( super ) async fn get_document_content ( & self , uri : & Url ) -> Option < String > {
192+ let uri = & self . store_uri ( uri) . await ;
193+
178194 // First check the cache
179195 {
180196 let docs = self . documents . read ( ) . await ;
@@ -212,17 +228,47 @@ impl RumdlLanguageServer {
212228 /// scoped to open documents. This avoids lingering diagnostics after a file
213229 /// is closed when clients use pull diagnostics.
214230 async fn get_open_document_content ( & self , uri : & Url ) -> Option < String > {
231+ let uri = self . store_uri ( uri) . await ;
215232 let docs = self . documents . read ( ) . await ;
216- docs. get ( uri)
233+ docs. get ( & uri)
217234 . and_then ( |entry| ( !entry. from_disk ) . then ( || entry. content . clone ( ) ) )
218235 }
219236
237+ /// The URI a document is stored under, given any spelling that names it.
238+ ///
239+ /// Answers with the request's own URI, except when the file is open only
240+ /// under a different spelling of the same path: an alias then finds the
241+ /// editor's buffer instead of falling through to the file on disk.
242+ ///
243+ /// An open buffer under the requested spelling wins over any alias, because
244+ /// one file can be open under several spellings at once and the editor holds
245+ /// a separate buffer for each. A disk copy cached under the requested
246+ /// spelling does not win: it was read before the document was opened
247+ /// elsewhere, and the buffer an alias names has since become the truth.
248+ async fn store_uri ( & self , uri : & Url ) -> Url {
249+ let Some ( spellings) = self . document_aliases . read ( ) . await . get ( uri) . cloned ( ) else {
250+ return uri. clone ( ) ;
251+ } ;
252+ let docs = self . documents . read ( ) . await ;
253+ let is_open = |u : & Url | matches ! ( docs. get( u) , Some ( entry) if !entry. from_disk) ;
254+ if is_open ( uri) {
255+ return uri. clone ( ) ;
256+ }
257+ // The most recently opened spelling, so a reopen supersedes an older one.
258+ spellings
259+ . iter ( )
260+ . rev ( )
261+ . find ( |u| is_open ( u) )
262+ . cloned ( )
263+ . unwrap_or_else ( || uri. clone ( ) )
264+ }
265+
220266 /// Resolve the Markdown flavor for a document, mirroring the per-file flavor
221267 /// resolution used by diagnostics and formatting so symbol parsing matches.
222268 pub ( super ) async fn resolve_flavor_for_uri ( & self , uri : & Url ) -> crate :: config:: MarkdownFlavor {
223- match uri . to_file_path ( ) {
224- Ok ( path) => self . resolve_config_for_file ( & path) . await . get_flavor_for_file ( & path) ,
225- Err ( _ ) => self . rumdl_config . read ( ) . await . markdown_flavor ( ) ,
269+ match super :: resolve_uri ( uri ) {
270+ Some ( path) => self . resolve_config_for_file ( & path) . await . get_flavor_for_file ( & path) ,
271+ None => self . rumdl_config . read ( ) . await . markdown_flavor ( ) ,
226272 }
227273 }
228274}
@@ -271,15 +317,15 @@ impl LanguageServer for RumdlLanguageServer {
271317 if let Some ( workspace_folders) = params. workspace_folders {
272318 for folder in workspace_folders {
273319 if let Ok ( path) = folder. uri . to_file_path ( ) {
274- let path = path . canonicalize ( ) . unwrap_or ( path) ;
320+ let path = super :: resolve_workspace_root ( & path) ;
275321 log:: info!( "Workspace root: {}" , path. display( ) ) ;
276322 roots. push ( path) ;
277323 }
278324 }
279325 } else if let Some ( root_uri) = params. root_uri
280326 && let Ok ( path) = root_uri. to_file_path ( )
281327 {
282- let path = path . canonicalize ( ) . unwrap_or ( path) ;
328+ let path = super :: resolve_workspace_root ( & path) ;
283329 log:: info!( "Workspace root: {}" , path. display( ) ) ;
284330 roots. push ( path) ;
285331 }
@@ -537,9 +583,12 @@ impl LanguageServer for RumdlLanguageServer {
537583 // Update workspace roots
538584 let mut roots = self . workspace_roots . write ( ) . await ;
539585
586+ // Resolved the same way `initialize` resolves a root, so a folder added
587+ // or removed later is comparable with the ones already recorded.
540588 // Remove deleted workspace folders
541589 for removed in & params. event . removed {
542590 if let Ok ( path) = removed. uri . to_file_path ( ) {
591+ let path = super :: resolve_workspace_root ( & path) ;
543592 roots. retain ( |r| r != & path) ;
544593 log:: info!( "Removed workspace root: {}" , path. display( ) ) ;
545594 }
@@ -548,6 +597,7 @@ impl LanguageServer for RumdlLanguageServer {
548597 // Add new workspace folders
549598 for added in & params. event . added {
550599 if let Ok ( path) = added. uri . to_file_path ( )
600+ && let path = super :: resolve_workspace_root ( & path)
551601 && !roots. contains ( & path)
552602 {
553603 log:: info!( "Added workspace root: {}" , path. display( ) ) ;
@@ -869,8 +919,18 @@ impl LanguageServer for RumdlLanguageServer {
869919 } ;
870920 self . documents . write ( ) . await . insert ( uri. clone ( ) , entry) ;
871921
922+ // Make the document reachable by the spelling navigation resolves it to.
923+ let resolved = super :: resolve_uri_spelling ( & uri) ;
924+ if resolved != uri {
925+ let mut aliases = self . document_aliases . write ( ) . await ;
926+ let spellings = aliases. entry ( resolved) . or_default ( ) ;
927+ if !spellings. contains ( & uri) {
928+ spellings. push ( uri. clone ( ) ) ;
929+ }
930+ }
931+
872932 // Send update to index worker for cross-file analysis
873- if let Ok ( path) = uri . to_file_path ( ) {
933+ if let Some ( path) = super :: resolve_uri ( & uri ) {
874934 let _ = self
875935 . update_tx
876936 . send ( IndexUpdate :: FileChanged {
@@ -898,7 +958,7 @@ impl LanguageServer for RumdlLanguageServer {
898958 self . documents . write ( ) . await . insert ( uri. clone ( ) , entry) ;
899959
900960 // Send update to index worker for cross-file analysis
901- if let Ok ( path) = uri . to_file_path ( ) {
961+ if let Some ( path) = super :: resolve_uri ( & uri ) {
902962 let _ = self
903963 . update_tx
904964 . send ( IndexUpdate :: FileChanged {
@@ -964,6 +1024,18 @@ impl LanguageServer for RumdlLanguageServer {
9641024 async fn did_close ( & self , params : DidCloseTextDocumentParams ) {
9651025 // Remove document from storage
9661026 self . documents . write ( ) . await . remove ( & params. text_document . uri ) ;
1027+ // Drop only this spelling. Another one naming the same file can still be
1028+ // open, and it stays reachable under the resolved URI.
1029+ let resolved = super :: resolve_uri_spelling ( & params. text_document . uri ) ;
1030+ if resolved != params. text_document . uri {
1031+ let mut aliases = self . document_aliases . write ( ) . await ;
1032+ if let Some ( spellings) = aliases. get_mut ( & resolved) {
1033+ spellings. retain ( |u| u != & params. text_document . uri ) ;
1034+ if spellings. is_empty ( ) {
1035+ aliases. remove ( & resolved) ;
1036+ }
1037+ }
1038+ }
9671039
9681040 // Always clear diagnostics on close to ensure cleanup
9691041 // (Ruff does this unconditionally as a defensive measure)
@@ -990,7 +1062,10 @@ impl LanguageServer for RumdlLanguageServer {
9901062 let reads_editorconfig = self . reads_editorconfig ( ) . await ;
9911063
9921064 for change in & params. changes {
993- if let Ok ( path) = change. uri . to_file_path ( ) {
1065+ // Resolved like every other path the server records, so a watch event
1066+ // is comparable with the workspace roots and with the index keys the
1067+ // scan produced. A deleted file still resolves: only its directory is.
1068+ if let Some ( path) = super :: resolve_uri ( & change. uri ) {
9941069 let file_name = path. file_name ( ) . and_then ( |f| f. to_str ( ) ) ;
9951070
9961071 // Handle config file changes
0 commit comments