Skip to content

Commit

Permalink
Implement LSP window/showDocument request
Browse files Browse the repository at this point in the history
  • Loading branch information
MDeiml committed Feb 22, 2023
1 parent 1a87d14 commit bb05b70
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
1 change: 1 addition & 0 deletions helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ impl Client {
}),
window: Some(lsp::WindowClientCapabilities {
work_done_progress: Some(true),
show_document: Some(lsp::ShowDocumentClientCapabilities { support: true }),
..Default::default()
}),
general: Some(lsp::GeneralClientCapabilities {
Expand Down
12 changes: 12 additions & 0 deletions helix-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ pub enum MethodCall {
ApplyWorkspaceEdit(lsp::ApplyWorkspaceEditParams),
WorkspaceFolders,
WorkspaceConfiguration(lsp::ConfigurationParams),
ShowDocument(lsp::ShowDocumentParams),
}

impl MethodCall {
Expand All @@ -356,6 +357,10 @@ impl MethodCall {
let params: lsp::ConfigurationParams = params.parse()?;
Self::WorkspaceConfiguration(params)
}
lsp::request::ShowDocument::METHOD => {
let params: lsp::ShowDocumentParams = params.parse()?;
Self::ShowDocument(params)
}
_ => {
return Err(Error::Unhandled);
}
Expand Down Expand Up @@ -439,6 +444,13 @@ impl Registry {
.map(|(_, client)| client.as_ref())
}

pub fn get_by_id_arc(&self, id: usize) -> Option<Arc<Client>> {
self.inner
.values()
.find(|(client_id, _)| client_id == &id)
.map(|(_, client)| client.clone())
}

pub fn remove_by_id(&mut self, id: usize) {
self.inner.retain(|_, (client_id, _)| client_id != &id)
}
Expand Down
73 changes: 65 additions & 8 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use helix_core::{
path::get_relative_path,
pos_at_coords, syntax, Selection,
};
use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap};
use helix_lsp::{
lsp,
util::{lsp_pos_to_pos, lsp_range_to_range},
LspProgressMap,
};
use helix_view::{
align_view,
document::DocumentSavedEventResult,
Expand Down Expand Up @@ -967,6 +971,14 @@ impl Application {
}
};

let language_server = match self.editor.language_servers.get_by_id_arc(server_id) {
Some(language_server) => language_server,
None => {
warn!("can't find language server with id `{}`", server_id);
return;
}
};

let reply = match call {
MethodCall::WorkDoneProgressCreate(params) => {
self.lsp_progress.create(server_id, params.token);
Expand All @@ -985,7 +997,7 @@ impl Application {
MethodCall::ApplyWorkspaceEdit(params) => {
apply_workspace_edit(
&mut self.editor,
helix_lsp::OffsetEncoding::Utf8,
language_server.offset_encoding(),
&params.edit,
);

Expand Down Expand Up @@ -1029,13 +1041,58 @@ impl Application {
.collect();
Ok(json!(result))
}
};
MethodCall::ShowDocument(params) => {
use helix_view::editor::Action;

let language_server = match self.editor.language_servers.get_by_id(server_id) {
Some(language_server) => language_server,
None => {
warn!("can't find language server with id `{}`", server_id);
return;
if params.external.unwrap_or(false) {
// TODO: Implement this
Ok(json!(lsp::ShowDocumentResult { success: false }))
} else {
match params.uri.to_file_path() {
Err(_) => {
let err = format!(
"unable to convert URI to filepath: {}",
params.uri
);
self.editor.set_error(err);
Ok(json!(lsp::ShowDocumentResult { success: false }))
}
Ok(path) => {
match self.editor.open(&path, Action::Replace) {
Err(err) => {
let err = format!(
"failed to open path: {:?}: {:?}",
params.uri, err
);
self.editor.set_error(err);
Ok(json!(lsp::ShowDocumentResult { success: false }))
}
Ok(_) => {
if let Some(range) = params.selection {
let (view, doc) = current!(self.editor);
// TODO: convert inside server
if let Some(new_range) = lsp_range_to_range(
doc.text(),
range,
language_server.offset_encoding(),
) {
doc.set_selection(
view.id,
Selection::single(
new_range.anchor,
new_range.head,
),
);
align_view(doc, view, Align::Center);
}
}

Ok(json!(lsp::ShowDocumentResult { success: true }))
}
}
}
}
}
}
};

Expand Down

0 comments on commit bb05b70

Please sign in to comment.