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