Pos conversions always operate on whole documents.

This commit is contained in:
Blaž Hrastnik 2021-03-18 15:23:42 +09:00
parent 175d38c88c
commit e9bd9e72c3
4 changed files with 16 additions and 19 deletions

View file

@ -297,7 +297,6 @@ impl Client {
}
let old_text = old_text.slice(..);
let new_text = new_text.slice(..);
while let Some(change) = iter.next() {
let len = match change {

View file

@ -28,14 +28,14 @@ pub enum Error {
pub mod util {
use super::*;
use helix_core::{Range, Rope, RopeSlice, Transaction};
use helix_core::{Range, Rope, Transaction};
pub fn lsp_pos_to_pos(doc: RopeSlice, pos: lsp::Position) -> usize {
pub fn lsp_pos_to_pos(doc: &Rope, pos: lsp::Position) -> usize {
let line = doc.line_to_char(pos.line as usize);
let line_start = doc.char_to_utf16_cu(line);
doc.utf16_cu_to_char(line_start + pos.character as usize)
}
pub fn pos_to_lsp_pos(doc: RopeSlice, pos: usize) -> lsp::Position {
pub fn pos_to_lsp_pos(doc: &Rope, pos: usize) -> lsp::Position {
let line = doc.char_to_line(pos);
let line_start = doc.char_to_utf16_cu(doc.line_to_char(line));
let col = doc.char_to_utf16_cu(pos) - line_start;
@ -43,7 +43,7 @@ pub mod util {
lsp::Position::new(line as u32, col as u32)
}
pub fn range_to_lsp_range(doc: RopeSlice, range: Range) -> lsp::Range {
pub fn range_to_lsp_range(doc: &Rope, range: Range) -> lsp::Range {
let start = pos_to_lsp_pos(doc, range.from());
let end = pos_to_lsp_pos(doc, range.to());
@ -51,7 +51,6 @@ pub mod util {
}
pub fn generate_transaction_from_edits(doc: &Rope, edits: Vec<lsp::TextEdit>) -> Transaction {
let text = doc.slice(..); // would be unnecessary if Transaction::change took Rope | RopeSlice
Transaction::change(
doc,
edits.into_iter().map(|edit| {
@ -62,8 +61,8 @@ pub mod util {
None
};
let start = lsp_pos_to_pos(text, edit.range.start);
let end = lsp_pos_to_pos(text, edit.range.end);
let start = lsp_pos_to_pos(doc, edit.range.start);
let end = lsp_pos_to_pos(doc, edit.range.end);
(start, end, replacement)
}),
)

View file

@ -134,7 +134,7 @@ impl Application {
.find(|view| view.doc.path() == path.as_ref());
if let Some(view) = view {
let doc = view.doc.text().slice(..);
let doc = view.doc.text();
let diagnostics = params
.diagnostics
.into_iter()

View file

@ -912,7 +912,7 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
cx.editor.open(PathBuf::from(location.uri.path()));
let doc = cx.doc();
let definition_pos = location.range.start;
let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos);
let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
doc.set_selection(Selection::point(new_pos));
}
[] => (), // maybe show user message that no definition was found?
@ -928,8 +928,7 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
editor.open(PathBuf::from(item.uri.path()));
let mut doc = &mut editor.view_mut().doc;
let definition_pos = item.range.start;
let new_pos =
helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos);
let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
doc.set_selection(Selection::point(new_pos));
},
);
@ -946,7 +945,7 @@ pub fn goto_definition(cx: &mut Context) {
};
// TODO: blocking here is not ideal
let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor());
let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection().cursor());
// TODO: handle fails
let res =
@ -962,7 +961,7 @@ pub fn goto_type_definition(cx: &mut Context) {
};
// TODO: blocking here is not ideal
let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor());
let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection().cursor());
// TODO: handle fails
let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
@ -978,7 +977,7 @@ pub fn goto_implementation(cx: &mut Context) {
};
// TODO: blocking here is not ideal
let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor());
let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection().cursor());
// TODO: handle fails
let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
@ -994,7 +993,7 @@ pub fn goto_reference(cx: &mut Context) {
};
// TODO: blocking here is not ideal
let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor());
let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection().cursor());
// TODO: handle fails
let res =
@ -1223,7 +1222,7 @@ pub fn format_selections(cx: &mut Context) {
.selection()
.ranges()
.iter()
.map(|range| helix_lsp::util::range_to_lsp_range(doc.text().slice(..), *range))
.map(|range| helix_lsp::util::range_to_lsp_range(doc.text(), *range))
.collect();
for range in ranges {
@ -1333,7 +1332,7 @@ pub fn completion(cx: &mut Context) {
};
// TODO: blocking here is not ideal
let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor());
let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection().cursor());
// TODO: handle fails
@ -1435,7 +1434,7 @@ pub fn hover(cx: &mut Context) {
// TODO: blocking here is not ideal, make commands async fn?
// not like we can process additional input meanwhile though
let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor());
let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection().cursor());
// TODO: handle fails
let res = smol::block_on(language_server.text_document_hover(doc.identifier(), pos))