@@ -45,6 +45,31 @@ pub fn lsp_formatting_options(settings: &LanguageSettings) -> lsp::FormattingOpt
}
}
+pub(crate) fn file_path_to_lsp_url(path: &Path) -> Result<lsp::Url> {
+ match lsp::Url::from_file_path(path) {
+ Ok(url) => Ok(url),
+ Err(()) => Err(anyhow!(
+ "Invalid file path provided to LSP request: {path:?}"
+ )),
+ }
+}
+
+pub(crate) fn make_text_document_identifier(path: &Path) -> Result<lsp::TextDocumentIdentifier> {
+ Ok(lsp::TextDocumentIdentifier {
+ uri: file_path_to_lsp_url(path)?,
+ })
+}
+
+pub(crate) fn make_lsp_text_document_position(
+ path: &Path,
+ position: PointUtf16,
+) -> Result<lsp::TextDocumentPositionParams> {
+ Ok(lsp::TextDocumentPositionParams {
+ text_document: make_text_document_identifier(path)?,
+ position: point_to_lsp(position),
+ })
+}
+
#[async_trait(?Send)]
pub trait LspCommand: 'static + Sized + Send + std::fmt::Debug {
type Response: 'static + Default + Send + std::fmt::Debug;
@@ -65,7 +90,7 @@ pub trait LspCommand: 'static + Sized + Send + std::fmt::Debug {
buffer: &Buffer,
language_server: &Arc<LanguageServer>,
cx: &AppContext,
- ) -> <Self::LspRequest as lsp::request::Request>::Params;
+ ) -> Result<<Self::LspRequest as lsp::request::Request>::Params>;
async fn response_from_lsp(
self,
@@ -202,13 +227,8 @@ impl LspCommand for PrepareRename {
_: &Buffer,
_: &Arc<LanguageServer>,
_: &AppContext,
- ) -> lsp::TextDocumentPositionParams {
- lsp::TextDocumentPositionParams {
- text_document: lsp::TextDocumentIdentifier {
- uri: lsp::Url::from_file_path(path).unwrap(),
- },
- position: point_to_lsp(self.position),
- }
+ ) -> Result<lsp::TextDocumentPositionParams> {
+ make_lsp_text_document_position(path, self.position)
}
async fn response_from_lsp(
@@ -325,17 +345,12 @@ impl LspCommand for PerformRename {
_: &Buffer,
_: &Arc<LanguageServer>,
_: &AppContext,
- ) -> lsp::RenameParams {
- lsp::RenameParams {
- text_document_position: lsp::TextDocumentPositionParams {
- text_document: lsp::TextDocumentIdentifier {
- uri: lsp::Url::from_file_path(path).unwrap(),
- },
- position: point_to_lsp(self.position),
- },
+ ) -> Result<lsp::RenameParams> {
+ Ok(lsp::RenameParams {
+ text_document_position: make_lsp_text_document_position(path, self.position)?,
new_name: self.new_name.clone(),
work_done_progress_params: Default::default(),
- }
+ })
}
async fn response_from_lsp(
@@ -455,17 +470,12 @@ impl LspCommand for GetDefinition {
_: &Buffer,
_: &Arc<LanguageServer>,
_: &AppContext,
- ) -> lsp::GotoDefinitionParams {
- lsp::GotoDefinitionParams {
- text_document_position_params: lsp::TextDocumentPositionParams {
- text_document: lsp::TextDocumentIdentifier {
- uri: lsp::Url::from_file_path(path).unwrap(),
- },
- position: point_to_lsp(self.position),
- },
+ ) -> Result<lsp::GotoDefinitionParams> {
+ Ok(lsp::GotoDefinitionParams {
+ text_document_position_params: make_lsp_text_document_position(path, self.position)?,
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
- }
+ })
}
async fn response_from_lsp(
@@ -555,17 +565,12 @@ impl LspCommand for GetDeclaration {
_: &Buffer,
_: &Arc<LanguageServer>,
_: &AppContext,
- ) -> lsp::GotoDeclarationParams {
- lsp::GotoDeclarationParams {
- text_document_position_params: lsp::TextDocumentPositionParams {
- text_document: lsp::TextDocumentIdentifier {
- uri: lsp::Url::from_file_path(path).unwrap(),
- },
- position: point_to_lsp(self.position),
- },
+ ) -> Result<lsp::GotoDeclarationParams> {
+ Ok(lsp::GotoDeclarationParams {
+ text_document_position_params: make_lsp_text_document_position(path, self.position)?,
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
- }
+ })
}
async fn response_from_lsp(
@@ -648,17 +653,12 @@ impl LspCommand for GetImplementation {
_: &Buffer,
_: &Arc<LanguageServer>,
_: &AppContext,
- ) -> lsp::GotoImplementationParams {
- lsp::GotoImplementationParams {
- text_document_position_params: lsp::TextDocumentPositionParams {
- text_document: lsp::TextDocumentIdentifier {
- uri: lsp::Url::from_file_path(path).unwrap(),
- },
- position: point_to_lsp(self.position),
- },
+ ) -> Result<lsp::GotoImplementationParams> {
+ Ok(lsp::GotoImplementationParams {
+ text_document_position_params: make_lsp_text_document_position(path, self.position)?,
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
- }
+ })
}
async fn response_from_lsp(
@@ -748,17 +748,12 @@ impl LspCommand for GetTypeDefinition {
_: &Buffer,
_: &Arc<LanguageServer>,
_: &AppContext,
- ) -> lsp::GotoTypeDefinitionParams {
- lsp::GotoTypeDefinitionParams {
- text_document_position_params: lsp::TextDocumentPositionParams {
- text_document: lsp::TextDocumentIdentifier {
- uri: lsp::Url::from_file_path(path).unwrap(),
- },
- position: point_to_lsp(self.position),
- },
+ ) -> Result<lsp::GotoTypeDefinitionParams> {
+ Ok(lsp::GotoTypeDefinitionParams {
+ text_document_position_params: make_lsp_text_document_position(path, self.position)?,
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
- }
+ })
}
async fn response_from_lsp(
@@ -1061,20 +1056,15 @@ impl LspCommand for GetReferences {
_: &Buffer,
_: &Arc<LanguageServer>,
_: &AppContext,
- ) -> lsp::ReferenceParams {
- lsp::ReferenceParams {
- text_document_position: lsp::TextDocumentPositionParams {
- text_document: lsp::TextDocumentIdentifier {
- uri: lsp::Url::from_file_path(path).unwrap(),
- },
- position: point_to_lsp(self.position),
- },
+ ) -> Result<lsp::ReferenceParams> {
+ Ok(lsp::ReferenceParams {
+ text_document_position: make_lsp_text_document_position(path, self.position)?,
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
context: lsp::ReferenceContext {
include_declaration: true,
},
- }
+ })
}
async fn response_from_lsp(
@@ -1237,17 +1227,12 @@ impl LspCommand for GetDocumentHighlights {
_: &Buffer,
_: &Arc<LanguageServer>,
_: &AppContext,
- ) -> lsp::DocumentHighlightParams {
- lsp::DocumentHighlightParams {
- text_document_position_params: lsp::TextDocumentPositionParams {
- text_document: lsp::TextDocumentIdentifier {
- uri: lsp::Url::from_file_path(path).unwrap(),
- },
- position: point_to_lsp(self.position),
- },
+ ) -> Result<lsp::DocumentHighlightParams> {
+ Ok(lsp::DocumentHighlightParams {
+ text_document_position_params: make_lsp_text_document_position(path, self.position)?,
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
- }
+ })
}
async fn response_from_lsp(
@@ -1391,22 +1376,12 @@ impl LspCommand for GetSignatureHelp {
_: &Buffer,
_: &Arc<LanguageServer>,
_cx: &AppContext,
- ) -> lsp::SignatureHelpParams {
- let url_result = lsp::Url::from_file_path(path);
- if url_result.is_err() {
- log::error!("an invalid file path has been specified");
- }
-
- lsp::SignatureHelpParams {
- text_document_position_params: lsp::TextDocumentPositionParams {
- text_document: lsp::TextDocumentIdentifier {
- uri: url_result.expect("invalid file path"),
- },
- position: point_to_lsp(self.position),
- },
+ ) -> Result<lsp::SignatureHelpParams> {
+ Ok(lsp::SignatureHelpParams {
+ text_document_position_params: make_lsp_text_document_position(path, self.position)?,
context: None,
work_done_progress_params: Default::default(),
- }
+ })
}
async fn response_from_lsp(
@@ -1505,16 +1480,11 @@ impl LspCommand for GetHover {
_: &Buffer,
_: &Arc<LanguageServer>,
_: &AppContext,
- ) -> lsp::HoverParams {
- lsp::HoverParams {
- text_document_position_params: lsp::TextDocumentPositionParams {
- text_document: lsp::TextDocumentIdentifier {
- uri: lsp::Url::from_file_path(path).unwrap(),
- },
- position: point_to_lsp(self.position),
- },
+ ) -> Result<lsp::HoverParams> {
+ Ok(lsp::HoverParams {
+ text_document_position_params: make_lsp_text_document_position(path, self.position)?,
work_done_progress_params: Default::default(),
- }
+ })
}
async fn response_from_lsp(
@@ -1728,16 +1698,13 @@ impl LspCommand for GetCompletions {
_: &Buffer,
_: &Arc<LanguageServer>,
_: &AppContext,
- ) -> lsp::CompletionParams {
- lsp::CompletionParams {
- text_document_position: lsp::TextDocumentPositionParams::new(
- lsp::TextDocumentIdentifier::new(lsp::Url::from_file_path(path).unwrap()),
- point_to_lsp(self.position),
- ),
+ ) -> Result<lsp::CompletionParams> {
+ Ok(lsp::CompletionParams {
+ text_document_position: make_lsp_text_document_position(path, self.position)?,
context: Some(self.context.clone()),
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
- }
+ })
}
async fn response_from_lsp(
@@ -2075,12 +2042,14 @@ impl LspCommand for GetCodeActions {
buffer: &Buffer,
language_server: &Arc<LanguageServer>,
_: &AppContext,
- ) -> lsp::CodeActionParams {
- let relevant_diagnostics = buffer
+ ) -> Result<lsp::CodeActionParams> {
+ let mut relevant_diagnostics = Vec::new();
+ for entry in buffer
.snapshot()
.diagnostics_in_range::<_, language::PointUtf16>(self.range.clone(), false)
- .map(|entry| entry.to_lsp_diagnostic_stub())
- .collect::<Vec<_>>();
+ {
+ relevant_diagnostics.push(entry.to_lsp_diagnostic_stub()?);
+ }
let supported =
Self::supported_code_action_kinds(language_server.adapter_server_capabilities());
@@ -2102,11 +2071,9 @@ impl LspCommand for GetCodeActions {
supported
};
- lsp::CodeActionParams {
- text_document: lsp::TextDocumentIdentifier::new(
- lsp::Url::from_file_path(path).unwrap(),
- ),
- range: range_to_lsp(self.range.to_point_utf16(buffer)),
+ Ok(lsp::CodeActionParams {
+ text_document: make_text_document_identifier(path)?,
+ range: range_to_lsp(self.range.to_point_utf16(buffer))?,
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
context: lsp::CodeActionContext {
@@ -2114,7 +2081,7 @@ impl LspCommand for GetCodeActions {
only,
..lsp::CodeActionContext::default()
},
- }
+ })
}
async fn response_from_lsp(
@@ -2286,15 +2253,12 @@ impl LspCommand for OnTypeFormatting {
_: &Buffer,
_: &Arc<LanguageServer>,
_: &AppContext,
- ) -> lsp::DocumentOnTypeFormattingParams {
- lsp::DocumentOnTypeFormattingParams {
- text_document_position: lsp::TextDocumentPositionParams::new(
- lsp::TextDocumentIdentifier::new(lsp::Url::from_file_path(path).unwrap()),
- point_to_lsp(self.position),
- ),
+ ) -> Result<lsp::DocumentOnTypeFormattingParams> {
+ Ok(lsp::DocumentOnTypeFormattingParams {
+ text_document_position: make_lsp_text_document_position(path, self.position)?,
ch: self.trigger.clone(),
options: self.options.clone(),
- }
+ })
}
async fn response_from_lsp(
@@ -2792,14 +2756,14 @@ impl LspCommand for InlayHints {
buffer: &Buffer,
_: &Arc<LanguageServer>,
_: &AppContext,
- ) -> lsp::InlayHintParams {
- lsp::InlayHintParams {
+ ) -> Result<lsp::InlayHintParams> {
+ Ok(lsp::InlayHintParams {
text_document: lsp::TextDocumentIdentifier {
- uri: lsp::Url::from_file_path(path).unwrap(),
+ uri: file_path_to_lsp_url(path)?,
},
- range: range_to_lsp(self.range.to_point_utf16(buffer)),
+ range: range_to_lsp(self.range.to_point_utf16(buffer))?,
work_done_progress_params: Default::default(),
- }
+ })
}
async fn response_from_lsp(
@@ -2949,15 +2913,12 @@ impl LspCommand for LinkedEditingRange {
buffer: &Buffer,
_server: &Arc<LanguageServer>,
_: &AppContext,
- ) -> lsp::LinkedEditingRangeParams {
+ ) -> Result<lsp::LinkedEditingRangeParams> {
let position = self.position.to_point_utf16(&buffer.snapshot());
- lsp::LinkedEditingRangeParams {
- text_document_position_params: lsp::TextDocumentPositionParams::new(
- lsp::TextDocumentIdentifier::new(lsp::Url::from_file_path(path).unwrap()),
- point_to_lsp(position),
- ),
+ Ok(lsp::LinkedEditingRangeParams {
+ text_document_position_params: make_lsp_text_document_position(path, position)?,
work_done_progress_params: Default::default(),
- }
+ })
}
async fn response_from_lsp(