on master help sign in
fix: lsp: be more defensive about URI conversions
6de6a3eBlaž Hrastnik
helix-term/src/commands/lsp.rs+20−8
@@ -46,10 +46,16 @@ fn jump_to_location(
46 offset_encoding: OffsetEncoding,
46 offset_encoding: OffsetEncoding,
47 action: Action,
47 action: Action,
48) {
48) {
49− let path = location
49+ let path = match location.uri.to_file_path() {
50− .uri
50+ Ok(path) => path,
51− .to_file_path()
51+ Err(_) => {
52+ editor.set_error(format!(
52− .expect("unable to convert URI to filepath");
53+ "unable to convert URI to filepath: {}",
54+ location.uri
55+ ));
56+ return;
57+ }
58+ };
53 let _id = editor.open(path, action).expect("editor.open failed");
59 let _id = editor.open(path, action).expect("editor.open failed");
54 let (view, doc) = current!(editor);
60 let (view, doc) = current!(editor);
55 let definition_pos = location.range.start;
61 let definition_pos = location.range.start;
@@ -344,9 +350,15 @@ pub fn apply_workspace_edit(
344 workspace_edit: &lsp::WorkspaceEdit,
350 workspace_edit: &lsp::WorkspaceEdit,
345) {
351) {
346 let mut apply_edits = |uri: &helix_lsp::Url, text_edits: Vec<lsp::TextEdit>| {
352 let mut apply_edits = |uri: &helix_lsp::Url, text_edits: Vec<lsp::TextEdit>| {
347− let path = uri
348− .to_file_path()
353+ let path = match uri.to_file_path() {
354+ Ok(path) => path,
355+ Err(_) => {
349− .expect("unable to convert URI to filepath");
356+ let err = format!("unable to convert URI to filepath: {}", uri);
357+ log::error!("{}", err);
358+ editor.set_error(err);
359+ return;
360+ }
361+ };
350
362
351 let current_view_id = view!(editor).id;
363 let current_view_id = view!(editor).id;
352 let doc_id = editor.open(path, Action::Load).unwrap();
364 let doc_id = editor.open(path, Action::Load).unwrap();
@@ -381,7 +393,7 @@ pub fn apply_workspace_edit(
381 log::debug!("workspace changes: {:?}", changes);
393 log::debug!("workspace changes: {:?}", changes);
382 for (uri, text_edits) in changes {
394 for (uri, text_edits) in changes {
383 let text_edits = text_edits.to_vec();
395 let text_edits = text_edits.to_vec();
384− apply_edits(uri, text_edits);
396+ apply_edits(uri, text_edits)
385 }
397 }
386 return;
398 return;
387 // Not sure if it works properly, it'll be safer to just panic here to avoid breaking some parts of code on which code actions will be used
399 // Not sure if it works properly, it'll be safer to just panic here to avoid breaking some parts of code on which code actions will be used