Skip to content

Commit

Permalink
Extract a lsp position helper
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed Feb 18, 2022
1 parent 5af9136 commit a449156
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 50 deletions.
60 changes: 10 additions & 50 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use helix_lsp::{
block_on, lsp,
util::{lsp_pos_to_pos, lsp_range_to_range, pos_to_lsp_pos, range_to_lsp_range},
util::{lsp_pos_to_pos, lsp_range_to_range, range_to_lsp_range},
OffsetEncoding,
};

Expand Down Expand Up @@ -495,13 +495,7 @@ pub fn goto_definition(cx: &mut Context) {
let language_server = language_server!(doc);
let offset_encoding = language_server.offset_encoding();

let pos = pos_to_lsp_pos(
doc.text(),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
offset_encoding,
);
let pos = doc.position(view.id, offset_encoding);

let future = language_server.goto_definition(doc.identifier(), pos, None);

Expand All @@ -519,13 +513,7 @@ pub fn goto_type_definition(cx: &mut Context) {
let language_server = language_server!(doc);
let offset_encoding = language_server.offset_encoding();

let pos = pos_to_lsp_pos(
doc.text(),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
offset_encoding,
);
let pos = doc.position(view.id, offset_encoding);

let future = language_server.goto_type_definition(doc.identifier(), pos, None);

Expand All @@ -543,13 +531,7 @@ pub fn goto_implementation(cx: &mut Context) {
let language_server = language_server!(doc);
let offset_encoding = language_server.offset_encoding();

let pos = pos_to_lsp_pos(
doc.text(),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
offset_encoding,
);
let pos = doc.position(view.id, offset_encoding);

let future = language_server.goto_implementation(doc.identifier(), pos, None);

Expand All @@ -567,13 +549,7 @@ pub fn goto_reference(cx: &mut Context) {
let language_server = language_server!(doc);
let offset_encoding = language_server.offset_encoding();

let pos = pos_to_lsp_pos(
doc.text(),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
offset_encoding,
);
let pos = doc.position(view.id, offset_encoding);

let future = language_server.goto_reference(doc.identifier(), pos, None);

Expand All @@ -589,14 +565,9 @@ pub fn goto_reference(cx: &mut Context) {
pub fn signature_help(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
let language_server = language_server!(doc);
let offset_encoding = language_server.offset_encoding();

let pos = pos_to_lsp_pos(
doc.text(),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
language_server.offset_encoding(),
);
let pos = doc.position(view.id, offset_encoding);

let future = language_server.text_document_signature_help(doc.identifier(), pos, None);

Expand All @@ -622,16 +593,11 @@ pub fn signature_help(cx: &mut Context) {
pub fn hover(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
let language_server = language_server!(doc);
let offset_encoding = language_server.offset_encoding();

// TODO: factor out a doc.position_identifier() that returns lsp::TextDocumentPositionIdentifier

let pos = pos_to_lsp_pos(
doc.text(),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
language_server.offset_encoding(),
);
let pos = doc.position(view.id, offset_encoding);

let future = language_server.text_document_hover(doc.identifier(), pos, None);

Expand Down Expand Up @@ -688,13 +654,7 @@ pub fn rename_symbol(cx: &mut Context) {
let language_server = language_server!(doc);
let offset_encoding = language_server.offset_encoding();

let pos = pos_to_lsp_pos(
doc.text(),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
offset_encoding,
);
let pos = doc.position(view.id, offset_encoding);

let task = language_server.rename_symbol(doc.identifier(), pos, input.to_string());
let edits = block_on(task).unwrap_or_default();
Expand Down
14 changes: 14 additions & 0 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,20 @@ impl Document {
lsp::VersionedTextDocumentIdentifier::new(self.url().unwrap(), self.version)
}

pub fn position(
&self,
view_id: ViewId,
offset_encoding: helix_lsp::OffsetEncoding,
) -> lsp::Position {
let text = self.text();

helix_lsp::util::pos_to_lsp_pos(
text,
self.selection(view_id).primary().cursor(text.slice(..)),
offset_encoding,
)
}

#[inline]
pub fn diagnostics(&self) -> &[Diagnostic] {
&self.diagnostics
Expand Down

0 comments on commit a449156

Please sign in to comment.