Remove unwrap() from `lsp::Uri::from_file_path` (#50244) (cherry-pick to preview) (#50310)

zed-zippy[bot] , Conrad Irwin , and MrSubidubi created

Cherry-pick of #50244 to preview

----
Fixes ZED-3BM
Fixes ZED-1RT

Release Notes:

- Windows: Fixed a panic registering a path with language servers when
the UNC path cannot be represented by a Rust URI.

---------

Co-authored-by: MrSubidubi <finn@zed.dev>

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
Co-authored-by: MrSubidubi <finn@zed.dev>

Change summary

crates/project/src/lsp_store.rs                 | 12 ++++++++++--
crates/project/src/lsp_store/lsp_ext_command.rs |  6 +++---
2 files changed, 13 insertions(+), 5 deletions(-)

Detailed changes

crates/project/src/lsp_store.rs 🔗

@@ -11406,6 +11406,15 @@ impl LspStore {
 
                 let buffer_id = buffer.remote_id();
                 if local.registered_buffers.contains_key(&buffer_id) {
+                    let abs_path = file.abs_path(cx);
+                    let uri = match lsp::Uri::from_file_path(&abs_path) {
+                        Ok(uri) => uri,
+                        Err(()) => {
+                            log::error!("failed to convert path to URI: {:?}", abs_path);
+                            continue;
+                        }
+                    };
+
                     let versions = local
                         .buffer_snapshots
                         .entry(buffer_id)
@@ -11427,14 +11436,13 @@ impl LspStore {
                     let snapshot = versions.last().unwrap();
                     let version = snapshot.version;
                     let initial_snapshot = &snapshot.snapshot;
-                    let uri = lsp::Uri::from_file_path(file.abs_path(cx)).unwrap();
                     language_server.register_buffer(
                         uri,
                         adapter.language_id(&language.name()),
                         version,
                         initial_snapshot.text(),
                     );
-                    buffer_paths_registered.push((buffer_id, file.abs_path(cx)));
+                    buffer_paths_registered.push((buffer_id, abs_path));
                     local
                         .buffers_opened_in_servers
                         .entry(buffer_id)

crates/project/src/lsp_store/lsp_ext_command.rs 🔗

@@ -211,10 +211,10 @@ impl LspCommand for OpenDocs {
         _: &Arc<LanguageServer>,
         _: &App,
     ) -> Result<OpenDocsParams> {
+        let uri = lsp::Uri::from_file_path(path)
+            .map_err(|()| anyhow::anyhow!("{path:?} is not a valid URI"))?;
         Ok(OpenDocsParams {
-            text_document: lsp::TextDocumentIdentifier {
-                uri: lsp::Uri::from_file_path(path).unwrap(),
-            },
+            text_document: lsp::TextDocumentIdentifier { uri },
             position: point_to_lsp(self.position),
         })
     }