Extract a lsp position helper

pull/1604/head
Blaž Hrastnik 3 years ago
parent 5af9136aec
commit a449156702

@ -1,6 +1,6 @@
use helix_lsp::{ use helix_lsp::{
block_on, 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, OffsetEncoding,
}; };
@ -495,13 +495,7 @@ pub fn goto_definition(cx: &mut Context) {
let language_server = language_server!(doc); let language_server = language_server!(doc);
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let pos = pos_to_lsp_pos( let pos = doc.position(view.id, offset_encoding);
doc.text(),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
offset_encoding,
);
let future = language_server.goto_definition(doc.identifier(), pos, None); let future = language_server.goto_definition(doc.identifier(), pos, None);
@ -519,13 +513,7 @@ pub fn goto_type_definition(cx: &mut Context) {
let language_server = language_server!(doc); let language_server = language_server!(doc);
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let pos = pos_to_lsp_pos( let pos = doc.position(view.id, offset_encoding);
doc.text(),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
offset_encoding,
);
let future = language_server.goto_type_definition(doc.identifier(), pos, None); let future = language_server.goto_type_definition(doc.identifier(), pos, None);
@ -543,13 +531,7 @@ pub fn goto_implementation(cx: &mut Context) {
let language_server = language_server!(doc); let language_server = language_server!(doc);
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let pos = pos_to_lsp_pos( let pos = doc.position(view.id, offset_encoding);
doc.text(),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
offset_encoding,
);
let future = language_server.goto_implementation(doc.identifier(), pos, None); let future = language_server.goto_implementation(doc.identifier(), pos, None);
@ -567,13 +549,7 @@ pub fn goto_reference(cx: &mut Context) {
let language_server = language_server!(doc); let language_server = language_server!(doc);
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let pos = pos_to_lsp_pos( let pos = doc.position(view.id, offset_encoding);
doc.text(),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
offset_encoding,
);
let future = language_server.goto_reference(doc.identifier(), pos, None); let future = language_server.goto_reference(doc.identifier(), pos, None);
@ -589,14 +565,9 @@ pub fn goto_reference(cx: &mut Context) {
pub fn signature_help(cx: &mut Context) { pub fn signature_help(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let language_server = language_server!(doc); let language_server = language_server!(doc);
let offset_encoding = language_server.offset_encoding();
let pos = pos_to_lsp_pos( let pos = doc.position(view.id, offset_encoding);
doc.text(),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
language_server.offset_encoding(),
);
let future = language_server.text_document_signature_help(doc.identifier(), pos, None); let future = language_server.text_document_signature_help(doc.identifier(), pos, None);
@ -622,16 +593,11 @@ pub fn signature_help(cx: &mut Context) {
pub fn hover(cx: &mut Context) { pub fn hover(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let language_server = language_server!(doc); let language_server = language_server!(doc);
let offset_encoding = language_server.offset_encoding();
// TODO: factor out a doc.position_identifier() that returns lsp::TextDocumentPositionIdentifier // TODO: factor out a doc.position_identifier() that returns lsp::TextDocumentPositionIdentifier
let pos = pos_to_lsp_pos( let pos = doc.position(view.id, offset_encoding);
doc.text(),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
language_server.offset_encoding(),
);
let future = language_server.text_document_hover(doc.identifier(), pos, None); let future = language_server.text_document_hover(doc.identifier(), pos, None);
@ -688,13 +654,7 @@ pub fn rename_symbol(cx: &mut Context) {
let language_server = language_server!(doc); let language_server = language_server!(doc);
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let pos = pos_to_lsp_pos( let pos = doc.position(view.id, offset_encoding);
doc.text(),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
offset_encoding,
);
let task = language_server.rename_symbol(doc.identifier(), pos, input.to_string()); let task = language_server.rename_symbol(doc.identifier(), pos, input.to_string());
let edits = block_on(task).unwrap_or_default(); let edits = block_on(task).unwrap_or_default();

@ -922,6 +922,20 @@ impl Document {
lsp::VersionedTextDocumentIdentifier::new(self.url().unwrap(), self.version) 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] #[inline]
pub fn diagnostics(&self) -> &[Diagnostic] { pub fn diagnostics(&self) -> &[Diagnostic] {
&self.diagnostics &self.diagnostics

Loading…
Cancel
Save