]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/to_proto.rs
Trigger hover requests on closing brace hints
[rust.git] / crates / rust-analyzer / src / to_proto.rs
1 //! Conversion of rust-analyzer specific types to lsp_types equivalents.
2 use std::{
3     iter::once,
4     path,
5     sync::atomic::{AtomicU32, Ordering},
6 };
7
8 use ide::{
9     Annotation, AnnotationKind, Assist, AssistKind, Cancellable, CompletionItem,
10     CompletionItemKind, CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit,
11     Fold, FoldKind, Highlight, HlMod, HlOperator, HlPunct, HlRange, HlTag, Indel, InlayHint,
12     InlayKind, Markup, NavigationTarget, ReferenceCategory, RenameError, Runnable, Severity,
13     SignatureHelp, SourceChange, StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize,
14 };
15 use itertools::Itertools;
16 use serde_json::to_value;
17 use vfs::AbsPath;
18
19 use crate::{
20     cargo_target_spec::CargoTargetSpec,
21     config::{CallInfoConfig, Config},
22     global_state::GlobalStateSnapshot,
23     line_index::{LineEndings, LineIndex, OffsetEncoding},
24     lsp_ext,
25     lsp_utils::invalid_params_error,
26     semantic_tokens, Result,
27 };
28
29 pub(crate) fn position(line_index: &LineIndex, offset: TextSize) -> lsp_types::Position {
30     let line_col = line_index.index.line_col(offset);
31     match line_index.encoding {
32         OffsetEncoding::Utf8 => lsp_types::Position::new(line_col.line, line_col.col),
33         OffsetEncoding::Utf16 => {
34             let line_col = line_index.index.to_utf16(line_col);
35             lsp_types::Position::new(line_col.line, line_col.col)
36         }
37     }
38 }
39
40 pub(crate) fn range(line_index: &LineIndex, range: TextRange) -> lsp_types::Range {
41     let start = position(line_index, range.start());
42     let end = position(line_index, range.end());
43     lsp_types::Range::new(start, end)
44 }
45
46 pub(crate) fn symbol_kind(symbol_kind: SymbolKind) -> lsp_types::SymbolKind {
47     match symbol_kind {
48         SymbolKind::Function => lsp_types::SymbolKind::FUNCTION,
49         SymbolKind::Struct => lsp_types::SymbolKind::STRUCT,
50         SymbolKind::Enum => lsp_types::SymbolKind::ENUM,
51         SymbolKind::Variant => lsp_types::SymbolKind::ENUM_MEMBER,
52         SymbolKind::Trait => lsp_types::SymbolKind::INTERFACE,
53         SymbolKind::Macro
54         | SymbolKind::BuiltinAttr
55         | SymbolKind::Attribute
56         | SymbolKind::Derive => lsp_types::SymbolKind::FUNCTION,
57         SymbolKind::Module | SymbolKind::ToolModule => lsp_types::SymbolKind::MODULE,
58         SymbolKind::TypeAlias | SymbolKind::TypeParam | SymbolKind::SelfType => {
59             lsp_types::SymbolKind::TYPE_PARAMETER
60         }
61         SymbolKind::Field => lsp_types::SymbolKind::FIELD,
62         SymbolKind::Static => lsp_types::SymbolKind::CONSTANT,
63         SymbolKind::Const => lsp_types::SymbolKind::CONSTANT,
64         SymbolKind::ConstParam => lsp_types::SymbolKind::CONSTANT,
65         SymbolKind::Impl => lsp_types::SymbolKind::OBJECT,
66         SymbolKind::Local
67         | SymbolKind::SelfParam
68         | SymbolKind::LifetimeParam
69         | SymbolKind::ValueParam
70         | SymbolKind::Label => lsp_types::SymbolKind::VARIABLE,
71         SymbolKind::Union => lsp_types::SymbolKind::STRUCT,
72     }
73 }
74
75 pub(crate) fn structure_node_kind(kind: StructureNodeKind) -> lsp_types::SymbolKind {
76     match kind {
77         StructureNodeKind::SymbolKind(symbol) => symbol_kind(symbol),
78         StructureNodeKind::Region => lsp_types::SymbolKind::NAMESPACE,
79     }
80 }
81
82 pub(crate) fn document_highlight_kind(
83     category: ReferenceCategory,
84 ) -> lsp_types::DocumentHighlightKind {
85     match category {
86         ReferenceCategory::Read => lsp_types::DocumentHighlightKind::READ,
87         ReferenceCategory::Write => lsp_types::DocumentHighlightKind::WRITE,
88     }
89 }
90
91 pub(crate) fn diagnostic_severity(severity: Severity) -> lsp_types::DiagnosticSeverity {
92     match severity {
93         Severity::Error => lsp_types::DiagnosticSeverity::ERROR,
94         Severity::WeakWarning => lsp_types::DiagnosticSeverity::HINT,
95     }
96 }
97
98 pub(crate) fn documentation(documentation: Documentation) -> lsp_types::Documentation {
99     let value = crate::markdown::format_docs(documentation.as_str());
100     let markup_content = lsp_types::MarkupContent { kind: lsp_types::MarkupKind::Markdown, value };
101     lsp_types::Documentation::MarkupContent(markup_content)
102 }
103
104 pub(crate) fn completion_item_kind(
105     completion_item_kind: CompletionItemKind,
106 ) -> lsp_types::CompletionItemKind {
107     match completion_item_kind {
108         CompletionItemKind::Binding => lsp_types::CompletionItemKind::VARIABLE,
109         CompletionItemKind::BuiltinType => lsp_types::CompletionItemKind::STRUCT,
110         CompletionItemKind::InferredType => lsp_types::CompletionItemKind::SNIPPET,
111         CompletionItemKind::Keyword => lsp_types::CompletionItemKind::KEYWORD,
112         CompletionItemKind::Method => lsp_types::CompletionItemKind::METHOD,
113         CompletionItemKind::Snippet => lsp_types::CompletionItemKind::SNIPPET,
114         CompletionItemKind::UnresolvedReference => lsp_types::CompletionItemKind::REFERENCE,
115         CompletionItemKind::SymbolKind(symbol) => match symbol {
116             SymbolKind::Attribute => lsp_types::CompletionItemKind::FUNCTION,
117             SymbolKind::Const => lsp_types::CompletionItemKind::CONSTANT,
118             SymbolKind::ConstParam => lsp_types::CompletionItemKind::TYPE_PARAMETER,
119             SymbolKind::Derive => lsp_types::CompletionItemKind::FUNCTION,
120             SymbolKind::Enum => lsp_types::CompletionItemKind::ENUM,
121             SymbolKind::Field => lsp_types::CompletionItemKind::FIELD,
122             SymbolKind::Function => lsp_types::CompletionItemKind::FUNCTION,
123             SymbolKind::Impl => lsp_types::CompletionItemKind::TEXT,
124             SymbolKind::Label => lsp_types::CompletionItemKind::VARIABLE,
125             SymbolKind::LifetimeParam => lsp_types::CompletionItemKind::TYPE_PARAMETER,
126             SymbolKind::Local => lsp_types::CompletionItemKind::VARIABLE,
127             SymbolKind::Macro => lsp_types::CompletionItemKind::FUNCTION,
128             SymbolKind::Module => lsp_types::CompletionItemKind::MODULE,
129             SymbolKind::SelfParam => lsp_types::CompletionItemKind::VALUE,
130             SymbolKind::SelfType => lsp_types::CompletionItemKind::TYPE_PARAMETER,
131             SymbolKind::Static => lsp_types::CompletionItemKind::VALUE,
132             SymbolKind::Struct => lsp_types::CompletionItemKind::STRUCT,
133             SymbolKind::Trait => lsp_types::CompletionItemKind::INTERFACE,
134             SymbolKind::TypeAlias => lsp_types::CompletionItemKind::STRUCT,
135             SymbolKind::TypeParam => lsp_types::CompletionItemKind::TYPE_PARAMETER,
136             SymbolKind::Union => lsp_types::CompletionItemKind::STRUCT,
137             SymbolKind::ValueParam => lsp_types::CompletionItemKind::VALUE,
138             SymbolKind::Variant => lsp_types::CompletionItemKind::ENUM_MEMBER,
139             SymbolKind::BuiltinAttr => lsp_types::CompletionItemKind::FUNCTION,
140             SymbolKind::ToolModule => lsp_types::CompletionItemKind::MODULE,
141         },
142     }
143 }
144
145 pub(crate) fn text_edit(line_index: &LineIndex, indel: Indel) -> lsp_types::TextEdit {
146     let range = range(line_index, indel.delete);
147     let new_text = match line_index.endings {
148         LineEndings::Unix => indel.insert,
149         LineEndings::Dos => indel.insert.replace('\n', "\r\n"),
150     };
151     lsp_types::TextEdit { range, new_text }
152 }
153
154 pub(crate) fn completion_text_edit(
155     line_index: &LineIndex,
156     insert_replace_support: Option<lsp_types::Position>,
157     indel: Indel,
158 ) -> lsp_types::CompletionTextEdit {
159     let text_edit = text_edit(line_index, indel);
160     match insert_replace_support {
161         Some(cursor_pos) => lsp_types::InsertReplaceEdit {
162             new_text: text_edit.new_text,
163             insert: lsp_types::Range { start: text_edit.range.start, end: cursor_pos },
164             replace: text_edit.range,
165         }
166         .into(),
167         None => text_edit.into(),
168     }
169 }
170
171 pub(crate) fn snippet_text_edit(
172     line_index: &LineIndex,
173     is_snippet: bool,
174     indel: Indel,
175 ) -> lsp_ext::SnippetTextEdit {
176     let text_edit = text_edit(line_index, indel);
177     let insert_text_format =
178         if is_snippet { Some(lsp_types::InsertTextFormat::SNIPPET) } else { None };
179     lsp_ext::SnippetTextEdit {
180         range: text_edit.range,
181         new_text: text_edit.new_text,
182         insert_text_format,
183         annotation_id: None,
184     }
185 }
186
187 pub(crate) fn text_edit_vec(
188     line_index: &LineIndex,
189     text_edit: TextEdit,
190 ) -> Vec<lsp_types::TextEdit> {
191     text_edit.into_iter().map(|indel| self::text_edit(line_index, indel)).collect()
192 }
193
194 pub(crate) fn snippet_text_edit_vec(
195     line_index: &LineIndex,
196     is_snippet: bool,
197     text_edit: TextEdit,
198 ) -> Vec<lsp_ext::SnippetTextEdit> {
199     text_edit
200         .into_iter()
201         .map(|indel| self::snippet_text_edit(line_index, is_snippet, indel))
202         .collect()
203 }
204
205 pub(crate) fn completion_items(
206     config: &Config,
207     line_index: &LineIndex,
208     tdpp: lsp_types::TextDocumentPositionParams,
209     items: Vec<CompletionItem>,
210 ) -> Vec<lsp_types::CompletionItem> {
211     let max_relevance = items.iter().map(|it| it.relevance().score()).max().unwrap_or_default();
212     let mut res = Vec::with_capacity(items.len());
213     for item in items {
214         completion_item(&mut res, config, line_index, &tdpp, max_relevance, item)
215     }
216     res
217 }
218
219 fn completion_item(
220     acc: &mut Vec<lsp_types::CompletionItem>,
221     config: &Config,
222     line_index: &LineIndex,
223     tdpp: &lsp_types::TextDocumentPositionParams,
224     max_relevance: u32,
225     item: CompletionItem,
226 ) {
227     let mut additional_text_edits = Vec::new();
228
229     // LSP does not allow arbitrary edits in completion, so we have to do a
230     // non-trivial mapping here.
231     let text_edit = {
232         let mut text_edit = None;
233         let source_range = item.source_range();
234         for indel in item.text_edit().iter() {
235             if indel.delete.contains_range(source_range) {
236                 let insert_replace_support = config.insert_replace_support().then(|| tdpp.position);
237                 text_edit = Some(if indel.delete == source_range {
238                     self::completion_text_edit(line_index, insert_replace_support, indel.clone())
239                 } else {
240                     assert!(source_range.end() == indel.delete.end());
241                     let range1 = TextRange::new(indel.delete.start(), source_range.start());
242                     let range2 = source_range;
243                     let indel1 = Indel::replace(range1, String::new());
244                     let indel2 = Indel::replace(range2, indel.insert.clone());
245                     additional_text_edits.push(self::text_edit(line_index, indel1));
246                     self::completion_text_edit(line_index, insert_replace_support, indel2)
247                 })
248             } else {
249                 assert!(source_range.intersect(indel.delete).is_none());
250                 let text_edit = self::text_edit(line_index, indel.clone());
251                 additional_text_edits.push(text_edit);
252             }
253         }
254         text_edit.unwrap()
255     };
256
257     let mut lsp_item = lsp_types::CompletionItem {
258         label: item.label().to_string(),
259         detail: item.detail().map(|it| it.to_string()),
260         filter_text: Some(item.lookup().to_string()),
261         kind: Some(completion_item_kind(item.kind())),
262         text_edit: Some(text_edit),
263         additional_text_edits: Some(additional_text_edits),
264         documentation: item.documentation().map(documentation),
265         deprecated: Some(item.deprecated()),
266         ..Default::default()
267     };
268
269     set_score(&mut lsp_item, max_relevance, item.relevance());
270
271     if item.deprecated() {
272         lsp_item.tags = Some(vec![lsp_types::CompletionItemTag::DEPRECATED])
273     }
274
275     if item.trigger_call_info() && config.client_commands().trigger_parameter_hints {
276         lsp_item.command = Some(command::trigger_parameter_hints());
277     }
278
279     if item.is_snippet() {
280         lsp_item.insert_text_format = Some(lsp_types::InsertTextFormat::SNIPPET);
281     }
282     if config.completion().enable_imports_on_the_fly {
283         if let imports @ [_, ..] = item.imports_to_add() {
284             let imports: Vec<_> = imports
285                 .iter()
286                 .filter_map(|import_edit| {
287                     let import_path = &import_edit.import_path;
288                     let import_name = import_path.segments().last()?;
289                     Some(lsp_ext::CompletionImport {
290                         full_import_path: import_path.to_string(),
291                         imported_name: import_name.to_string(),
292                     })
293                 })
294                 .collect();
295             if !imports.is_empty() {
296                 let data = lsp_ext::CompletionResolveData { position: tdpp.clone(), imports };
297                 lsp_item.data = Some(to_value(data).unwrap());
298             }
299         }
300     }
301
302     if let Some((mutability, relevance)) = item.ref_match() {
303         let mut lsp_item_with_ref = lsp_item.clone();
304         set_score(&mut lsp_item_with_ref, max_relevance, relevance);
305         lsp_item_with_ref.label =
306             format!("&{}{}", mutability.as_keyword_for_ref(), lsp_item_with_ref.label);
307         if let Some(it) = &mut lsp_item_with_ref.text_edit {
308             let new_text = match it {
309                 lsp_types::CompletionTextEdit::Edit(it) => &mut it.new_text,
310                 lsp_types::CompletionTextEdit::InsertAndReplace(it) => &mut it.new_text,
311             };
312             *new_text = format!("&{}{}", mutability.as_keyword_for_ref(), new_text);
313         }
314
315         acc.push(lsp_item_with_ref);
316     };
317
318     acc.push(lsp_item);
319
320     fn set_score(
321         res: &mut lsp_types::CompletionItem,
322         max_relevance: u32,
323         relevance: CompletionRelevance,
324     ) {
325         if relevance.is_relevant() && relevance.score() == max_relevance {
326             res.preselect = Some(true);
327         }
328         // The relevance needs to be inverted to come up with a sort score
329         // because the client will sort ascending.
330         let sort_score = relevance.score() ^ 0xFF_FF_FF_FF;
331         // Zero pad the string to ensure values can be properly sorted
332         // by the client. Hex format is used because it is easier to
333         // visually compare very large values, which the sort text
334         // tends to be since it is the opposite of the score.
335         res.sort_text = Some(format!("{:08x}", sort_score));
336     }
337 }
338
339 pub(crate) fn signature_help(
340     call_info: SignatureHelp,
341     config: CallInfoConfig,
342     label_offsets: bool,
343 ) -> lsp_types::SignatureHelp {
344     let (label, parameters) = match (config.params_only, label_offsets) {
345         (concise, false) => {
346             let params = call_info
347                 .parameter_labels()
348                 .map(|label| lsp_types::ParameterInformation {
349                     label: lsp_types::ParameterLabel::Simple(label.to_string()),
350                     documentation: None,
351                 })
352                 .collect::<Vec<_>>();
353             let label =
354                 if concise { call_info.parameter_labels().join(", ") } else { call_info.signature };
355             (label, params)
356         }
357         (false, true) => {
358             let params = call_info
359                 .parameter_ranges()
360                 .iter()
361                 .map(|it| {
362                     let start = call_info.signature[..it.start().into()].chars().count() as u32;
363                     let end = call_info.signature[..it.end().into()].chars().count() as u32;
364                     [start, end]
365                 })
366                 .map(|label_offsets| lsp_types::ParameterInformation {
367                     label: lsp_types::ParameterLabel::LabelOffsets(label_offsets),
368                     documentation: None,
369                 })
370                 .collect::<Vec<_>>();
371             (call_info.signature, params)
372         }
373         (true, true) => {
374             let mut params = Vec::new();
375             let mut label = String::new();
376             let mut first = true;
377             for param in call_info.parameter_labels() {
378                 if !first {
379                     label.push_str(", ");
380                 }
381                 first = false;
382                 let start = label.chars().count() as u32;
383                 label.push_str(param);
384                 let end = label.chars().count() as u32;
385                 params.push(lsp_types::ParameterInformation {
386                     label: lsp_types::ParameterLabel::LabelOffsets([start, end]),
387                     documentation: None,
388                 });
389             }
390
391             (label, params)
392         }
393     };
394
395     let documentation = call_info.doc.filter(|_| config.docs).map(|doc| {
396         lsp_types::Documentation::MarkupContent(lsp_types::MarkupContent {
397             kind: lsp_types::MarkupKind::Markdown,
398             value: doc,
399         })
400     });
401
402     let active_parameter = call_info.active_parameter.map(|it| it as u32);
403
404     let signature = lsp_types::SignatureInformation {
405         label,
406         documentation,
407         parameters: Some(parameters),
408         active_parameter,
409     };
410     lsp_types::SignatureHelp {
411         signatures: vec![signature],
412         active_signature: Some(0),
413         active_parameter,
414     }
415 }
416
417 pub(crate) fn inlay_hint(
418     line_index: &LineIndex,
419     text_document: &lsp_types::TextDocumentIdentifier,
420     render_colons: bool,
421     inlay_hint: InlayHint,
422 ) -> lsp_types::InlayHint {
423     lsp_types::InlayHint {
424         position: match inlay_hint.kind {
425             // before annotated thing
426             InlayKind::ParameterHint
427             | InlayKind::ImplicitReborrowHint
428             | InlayKind::BindingModeHint => position(line_index, inlay_hint.range.start()),
429             // after annotated thing
430             InlayKind::ClosureReturnTypeHint
431             | InlayKind::TypeHint
432             | InlayKind::ChainingHint
433             | InlayKind::GenericParamListHint
434             | InlayKind::LifetimeHint
435             | InlayKind::ClosingBraceHint(_) => position(line_index, inlay_hint.range.end()),
436         },
437         padding_left: Some(match inlay_hint.kind {
438             InlayKind::TypeHint => !render_colons,
439             InlayKind::ChainingHint | InlayKind::ClosingBraceHint(_) => true,
440             InlayKind::BindingModeHint
441             | InlayKind::ClosureReturnTypeHint
442             | InlayKind::GenericParamListHint
443             | InlayKind::ImplicitReborrowHint
444             | InlayKind::LifetimeHint
445             | InlayKind::ParameterHint => false,
446         }),
447         padding_right: Some(match inlay_hint.kind {
448             InlayKind::ChainingHint
449             | InlayKind::ClosureReturnTypeHint
450             | InlayKind::GenericParamListHint
451             | InlayKind::ImplicitReborrowHint
452             | InlayKind::TypeHint
453             | InlayKind::ClosingBraceHint(_) => false,
454             InlayKind::BindingModeHint => inlay_hint.label != "&",
455             InlayKind::ParameterHint | InlayKind::LifetimeHint => true,
456         }),
457         label: lsp_types::InlayHintLabel::String(match inlay_hint.kind {
458             InlayKind::ParameterHint if render_colons => format!("{}:", inlay_hint.label),
459             InlayKind::TypeHint if render_colons => format!(": {}", inlay_hint.label),
460             InlayKind::ClosureReturnTypeHint => format!(" -> {}", inlay_hint.label),
461             _ => inlay_hint.label,
462         }),
463         kind: match inlay_hint.kind {
464             InlayKind::ParameterHint => Some(lsp_types::InlayHintKind::PARAMETER),
465             InlayKind::ClosureReturnTypeHint | InlayKind::TypeHint | InlayKind::ChainingHint => {
466                 Some(lsp_types::InlayHintKind::TYPE)
467             }
468             InlayKind::BindingModeHint
469             | InlayKind::GenericParamListHint
470             | InlayKind::LifetimeHint
471             | InlayKind::ImplicitReborrowHint
472             | InlayKind::ClosingBraceHint(_) => None,
473         },
474         text_edits: None,
475         tooltip: None,
476         data: match inlay_hint.kind {
477             InlayKind::ClosingBraceHint(Some(offset)) => Some(
478                 to_value(lsp_ext::InlayHintResolveData {
479                     position: lsp_types::TextDocumentPositionParams {
480                         text_document: text_document.clone(),
481                         position: position(line_index, offset),
482                     },
483                 })
484                 .unwrap(),
485             ),
486             _ => None,
487         },
488     }
489 }
490
491 static TOKEN_RESULT_COUNTER: AtomicU32 = AtomicU32::new(1);
492
493 pub(crate) fn semantic_tokens(
494     text: &str,
495     line_index: &LineIndex,
496     highlights: Vec<HlRange>,
497     highlight_strings: bool,
498 ) -> lsp_types::SemanticTokens {
499     let id = TOKEN_RESULT_COUNTER.fetch_add(1, Ordering::SeqCst).to_string();
500     let mut builder = semantic_tokens::SemanticTokensBuilder::new(id);
501
502     for highlight_range in highlights {
503         if highlight_range.highlight.is_empty() {
504             continue;
505         }
506         let (ty, mods) = semantic_token_type_and_modifiers(highlight_range.highlight);
507         if !highlight_strings && ty == lsp_types::SemanticTokenType::STRING {
508             continue;
509         }
510         let token_index = semantic_tokens::type_index(ty);
511         let modifier_bitset = mods.0;
512
513         for mut text_range in line_index.index.lines(highlight_range.range) {
514             if text[text_range].ends_with('\n') {
515                 text_range =
516                     TextRange::new(text_range.start(), text_range.end() - TextSize::of('\n'));
517             }
518             let range = range(line_index, text_range);
519             builder.push(range, token_index, modifier_bitset);
520         }
521     }
522
523     builder.build()
524 }
525
526 pub(crate) fn semantic_token_delta(
527     previous: &lsp_types::SemanticTokens,
528     current: &lsp_types::SemanticTokens,
529 ) -> lsp_types::SemanticTokensDelta {
530     let result_id = current.result_id.clone();
531     let edits = semantic_tokens::diff_tokens(&previous.data, &current.data);
532     lsp_types::SemanticTokensDelta { result_id, edits }
533 }
534
535 fn semantic_token_type_and_modifiers(
536     highlight: Highlight,
537 ) -> (lsp_types::SemanticTokenType, semantic_tokens::ModifierSet) {
538     let mut mods = semantic_tokens::ModifierSet::default();
539     let type_ = match highlight.tag {
540         HlTag::Symbol(symbol) => match symbol {
541             SymbolKind::Attribute => semantic_tokens::ATTRIBUTE,
542             SymbolKind::Derive => semantic_tokens::DERIVE,
543             SymbolKind::Module => lsp_types::SemanticTokenType::NAMESPACE,
544             SymbolKind::Impl => semantic_tokens::TYPE_ALIAS,
545             SymbolKind::Field => lsp_types::SemanticTokenType::PROPERTY,
546             SymbolKind::TypeParam => lsp_types::SemanticTokenType::TYPE_PARAMETER,
547             SymbolKind::ConstParam => semantic_tokens::CONST_PARAMETER,
548             SymbolKind::LifetimeParam => semantic_tokens::LIFETIME,
549             SymbolKind::Label => semantic_tokens::LABEL,
550             SymbolKind::ValueParam => lsp_types::SemanticTokenType::PARAMETER,
551             SymbolKind::SelfParam => semantic_tokens::SELF_KEYWORD,
552             SymbolKind::SelfType => semantic_tokens::SELF_TYPE_KEYWORD,
553             SymbolKind::Local => lsp_types::SemanticTokenType::VARIABLE,
554             SymbolKind::Function => {
555                 if highlight.mods.contains(HlMod::Associated) {
556                     lsp_types::SemanticTokenType::METHOD
557                 } else {
558                     lsp_types::SemanticTokenType::FUNCTION
559                 }
560             }
561             SymbolKind::Const => {
562                 mods |= semantic_tokens::CONSTANT;
563                 mods |= lsp_types::SemanticTokenModifier::STATIC;
564                 lsp_types::SemanticTokenType::VARIABLE
565             }
566             SymbolKind::Static => {
567                 mods |= lsp_types::SemanticTokenModifier::STATIC;
568                 lsp_types::SemanticTokenType::VARIABLE
569             }
570             SymbolKind::Struct => lsp_types::SemanticTokenType::STRUCT,
571             SymbolKind::Enum => lsp_types::SemanticTokenType::ENUM,
572             SymbolKind::Variant => lsp_types::SemanticTokenType::ENUM_MEMBER,
573             SymbolKind::Union => semantic_tokens::UNION,
574             SymbolKind::TypeAlias => semantic_tokens::TYPE_ALIAS,
575             SymbolKind::Trait => lsp_types::SemanticTokenType::INTERFACE,
576             SymbolKind::Macro => lsp_types::SemanticTokenType::MACRO,
577             SymbolKind::BuiltinAttr => semantic_tokens::BUILTIN_ATTRIBUTE,
578             SymbolKind::ToolModule => semantic_tokens::TOOL_MODULE,
579         },
580         HlTag::AttributeBracket => semantic_tokens::ATTRIBUTE_BRACKET,
581         HlTag::BoolLiteral => semantic_tokens::BOOLEAN,
582         HlTag::BuiltinType => semantic_tokens::BUILTIN_TYPE,
583         HlTag::ByteLiteral | HlTag::NumericLiteral => lsp_types::SemanticTokenType::NUMBER,
584         HlTag::CharLiteral => semantic_tokens::CHAR,
585         HlTag::Comment => lsp_types::SemanticTokenType::COMMENT,
586         HlTag::EscapeSequence => semantic_tokens::ESCAPE_SEQUENCE,
587         HlTag::FormatSpecifier => semantic_tokens::FORMAT_SPECIFIER,
588         HlTag::Keyword => lsp_types::SemanticTokenType::KEYWORD,
589         HlTag::None => semantic_tokens::GENERIC,
590         HlTag::Operator(op) => match op {
591             HlOperator::Bitwise => semantic_tokens::BITWISE,
592             HlOperator::Arithmetic => semantic_tokens::ARITHMETIC,
593             HlOperator::Logical => semantic_tokens::LOGICAL,
594             HlOperator::Comparison => semantic_tokens::COMPARISON,
595             HlOperator::Other => semantic_tokens::OPERATOR,
596         },
597         HlTag::StringLiteral => lsp_types::SemanticTokenType::STRING,
598         HlTag::UnresolvedReference => semantic_tokens::UNRESOLVED_REFERENCE,
599         HlTag::Punctuation(punct) => match punct {
600             HlPunct::Bracket => semantic_tokens::BRACKET,
601             HlPunct::Brace => semantic_tokens::BRACE,
602             HlPunct::Parenthesis => semantic_tokens::PARENTHESIS,
603             HlPunct::Angle => semantic_tokens::ANGLE,
604             HlPunct::Comma => semantic_tokens::COMMA,
605             HlPunct::Dot => semantic_tokens::DOT,
606             HlPunct::Colon => semantic_tokens::COLON,
607             HlPunct::Semi => semantic_tokens::SEMICOLON,
608             HlPunct::Other => semantic_tokens::PUNCTUATION,
609             HlPunct::MacroBang => semantic_tokens::MACRO_BANG,
610         },
611     };
612
613     for modifier in highlight.mods.iter() {
614         let modifier = match modifier {
615             HlMod::Associated => continue,
616             HlMod::Async => semantic_tokens::ASYNC,
617             HlMod::Attribute => semantic_tokens::ATTRIBUTE_MODIFIER,
618             HlMod::Callable => semantic_tokens::CALLABLE,
619             HlMod::Consuming => semantic_tokens::CONSUMING,
620             HlMod::ControlFlow => semantic_tokens::CONTROL_FLOW,
621             HlMod::CrateRoot => semantic_tokens::CRATE_ROOT,
622             HlMod::DefaultLibrary => lsp_types::SemanticTokenModifier::DEFAULT_LIBRARY,
623             HlMod::Definition => lsp_types::SemanticTokenModifier::DECLARATION,
624             HlMod::Documentation => lsp_types::SemanticTokenModifier::DOCUMENTATION,
625             HlMod::Injected => semantic_tokens::INJECTED,
626             HlMod::IntraDocLink => semantic_tokens::INTRA_DOC_LINK,
627             HlMod::Library => semantic_tokens::LIBRARY,
628             HlMod::Mutable => semantic_tokens::MUTABLE,
629             HlMod::Public => semantic_tokens::PUBLIC,
630             HlMod::Reference => semantic_tokens::REFERENCE,
631             HlMod::Static => lsp_types::SemanticTokenModifier::STATIC,
632             HlMod::Trait => semantic_tokens::TRAIT_MODIFIER,
633             HlMod::Unsafe => semantic_tokens::UNSAFE,
634         };
635         mods |= modifier;
636     }
637
638     (type_, mods)
639 }
640
641 pub(crate) fn folding_range(
642     text: &str,
643     line_index: &LineIndex,
644     line_folding_only: bool,
645     fold: Fold,
646 ) -> lsp_types::FoldingRange {
647     let kind = match fold.kind {
648         FoldKind::Comment => Some(lsp_types::FoldingRangeKind::Comment),
649         FoldKind::Imports => Some(lsp_types::FoldingRangeKind::Imports),
650         FoldKind::Region => Some(lsp_types::FoldingRangeKind::Region),
651         FoldKind::Mods
652         | FoldKind::Block
653         | FoldKind::ArgList
654         | FoldKind::Consts
655         | FoldKind::Statics
656         | FoldKind::WhereClause
657         | FoldKind::ReturnType
658         | FoldKind::Array => None,
659     };
660
661     let range = range(line_index, fold.range);
662
663     if line_folding_only {
664         // Clients with line_folding_only == true (such as VSCode) will fold the whole end line
665         // even if it contains text not in the folding range. To prevent that we exclude
666         // range.end.line from the folding region if there is more text after range.end
667         // on the same line.
668         let has_more_text_on_end_line = text[TextRange::new(fold.range.end(), TextSize::of(text))]
669             .chars()
670             .take_while(|it| *it != '\n')
671             .any(|it| !it.is_whitespace());
672
673         let end_line = if has_more_text_on_end_line {
674             range.end.line.saturating_sub(1)
675         } else {
676             range.end.line
677         };
678
679         lsp_types::FoldingRange {
680             start_line: range.start.line,
681             start_character: None,
682             end_line,
683             end_character: None,
684             kind,
685         }
686     } else {
687         lsp_types::FoldingRange {
688             start_line: range.start.line,
689             start_character: Some(range.start.character),
690             end_line: range.end.line,
691             end_character: Some(range.end.character),
692             kind,
693         }
694     }
695 }
696
697 pub(crate) fn url(snap: &GlobalStateSnapshot, file_id: FileId) -> lsp_types::Url {
698     snap.file_id_to_url(file_id)
699 }
700
701 /// Returns a `Url` object from a given path, will lowercase drive letters if present.
702 /// This will only happen when processing windows paths.
703 ///
704 /// When processing non-windows path, this is essentially the same as `Url::from_file_path`.
705 pub(crate) fn url_from_abs_path(path: &AbsPath) -> lsp_types::Url {
706     let url = lsp_types::Url::from_file_path(path).unwrap();
707     match path.as_ref().components().next() {
708         Some(path::Component::Prefix(prefix))
709             if matches!(prefix.kind(), path::Prefix::Disk(_) | path::Prefix::VerbatimDisk(_)) =>
710         {
711             // Need to lowercase driver letter
712         }
713         _ => return url,
714     }
715
716     let driver_letter_range = {
717         let (scheme, drive_letter, _rest) = match url.as_str().splitn(3, ':').collect_tuple() {
718             Some(it) => it,
719             None => return url,
720         };
721         let start = scheme.len() + ':'.len_utf8();
722         start..(start + drive_letter.len())
723     };
724
725     // Note: lowercasing the `path` itself doesn't help, the `Url::parse`
726     // machinery *also* canonicalizes the drive letter. So, just massage the
727     // string in place.
728     let mut url: String = url.into();
729     url[driver_letter_range].make_ascii_lowercase();
730     lsp_types::Url::parse(&url).unwrap()
731 }
732
733 pub(crate) fn optional_versioned_text_document_identifier(
734     snap: &GlobalStateSnapshot,
735     file_id: FileId,
736 ) -> lsp_types::OptionalVersionedTextDocumentIdentifier {
737     let url = url(snap, file_id);
738     let version = snap.url_file_version(&url);
739     lsp_types::OptionalVersionedTextDocumentIdentifier { uri: url, version }
740 }
741
742 pub(crate) fn location(
743     snap: &GlobalStateSnapshot,
744     frange: FileRange,
745 ) -> Result<lsp_types::Location> {
746     let url = url(snap, frange.file_id);
747     let line_index = snap.file_line_index(frange.file_id)?;
748     let range = range(&line_index, frange.range);
749     let loc = lsp_types::Location::new(url, range);
750     Ok(loc)
751 }
752
753 /// Prefer using `location_link`, if the client has the cap.
754 pub(crate) fn location_from_nav(
755     snap: &GlobalStateSnapshot,
756     nav: NavigationTarget,
757 ) -> Result<lsp_types::Location> {
758     let url = url(snap, nav.file_id);
759     let line_index = snap.file_line_index(nav.file_id)?;
760     let range = range(&line_index, nav.full_range);
761     let loc = lsp_types::Location::new(url, range);
762     Ok(loc)
763 }
764
765 pub(crate) fn location_link(
766     snap: &GlobalStateSnapshot,
767     src: Option<FileRange>,
768     target: NavigationTarget,
769 ) -> Result<lsp_types::LocationLink> {
770     let origin_selection_range = match src {
771         Some(src) => {
772             let line_index = snap.file_line_index(src.file_id)?;
773             let range = range(&line_index, src.range);
774             Some(range)
775         }
776         None => None,
777     };
778     let (target_uri, target_range, target_selection_range) = location_info(snap, target)?;
779     let res = lsp_types::LocationLink {
780         origin_selection_range,
781         target_uri,
782         target_range,
783         target_selection_range,
784     };
785     Ok(res)
786 }
787
788 fn location_info(
789     snap: &GlobalStateSnapshot,
790     target: NavigationTarget,
791 ) -> Result<(lsp_types::Url, lsp_types::Range, lsp_types::Range)> {
792     let line_index = snap.file_line_index(target.file_id)?;
793
794     let target_uri = url(snap, target.file_id);
795     let target_range = range(&line_index, target.full_range);
796     let target_selection_range =
797         target.focus_range.map(|it| range(&line_index, it)).unwrap_or(target_range);
798     Ok((target_uri, target_range, target_selection_range))
799 }
800
801 pub(crate) fn goto_definition_response(
802     snap: &GlobalStateSnapshot,
803     src: Option<FileRange>,
804     targets: Vec<NavigationTarget>,
805 ) -> Result<lsp_types::GotoDefinitionResponse> {
806     if snap.config.location_link() {
807         let links = targets
808             .into_iter()
809             .map(|nav| location_link(snap, src, nav))
810             .collect::<Result<Vec<_>>>()?;
811         Ok(links.into())
812     } else {
813         let locations = targets
814             .into_iter()
815             .map(|nav| {
816                 location(snap, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
817             })
818             .collect::<Result<Vec<_>>>()?;
819         Ok(locations.into())
820     }
821 }
822
823 fn outside_workspace_annotation_id() -> String {
824     String::from("OutsideWorkspace")
825 }
826
827 pub(crate) fn snippet_text_document_edit(
828     snap: &GlobalStateSnapshot,
829     is_snippet: bool,
830     file_id: FileId,
831     edit: TextEdit,
832 ) -> Result<lsp_ext::SnippetTextDocumentEdit> {
833     let text_document = optional_versioned_text_document_identifier(snap, file_id);
834     let line_index = snap.file_line_index(file_id)?;
835     let mut edits: Vec<_> =
836         edit.into_iter().map(|it| snippet_text_edit(&line_index, is_snippet, it)).collect();
837
838     if snap.analysis.is_library_file(file_id)? && snap.config.change_annotation_support() {
839         for edit in &mut edits {
840             edit.annotation_id = Some(outside_workspace_annotation_id())
841         }
842     }
843     Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits })
844 }
845
846 pub(crate) fn snippet_text_document_ops(
847     snap: &GlobalStateSnapshot,
848     file_system_edit: FileSystemEdit,
849 ) -> Cancellable<Vec<lsp_ext::SnippetDocumentChangeOperation>> {
850     let mut ops = Vec::new();
851     match file_system_edit {
852         FileSystemEdit::CreateFile { dst, initial_contents } => {
853             let uri = snap.anchored_path(&dst);
854             let create_file = lsp_types::ResourceOp::Create(lsp_types::CreateFile {
855                 uri: uri.clone(),
856                 options: None,
857                 annotation_id: None,
858             });
859             ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(create_file));
860             if !initial_contents.is_empty() {
861                 let text_document =
862                     lsp_types::OptionalVersionedTextDocumentIdentifier { uri, version: None };
863                 let text_edit = lsp_ext::SnippetTextEdit {
864                     range: lsp_types::Range::default(),
865                     new_text: initial_contents,
866                     insert_text_format: Some(lsp_types::InsertTextFormat::PLAIN_TEXT),
867                     annotation_id: None,
868                 };
869                 let edit_file =
870                     lsp_ext::SnippetTextDocumentEdit { text_document, edits: vec![text_edit] };
871                 ops.push(lsp_ext::SnippetDocumentChangeOperation::Edit(edit_file));
872             }
873         }
874         FileSystemEdit::MoveFile { src, dst } => {
875             let old_uri = snap.file_id_to_url(src);
876             let new_uri = snap.anchored_path(&dst);
877             let mut rename_file =
878                 lsp_types::RenameFile { old_uri, new_uri, options: None, annotation_id: None };
879             if snap.analysis.is_library_file(src).ok() == Some(true)
880                 && snap.config.change_annotation_support()
881             {
882                 rename_file.annotation_id = Some(outside_workspace_annotation_id())
883             }
884             ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(lsp_types::ResourceOp::Rename(
885                 rename_file,
886             )))
887         }
888         FileSystemEdit::MoveDir { src, src_id, dst } => {
889             let old_uri = snap.anchored_path(&src);
890             let new_uri = snap.anchored_path(&dst);
891             let mut rename_file =
892                 lsp_types::RenameFile { old_uri, new_uri, options: None, annotation_id: None };
893             if snap.analysis.is_library_file(src_id).ok() == Some(true)
894                 && snap.config.change_annotation_support()
895             {
896                 rename_file.annotation_id = Some(outside_workspace_annotation_id())
897             }
898             ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(lsp_types::ResourceOp::Rename(
899                 rename_file,
900             )))
901         }
902     }
903     Ok(ops)
904 }
905
906 pub(crate) fn snippet_workspace_edit(
907     snap: &GlobalStateSnapshot,
908     source_change: SourceChange,
909 ) -> Result<lsp_ext::SnippetWorkspaceEdit> {
910     let mut document_changes: Vec<lsp_ext::SnippetDocumentChangeOperation> = Vec::new();
911
912     for op in source_change.file_system_edits {
913         let ops = snippet_text_document_ops(snap, op)?;
914         document_changes.extend_from_slice(&ops);
915     }
916     for (file_id, edit) in source_change.source_file_edits {
917         let edit = snippet_text_document_edit(snap, source_change.is_snippet, file_id, edit)?;
918         document_changes.push(lsp_ext::SnippetDocumentChangeOperation::Edit(edit));
919     }
920     let mut workspace_edit = lsp_ext::SnippetWorkspaceEdit {
921         changes: None,
922         document_changes: Some(document_changes),
923         change_annotations: None,
924     };
925     if snap.config.change_annotation_support() {
926         workspace_edit.change_annotations = Some(
927             once((
928                 outside_workspace_annotation_id(),
929                 lsp_types::ChangeAnnotation {
930                     label: String::from("Edit outside of the workspace"),
931                     needs_confirmation: Some(true),
932                     description: Some(String::from(
933                         "This edit lies outside of the workspace and may affect dependencies",
934                     )),
935                 },
936             ))
937             .collect(),
938         )
939     }
940     Ok(workspace_edit)
941 }
942
943 pub(crate) fn workspace_edit(
944     snap: &GlobalStateSnapshot,
945     source_change: SourceChange,
946 ) -> Result<lsp_types::WorkspaceEdit> {
947     assert!(!source_change.is_snippet);
948     snippet_workspace_edit(snap, source_change).map(|it| it.into())
949 }
950
951 impl From<lsp_ext::SnippetWorkspaceEdit> for lsp_types::WorkspaceEdit {
952     fn from(snippet_workspace_edit: lsp_ext::SnippetWorkspaceEdit) -> lsp_types::WorkspaceEdit {
953         lsp_types::WorkspaceEdit {
954             changes: None,
955             document_changes: snippet_workspace_edit.document_changes.map(|changes| {
956                 lsp_types::DocumentChanges::Operations(
957                     changes
958                         .into_iter()
959                         .map(|change| match change {
960                             lsp_ext::SnippetDocumentChangeOperation::Op(op) => {
961                                 lsp_types::DocumentChangeOperation::Op(op)
962                             }
963                             lsp_ext::SnippetDocumentChangeOperation::Edit(edit) => {
964                                 lsp_types::DocumentChangeOperation::Edit(
965                                     lsp_types::TextDocumentEdit {
966                                         text_document: edit.text_document,
967                                         edits: edit.edits.into_iter().map(From::from).collect(),
968                                     },
969                                 )
970                             }
971                         })
972                         .collect(),
973                 )
974             }),
975             change_annotations: snippet_workspace_edit.change_annotations,
976         }
977     }
978 }
979
980 impl From<lsp_ext::SnippetTextEdit>
981     for lsp_types::OneOf<lsp_types::TextEdit, lsp_types::AnnotatedTextEdit>
982 {
983     fn from(
984         lsp_ext::SnippetTextEdit { annotation_id, insert_text_format:_, new_text, range }: lsp_ext::SnippetTextEdit,
985     ) -> Self {
986         match annotation_id {
987             Some(annotation_id) => lsp_types::OneOf::Right(lsp_types::AnnotatedTextEdit {
988                 text_edit: lsp_types::TextEdit { range, new_text },
989                 annotation_id,
990             }),
991             None => lsp_types::OneOf::Left(lsp_types::TextEdit { range, new_text }),
992         }
993     }
994 }
995
996 pub(crate) fn call_hierarchy_item(
997     snap: &GlobalStateSnapshot,
998     target: NavigationTarget,
999 ) -> Result<lsp_types::CallHierarchyItem> {
1000     let name = target.name.to_string();
1001     let detail = target.description.clone();
1002     let kind = target.kind.map(symbol_kind).unwrap_or(lsp_types::SymbolKind::FUNCTION);
1003     let (uri, range, selection_range) = location_info(snap, target)?;
1004     Ok(lsp_types::CallHierarchyItem {
1005         name,
1006         kind,
1007         tags: None,
1008         detail,
1009         uri,
1010         range,
1011         selection_range,
1012         data: None,
1013     })
1014 }
1015
1016 pub(crate) fn code_action_kind(kind: AssistKind) -> lsp_types::CodeActionKind {
1017     match kind {
1018         AssistKind::None | AssistKind::Generate => lsp_types::CodeActionKind::EMPTY,
1019         AssistKind::QuickFix => lsp_types::CodeActionKind::QUICKFIX,
1020         AssistKind::Refactor => lsp_types::CodeActionKind::REFACTOR,
1021         AssistKind::RefactorExtract => lsp_types::CodeActionKind::REFACTOR_EXTRACT,
1022         AssistKind::RefactorInline => lsp_types::CodeActionKind::REFACTOR_INLINE,
1023         AssistKind::RefactorRewrite => lsp_types::CodeActionKind::REFACTOR_REWRITE,
1024     }
1025 }
1026
1027 pub(crate) fn code_action(
1028     snap: &GlobalStateSnapshot,
1029     assist: Assist,
1030     resolve_data: Option<(usize, lsp_types::CodeActionParams)>,
1031 ) -> Result<lsp_ext::CodeAction> {
1032     let mut res = lsp_ext::CodeAction {
1033         title: assist.label.to_string(),
1034         group: assist.group.filter(|_| snap.config.code_action_group()).map(|gr| gr.0),
1035         kind: Some(code_action_kind(assist.id.1)),
1036         edit: None,
1037         is_preferred: None,
1038         data: None,
1039         command: None,
1040     };
1041
1042     if assist.trigger_signature_help && snap.config.client_commands().trigger_parameter_hints {
1043         res.command = Some(command::trigger_parameter_hints());
1044     }
1045
1046     match (assist.source_change, resolve_data) {
1047         (Some(it), _) => res.edit = Some(snippet_workspace_edit(snap, it)?),
1048         (None, Some((index, code_action_params))) => {
1049             res.data = Some(lsp_ext::CodeActionData {
1050                 id: format!("{}:{}:{}", assist.id.0, assist.id.1.name(), index),
1051                 code_action_params,
1052             });
1053         }
1054         (None, None) => {
1055             stdx::never!("assist should always be resolved if client can't do lazy resolving")
1056         }
1057     };
1058     Ok(res)
1059 }
1060
1061 pub(crate) fn runnable(
1062     snap: &GlobalStateSnapshot,
1063     runnable: Runnable,
1064 ) -> Result<lsp_ext::Runnable> {
1065     let config = snap.config.runnables();
1066     let spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id)?;
1067     let workspace_root = spec.as_ref().map(|it| it.workspace_root.clone());
1068     let target = spec.as_ref().map(|s| s.target.clone());
1069     let (cargo_args, executable_args) =
1070         CargoTargetSpec::runnable_args(snap, spec, &runnable.kind, &runnable.cfg)?;
1071     let label = runnable.label(target);
1072     let location = location_link(snap, None, runnable.nav)?;
1073
1074     Ok(lsp_ext::Runnable {
1075         label,
1076         location: Some(location),
1077         kind: lsp_ext::RunnableKind::Cargo,
1078         args: lsp_ext::CargoRunnable {
1079             workspace_root: workspace_root.map(|it| it.into()),
1080             override_cargo: config.override_cargo,
1081             cargo_args,
1082             cargo_extra_args: config.cargo_extra_args,
1083             executable_args,
1084             expect_test: None,
1085         },
1086     })
1087 }
1088
1089 pub(crate) fn code_lens(
1090     acc: &mut Vec<lsp_types::CodeLens>,
1091     snap: &GlobalStateSnapshot,
1092     annotation: Annotation,
1093 ) -> Result<()> {
1094     let client_commands_config = snap.config.client_commands();
1095     match annotation.kind {
1096         AnnotationKind::Runnable(run) => {
1097             let line_index = snap.file_line_index(run.nav.file_id)?;
1098             let annotation_range = range(&line_index, annotation.range);
1099
1100             let title = run.title();
1101             let can_debug = match run.kind {
1102                 ide::RunnableKind::DocTest { .. } => false,
1103                 ide::RunnableKind::TestMod { .. }
1104                 | ide::RunnableKind::Test { .. }
1105                 | ide::RunnableKind::Bench { .. }
1106                 | ide::RunnableKind::Bin => true,
1107             };
1108             let r = runnable(snap, run)?;
1109
1110             let lens_config = snap.config.lens();
1111             if lens_config.run && client_commands_config.run_single {
1112                 let command = command::run_single(&r, &title);
1113                 acc.push(lsp_types::CodeLens {
1114                     range: annotation_range,
1115                     command: Some(command),
1116                     data: None,
1117                 })
1118             }
1119             if lens_config.debug && can_debug && client_commands_config.debug_single {
1120                 let command = command::debug_single(&r);
1121                 acc.push(lsp_types::CodeLens {
1122                     range: annotation_range,
1123                     command: Some(command),
1124                     data: None,
1125                 })
1126             }
1127         }
1128         AnnotationKind::HasImpls { file_id, data } => {
1129             if !client_commands_config.show_reference {
1130                 return Ok(());
1131             }
1132             let line_index = snap.file_line_index(file_id)?;
1133             let annotation_range = range(&line_index, annotation.range);
1134             let url = url(snap, file_id);
1135
1136             let id = lsp_types::TextDocumentIdentifier { uri: url.clone() };
1137
1138             let doc_pos = lsp_types::TextDocumentPositionParams::new(id, annotation_range.start);
1139
1140             let goto_params = lsp_types::request::GotoImplementationParams {
1141                 text_document_position_params: doc_pos,
1142                 work_done_progress_params: Default::default(),
1143                 partial_result_params: Default::default(),
1144             };
1145
1146             let command = data.map(|ranges| {
1147                 let locations: Vec<lsp_types::Location> = ranges
1148                     .into_iter()
1149                     .filter_map(|target| {
1150                         location(
1151                             snap,
1152                             FileRange { file_id: target.file_id, range: target.full_range },
1153                         )
1154                         .ok()
1155                     })
1156                     .collect();
1157
1158                 command::show_references(
1159                     implementation_title(locations.len()),
1160                     &url,
1161                     annotation_range.start,
1162                     locations,
1163                 )
1164             });
1165
1166             acc.push(lsp_types::CodeLens {
1167                 range: annotation_range,
1168                 command,
1169                 data: Some(to_value(lsp_ext::CodeLensResolveData::Impls(goto_params)).unwrap()),
1170             })
1171         }
1172         AnnotationKind::HasReferences { file_id, data } => {
1173             if !client_commands_config.show_reference {
1174                 return Ok(());
1175             }
1176             let line_index = snap.file_line_index(file_id)?;
1177             let annotation_range = range(&line_index, annotation.range);
1178             let url = url(snap, file_id);
1179
1180             let id = lsp_types::TextDocumentIdentifier { uri: url.clone() };
1181
1182             let doc_pos = lsp_types::TextDocumentPositionParams::new(id, annotation_range.start);
1183
1184             let command = data.map(|ranges| {
1185                 let locations: Vec<lsp_types::Location> =
1186                     ranges.into_iter().filter_map(|range| location(snap, range).ok()).collect();
1187
1188                 command::show_references(
1189                     reference_title(locations.len()),
1190                     &url,
1191                     annotation_range.start,
1192                     locations,
1193                 )
1194             });
1195
1196             acc.push(lsp_types::CodeLens {
1197                 range: annotation_range,
1198                 command,
1199                 data: Some(to_value(lsp_ext::CodeLensResolveData::References(doc_pos)).unwrap()),
1200             })
1201         }
1202     }
1203     Ok(())
1204 }
1205
1206 pub(crate) mod command {
1207     use ide::{FileRange, NavigationTarget};
1208     use serde_json::to_value;
1209
1210     use crate::{
1211         global_state::GlobalStateSnapshot,
1212         lsp_ext,
1213         to_proto::{location, location_link},
1214     };
1215
1216     pub(crate) fn show_references(
1217         title: String,
1218         uri: &lsp_types::Url,
1219         position: lsp_types::Position,
1220         locations: Vec<lsp_types::Location>,
1221     ) -> lsp_types::Command {
1222         // We cannot use the 'editor.action.showReferences' command directly
1223         // because that command requires vscode types which we convert in the handler
1224         // on the client side.
1225
1226         lsp_types::Command {
1227             title,
1228             command: "rust-analyzer.showReferences".into(),
1229             arguments: Some(vec![
1230                 to_value(uri).unwrap(),
1231                 to_value(position).unwrap(),
1232                 to_value(locations).unwrap(),
1233             ]),
1234         }
1235     }
1236
1237     pub(crate) fn run_single(runnable: &lsp_ext::Runnable, title: &str) -> lsp_types::Command {
1238         lsp_types::Command {
1239             title: title.to_string(),
1240             command: "rust-analyzer.runSingle".into(),
1241             arguments: Some(vec![to_value(runnable).unwrap()]),
1242         }
1243     }
1244
1245     pub(crate) fn debug_single(runnable: &lsp_ext::Runnable) -> lsp_types::Command {
1246         lsp_types::Command {
1247             title: "Debug".into(),
1248             command: "rust-analyzer.debugSingle".into(),
1249             arguments: Some(vec![to_value(runnable).unwrap()]),
1250         }
1251     }
1252
1253     pub(crate) fn goto_location(
1254         snap: &GlobalStateSnapshot,
1255         nav: &NavigationTarget,
1256     ) -> Option<lsp_types::Command> {
1257         let value = if snap.config.location_link() {
1258             let link = location_link(snap, None, nav.clone()).ok()?;
1259             to_value(link).ok()?
1260         } else {
1261             let range = FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() };
1262             let location = location(snap, range).ok()?;
1263             to_value(location).ok()?
1264         };
1265
1266         Some(lsp_types::Command {
1267             title: nav.name.to_string(),
1268             command: "rust-analyzer.gotoLocation".into(),
1269             arguments: Some(vec![value]),
1270         })
1271     }
1272
1273     pub(crate) fn trigger_parameter_hints() -> lsp_types::Command {
1274         lsp_types::Command {
1275             title: "triggerParameterHints".into(),
1276             command: "editor.action.triggerParameterHints".into(),
1277             arguments: None,
1278         }
1279     }
1280 }
1281
1282 pub(crate) fn implementation_title(count: usize) -> String {
1283     if count == 1 {
1284         "1 implementation".into()
1285     } else {
1286         format!("{} implementations", count)
1287     }
1288 }
1289
1290 pub(crate) fn reference_title(count: usize) -> String {
1291     if count == 1 {
1292         "1 reference".into()
1293     } else {
1294         format!("{} references", count)
1295     }
1296 }
1297
1298 pub(crate) fn markup_content(
1299     markup: Markup,
1300     kind: ide::HoverDocFormat,
1301 ) -> lsp_types::MarkupContent {
1302     let kind = match kind {
1303         ide::HoverDocFormat::Markdown => lsp_types::MarkupKind::Markdown,
1304         ide::HoverDocFormat::PlainText => lsp_types::MarkupKind::PlainText,
1305     };
1306     let value = crate::markdown::format_docs(markup.as_str());
1307     lsp_types::MarkupContent { kind, value }
1308 }
1309
1310 pub(crate) fn rename_error(err: RenameError) -> crate::LspError {
1311     // This is wrong, but we don't have a better alternative I suppose?
1312     // https://github.com/microsoft/language-server-protocol/issues/1341
1313     invalid_params_error(err.to_string())
1314 }
1315
1316 #[cfg(test)]
1317 mod tests {
1318     use std::sync::Arc;
1319
1320     use ide::Analysis;
1321
1322     use super::*;
1323
1324     #[test]
1325     fn conv_fold_line_folding_only_fixup() {
1326         let text = r#"mod a;
1327 mod b;
1328 mod c;
1329
1330 fn main() {
1331     if cond {
1332         a::do_a();
1333     } else {
1334         b::do_b();
1335     }
1336 }"#;
1337
1338         let (analysis, file_id) = Analysis::from_single_file(text.to_string());
1339         let folds = analysis.folding_ranges(file_id).unwrap();
1340         assert_eq!(folds.len(), 4);
1341
1342         let line_index = LineIndex {
1343             index: Arc::new(ide::LineIndex::new(text)),
1344             endings: LineEndings::Unix,
1345             encoding: OffsetEncoding::Utf16,
1346         };
1347         let converted: Vec<lsp_types::FoldingRange> =
1348             folds.into_iter().map(|it| folding_range(text, &line_index, true, it)).collect();
1349
1350         let expected_lines = [(0, 2), (4, 10), (5, 6), (7, 9)];
1351         assert_eq!(converted.len(), expected_lines.len());
1352         for (folding_range, (start_line, end_line)) in converted.iter().zip(expected_lines.iter()) {
1353             assert_eq!(folding_range.start_line, *start_line);
1354             assert_eq!(folding_range.start_character, None);
1355             assert_eq!(folding_range.end_line, *end_line);
1356             assert_eq!(folding_range.end_character, None);
1357         }
1358     }
1359
1360     // `Url` is not able to parse windows paths on unix machines.
1361     #[test]
1362     #[cfg(target_os = "windows")]
1363     fn test_lowercase_drive_letter() {
1364         use std::{convert::TryInto, path::Path};
1365
1366         let url = url_from_abs_path(Path::new("C:\\Test").try_into().unwrap());
1367         assert_eq!(url.to_string(), "file:///c:/Test");
1368
1369         let url = url_from_abs_path(Path::new(r#"\\localhost\C$\my_dir"#).try_into().unwrap());
1370         assert_eq!(url.to_string(), "file://localhost/C$/my_dir");
1371     }
1372 }