rust_analyzer/lsp/
from_proto.rs1use anyhow::format_err;
4use ide::{Annotation, AnnotationKind, AssistKind, LineCol};
5use ide_db::{FileId, FilePosition, FileRange, line_index::WideLineCol};
6use paths::Utf8PathBuf;
7use syntax::{TextRange, TextSize};
8use vfs::AbsPathBuf;
9
10use crate::{
11 global_state::GlobalStateSnapshot,
12 line_index::{LineIndex, PositionEncoding},
13 lsp_ext, try_default,
14};
15
16pub(crate) fn abs_path(url: &lsp_types::Uri) -> anyhow::Result<AbsPathBuf> {
17 let path = url.to_file_path().map_err(|()| anyhow::format_err!("url is not a file"))?;
18 Ok(AbsPathBuf::try_from(Utf8PathBuf::from_path_buf(path).unwrap()).unwrap())
19}
20
21pub(crate) fn vfs_path(url: &lsp_types::Uri) -> anyhow::Result<vfs::VfsPath> {
22 abs_path(url).map(vfs::VfsPath::from)
23}
24
25pub(crate) fn offset(
26 line_index: &LineIndex,
27 position: lsp_types::Position,
28) -> anyhow::Result<TextSize> {
29 let line_col = match line_index.encoding {
30 PositionEncoding::Utf8 => LineCol { line: position.line, col: position.character },
31 PositionEncoding::Wide(enc) => {
32 let line_col = WideLineCol { line: position.line, col: position.character };
33 line_index
34 .index
35 .to_utf8(enc, line_col)
36 .ok_or_else(|| format_err!("Invalid wide col offset"))?
37 }
38 };
39 let line_range = line_index.index.line(line_col.line).ok_or_else(|| {
40 format_err!("Invalid offset {line_col:?} (line index length: {:?})", line_index.index.len())
41 })?;
42 let col = TextSize::from(line_col.col);
43 let clamped_len = col.min(line_range.len());
44 Ok(line_range.start() + clamped_len)
52}
53
54pub(crate) fn text_range(
55 line_index: &LineIndex,
56 range: lsp_types::Range,
57) -> anyhow::Result<TextRange> {
58 let start = offset(line_index, range.start)?;
59 let end = offset(line_index, range.end)?;
60 match end < start {
61 true => Err(format_err!("Invalid Range")),
62 false => Ok(TextRange::new(start, end)),
63 }
64}
65
66pub(crate) fn file_id(
68 snap: &GlobalStateSnapshot,
69 url: &lsp_types::Uri,
70) -> anyhow::Result<Option<FileId>> {
71 snap.url_to_file_id(url)
72}
73
74pub(crate) fn file_position(
76 snap: &GlobalStateSnapshot,
77 tdpp: &lsp_types::TextDocumentPositionParams,
78) -> anyhow::Result<Option<FilePosition>> {
79 let file_id = try_default!(file_id(snap, &tdpp.text_document.uri)?);
80 let line_index = snap.file_line_index(file_id)?;
81 let offset = offset(&line_index, tdpp.position)?;
82 Ok(Some(FilePosition { file_id, offset }))
83}
84
85pub(crate) fn file_range(
87 snap: &GlobalStateSnapshot,
88 text_document_identifier: &lsp_types::TextDocumentIdentifier,
89 range: lsp_types::Range,
90) -> anyhow::Result<Option<FileRange>> {
91 file_range_uri(snap, &text_document_identifier.uri, range)
92}
93
94pub(crate) fn file_range_uri(
96 snap: &GlobalStateSnapshot,
97 document: &lsp_types::Uri,
98 range: lsp_types::Range,
99) -> anyhow::Result<Option<FileRange>> {
100 let file_id = try_default!(file_id(snap, document)?);
101 let line_index = snap.file_line_index(file_id)?;
102 let range = text_range(&line_index, range)?;
103 Ok(Some(FileRange { file_id, range }))
104}
105
106pub(crate) fn assist_kind(kind: lsp_types::CodeActionKind) -> Option<AssistKind> {
107 let assist_kind = match &kind {
108 k if k == &lsp_types::CodeActionKind::Empty => AssistKind::Generate,
109 k if k == &lsp_types::CodeActionKind::QuickFix => AssistKind::QuickFix,
110 k if k == &lsp_types::CodeActionKind::Refactor => AssistKind::Refactor,
111 k if k == &lsp_types::CodeActionKind::RefactorExtract => AssistKind::RefactorExtract,
112 k if k == &lsp_types::CodeActionKind::RefactorInline => AssistKind::RefactorInline,
113 k if k == &lsp_types::CodeActionKind::RefactorRewrite => AssistKind::RefactorRewrite,
114 _ => return None,
115 };
116
117 Some(assist_kind)
118}
119
120pub(crate) fn annotation(
122 snap: &GlobalStateSnapshot,
123 range: lsp_types::Range,
124 data: lsp_ext::CodeLensResolveData,
125) -> anyhow::Result<Option<Annotation>> {
126 match data.kind {
127 lsp_ext::CodeLensResolveDataKind::Impls(params) => {
128 if snap.url_file_version(¶ms.text_document_position_params.text_document.uri)
129 != Some(data.version)
130 {
131 return Ok(None);
132 }
133 let pos @ FilePosition { file_id, .. } =
134 try_default!(file_position(snap, ¶ms.text_document_position_params)?);
135 let line_index = snap.file_line_index(file_id)?;
136
137 Ok(Annotation {
138 range: text_range(&line_index, range)?,
139 kind: AnnotationKind::HasImpls { pos, data: None },
140 })
141 }
142 lsp_ext::CodeLensResolveDataKind::References(params) => {
143 if snap.url_file_version(¶ms.text_document.uri) != Some(data.version) {
144 return Ok(None);
145 }
146 let pos @ FilePosition { file_id, .. } = try_default!(file_position(snap, ¶ms)?);
147 let line_index = snap.file_line_index(file_id)?;
148
149 Ok(Annotation {
150 range: text_range(&line_index, range)?,
151 kind: AnnotationKind::HasReferences { pos, data: None },
152 })
153 }
154 }
155 .map(Some)
156}