Implement utf8 offsets

This commit is contained in:
Aleksey Kladov
2021-02-13 00:55:27 +03:00
parent 0025836f26
commit c8b9ec8e62
5 changed files with 45 additions and 15 deletions

View File

@@ -1,13 +1,16 @@
//! Conversion lsp_types types to rust-analyzer specific ones.
use std::convert::TryFrom;
use ide::{Annotation, AnnotationKind, AssistKind, LineColUtf16};
use ide::{Annotation, AnnotationKind, AssistKind, LineCol, LineColUtf16};
use ide_db::base_db::{FileId, FilePosition, FileRange};
use syntax::{TextRange, TextSize};
use vfs::AbsPathBuf;
use crate::{
from_json, global_state::GlobalStateSnapshot, line_endings::LineIndex, lsp_ext, Result,
from_json,
global_state::GlobalStateSnapshot,
line_endings::{LineIndex, OffsetEncoding},
lsp_ext, Result,
};
pub(crate) fn abs_path(url: &lsp_types::Url) -> Result<AbsPathBuf> {
@@ -20,8 +23,16 @@ pub(crate) fn vfs_path(url: &lsp_types::Url) -> Result<vfs::VfsPath> {
}
pub(crate) fn offset(line_index: &LineIndex, position: lsp_types::Position) -> TextSize {
let line_col = LineColUtf16 { line: position.line as u32, col: position.character as u32 };
let line_col = line_index.index.to_utf8(line_col);
let line_col = match line_index.encoding {
OffsetEncoding::Utf8 => {
LineCol { line: position.line as u32, col: position.character as u32 }
}
OffsetEncoding::Utf16 => {
let line_col =
LineColUtf16 { line: position.line as u32, col: position.character as u32 };
line_index.index.to_utf8(line_col)
}
};
line_index.index.offset(line_col)
}