Make it easy to add additional context for offset conversion

This commit is contained in:
Aleksey Kladov
2021-02-13 00:44:28 +03:00
parent 95209aa3f8
commit 0025836f26
6 changed files with 111 additions and 113 deletions

View File

@@ -1,11 +1,14 @@
//! Utilities for LSP-related boilerplate code.
use std::{error::Error, ops::Range};
use std::{error::Error, ops::Range, sync::Arc};
use ide::LineIndex;
use ide_db::base_db::Canceled;
use lsp_server::Notification;
use crate::{from_proto, global_state::GlobalState};
use crate::{
from_proto,
global_state::GlobalState,
line_endings::{LineEndings, LineIndex},
};
pub(crate) fn is_canceled(e: &(dyn Error + 'static)) -> bool {
e.downcast_ref::<Canceled>().is_some()
@@ -90,7 +93,12 @@ pub(crate) fn apply_document_changes(
old_text: &mut String,
content_changes: Vec<lsp_types::TextDocumentContentChangeEvent>,
) {
let mut line_index = LineIndex::new(old_text);
let mut line_index = LineIndex {
index: Arc::new(ide::LineIndex::new(old_text)),
// We don't care about line endings here.
endings: LineEndings::Unix,
};
// The changes we got must be applied sequentially, but can cross lines so we
// have to keep our line index updated.
// Some clients (e.g. Code) sort the ranges in reverse. As an optimization, we
@@ -115,7 +123,7 @@ pub(crate) fn apply_document_changes(
match change.range {
Some(range) => {
if !index_valid.covers(range.end.line) {
line_index = LineIndex::new(&old_text);
line_index.index = Arc::new(ide::LineIndex::new(&old_text));
}
index_valid = IndexValid::UpToLineExclusive(range.start.line);
let range = from_proto::text_range(&line_index, range);