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};
6 use crate::{global_state::GlobalStateSnapshot, Result};
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)
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)
19 pub(crate) fn file_id(world: &GlobalStateSnapshot, url: &lsp_types::Url) -> Result<FileId> {
20 world.url_to_file_id(url)
23 pub(crate) fn file_position(
24 world: &GlobalStateSnapshot,
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 })
33 pub(crate) fn file_range(
34 world: &GlobalStateSnapshot,
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 })