Add validation in LspCommand::to_lsp
+ check for inverted ranges (#22731)
#22690 logged errors and flipped the range in this case. Instead it brings more visibility to the issue to return errors. Release Notes: - N/A
This commit is contained in:
parent
1c223d8940
commit
141393232e
6 changed files with 134 additions and 162 deletions
|
@ -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(
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{lsp_command::LspCommand, lsp_store::LspStore};
|
||||
use crate::{lsp_command::LspCommand, lsp_store::LspStore, make_text_document_identifier};
|
||||
use anyhow::{Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use gpui::{AppContext, AsyncAppContext, Model};
|
||||
|
@ -53,13 +53,11 @@ impl LspCommand for ExpandMacro {
|
|||
_: &Buffer,
|
||||
_: &Arc<LanguageServer>,
|
||||
_: &AppContext,
|
||||
) -> ExpandMacroParams {
|
||||
ExpandMacroParams {
|
||||
text_document: lsp::TextDocumentIdentifier {
|
||||
uri: lsp::Url::from_file_path(path).unwrap(),
|
||||
},
|
||||
) -> Result<ExpandMacroParams> {
|
||||
Ok(ExpandMacroParams {
|
||||
text_document: make_text_document_identifier(path)?,
|
||||
position: point_to_lsp(self.position),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async fn response_from_lsp(
|
||||
|
@ -179,13 +177,13 @@ impl LspCommand for OpenDocs {
|
|||
_: &Buffer,
|
||||
_: &Arc<LanguageServer>,
|
||||
_: &AppContext,
|
||||
) -> OpenDocsParams {
|
||||
OpenDocsParams {
|
||||
) -> Result<OpenDocsParams> {
|
||||
Ok(OpenDocsParams {
|
||||
text_document: lsp::TextDocumentIdentifier {
|
||||
uri: lsp::Url::from_file_path(path).unwrap(),
|
||||
},
|
||||
position: point_to_lsp(self.position),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async fn response_from_lsp(
|
||||
|
@ -292,10 +290,10 @@ impl LspCommand for SwitchSourceHeader {
|
|||
_: &Buffer,
|
||||
_: &Arc<LanguageServer>,
|
||||
_: &AppContext,
|
||||
) -> SwitchSourceHeaderParams {
|
||||
SwitchSourceHeaderParams(lsp::TextDocumentIdentifier {
|
||||
uri: lsp::Url::from_file_path(path).unwrap(),
|
||||
})
|
||||
) -> Result<SwitchSourceHeaderParams> {
|
||||
Ok(SwitchSourceHeaderParams(make_text_document_identifier(
|
||||
path,
|
||||
)?))
|
||||
}
|
||||
|
||||
async fn response_from_lsp(
|
||||
|
|
|
@ -3487,7 +3487,18 @@ impl LspStore {
|
|||
};
|
||||
let file = File::from_dyn(buffer.file()).and_then(File::as_local);
|
||||
if let (Some(file), Some(language_server)) = (file, language_server) {
|
||||
let lsp_params = request.to_lsp(&file.abs_path(cx), buffer, &language_server, cx);
|
||||
let lsp_params = match request.to_lsp(&file.abs_path(cx), buffer, &language_server, cx)
|
||||
{
|
||||
Ok(lsp_params) => lsp_params,
|
||||
Err(err) => {
|
||||
log::error!(
|
||||
"Preparing LSP request to {} failed: {}",
|
||||
language_server.name(),
|
||||
err
|
||||
);
|
||||
return Task::ready(Err(err));
|
||||
}
|
||||
};
|
||||
let status = request.status();
|
||||
return cx.spawn(move |this, cx| async move {
|
||||
if !request.check_capabilities(language_server.adapter_server_capabilities()) {
|
||||
|
@ -3536,11 +3547,7 @@ impl LspStore {
|
|||
let result = lsp_request.await;
|
||||
|
||||
let response = result.map_err(|err| {
|
||||
log::warn!(
|
||||
"Generic lsp request to {} failed: {}",
|
||||
language_server.name(),
|
||||
err
|
||||
);
|
||||
log::warn!("LSP request to {} failed: {}", language_server.name(), err);
|
||||
err
|
||||
})?;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue