1 //! Conversion of rust-analyzer specific types to lsp_types equivalents.
5 sync::atomic::{AtomicU32, Ordering},
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,
15 use itertools::Itertools;
16 use serde_json::to_value;
20 cargo_target_spec::CargoTargetSpec,
21 config::{CallInfoConfig, Config},
22 global_state::GlobalStateSnapshot,
23 line_index::{LineEndings, LineIndex, OffsetEncoding},
25 lsp_utils::invalid_params_error,
26 semantic_tokens, Result,
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)
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)
46 pub(crate) fn symbol_kind(symbol_kind: SymbolKind) -> lsp_types::SymbolKind {
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,
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
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,
67 | SymbolKind::SelfParam
68 | SymbolKind::LifetimeParam
69 | SymbolKind::ValueParam
70 | SymbolKind::Label => lsp_types::SymbolKind::VARIABLE,
71 SymbolKind::Union => lsp_types::SymbolKind::STRUCT,
75 pub(crate) fn structure_node_kind(kind: StructureNodeKind) -> lsp_types::SymbolKind {
77 StructureNodeKind::SymbolKind(symbol) => symbol_kind(symbol),
78 StructureNodeKind::Region => lsp_types::SymbolKind::NAMESPACE,
82 pub(crate) fn document_highlight_kind(
83 category: ReferenceCategory,
84 ) -> lsp_types::DocumentHighlightKind {
86 ReferenceCategory::Read => lsp_types::DocumentHighlightKind::READ,
87 ReferenceCategory::Write => lsp_types::DocumentHighlightKind::WRITE,
91 pub(crate) fn diagnostic_severity(severity: Severity) -> lsp_types::DiagnosticSeverity {
93 Severity::Error => lsp_types::DiagnosticSeverity::ERROR,
94 Severity::WeakWarning => lsp_types::DiagnosticSeverity::HINT,
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)
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,
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"),
151 lsp_types::TextEdit { range, new_text }
154 pub(crate) fn completion_text_edit(
155 line_index: &LineIndex,
156 insert_replace_support: Option<lsp_types::Position>,
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,
167 None => text_edit.into(),
171 pub(crate) fn snippet_text_edit(
172 line_index: &LineIndex,
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,
187 pub(crate) fn text_edit_vec(
188 line_index: &LineIndex,
190 ) -> Vec<lsp_types::TextEdit> {
191 text_edit.into_iter().map(|indel| self::text_edit(line_index, indel)).collect()
194 pub(crate) fn snippet_text_edit_vec(
195 line_index: &LineIndex,
198 ) -> Vec<lsp_ext::SnippetTextEdit> {
201 .map(|indel| self::snippet_text_edit(line_index, is_snippet, indel))
205 pub(crate) fn completion_items(
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());
214 completion_item(&mut res, config, line_index, &tdpp, max_relevance, item)
220 acc: &mut Vec<lsp_types::CompletionItem>,
222 line_index: &LineIndex,
223 tdpp: &lsp_types::TextDocumentPositionParams,
225 item: CompletionItem,
227 let mut additional_text_edits = Vec::new();
229 // LSP does not allow arbitrary edits in completion, so we have to do a
230 // non-trivial mapping here.
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())
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)
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);
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()),
269 set_score(&mut lsp_item, max_relevance, item.relevance());
271 if item.deprecated() {
272 lsp_item.tags = Some(vec![lsp_types::CompletionItemTag::DEPRECATED])
275 if item.trigger_call_info() && config.client_commands().trigger_parameter_hints {
276 lsp_item.command = Some(command::trigger_parameter_hints());
279 if item.is_snippet() {
280 lsp_item.insert_text_format = Some(lsp_types::InsertTextFormat::SNIPPET);
282 if config.completion().enable_imports_on_the_fly {
283 if let imports @ [_, ..] = item.imports_to_add() {
284 let imports: Vec<_> = imports
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(),
295 if !imports.is_empty() {
296 let data = lsp_ext::CompletionResolveData { position: tdpp.clone(), imports };
297 lsp_item.data = Some(to_value(data).unwrap());
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,
312 *new_text = format!("&{}{}", mutability.as_keyword_for_ref(), new_text);
315 acc.push(lsp_item_with_ref);
321 res: &mut lsp_types::CompletionItem,
323 relevance: CompletionRelevance,
325 if relevance.is_relevant() && relevance.score() == max_relevance {
326 res.preselect = Some(true);
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));
339 pub(crate) fn signature_help(
340 call_info: SignatureHelp,
341 config: CallInfoConfig,
343 ) -> lsp_types::SignatureHelp {
344 let (label, parameters) = match (config.params_only, label_offsets) {
345 (concise, false) => {
346 let params = call_info
348 .map(|label| lsp_types::ParameterInformation {
349 label: lsp_types::ParameterLabel::Simple(label.to_string()),
352 .collect::<Vec<_>>();
354 if concise { call_info.parameter_labels().join(", ") } else { call_info.signature };
358 let params = call_info
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;
366 .map(|label_offsets| lsp_types::ParameterInformation {
367 label: lsp_types::ParameterLabel::LabelOffsets(label_offsets),
370 .collect::<Vec<_>>();
371 (call_info.signature, params)
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() {
379 label.push_str(", ");
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]),
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,
402 let active_parameter = call_info.active_parameter.map(|it| it as u32);
404 let signature = lsp_types::SignatureInformation {
407 parameters: Some(parameters),
410 lsp_types::SignatureHelp {
411 signatures: vec![signature],
412 active_signature: Some(0),
417 pub(crate) fn inlay_hint(
418 line_index: &LineIndex,
419 text_document: &lsp_types::TextDocumentIdentifier,
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()),
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,
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,
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,
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)
468 InlayKind::BindingModeHint
469 | InlayKind::GenericParamListHint
470 | InlayKind::LifetimeHint
471 | InlayKind::ImplicitReborrowHint
472 | InlayKind::ClosingBraceHint(_) => 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),
491 static TOKEN_RESULT_COUNTER: AtomicU32 = AtomicU32::new(1);
493 pub(crate) fn semantic_tokens(
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);
502 for highlight_range in highlights {
503 if highlight_range.highlight.is_empty() {
506 let (ty, mods) = semantic_token_type_and_modifiers(highlight_range.highlight);
507 if !highlight_strings && ty == lsp_types::SemanticTokenType::STRING {
510 let token_index = semantic_tokens::type_index(ty);
511 let modifier_bitset = mods.0;
513 for mut text_range in line_index.index.lines(highlight_range.range) {
514 if text[text_range].ends_with('\n') {
516 TextRange::new(text_range.start(), text_range.end() - TextSize::of('\n'));
518 let range = range(line_index, text_range);
519 builder.push(range, token_index, modifier_bitset);
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, ¤t.data);
532 lsp_types::SemanticTokensDelta { result_id, edits }
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
558 lsp_types::SemanticTokenType::FUNCTION
561 SymbolKind::Const => {
562 mods |= semantic_tokens::CONSTANT;
563 mods |= lsp_types::SemanticTokenModifier::STATIC;
564 lsp_types::SemanticTokenType::VARIABLE
566 SymbolKind::Static => {
567 mods |= lsp_types::SemanticTokenModifier::STATIC;
568 lsp_types::SemanticTokenType::VARIABLE
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,
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,
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,
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,
641 pub(crate) fn folding_range(
643 line_index: &LineIndex,
644 line_folding_only: bool,
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),
656 | FoldKind::WhereClause
657 | FoldKind::ReturnType
658 | FoldKind::Array => None,
661 let range = range(line_index, fold.range);
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
668 let has_more_text_on_end_line = text[TextRange::new(fold.range.end(), TextSize::of(text))]
670 .take_while(|it| *it != '\n')
671 .any(|it| !it.is_whitespace());
673 let end_line = if has_more_text_on_end_line {
674 range.end.line.saturating_sub(1)
679 lsp_types::FoldingRange {
680 start_line: range.start.line,
681 start_character: None,
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),
697 pub(crate) fn url(snap: &GlobalStateSnapshot, file_id: FileId) -> lsp_types::Url {
698 snap.file_id_to_url(file_id)
701 /// Returns a `Url` object from a given path, will lowercase drive letters if present.
702 /// This will only happen when processing windows paths.
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(_)) =>
711 // Need to lowercase driver letter
716 let driver_letter_range = {
717 let (scheme, drive_letter, _rest) = match url.as_str().splitn(3, ':').collect_tuple() {
721 let start = scheme.len() + ':'.len_utf8();
722 start..(start + drive_letter.len())
725 // Note: lowercasing the `path` itself doesn't help, the `Url::parse`
726 // machinery *also* canonicalizes the drive letter. So, just massage the
728 let mut url: String = url.into();
729 url[driver_letter_range].make_ascii_lowercase();
730 lsp_types::Url::parse(&url).unwrap()
733 pub(crate) fn optional_versioned_text_document_identifier(
734 snap: &GlobalStateSnapshot,
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 }
742 pub(crate) fn location(
743 snap: &GlobalStateSnapshot,
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);
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);
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 {
772 let line_index = snap.file_line_index(src.file_id)?;
773 let range = range(&line_index, src.range);
778 let (target_uri, target_range, target_selection_range) = location_info(snap, target)?;
779 let res = lsp_types::LocationLink {
780 origin_selection_range,
783 target_selection_range,
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)?;
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))
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() {
809 .map(|nav| location_link(snap, src, nav))
810 .collect::<Result<Vec<_>>>()?;
813 let locations = targets
816 location(snap, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
818 .collect::<Result<Vec<_>>>()?;
823 fn outside_workspace_annotation_id() -> String {
824 String::from("OutsideWorkspace")
827 pub(crate) fn snippet_text_document_edit(
828 snap: &GlobalStateSnapshot,
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();
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())
843 Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits })
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 {
859 ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(create_file));
860 if !initial_contents.is_empty() {
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),
870 lsp_ext::SnippetTextDocumentEdit { text_document, edits: vec![text_edit] };
871 ops.push(lsp_ext::SnippetDocumentChangeOperation::Edit(edit_file));
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()
882 rename_file.annotation_id = Some(outside_workspace_annotation_id())
884 ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(lsp_types::ResourceOp::Rename(
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()
896 rename_file.annotation_id = Some(outside_workspace_annotation_id())
898 ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(lsp_types::ResourceOp::Rename(
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();
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);
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));
920 let mut workspace_edit = lsp_ext::SnippetWorkspaceEdit {
922 document_changes: Some(document_changes),
923 change_annotations: None,
925 if snap.config.change_annotation_support() {
926 workspace_edit.change_annotations = Some(
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",
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())
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 {
955 document_changes: snippet_workspace_edit.document_changes.map(|changes| {
956 lsp_types::DocumentChanges::Operations(
959 .map(|change| match change {
960 lsp_ext::SnippetDocumentChangeOperation::Op(op) => {
961 lsp_types::DocumentChangeOperation::Op(op)
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(),
975 change_annotations: snippet_workspace_edit.change_annotations,
980 impl From<lsp_ext::SnippetTextEdit>
981 for lsp_types::OneOf<lsp_types::TextEdit, lsp_types::AnnotatedTextEdit>
984 lsp_ext::SnippetTextEdit { annotation_id, insert_text_format:_, new_text, range }: lsp_ext::SnippetTextEdit,
986 match annotation_id {
987 Some(annotation_id) => lsp_types::OneOf::Right(lsp_types::AnnotatedTextEdit {
988 text_edit: lsp_types::TextEdit { range, new_text },
991 None => lsp_types::OneOf::Left(lsp_types::TextEdit { range, new_text }),
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 {
1016 pub(crate) fn code_action_kind(kind: AssistKind) -> lsp_types::CodeActionKind {
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,
1027 pub(crate) fn code_action(
1028 snap: &GlobalStateSnapshot,
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)),
1042 if assist.trigger_signature_help && snap.config.client_commands().trigger_parameter_hints {
1043 res.command = Some(command::trigger_parameter_hints());
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),
1055 stdx::never!("assist should always be resolved if client can't do lazy resolving")
1061 pub(crate) fn runnable(
1062 snap: &GlobalStateSnapshot,
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)?;
1074 Ok(lsp_ext::Runnable {
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,
1082 cargo_extra_args: config.cargo_extra_args,
1089 pub(crate) fn code_lens(
1090 acc: &mut Vec<lsp_types::CodeLens>,
1091 snap: &GlobalStateSnapshot,
1092 annotation: Annotation,
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);
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,
1108 let r = runnable(snap, run)?;
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),
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),
1128 AnnotationKind::HasImpls { file_id, data } => {
1129 if !client_commands_config.show_reference {
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);
1136 let id = lsp_types::TextDocumentIdentifier { uri: url.clone() };
1138 let doc_pos = lsp_types::TextDocumentPositionParams::new(id, annotation_range.start);
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(),
1146 let command = data.map(|ranges| {
1147 let locations: Vec<lsp_types::Location> = ranges
1149 .filter_map(|target| {
1152 FileRange { file_id: target.file_id, range: target.full_range },
1158 command::show_references(
1159 implementation_title(locations.len()),
1161 annotation_range.start,
1166 acc.push(lsp_types::CodeLens {
1167 range: annotation_range,
1169 data: Some(to_value(lsp_ext::CodeLensResolveData::Impls(goto_params)).unwrap()),
1172 AnnotationKind::HasReferences { file_id, data } => {
1173 if !client_commands_config.show_reference {
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);
1180 let id = lsp_types::TextDocumentIdentifier { uri: url.clone() };
1182 let doc_pos = lsp_types::TextDocumentPositionParams::new(id, annotation_range.start);
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();
1188 command::show_references(
1189 reference_title(locations.len()),
1191 annotation_range.start,
1196 acc.push(lsp_types::CodeLens {
1197 range: annotation_range,
1199 data: Some(to_value(lsp_ext::CodeLensResolveData::References(doc_pos)).unwrap()),
1206 pub(crate) mod command {
1207 use ide::{FileRange, NavigationTarget};
1208 use serde_json::to_value;
1211 global_state::GlobalStateSnapshot,
1213 to_proto::{location, location_link},
1216 pub(crate) fn show_references(
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.
1226 lsp_types::Command {
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(),
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()]),
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()]),
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()?
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()?
1266 Some(lsp_types::Command {
1267 title: nav.name.to_string(),
1268 command: "rust-analyzer.gotoLocation".into(),
1269 arguments: Some(vec![value]),
1273 pub(crate) fn trigger_parameter_hints() -> lsp_types::Command {
1274 lsp_types::Command {
1275 title: "triggerParameterHints".into(),
1276 command: "editor.action.triggerParameterHints".into(),
1282 pub(crate) fn implementation_title(count: usize) -> String {
1284 "1 implementation".into()
1286 format!("{} implementations", count)
1290 pub(crate) fn reference_title(count: usize) -> String {
1292 "1 reference".into()
1294 format!("{} references", count)
1298 pub(crate) fn markup_content(
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,
1306 let value = crate::markdown::format_docs(markup.as_str());
1307 lsp_types::MarkupContent { kind, value }
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())
1325 fn conv_fold_line_folding_only_fixup() {
1326 let text = r#"mod a;
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);
1342 let line_index = LineIndex {
1343 index: Arc::new(ide::LineIndex::new(text)),
1344 endings: LineEndings::Unix,
1345 encoding: OffsetEncoding::Utf16,
1347 let converted: Vec<lsp_types::FoldingRange> =
1348 folds.into_iter().map(|it| folding_range(text, &line_index, true, it)).collect();
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);
1360 // `Url` is not able to parse windows paths on unix machines.
1362 #[cfg(target_os = "windows")]
1363 fn test_lowercase_drive_letter() {
1364 use std::{convert::TryInto, path::Path};
1366 let url = url_from_abs_path(Path::new("C:\\Test").try_into().unwrap());
1367 assert_eq!(url.to_string(), "file:///c:/Test");
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");