more goto lsp functions

This commit is contained in:
Jan Hrastnik 2021-03-02 23:57:18 +01:00 committed by Blaž Hrastnik
parent 294791dffd
commit b738ae1bc7
3 changed files with 112 additions and 20 deletions

View file

@ -583,6 +583,30 @@ impl Client {
Ok(response.unwrap_or_default())
}
pub async fn goto_generic(
&self,
response: Option<lsp::GotoDefinitionResponse>,
) -> anyhow::Result<Vec<lsp::Location>> {
let items = match response {
Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location],
Some(lsp::GotoDefinitionResponse::Array(location_vec)) => location_vec,
Some(lsp::GotoDefinitionResponse::Link(location_link_vec)) => {
let mut location_vec: Vec<lsp::Location> = Vec::new();
location_link_vec.into_iter().for_each(|location_link| {
let link = lsp::Location {
uri: location_link.target_uri,
range: location_link.target_range,
};
location_vec.push(link)
});
location_vec
}
None => Vec::new(),
};
Ok(items)
}
pub async fn goto_definition(
&self,
text_document: lsp::TextDocumentIdentifier,
@ -603,23 +627,83 @@ impl Client {
let response = self.request::<lsp::request::GotoDefinition>(params).await?;
let items = match response {
Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location],
Some(lsp::GotoDefinitionResponse::Array(location_vec)) => location_vec,
Some(lsp::GotoDefinitionResponse::Link(location_link_vec)) => {
let mut location_vec: Vec<lsp::Location> = Vec::new();
location_link_vec.into_iter().for_each(|location_link| {
let link = lsp::Location {
uri: location_link.target_uri,
range: location_link.target_range,
};
location_vec.push(link)
});
location_vec
}
None => Vec::new(),
self.goto_generic(response).await
}
pub async fn goto_type_definition(
&self,
text_document: lsp::TextDocumentIdentifier,
position: lsp::Position,
) -> anyhow::Result<Vec<lsp::Location>> {
let params = lsp::GotoDefinitionParams {
text_document_position_params: lsp::TextDocumentPositionParams {
text_document,
position,
},
work_done_progress_params: lsp::WorkDoneProgressParams {
work_done_token: None,
},
partial_result_params: lsp::PartialResultParams {
partial_result_token: None,
},
};
Ok(items)
let response = self
.request::<lsp::request::GotoTypeDefinition>(params)
.await?;
self.goto_generic(response).await
}
pub async fn goto_implementation(
&self,
text_document: lsp::TextDocumentIdentifier,
position: lsp::Position,
) -> anyhow::Result<Vec<lsp::Location>> {
let params = lsp::GotoDefinitionParams {
text_document_position_params: lsp::TextDocumentPositionParams {
text_document,
position,
},
work_done_progress_params: lsp::WorkDoneProgressParams {
work_done_token: None,
},
partial_result_params: lsp::PartialResultParams {
partial_result_token: None,
},
};
let response = self
.request::<lsp::request::GotoImplementation>(params)
.await?;
self.goto_generic(response).await
}
pub async fn goto_reference(
&self,
text_document: lsp::TextDocumentIdentifier,
position: lsp::Position,
) -> anyhow::Result<Vec<lsp::Location>> {
let params = lsp::ReferenceParams {
text_document_position: lsp::TextDocumentPositionParams {
text_document,
position,
},
context: lsp::ReferenceContext {
include_declaration: true,
},
work_done_progress_params: lsp::WorkDoneProgressParams {
work_done_token: None,
},
partial_result_params: lsp::PartialResultParams {
partial_result_token: None,
},
};
let response = self.request::<lsp::request::References>(params).await?;
self.goto_generic(response.map(lsp::GotoDefinitionResponse::Array))
.await
}
}

View file

@ -872,11 +872,19 @@ pub fn goto_definition(cx: &mut Context) {
doc.mode = Mode::Normal;
log::info!("{:?}", res);
let filepath = doc.path.clone().unwrap();
log::info!("{:?}", filepath);
match &res.as_slice() {
[location] => {
let definition_pos = location.range.start;
let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos);
doc.set_selection(Selection::point(new_pos));
if filepath.to_str().unwrap() == location.uri.path() {
let definition_pos = location.range.start;
let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos);
doc.set_selection(Selection::point(new_pos));
} else {
// open new file
}
}
[] => (), // maybe show user message that no definition was found?
_ => {

View file

@ -19,7 +19,7 @@ pub enum Mode {
pub struct Document {
pub state: State, // rope + selection
/// File path on disk.
path: Option<PathBuf>,
pub path: Option<PathBuf>, // pub for testing
/// Current editing mode.
pub mode: Mode,