Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LSP window/showDocument request #6079

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,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
5 changes: 5 additions & 0 deletions helix-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ pub enum MethodCall {
WorkspaceFolders,
WorkspaceConfiguration(lsp::ConfigurationParams),
RegisterCapability(lsp::RegistrationParams),
ShowDocument(lsp::ShowDocumentParams),
}

impl MethodCall {
Expand All @@ -550,6 +551,10 @@ impl MethodCall {
let params: lsp::RegistrationParams = params.parse()?;
Self::RegisterCapability(params)
}
lsp::request::ShowDocument::METHOD => {
let params: lsp::ShowDocumentParams = params.parse()?;
Self::ShowDocument(params)
}
_ => {
return Err(Error::Unhandled);
}
Expand Down
85 changes: 73 additions & 12 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 @@ -959,6 +963,14 @@ impl Application {
Call::MethodCall(helix_lsp::jsonrpc::MethodCall {
method, params, id, ..
}) => {
let offset_encoding = match self.editor.language_servers.get_by_id(server_id) {
Some(language_server) => language_server.offset_encoding(),
None => {
warn!("can't find language server with id `{}`", server_id);
return;
}
};

let reply = match MethodCall::parse(&method, params) {
Err(helix_lsp::Error::Unhandled) => {
error!(
Expand Down Expand Up @@ -999,11 +1011,8 @@ impl Application {
Ok(serde_json::Value::Null)
}
Ok(MethodCall::ApplyWorkspaceEdit(params)) => {
let res = apply_workspace_edit(
&mut self.editor,
helix_lsp::OffsetEncoding::Utf8,
&params.edit,
);
let res =
apply_workspace_edit(&mut self.editor, offset_encoding, &params.edit);

Ok(json!(lsp::ApplyWorkspaceEditResponse {
applied: res.is_ok(),
Expand Down Expand Up @@ -1059,16 +1068,68 @@ impl Application {

Ok(serde_json::Value::Null)
}
};
Ok(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,
offset_encoding,
) {
let jump =
(doc.id(), doc.selection(view.id).clone());
view.jumps.push(jump);
doc.set_selection(
MDeiml marked this conversation as resolved.
Show resolved Hide resolved
view.id,
Selection::single(
new_range.anchor,
new_range.head,
),
);
align_view(doc, view, Align::Center);
}
}

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

// We can `unwrap` here, because we would have already returned erlier if the
// resusult was `None`.
let language_server = self.editor.language_servers.get_by_id(server_id).unwrap();

tokio::spawn(language_server.reply(id, reply));
}
Call::Invalid { id } => log::error!("LSP invalid method call id={:?}", id),
Expand Down