2020-05-10 13:55:24 +02:00
|
|
|
//! Conversion lsp_types types to rust-analyzer specific ones.
|
2020-06-11 11:04:09 +02:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2020-05-10 13:55:24 +02:00
|
|
|
use ra_db::{FileId, FilePosition, FileRange};
|
2020-07-13 17:41:47 -04:00
|
|
|
use ra_ide::{AssistKind, LineCol, LineIndex};
|
2020-05-10 13:55:24 +02:00
|
|
|
use ra_syntax::{TextRange, TextSize};
|
2020-06-11 11:04:09 +02:00
|
|
|
use vfs::AbsPathBuf;
|
2020-05-10 13:55:24 +02:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
use crate::{global_state::GlobalStateSnapshot, Result};
|
2020-05-10 13:55:24 +02:00
|
|
|
|
2020-06-11 11:04:09 +02:00
|
|
|
pub(crate) fn abs_path(url: &lsp_types::Url) -> Result<AbsPathBuf> {
|
|
|
|
let path = url.to_file_path().map_err(|()| "url is not a file")?;
|
|
|
|
Ok(AbsPathBuf::try_from(path).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn vfs_path(url: &lsp_types::Url) -> Result<vfs::VfsPath> {
|
|
|
|
abs_path(url).map(vfs::VfsPath::from)
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:55:24 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
pub(crate) fn file_id(world: &GlobalStateSnapshot, url: &lsp_types::Url) -> Result<FileId> {
|
2020-06-13 11:00:06 +02:00
|
|
|
world.url_to_file_id(url)
|
2020-05-10 13:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn file_position(
|
2020-06-03 11:16:08 +02:00
|
|
|
world: &GlobalStateSnapshot,
|
2020-05-10 13:55:24 +02:00
|
|
|
tdpp: lsp_types::TextDocumentPositionParams,
|
|
|
|
) -> Result<FilePosition> {
|
|
|
|
let file_id = file_id(world, &tdpp.text_document.uri)?;
|
2020-06-25 00:41:08 +02:00
|
|
|
let line_index = world.analysis.file_line_index(file_id)?;
|
2020-05-10 13:55:24 +02:00
|
|
|
let offset = offset(&*line_index, tdpp.position);
|
|
|
|
Ok(FilePosition { file_id, offset })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn file_range(
|
2020-06-03 11:16:08 +02:00
|
|
|
world: &GlobalStateSnapshot,
|
2020-05-10 13:55:24 +02:00
|
|
|
text_document_identifier: lsp_types::TextDocumentIdentifier,
|
|
|
|
range: lsp_types::Range,
|
|
|
|
) -> Result<FileRange> {
|
|
|
|
let file_id = file_id(world, &text_document_identifier.uri)?;
|
2020-06-25 00:41:08 +02:00
|
|
|
let line_index = world.analysis.file_line_index(file_id)?;
|
2020-05-10 13:55:24 +02:00
|
|
|
let range = text_range(&line_index, range);
|
|
|
|
Ok(FileRange { file_id, range })
|
|
|
|
}
|
2020-07-13 17:41:47 -04:00
|
|
|
|
|
|
|
pub(crate) fn assist_kind(kind: lsp_types::CodeActionKind) -> Option<AssistKind> {
|
|
|
|
let assist_kind = match &kind {
|
|
|
|
k if k == &lsp_types::CodeActionKind::EMPTY => AssistKind::None,
|
|
|
|
k if k == &lsp_types::CodeActionKind::QUICKFIX => AssistKind::QuickFix,
|
|
|
|
k if k == &lsp_types::CodeActionKind::REFACTOR => AssistKind::Refactor,
|
|
|
|
k if k == &lsp_types::CodeActionKind::REFACTOR => AssistKind::RefactorExtract,
|
|
|
|
k if k == &lsp_types::CodeActionKind::REFACTOR => AssistKind::RefactorInline,
|
|
|
|
k if k == &lsp_types::CodeActionKind::REFACTOR => AssistKind::RefactorRewrite,
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(assist_kind)
|
|
|
|
}
|