more goto lsp functions

pull/8/head
Jan Hrastnik 4 years ago committed by Blaž Hrastnik
parent 294791dffd
commit b738ae1bc7

@ -583,6 +583,30 @@ impl Client {
Ok(response.unwrap_or_default()) 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( pub async fn goto_definition(
&self, &self,
text_document: lsp::TextDocumentIdentifier, text_document: lsp::TextDocumentIdentifier,
@ -603,23 +627,83 @@ impl Client {
let response = self.request::<lsp::request::GotoDefinition>(params).await?; let response = self.request::<lsp::request::GotoDefinition>(params).await?;
let items = match response { self.goto_generic(response).await
Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location], }
Some(lsp::GotoDefinitionResponse::Array(location_vec)) => location_vec,
Some(lsp::GotoDefinitionResponse::Link(location_link_vec)) => { pub async fn goto_type_definition(
let mut location_vec: Vec<lsp::Location> = Vec::new(); &self,
location_link_vec.into_iter().for_each(|location_link| { text_document: lsp::TextDocumentIdentifier,
let link = lsp::Location { position: lsp::Position,
uri: location_link.target_uri, ) -> anyhow::Result<Vec<lsp::Location>> {
range: location_link.target_range, 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,
},
}; };
location_vec.push(link)
}); let response = self
location_vec .request::<lsp::request::GotoTypeDefinition>(params)
.await?;
self.goto_generic(response).await
} }
None => Vec::new(),
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,
},
}; };
Ok(items) 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
} }
} }

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

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

Loading…
Cancel
Save