]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/from_proto.rs
Merge #4358
[rust.git] / crates / rust-analyzer / src / from_proto.rs
1 //! Conversion lsp_types types to rust-analyzer specific ones.
2 use ra_db::{FileId, FilePosition, FileRange};
3 use ra_ide::{LineCol, LineIndex};
4 use ra_syntax::{TextRange, TextSize};
5
6 use crate::{world::WorldSnapshot, Result};
7
8 pub(crate) fn offset(line_index: &LineIndex, position: lsp_types::Position) -> TextSize {
9     let line_col = LineCol { line: position.line as u32, col_utf16: position.character as u32 };
10     line_index.offset(line_col)
11 }
12
13 pub(crate) fn text_range(line_index: &LineIndex, range: lsp_types::Range) -> TextRange {
14     let start = offset(line_index, range.start);
15     let end = offset(line_index, range.end);
16     TextRange::new(start, end)
17 }
18
19 pub(crate) fn file_id(world: &WorldSnapshot, url: &lsp_types::Url) -> Result<FileId> {
20     world.uri_to_file_id(url)
21 }
22
23 pub(crate) fn file_position(
24     world: &WorldSnapshot,
25     tdpp: lsp_types::TextDocumentPositionParams,
26 ) -> Result<FilePosition> {
27     let file_id = file_id(world, &tdpp.text_document.uri)?;
28     let line_index = world.analysis().file_line_index(file_id)?;
29     let offset = offset(&*line_index, tdpp.position);
30     Ok(FilePosition { file_id, offset })
31 }
32
33 pub(crate) fn file_range(
34     world: &WorldSnapshot,
35     text_document_identifier: lsp_types::TextDocumentIdentifier,
36     range: lsp_types::Range,
37 ) -> Result<FileRange> {
38     let file_id = file_id(world, &text_document_identifier.uri)?;
39     let line_index = world.analysis().file_line_index(file_id)?;
40     let range = text_range(&line_index, range);
41     Ok(FileRange { file_id, range })
42 }