Skip to content

Commit

Permalink
more goto lsp functions
Browse files Browse the repository at this point in the history
  • Loading branch information
janhrastnik authored and archseer committed Mar 16, 2021
1 parent 294791d commit b738ae1
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 20 deletions.
116 changes: 100 additions & 16 deletions helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
}
14 changes: 11 additions & 3 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
_ => {
Expand Down
2 changes: 1 addition & 1 deletion helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b738ae1

Please sign in to comment.