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
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
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,
68 | SymbolKind::SelfParam
69 | SymbolKind::LifetimeParam
70 | SymbolKind::ValueParam
71 | SymbolKind::Label => lsp_types::SymbolKind::VARIABLE,
72 SymbolKind::Union => lsp_types::SymbolKind::STRUCT,
76 pub(crate) fn structure_node_kind(kind: StructureNodeKind) -> lsp_types::SymbolKind {
78 StructureNodeKind::SymbolKind(symbol) => symbol_kind(symbol),
79 StructureNodeKind::Region => lsp_types::SymbolKind::NAMESPACE,
83 pub(crate) fn document_highlight_kind(
84 category: ReferenceCategory,
85 ) -> lsp_types::DocumentHighlightKind {
87 ReferenceCategory::Read => lsp_types::DocumentHighlightKind::READ,
88 ReferenceCategory::Write => lsp_types::DocumentHighlightKind::WRITE,
92 pub(crate) fn diagnostic_severity(severity: Severity) -> lsp_types::DiagnosticSeverity {
94 Severity::Error => lsp_types::DiagnosticSeverity::ERROR,
95 Severity::WeakWarning => lsp_types::DiagnosticSeverity::HINT,
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)
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,
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"),
153 lsp_types::TextEdit { range, new_text }
156 pub(crate) fn completion_text_edit(
157 line_index: &LineIndex,
158 insert_replace_support: Option<lsp_types::Position>,
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,
169 None => text_edit.into(),
173 pub(crate) fn snippet_text_edit(
174 line_index: &LineIndex,
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,
189 pub(crate) fn text_edit_vec(
190 line_index: &LineIndex,
192 ) -> Vec<lsp_types::TextEdit> {
193 text_edit.into_iter().map(|indel| self::text_edit(line_index, indel)).collect()
196 pub(crate) fn snippet_text_edit_vec(
197 line_index: &LineIndex,
200 ) -> Vec<lsp_ext::SnippetTextEdit> {
203 .map(|indel| self::snippet_text_edit(line_index, is_snippet, indel))
207 pub(crate) fn completion_items(
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());
216 completion_item(&mut res, config, line_index, &tdpp, max_relevance, item)
222 acc: &mut Vec<lsp_types::CompletionItem>,
224 line_index: &LineIndex,
225 tdpp: &lsp_types::TextDocumentPositionParams,
227 item: CompletionItem,
229 let insert_replace_support = config.insert_replace_support().then(|| tdpp.position);
230 let mut additional_text_edits = Vec::new();
232 // LSP does not allow arbitrary edits in completion, so we have to do a
233 // non-trivial mapping here.
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())
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)
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);
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())
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()),
282 if config.completion_label_details_support() {
283 lsp_item.label_details = Some(lsp_types::CompletionItemLabelDetails {
285 description: lsp_item.detail.clone(),
289 set_score(&mut lsp_item, max_relevance, item.relevance());
291 if config.completion().enable_imports_on_the_fly {
292 if let imports @ [_, ..] = item.imports_to_add() {
293 let imports: Vec<_> = imports
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(),
304 if !imports.is_empty() {
305 let data = lsp_ext::CompletionResolveData { position: tdpp.clone(), imports };
306 lsp_item.data = Some(to_value(data).unwrap());
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(
319 Indel::insert(offset, format!("&{}", mutability.as_keyword_for_ref())),
323 acc.push(lsp_item_with_ref);
329 res: &mut lsp_types::CompletionItem,
331 relevance: CompletionRelevance,
333 if relevance.is_relevant() && relevance.score() == max_relevance {
334 res.preselect = Some(true);
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));
347 pub(crate) fn signature_help(
348 call_info: SignatureHelp,
349 config: CallInfoConfig,
351 ) -> lsp_types::SignatureHelp {
352 let (label, parameters) = match (config.params_only, label_offsets) {
353 (concise, false) => {
354 let params = call_info
356 .map(|label| lsp_types::ParameterInformation {
357 label: lsp_types::ParameterLabel::Simple(label.to_string()),
360 .collect::<Vec<_>>();
362 if concise { call_info.parameter_labels().join(", ") } else { call_info.signature };
366 let params = call_info
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;
374 .map(|label_offsets| lsp_types::ParameterInformation {
375 label: lsp_types::ParameterLabel::LabelOffsets(label_offsets),
378 .collect::<Vec<_>>();
379 (call_info.signature, params)
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() {
387 label.push_str(", ");
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]),
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,
410 let active_parameter = call_info.active_parameter.map(|it| it as u32);
412 let signature = lsp_types::SignatureInformation {
415 parameters: Some(parameters),
418 lsp_types::SignatureHelp {
419 signatures: vec![signature],
420 active_signature: Some(0),
425 pub(crate) fn inlay_hint(
426 snap: &GlobalStateSnapshot,
427 line_index: &LineIndex,
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()),
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,
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,
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(),
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)
476 InlayKind::BindingModeHint
477 | InlayKind::GenericParamListHint
478 | InlayKind::LifetimeHint
479 | InlayKind::ImplicitReborrowHint
480 | InlayKind::ClosingBraceHint => 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()?;
488 let text_document = lsp_types::TextDocumentIdentifier { uri };
489 to_value(lsp_ext::InlayHintResolveData {
491 position: lsp_ext::PositionOrRange::Position(position(&line_index, offset)),
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 {
501 position: lsp_ext::PositionOrRange::Range(range(&line_index, text_range)),
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),
514 static TOKEN_RESULT_COUNTER: AtomicU32 = AtomicU32::new(1);
516 pub(crate) fn semantic_tokens(
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);
524 for highlight_range in highlights {
525 if highlight_range.highlight.is_empty() {
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;
533 for mut text_range in line_index.index.lines(highlight_range.range) {
534 if text[text_range].ends_with('\n') {
536 TextRange::new(text_range.start(), text_range.end() - TextSize::of('\n'));
538 let range = range(line_index, text_range);
539 builder.push(range, token_index, modifier_bitset);
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, ¤t.data);
552 lsp_types::SemanticTokensDelta { result_id, edits }
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
579 lsp_types::SemanticTokenType::FUNCTION
582 SymbolKind::Const => {
583 mods |= semantic_tokens::CONSTANT;
584 mods |= lsp_types::SemanticTokenModifier::STATIC;
585 lsp_types::SemanticTokenType::VARIABLE
587 SymbolKind::Static => {
588 mods |= lsp_types::SemanticTokenModifier::STATIC;
589 lsp_types::SemanticTokenType::VARIABLE
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,
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,
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,
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,
662 pub(crate) fn folding_range(
664 line_index: &LineIndex,
665 line_folding_only: bool,
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),
677 | FoldKind::WhereClause
678 | FoldKind::ReturnType
680 | FoldKind::MatchArm => None,
683 let range = range(line_index, fold.range);
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
690 let has_more_text_on_end_line = text[TextRange::new(fold.range.end(), TextSize::of(text))]
692 .take_while(|it| *it != '\n')
693 .any(|it| !it.is_whitespace());
695 let end_line = if has_more_text_on_end_line {
696 range.end.line.saturating_sub(1)
701 lsp_types::FoldingRange {
702 start_line: range.start.line,
703 start_character: None,
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),
719 pub(crate) fn url(snap: &GlobalStateSnapshot, file_id: FileId) -> lsp_types::Url {
720 snap.file_id_to_url(file_id)
723 /// Returns a `Url` object from a given path, will lowercase drive letters if present.
724 /// This will only happen when processing windows paths.
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(_)) =>
733 // Need to lowercase driver letter
738 let driver_letter_range = {
739 let (scheme, drive_letter, _rest) = match url.as_str().splitn(3, ':').collect_tuple() {
743 let start = scheme.len() + ':'.len_utf8();
744 start..(start + drive_letter.len())
747 // Note: lowercasing the `path` itself doesn't help, the `Url::parse`
748 // machinery *also* canonicalizes the drive letter. So, just massage the
750 let mut url: String = url.into();
751 url[driver_letter_range].make_ascii_lowercase();
752 lsp_types::Url::parse(&url).unwrap()
755 pub(crate) fn optional_versioned_text_document_identifier(
756 snap: &GlobalStateSnapshot,
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 }
764 pub(crate) fn location(
765 snap: &GlobalStateSnapshot,
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);
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);
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 {
794 let line_index = snap.file_line_index(src.file_id)?;
795 let range = range(&line_index, src.range);
800 let (target_uri, target_range, target_selection_range) = location_info(snap, target)?;
801 let res = lsp_types::LocationLink {
802 origin_selection_range,
805 target_selection_range,
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)?;
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))
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() {
831 .map(|nav| location_link(snap, src, nav))
832 .collect::<Result<Vec<_>>>()?;
835 let locations = targets
838 location(snap, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
840 .collect::<Result<Vec<_>>>()?;
845 fn outside_workspace_annotation_id() -> String {
846 String::from("OutsideWorkspace")
849 pub(crate) fn snippet_text_document_edit(
850 snap: &GlobalStateSnapshot,
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();
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())
865 Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits })
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 {
881 ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(create_file));
882 if !initial_contents.is_empty() {
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),
892 lsp_ext::SnippetTextDocumentEdit { text_document, edits: vec![text_edit] };
893 ops.push(lsp_ext::SnippetDocumentChangeOperation::Edit(edit_file));
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()
904 rename_file.annotation_id = Some(outside_workspace_annotation_id())
906 ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(lsp_types::ResourceOp::Rename(
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()
918 rename_file.annotation_id = Some(outside_workspace_annotation_id())
920 ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(lsp_types::ResourceOp::Rename(
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();
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);
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));
942 let mut workspace_edit = lsp_ext::SnippetWorkspaceEdit {
944 document_changes: Some(document_changes),
945 change_annotations: None,
947 if snap.config.change_annotation_support() {
948 workspace_edit.change_annotations = Some(
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",
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())
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 {
977 document_changes: snippet_workspace_edit.document_changes.map(|changes| {
978 lsp_types::DocumentChanges::Operations(
981 .map(|change| match change {
982 lsp_ext::SnippetDocumentChangeOperation::Op(op) => {
983 lsp_types::DocumentChangeOperation::Op(op)
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(),
997 change_annotations: snippet_workspace_edit.change_annotations,
1002 impl From<lsp_ext::SnippetTextEdit>
1003 for lsp_types::OneOf<lsp_types::TextEdit, lsp_types::AnnotatedTextEdit>
1006 lsp_ext::SnippetTextEdit { annotation_id, insert_text_format:_, new_text, range }: lsp_ext::SnippetTextEdit,
1008 match annotation_id {
1009 Some(annotation_id) => lsp_types::OneOf::Right(lsp_types::AnnotatedTextEdit {
1010 text_edit: lsp_types::TextEdit { range, new_text },
1013 None => lsp_types::OneOf::Left(lsp_types::TextEdit { range, new_text }),
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 {
1038 pub(crate) fn code_action_kind(kind: AssistKind) -> lsp_types::CodeActionKind {
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,
1049 pub(crate) fn code_action(
1050 snap: &GlobalStateSnapshot,
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)),
1064 if assist.trigger_signature_help && snap.config.client_commands().trigger_parameter_hints {
1065 res.command = Some(command::trigger_parameter_hints());
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),
1077 stdx::never!("assist should always be resolved if client can't do lazy resolving")
1083 pub(crate) fn runnable(
1084 snap: &GlobalStateSnapshot,
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)?;
1096 Ok(lsp_ext::Runnable {
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,
1104 cargo_extra_args: config.cargo_extra_args,
1111 pub(crate) fn code_lens(
1112 acc: &mut Vec<lsp_types::CodeLens>,
1113 snap: &GlobalStateSnapshot,
1114 annotation: Annotation,
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);
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,
1130 let r = runnable(snap, run)?;
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),
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),
1150 AnnotationKind::HasImpls { file_id, data } => {
1151 if !client_commands_config.show_reference {
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);
1158 let id = lsp_types::TextDocumentIdentifier { uri: url.clone() };
1160 let doc_pos = lsp_types::TextDocumentPositionParams::new(id, annotation_range.start);
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(),
1168 let command = data.map(|ranges| {
1169 let locations: Vec<lsp_types::Location> = ranges
1171 .filter_map(|target| {
1174 FileRange { file_id: target.file_id, range: target.full_range },
1180 command::show_references(
1181 implementation_title(locations.len()),
1183 annotation_range.start,
1188 acc.push(lsp_types::CodeLens {
1189 range: annotation_range,
1191 data: Some(to_value(lsp_ext::CodeLensResolveData::Impls(goto_params)).unwrap()),
1194 AnnotationKind::HasReferences { file_id, data } => {
1195 if !client_commands_config.show_reference {
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);
1202 let id = lsp_types::TextDocumentIdentifier { uri: url.clone() };
1204 let doc_pos = lsp_types::TextDocumentPositionParams::new(id, annotation_range.start);
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();
1210 command::show_references(
1211 reference_title(locations.len()),
1213 annotation_range.start,
1218 acc.push(lsp_types::CodeLens {
1219 range: annotation_range,
1221 data: Some(to_value(lsp_ext::CodeLensResolveData::References(doc_pos)).unwrap()),
1228 pub(crate) mod command {
1229 use ide::{FileRange, NavigationTarget};
1230 use serde_json::to_value;
1233 global_state::GlobalStateSnapshot,
1235 to_proto::{location, location_link},
1238 pub(crate) fn show_references(
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.
1248 lsp_types::Command {
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(),
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()]),
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()]),
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()?
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()?
1288 Some(lsp_types::Command {
1289 title: nav.name.to_string(),
1290 command: "rust-analyzer.gotoLocation".into(),
1291 arguments: Some(vec![value]),
1295 pub(crate) fn trigger_parameter_hints() -> lsp_types::Command {
1296 lsp_types::Command {
1297 title: "triggerParameterHints".into(),
1298 command: "editor.action.triggerParameterHints".into(),
1304 pub(crate) fn implementation_title(count: usize) -> String {
1306 "1 implementation".into()
1308 format!("{} implementations", count)
1312 pub(crate) fn reference_title(count: usize) -> String {
1314 "1 reference".into()
1316 format!("{} references", count)
1320 pub(crate) fn markup_content(
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,
1328 let value = crate::markdown::format_docs(markup.as_str());
1329 lsp_types::MarkupContent { kind, value }
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())
1347 fn conv_fold_line_folding_only_fixup() {
1348 let text = r#"mod a;
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);
1364 let line_index = LineIndex {
1365 index: Arc::new(ide::LineIndex::new(text)),
1366 endings: LineEndings::Unix,
1367 encoding: OffsetEncoding::Utf16,
1369 let converted: Vec<lsp_types::FoldingRange> =
1370 folds.into_iter().map(|it| folding_range(text, &line_index, true, it)).collect();
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);
1382 // `Url` is not able to parse windows paths on unix machines.
1384 #[cfg(target_os = "windows")]
1385 fn test_lowercase_drive_letter() {
1386 use std::path::Path;
1388 let url = url_from_abs_path(Path::new("C:\\Test").try_into().unwrap());
1389 assert_eq!(url.to_string(), "file:///c:/Test");
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");