Simplify proto conversion
Trait based infra in conv.rs is significantly more complicated than what we actually need here.
This commit is contained in:
42
crates/rust-analyzer/src/from_proto.rs
Normal file
42
crates/rust-analyzer/src/from_proto.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
//! Conversion lsp_types types to rust-analyzer specific ones.
|
||||
use ra_db::{FileId, FilePosition, FileRange};
|
||||
use ra_ide::{LineCol, LineIndex};
|
||||
use ra_syntax::{TextRange, TextSize};
|
||||
|
||||
use crate::{world::WorldSnapshot, Result};
|
||||
|
||||
pub(crate) fn offset(line_index: &LineIndex, position: lsp_types::Position) -> TextSize {
|
||||
let line_col = LineCol { line: position.line as u32, col_utf16: position.character as u32 };
|
||||
line_index.offset(line_col)
|
||||
}
|
||||
|
||||
pub(crate) fn text_range(line_index: &LineIndex, range: lsp_types::Range) -> TextRange {
|
||||
let start = offset(line_index, range.start);
|
||||
let end = offset(line_index, range.end);
|
||||
TextRange::new(start, end)
|
||||
}
|
||||
|
||||
pub(crate) fn file_id(world: &WorldSnapshot, url: &lsp_types::Url) -> Result<FileId> {
|
||||
world.uri_to_file_id(url)
|
||||
}
|
||||
|
||||
pub(crate) fn file_position(
|
||||
world: &WorldSnapshot,
|
||||
tdpp: lsp_types::TextDocumentPositionParams,
|
||||
) -> Result<FilePosition> {
|
||||
let file_id = file_id(world, &tdpp.text_document.uri)?;
|
||||
let line_index = world.analysis().file_line_index(file_id)?;
|
||||
let offset = offset(&*line_index, tdpp.position);
|
||||
Ok(FilePosition { file_id, offset })
|
||||
}
|
||||
|
||||
pub(crate) fn file_range(
|
||||
world: &WorldSnapshot,
|
||||
text_document_identifier: lsp_types::TextDocumentIdentifier,
|
||||
range: lsp_types::Range,
|
||||
) -> Result<FileRange> {
|
||||
let file_id = file_id(world, &text_document_identifier.uri)?;
|
||||
let line_index = world.analysis().file_line_index(file_id)?;
|
||||
let range = text_range(&line_index, range);
|
||||
Ok(FileRange { file_id, range })
|
||||
}
|
||||
Reference in New Issue
Block a user