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 Mar 11, 2023
1 parent d019371 commit 6148f41
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,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 @@ -526,6 +526,7 @@ pub enum MethodCall {
ApplyWorkspaceEdit(lsp::ApplyWorkspaceEditParams),
WorkspaceFolders,
WorkspaceConfiguration(lsp::ConfigurationParams),
ShowDocument(lsp::ShowDocumentParams),
}

impl MethodCall {
Expand All @@ -545,6 +546,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
62 changes: 61 additions & 1 deletion 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 @@ -1018,6 +1022,62 @@ impl Application {
.collect();
Ok(json!(result))
}
MethodCall::ShowDocument(params) => {
use helix_view::editor::Action;

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(
view.id,
Selection::single(
new_range.anchor,
new_range.head,
),
);
align_view(doc, view, Align::Center);
}
}

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

let language_server = match self.editor.language_servers.get_by_id(server_id) {
Expand Down

0 comments on commit 6148f41

Please sign in to comment.