2020-05-10 13:55:24 +02:00
|
|
|
//! Conversion of rust-analyzer specific types to lsp_types equivalents.
|
2020-06-13 11:00:06 +02:00
|
|
|
use std::path::{self, Path};
|
|
|
|
|
|
|
|
use itertools::Itertools;
|
2020-05-10 13:55:24 +02:00
|
|
|
use ra_db::{FileId, FileRange};
|
|
|
|
use ra_ide::{
|
2020-05-21 14:34:27 +02:00
|
|
|
Assist, CompletionItem, CompletionItemKind, Documentation, FileSystemEdit, Fold, FoldKind,
|
2020-05-21 19:50:23 +02:00
|
|
|
FunctionSignature, Highlight, HighlightModifier, HighlightTag, HighlightedRange, Indel,
|
2020-06-02 22:21:48 +02:00
|
|
|
InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget, ReferenceAccess,
|
2020-06-06 12:00:46 +03:00
|
|
|
ResolvedAssist, Runnable, Severity, SourceChange, SourceFileEdit, TextEdit,
|
2020-05-10 13:55:24 +02:00
|
|
|
};
|
|
|
|
use ra_syntax::{SyntaxKind, TextRange, TextSize};
|
|
|
|
use ra_vfs::LineEndings;
|
|
|
|
|
2020-06-02 16:30:26 +02:00
|
|
|
use crate::{
|
2020-06-03 11:16:08 +02:00
|
|
|
cargo_target_spec::CargoTargetSpec, global_state::GlobalStateSnapshot, lsp_ext,
|
|
|
|
semantic_tokens, Result,
|
2020-06-02 16:30:26 +02:00
|
|
|
};
|
2020-05-10 13:55:24 +02:00
|
|
|
|
|
|
|
pub(crate) fn position(line_index: &LineIndex, offset: TextSize) -> lsp_types::Position {
|
|
|
|
let line_col = line_index.line_col(offset);
|
|
|
|
let line = u64::from(line_col.line);
|
|
|
|
let character = u64::from(line_col.col_utf16);
|
|
|
|
lsp_types::Position::new(line, character)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn range(line_index: &LineIndex, range: TextRange) -> lsp_types::Range {
|
|
|
|
let start = position(line_index, range.start());
|
|
|
|
let end = position(line_index, range.end());
|
|
|
|
lsp_types::Range::new(start, end)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn symbol_kind(syntax_kind: SyntaxKind) -> lsp_types::SymbolKind {
|
|
|
|
match syntax_kind {
|
|
|
|
SyntaxKind::FN_DEF => lsp_types::SymbolKind::Function,
|
|
|
|
SyntaxKind::STRUCT_DEF => lsp_types::SymbolKind::Struct,
|
|
|
|
SyntaxKind::ENUM_DEF => lsp_types::SymbolKind::Enum,
|
|
|
|
SyntaxKind::ENUM_VARIANT => lsp_types::SymbolKind::EnumMember,
|
|
|
|
SyntaxKind::TRAIT_DEF => lsp_types::SymbolKind::Interface,
|
|
|
|
SyntaxKind::MACRO_CALL => lsp_types::SymbolKind::Function,
|
|
|
|
SyntaxKind::MODULE => lsp_types::SymbolKind::Module,
|
|
|
|
SyntaxKind::TYPE_ALIAS_DEF => lsp_types::SymbolKind::TypeParameter,
|
|
|
|
SyntaxKind::RECORD_FIELD_DEF => lsp_types::SymbolKind::Field,
|
|
|
|
SyntaxKind::STATIC_DEF => lsp_types::SymbolKind::Constant,
|
|
|
|
SyntaxKind::CONST_DEF => lsp_types::SymbolKind::Constant,
|
|
|
|
SyntaxKind::IMPL_DEF => lsp_types::SymbolKind::Object,
|
|
|
|
_ => lsp_types::SymbolKind::Variable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn document_highlight_kind(
|
|
|
|
reference_access: ReferenceAccess,
|
|
|
|
) -> lsp_types::DocumentHighlightKind {
|
|
|
|
match reference_access {
|
|
|
|
ReferenceAccess::Read => lsp_types::DocumentHighlightKind::Read,
|
|
|
|
ReferenceAccess::Write => lsp_types::DocumentHighlightKind::Write,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn diagnostic_severity(severity: Severity) -> lsp_types::DiagnosticSeverity {
|
|
|
|
match severity {
|
|
|
|
Severity::Error => lsp_types::DiagnosticSeverity::Error,
|
|
|
|
Severity::WeakWarning => lsp_types::DiagnosticSeverity::Hint,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn documentation(documentation: Documentation) -> lsp_types::Documentation {
|
|
|
|
let value = crate::markdown::format_docs(documentation.as_str());
|
|
|
|
let markup_content = lsp_types::MarkupContent { kind: lsp_types::MarkupKind::Markdown, value };
|
|
|
|
lsp_types::Documentation::MarkupContent(markup_content)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn insert_text_format(
|
|
|
|
insert_text_format: InsertTextFormat,
|
|
|
|
) -> lsp_types::InsertTextFormat {
|
|
|
|
match insert_text_format {
|
|
|
|
InsertTextFormat::Snippet => lsp_types::InsertTextFormat::Snippet,
|
|
|
|
InsertTextFormat::PlainText => lsp_types::InsertTextFormat::PlainText,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn completion_item_kind(
|
|
|
|
completion_item_kind: CompletionItemKind,
|
|
|
|
) -> lsp_types::CompletionItemKind {
|
|
|
|
match completion_item_kind {
|
|
|
|
CompletionItemKind::Keyword => lsp_types::CompletionItemKind::Keyword,
|
|
|
|
CompletionItemKind::Snippet => lsp_types::CompletionItemKind::Snippet,
|
|
|
|
CompletionItemKind::Module => lsp_types::CompletionItemKind::Module,
|
|
|
|
CompletionItemKind::Function => lsp_types::CompletionItemKind::Function,
|
|
|
|
CompletionItemKind::Struct => lsp_types::CompletionItemKind::Struct,
|
|
|
|
CompletionItemKind::Enum => lsp_types::CompletionItemKind::Enum,
|
|
|
|
CompletionItemKind::EnumVariant => lsp_types::CompletionItemKind::EnumMember,
|
|
|
|
CompletionItemKind::BuiltinType => lsp_types::CompletionItemKind::Struct,
|
|
|
|
CompletionItemKind::Binding => lsp_types::CompletionItemKind::Variable,
|
|
|
|
CompletionItemKind::Field => lsp_types::CompletionItemKind::Field,
|
|
|
|
CompletionItemKind::Trait => lsp_types::CompletionItemKind::Interface,
|
|
|
|
CompletionItemKind::TypeAlias => lsp_types::CompletionItemKind::Struct,
|
|
|
|
CompletionItemKind::Const => lsp_types::CompletionItemKind::Constant,
|
|
|
|
CompletionItemKind::Static => lsp_types::CompletionItemKind::Value,
|
|
|
|
CompletionItemKind::Method => lsp_types::CompletionItemKind::Method,
|
|
|
|
CompletionItemKind::TypeParam => lsp_types::CompletionItemKind::TypeParameter,
|
|
|
|
CompletionItemKind::Macro => lsp_types::CompletionItemKind::Method,
|
|
|
|
CompletionItemKind::Attribute => lsp_types::CompletionItemKind::EnumMember,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn text_edit(
|
|
|
|
line_index: &LineIndex,
|
|
|
|
line_endings: LineEndings,
|
|
|
|
indel: Indel,
|
|
|
|
) -> lsp_types::TextEdit {
|
|
|
|
let range = range(line_index, indel.delete);
|
|
|
|
let new_text = match line_endings {
|
|
|
|
LineEndings::Unix => indel.insert,
|
|
|
|
LineEndings::Dos => indel.insert.replace('\n', "\r\n"),
|
|
|
|
};
|
|
|
|
lsp_types::TextEdit { range, new_text }
|
|
|
|
}
|
|
|
|
|
2020-05-18 00:11:40 +02:00
|
|
|
pub(crate) fn snippet_text_edit(
|
|
|
|
line_index: &LineIndex,
|
|
|
|
line_endings: LineEndings,
|
|
|
|
is_snippet: bool,
|
|
|
|
indel: Indel,
|
|
|
|
) -> lsp_ext::SnippetTextEdit {
|
|
|
|
let text_edit = text_edit(line_index, line_endings, indel);
|
|
|
|
let insert_text_format =
|
|
|
|
if is_snippet { Some(lsp_types::InsertTextFormat::Snippet) } else { None };
|
|
|
|
lsp_ext::SnippetTextEdit {
|
|
|
|
range: text_edit.range,
|
|
|
|
new_text: text_edit.new_text,
|
|
|
|
insert_text_format,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:55:24 +02:00
|
|
|
pub(crate) fn text_edit_vec(
|
|
|
|
line_index: &LineIndex,
|
|
|
|
line_endings: LineEndings,
|
|
|
|
text_edit: TextEdit,
|
|
|
|
) -> Vec<lsp_types::TextEdit> {
|
2020-05-21 15:56:18 +02:00
|
|
|
text_edit.into_iter().map(|indel| self::text_edit(line_index, line_endings, indel)).collect()
|
2020-05-10 13:55:24 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:12:53 +02:00
|
|
|
pub(crate) fn snippet_text_edit_vec(
|
|
|
|
line_index: &LineIndex,
|
|
|
|
line_endings: LineEndings,
|
|
|
|
is_snippet: bool,
|
|
|
|
text_edit: TextEdit,
|
|
|
|
) -> Vec<lsp_ext::SnippetTextEdit> {
|
|
|
|
text_edit
|
|
|
|
.into_iter()
|
|
|
|
.map(|indel| self::snippet_text_edit(line_index, line_endings, is_snippet, indel))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:55:24 +02:00
|
|
|
pub(crate) fn completion_item(
|
|
|
|
line_index: &LineIndex,
|
|
|
|
line_endings: LineEndings,
|
|
|
|
completion_item: CompletionItem,
|
|
|
|
) -> lsp_types::CompletionItem {
|
|
|
|
let mut additional_text_edits = Vec::new();
|
|
|
|
let mut text_edit = None;
|
|
|
|
// LSP does not allow arbitrary edits in completion, so we have to do a
|
|
|
|
// non-trivial mapping here.
|
|
|
|
let source_range = completion_item.source_range();
|
2020-05-21 15:56:18 +02:00
|
|
|
for indel in completion_item.text_edit().iter() {
|
2020-05-10 13:55:24 +02:00
|
|
|
if indel.delete.contains_range(source_range) {
|
|
|
|
text_edit = Some(if indel.delete == source_range {
|
|
|
|
self::text_edit(line_index, line_endings, indel.clone())
|
|
|
|
} else {
|
|
|
|
assert!(source_range.end() == indel.delete.end());
|
|
|
|
let range1 = TextRange::new(indel.delete.start(), source_range.start());
|
|
|
|
let range2 = source_range;
|
|
|
|
let indel1 = Indel::replace(range1, String::new());
|
|
|
|
let indel2 = Indel::replace(range2, indel.insert.clone());
|
|
|
|
additional_text_edits.push(self::text_edit(line_index, line_endings, indel1));
|
|
|
|
self::text_edit(line_index, line_endings, indel2)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
assert!(source_range.intersect(indel.delete).is_none());
|
|
|
|
let text_edit = self::text_edit(line_index, line_endings, indel.clone());
|
|
|
|
additional_text_edits.push(text_edit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let text_edit = text_edit.unwrap();
|
|
|
|
|
|
|
|
let mut res = lsp_types::CompletionItem {
|
|
|
|
label: completion_item.label().to_string(),
|
|
|
|
detail: completion_item.detail().map(|it| it.to_string()),
|
|
|
|
filter_text: Some(completion_item.lookup().to_string()),
|
|
|
|
kind: completion_item.kind().map(completion_item_kind),
|
|
|
|
text_edit: Some(text_edit.into()),
|
|
|
|
additional_text_edits: Some(additional_text_edits),
|
|
|
|
documentation: completion_item.documentation().map(documentation),
|
|
|
|
deprecated: Some(completion_item.deprecated()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
if completion_item.score().is_some() {
|
2020-05-14 15:29:40 +02:00
|
|
|
res.preselect = Some(true);
|
|
|
|
// HACK: sort preselect items first
|
|
|
|
res.sort_text = Some(format!(" {}", completion_item.label()));
|
2020-05-10 13:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if completion_item.deprecated() {
|
|
|
|
res.tags = Some(vec![lsp_types::CompletionItemTag::Deprecated])
|
|
|
|
}
|
|
|
|
|
2020-05-14 15:36:15 +02:00
|
|
|
if completion_item.trigger_call_info() {
|
|
|
|
res.command = Some(lsp_types::Command {
|
|
|
|
title: "triggerParameterHints".into(),
|
|
|
|
command: "editor.action.triggerParameterHints".into(),
|
|
|
|
arguments: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:55:24 +02:00
|
|
|
res.insert_text_format = Some(insert_text_format(completion_item.insert_text_format()));
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn signature_information(
|
|
|
|
signature: FunctionSignature,
|
|
|
|
concise: bool,
|
|
|
|
) -> lsp_types::SignatureInformation {
|
|
|
|
let (label, documentation, params) = if concise {
|
|
|
|
let mut params = signature.parameters;
|
|
|
|
if signature.has_self_param {
|
|
|
|
params.remove(0);
|
|
|
|
}
|
|
|
|
(params.join(", "), None, params)
|
|
|
|
} else {
|
|
|
|
(signature.to_string(), signature.doc.map(documentation), signature.parameters)
|
|
|
|
};
|
|
|
|
|
|
|
|
let parameters: Vec<lsp_types::ParameterInformation> = params
|
|
|
|
.into_iter()
|
|
|
|
.map(|param| lsp_types::ParameterInformation {
|
|
|
|
label: lsp_types::ParameterLabel::Simple(param),
|
|
|
|
documentation: None,
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
lsp_types::SignatureInformation { label, documentation, parameters: Some(parameters) }
|
|
|
|
}
|
|
|
|
|
2020-05-10 19:25:37 +02:00
|
|
|
pub(crate) fn inlay_int(line_index: &LineIndex, inlay_hint: InlayHint) -> lsp_ext::InlayHint {
|
|
|
|
lsp_ext::InlayHint {
|
2020-05-10 13:55:24 +02:00
|
|
|
label: inlay_hint.label.to_string(),
|
|
|
|
range: range(line_index, inlay_hint.range),
|
|
|
|
kind: match inlay_hint.kind {
|
2020-05-10 19:25:37 +02:00
|
|
|
InlayKind::ParameterHint => lsp_ext::InlayKind::ParameterHint,
|
|
|
|
InlayKind::TypeHint => lsp_ext::InlayKind::TypeHint,
|
|
|
|
InlayKind::ChainingHint => lsp_ext::InlayKind::ChainingHint,
|
2020-05-10 13:55:24 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 19:09:22 +02:00
|
|
|
pub(crate) fn semantic_tokens(
|
|
|
|
text: &str,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
highlights: Vec<HighlightedRange>,
|
|
|
|
) -> lsp_types::SemanticTokens {
|
|
|
|
let mut builder = semantic_tokens::SemanticTokensBuilder::default();
|
|
|
|
|
|
|
|
for highlight_range in highlights {
|
2020-05-10 19:14:02 +02:00
|
|
|
let (type_, mods) = semantic_token_type_and_modifiers(highlight_range.highlight);
|
|
|
|
let token_index = semantic_tokens::type_index(type_);
|
|
|
|
let modifier_bitset = mods.0;
|
|
|
|
|
2020-05-10 19:09:22 +02:00
|
|
|
for mut text_range in line_index.lines(highlight_range.range) {
|
|
|
|
if text[text_range].ends_with('\n') {
|
|
|
|
text_range =
|
|
|
|
TextRange::new(text_range.start(), text_range.end() - TextSize::of('\n'));
|
|
|
|
}
|
|
|
|
let range = range(&line_index, text_range);
|
|
|
|
builder.push(range, token_index, modifier_bitset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.build()
|
|
|
|
}
|
|
|
|
|
2020-05-10 19:14:02 +02:00
|
|
|
fn semantic_token_type_and_modifiers(
|
|
|
|
highlight: Highlight,
|
|
|
|
) -> (lsp_types::SemanticTokenType, semantic_tokens::ModifierSet) {
|
2020-05-10 13:55:24 +02:00
|
|
|
let mut mods = semantic_tokens::ModifierSet::default();
|
|
|
|
let type_ = match highlight.tag {
|
|
|
|
HighlightTag::Struct => lsp_types::SemanticTokenType::STRUCT,
|
|
|
|
HighlightTag::Enum => lsp_types::SemanticTokenType::ENUM,
|
|
|
|
HighlightTag::Union => semantic_tokens::UNION,
|
|
|
|
HighlightTag::TypeAlias => semantic_tokens::TYPE_ALIAS,
|
|
|
|
HighlightTag::Trait => lsp_types::SemanticTokenType::INTERFACE,
|
|
|
|
HighlightTag::BuiltinType => semantic_tokens::BUILTIN_TYPE,
|
2020-05-25 11:51:56 +03:00
|
|
|
HighlightTag::SelfKeyword => semantic_tokens::SELF_KEYWORD,
|
2020-05-10 13:55:24 +02:00
|
|
|
HighlightTag::SelfType => lsp_types::SemanticTokenType::TYPE,
|
2020-05-09 10:24:57 -07:00
|
|
|
HighlightTag::Field => lsp_types::SemanticTokenType::PROPERTY,
|
2020-05-10 13:55:24 +02:00
|
|
|
HighlightTag::Function => lsp_types::SemanticTokenType::FUNCTION,
|
|
|
|
HighlightTag::Module => lsp_types::SemanticTokenType::NAMESPACE,
|
|
|
|
HighlightTag::Constant => {
|
|
|
|
mods |= semantic_tokens::CONSTANT;
|
|
|
|
mods |= lsp_types::SemanticTokenModifier::STATIC;
|
|
|
|
lsp_types::SemanticTokenType::VARIABLE
|
|
|
|
}
|
|
|
|
HighlightTag::Static => {
|
|
|
|
mods |= lsp_types::SemanticTokenModifier::STATIC;
|
|
|
|
lsp_types::SemanticTokenType::VARIABLE
|
|
|
|
}
|
|
|
|
HighlightTag::EnumVariant => semantic_tokens::ENUM_MEMBER,
|
|
|
|
HighlightTag::Macro => lsp_types::SemanticTokenType::MACRO,
|
|
|
|
HighlightTag::Local => lsp_types::SemanticTokenType::VARIABLE,
|
|
|
|
HighlightTag::TypeParam => lsp_types::SemanticTokenType::TYPE_PARAMETER,
|
|
|
|
HighlightTag::Lifetime => semantic_tokens::LIFETIME,
|
|
|
|
HighlightTag::ByteLiteral | HighlightTag::NumericLiteral => {
|
|
|
|
lsp_types::SemanticTokenType::NUMBER
|
|
|
|
}
|
2020-05-21 17:40:52 +01:00
|
|
|
HighlightTag::BoolLiteral => semantic_tokens::BOOLEAN,
|
2020-05-10 13:55:24 +02:00
|
|
|
HighlightTag::CharLiteral | HighlightTag::StringLiteral => {
|
|
|
|
lsp_types::SemanticTokenType::STRING
|
|
|
|
}
|
|
|
|
HighlightTag::Comment => lsp_types::SemanticTokenType::COMMENT,
|
|
|
|
HighlightTag::Attribute => semantic_tokens::ATTRIBUTE,
|
|
|
|
HighlightTag::Keyword => lsp_types::SemanticTokenType::KEYWORD,
|
|
|
|
HighlightTag::UnresolvedReference => semantic_tokens::UNRESOLVED_REFERENCE,
|
|
|
|
HighlightTag::FormatSpecifier => semantic_tokens::FORMAT_SPECIFIER,
|
2020-05-29 21:17:14 +02:00
|
|
|
HighlightTag::Operator => lsp_types::SemanticTokenType::OPERATOR,
|
2020-06-17 15:27:13 +02:00
|
|
|
HighlightTag::EscapeSequence => semantic_tokens::ESCAPE_SEQUENCE,
|
2020-05-10 13:55:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
for modifier in highlight.modifiers.iter() {
|
|
|
|
let modifier = match modifier {
|
2020-05-12 21:58:51 -07:00
|
|
|
HighlightModifier::Attribute => semantic_tokens::ATTRIBUTE_MODIFIER,
|
2020-05-10 13:55:24 +02:00
|
|
|
HighlightModifier::Definition => lsp_types::SemanticTokenModifier::DECLARATION,
|
2020-06-15 20:17:26 -04:00
|
|
|
HighlightModifier::Documentation => lsp_types::SemanticTokenModifier::DOCUMENTATION,
|
2020-05-10 13:55:24 +02:00
|
|
|
HighlightModifier::ControlFlow => semantic_tokens::CONTROL_FLOW,
|
|
|
|
HighlightModifier::Mutable => semantic_tokens::MUTABLE,
|
|
|
|
HighlightModifier::Unsafe => semantic_tokens::UNSAFE,
|
|
|
|
};
|
|
|
|
mods |= modifier;
|
|
|
|
}
|
|
|
|
|
2020-05-10 19:14:02 +02:00
|
|
|
(type_, mods)
|
2020-05-10 13:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn folding_range(
|
|
|
|
text: &str,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
line_folding_only: bool,
|
|
|
|
fold: Fold,
|
|
|
|
) -> lsp_types::FoldingRange {
|
|
|
|
let kind = match fold.kind {
|
|
|
|
FoldKind::Comment => Some(lsp_types::FoldingRangeKind::Comment),
|
|
|
|
FoldKind::Imports => Some(lsp_types::FoldingRangeKind::Imports),
|
|
|
|
FoldKind::Mods | FoldKind::Block => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let range = range(line_index, fold.range);
|
|
|
|
|
|
|
|
if line_folding_only {
|
|
|
|
// Clients with line_folding_only == true (such as VSCode) will fold the whole end line
|
|
|
|
// even if it contains text not in the folding range. To prevent that we exclude
|
|
|
|
// range.end.line from the folding region if there is more text after range.end
|
|
|
|
// on the same line.
|
|
|
|
let has_more_text_on_end_line = text[TextRange::new(fold.range.end(), TextSize::of(text))]
|
|
|
|
.chars()
|
|
|
|
.take_while(|it| *it != '\n')
|
|
|
|
.any(|it| !it.is_whitespace());
|
|
|
|
|
|
|
|
let end_line = if has_more_text_on_end_line {
|
|
|
|
range.end.line.saturating_sub(1)
|
|
|
|
} else {
|
|
|
|
range.end.line
|
|
|
|
};
|
|
|
|
|
|
|
|
lsp_types::FoldingRange {
|
|
|
|
start_line: range.start.line,
|
|
|
|
start_character: None,
|
|
|
|
end_line,
|
|
|
|
end_character: None,
|
|
|
|
kind,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lsp_types::FoldingRange {
|
|
|
|
start_line: range.start.line,
|
|
|
|
start_character: Some(range.start.character),
|
|
|
|
end_line: range.end.line,
|
|
|
|
end_character: Some(range.end.character),
|
|
|
|
kind,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 11:00:06 +02:00
|
|
|
pub(crate) fn url(snap: &GlobalStateSnapshot, file_id: FileId) -> lsp_types::Url {
|
|
|
|
snap.file_id_to_url(file_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a `Url` object from a given path, will lowercase drive letters if present.
|
|
|
|
/// This will only happen when processing windows paths.
|
|
|
|
///
|
|
|
|
/// When processing non-windows path, this is essentially the same as `Url::from_file_path`.
|
|
|
|
pub(crate) fn url_from_abs_path(path: &Path) -> lsp_types::Url {
|
|
|
|
assert!(path.is_absolute());
|
|
|
|
let url = lsp_types::Url::from_file_path(path).unwrap();
|
|
|
|
match path.components().next() {
|
|
|
|
Some(path::Component::Prefix(prefix)) if matches!(prefix.kind(), path::Prefix::Disk(_) | path::Prefix::VerbatimDisk(_)) =>
|
|
|
|
{
|
|
|
|
// Need to lowercase driver letter
|
|
|
|
}
|
|
|
|
_ => return url,
|
|
|
|
}
|
|
|
|
|
|
|
|
let driver_letter_range = {
|
|
|
|
let (scheme, drive_letter, _rest) = match url.as_str().splitn(3, ':').collect_tuple() {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return url,
|
|
|
|
};
|
|
|
|
let start = scheme.len() + ':'.len_utf8();
|
|
|
|
start..(start + drive_letter.len())
|
|
|
|
};
|
|
|
|
|
|
|
|
// Note: lowercasing the `path` itself doesn't help, the `Url::parse`
|
|
|
|
// machinery *also* canonicalizes the drive letter. So, just massage the
|
|
|
|
// string in place.
|
|
|
|
let mut url = url.into_string();
|
|
|
|
url[driver_letter_range].make_ascii_lowercase();
|
|
|
|
lsp_types::Url::parse(&url).unwrap()
|
2020-05-10 13:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn versioned_text_document_identifier(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-05-10 13:55:24 +02:00
|
|
|
file_id: FileId,
|
|
|
|
version: Option<i64>,
|
2020-06-13 11:00:06 +02:00
|
|
|
) -> lsp_types::VersionedTextDocumentIdentifier {
|
|
|
|
lsp_types::VersionedTextDocumentIdentifier { uri: url(snap, file_id), version }
|
2020-05-10 13:55:24 +02:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
pub(crate) fn location(
|
|
|
|
snap: &GlobalStateSnapshot,
|
|
|
|
frange: FileRange,
|
|
|
|
) -> Result<lsp_types::Location> {
|
2020-06-13 11:00:06 +02:00
|
|
|
let url = url(snap, frange.file_id);
|
2020-06-03 11:16:08 +02:00
|
|
|
let line_index = snap.analysis().file_line_index(frange.file_id)?;
|
2020-05-10 13:55:24 +02:00
|
|
|
let range = range(&line_index, frange.range);
|
|
|
|
let loc = lsp_types::Location::new(url, range);
|
|
|
|
Ok(loc)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn location_link(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-05-25 15:55:25 +02:00
|
|
|
src: Option<FileRange>,
|
2020-05-10 13:55:24 +02:00
|
|
|
target: NavigationTarget,
|
|
|
|
) -> Result<lsp_types::LocationLink> {
|
2020-05-25 15:55:25 +02:00
|
|
|
let origin_selection_range = match src {
|
|
|
|
Some(src) => {
|
2020-06-03 11:16:08 +02:00
|
|
|
let line_index = snap.analysis().file_line_index(src.file_id)?;
|
2020-05-25 15:55:25 +02:00
|
|
|
let range = range(&line_index, src.range);
|
|
|
|
Some(range)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
};
|
2020-06-03 11:16:08 +02:00
|
|
|
let (target_uri, target_range, target_selection_range) = location_info(snap, target)?;
|
2020-05-10 13:55:24 +02:00
|
|
|
let res = lsp_types::LocationLink {
|
2020-05-25 15:55:25 +02:00
|
|
|
origin_selection_range,
|
2020-05-10 13:55:24 +02:00
|
|
|
target_uri,
|
|
|
|
target_range,
|
|
|
|
target_selection_range,
|
|
|
|
};
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn location_info(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-05-10 13:55:24 +02:00
|
|
|
target: NavigationTarget,
|
|
|
|
) -> Result<(lsp_types::Url, lsp_types::Range, lsp_types::Range)> {
|
2020-06-03 11:16:08 +02:00
|
|
|
let line_index = snap.analysis().file_line_index(target.file_id())?;
|
2020-05-10 13:55:24 +02:00
|
|
|
|
2020-06-13 11:00:06 +02:00
|
|
|
let target_uri = url(snap, target.file_id());
|
2020-05-10 13:55:24 +02:00
|
|
|
let target_range = range(&line_index, target.full_range());
|
|
|
|
let target_selection_range =
|
|
|
|
target.focus_range().map(|it| range(&line_index, it)).unwrap_or(target_range);
|
|
|
|
Ok((target_uri, target_range, target_selection_range))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn goto_definition_response(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-05-25 15:55:25 +02:00
|
|
|
src: Option<FileRange>,
|
2020-05-10 13:55:24 +02:00
|
|
|
targets: Vec<NavigationTarget>,
|
|
|
|
) -> Result<lsp_types::GotoDefinitionResponse> {
|
2020-06-03 11:16:08 +02:00
|
|
|
if snap.config.client_caps.location_link {
|
2020-05-10 13:55:24 +02:00
|
|
|
let links = targets
|
|
|
|
.into_iter()
|
2020-06-03 11:16:08 +02:00
|
|
|
.map(|nav| location_link(snap, src, nav))
|
2020-05-10 13:55:24 +02:00
|
|
|
.collect::<Result<Vec<_>>>()?;
|
|
|
|
Ok(links.into())
|
|
|
|
} else {
|
|
|
|
let locations = targets
|
|
|
|
.into_iter()
|
|
|
|
.map(|nav| {
|
|
|
|
location(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap,
|
2020-05-10 13:55:24 +02:00
|
|
|
FileRange {
|
|
|
|
file_id: nav.file_id(),
|
|
|
|
range: nav.focus_range().unwrap_or(nav.range()),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>>>()?;
|
|
|
|
Ok(locations.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 00:11:40 +02:00
|
|
|
pub(crate) fn snippet_text_document_edit(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-05-18 00:11:40 +02:00
|
|
|
is_snippet: bool,
|
2020-05-10 13:55:24 +02:00
|
|
|
source_file_edit: SourceFileEdit,
|
2020-05-18 00:11:40 +02:00
|
|
|
) -> Result<lsp_ext::SnippetTextDocumentEdit> {
|
2020-06-13 11:00:06 +02:00
|
|
|
let text_document = versioned_text_document_identifier(snap, source_file_edit.file_id, None);
|
2020-06-03 11:16:08 +02:00
|
|
|
let line_index = snap.analysis().file_line_index(source_file_edit.file_id)?;
|
|
|
|
let line_endings = snap.file_line_endings(source_file_edit.file_id);
|
2020-05-10 13:55:24 +02:00
|
|
|
let edits = source_file_edit
|
|
|
|
.edit
|
2020-05-21 15:56:18 +02:00
|
|
|
.into_iter()
|
2020-05-22 17:26:31 -04:00
|
|
|
.map(|it| snippet_text_edit(&line_index, line_endings, is_snippet, it))
|
2020-05-10 13:55:24 +02:00
|
|
|
.collect();
|
2020-05-18 00:11:40 +02:00
|
|
|
Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits })
|
2020-05-10 13:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn resource_op(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-05-10 13:55:24 +02:00
|
|
|
file_system_edit: FileSystemEdit,
|
2020-06-13 11:00:06 +02:00
|
|
|
) -> lsp_types::ResourceOp {
|
|
|
|
match file_system_edit {
|
2020-06-16 18:45:58 +02:00
|
|
|
FileSystemEdit::CreateFile { anchor, dst } => {
|
|
|
|
let uri = snap.anchored_path(anchor, &dst);
|
2020-05-10 13:55:24 +02:00
|
|
|
lsp_types::ResourceOp::Create(lsp_types::CreateFile { uri, options: None })
|
|
|
|
}
|
2020-06-16 18:45:58 +02:00
|
|
|
FileSystemEdit::MoveFile { src, anchor, dst } => {
|
2020-06-13 11:00:06 +02:00
|
|
|
let old_uri = snap.file_id_to_url(src);
|
2020-06-16 18:45:58 +02:00
|
|
|
let new_uri = snap.anchored_path(anchor, &dst);
|
2020-05-10 13:55:24 +02:00
|
|
|
lsp_types::ResourceOp::Rename(lsp_types::RenameFile { old_uri, new_uri, options: None })
|
|
|
|
}
|
2020-06-13 11:00:06 +02:00
|
|
|
}
|
2020-05-10 13:55:24 +02:00
|
|
|
}
|
|
|
|
|
2020-05-18 00:11:40 +02:00
|
|
|
pub(crate) fn snippet_workspace_edit(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-05-18 00:11:40 +02:00
|
|
|
source_change: SourceChange,
|
|
|
|
) -> Result<lsp_ext::SnippetWorkspaceEdit> {
|
|
|
|
let mut document_changes: Vec<lsp_ext::SnippetDocumentChangeOperation> = Vec::new();
|
2020-05-10 13:55:24 +02:00
|
|
|
for op in source_change.file_system_edits {
|
2020-06-13 11:00:06 +02:00
|
|
|
let op = resource_op(&snap, op);
|
2020-05-18 00:11:40 +02:00
|
|
|
document_changes.push(lsp_ext::SnippetDocumentChangeOperation::Op(op));
|
2020-05-10 13:55:24 +02:00
|
|
|
}
|
|
|
|
for edit in source_change.source_file_edits {
|
2020-06-03 11:16:08 +02:00
|
|
|
let edit = snippet_text_document_edit(&snap, source_change.is_snippet, edit)?;
|
2020-05-18 00:11:40 +02:00
|
|
|
document_changes.push(lsp_ext::SnippetDocumentChangeOperation::Edit(edit));
|
|
|
|
}
|
|
|
|
let workspace_edit =
|
|
|
|
lsp_ext::SnippetWorkspaceEdit { changes: None, document_changes: Some(document_changes) };
|
|
|
|
Ok(workspace_edit)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn workspace_edit(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-05-18 00:11:40 +02:00
|
|
|
source_change: SourceChange,
|
|
|
|
) -> Result<lsp_types::WorkspaceEdit> {
|
|
|
|
assert!(!source_change.is_snippet);
|
2020-06-03 11:16:08 +02:00
|
|
|
snippet_workspace_edit(snap, source_change).map(|it| it.into())
|
2020-05-18 00:11:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<lsp_ext::SnippetWorkspaceEdit> for lsp_types::WorkspaceEdit {
|
|
|
|
fn from(snippet_workspace_edit: lsp_ext::SnippetWorkspaceEdit) -> lsp_types::WorkspaceEdit {
|
|
|
|
lsp_types::WorkspaceEdit {
|
|
|
|
changes: None,
|
|
|
|
document_changes: snippet_workspace_edit.document_changes.map(|changes| {
|
|
|
|
lsp_types::DocumentChanges::Operations(
|
|
|
|
changes
|
|
|
|
.into_iter()
|
|
|
|
.map(|change| match change {
|
|
|
|
lsp_ext::SnippetDocumentChangeOperation::Op(op) => {
|
|
|
|
lsp_types::DocumentChangeOperation::Op(op)
|
|
|
|
}
|
|
|
|
lsp_ext::SnippetDocumentChangeOperation::Edit(edit) => {
|
|
|
|
lsp_types::DocumentChangeOperation::Edit(
|
|
|
|
lsp_types::TextDocumentEdit {
|
|
|
|
text_document: edit.text_document,
|
|
|
|
edits: edit
|
|
|
|
.edits
|
|
|
|
.into_iter()
|
|
|
|
.map(|edit| lsp_types::TextEdit {
|
|
|
|
range: edit.range,
|
|
|
|
new_text: edit.new_text,
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
}
|
2020-05-10 13:55:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 11:00:06 +02:00
|
|
|
pub(crate) fn call_hierarchy_item(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-05-10 13:55:24 +02:00
|
|
|
target: NavigationTarget,
|
|
|
|
) -> Result<lsp_types::CallHierarchyItem> {
|
|
|
|
let name = target.name().to_string();
|
|
|
|
let detail = target.description().map(|it| it.to_string());
|
|
|
|
let kind = symbol_kind(target.kind());
|
2020-06-03 11:16:08 +02:00
|
|
|
let (uri, range, selection_range) = location_info(snap, target)?;
|
2020-05-10 13:55:24 +02:00
|
|
|
Ok(lsp_types::CallHierarchyItem { name, kind, tags: None, detail, uri, range, selection_range })
|
|
|
|
}
|
|
|
|
|
2020-06-02 22:21:48 +02:00
|
|
|
pub(crate) fn unresolved_code_action(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-06-02 22:21:48 +02:00
|
|
|
assist: Assist,
|
2020-06-03 18:39:01 +02:00
|
|
|
index: usize,
|
2020-06-02 22:21:48 +02:00
|
|
|
) -> Result<lsp_ext::CodeAction> {
|
2020-05-21 14:34:27 +02:00
|
|
|
let res = lsp_ext::CodeAction {
|
|
|
|
title: assist.label,
|
2020-06-03 18:39:01 +02:00
|
|
|
id: Some(format!("{}:{}", assist.id.0.to_owned(), index.to_string())),
|
2020-06-03 19:26:01 +02:00
|
|
|
group: assist.group.filter(|_| snap.config.client_caps.code_action_group).map(|gr| gr.0),
|
2020-06-02 22:21:48 +02:00
|
|
|
kind: Some(String::new()),
|
|
|
|
edit: None,
|
|
|
|
command: None,
|
|
|
|
};
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn resolved_code_action(
|
2020-06-03 19:26:01 +02:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-06-02 22:21:48 +02:00
|
|
|
assist: ResolvedAssist,
|
|
|
|
) -> Result<lsp_ext::CodeAction> {
|
2020-06-03 18:39:01 +02:00
|
|
|
let change = assist.source_change;
|
2020-06-03 19:26:01 +02:00
|
|
|
unresolved_code_action(snap, assist.assist, 0).and_then(|it| {
|
2020-06-03 18:39:01 +02:00
|
|
|
Ok(lsp_ext::CodeAction {
|
|
|
|
id: None,
|
2020-06-03 19:26:01 +02:00
|
|
|
edit: Some(snippet_workspace_edit(snap, change)?),
|
2020-06-03 18:39:01 +02:00
|
|
|
..it
|
|
|
|
})
|
|
|
|
})
|
2020-05-10 13:55:24 +02:00
|
|
|
}
|
2020-06-02 16:30:26 +02:00
|
|
|
|
|
|
|
pub(crate) fn runnable(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-06-02 16:30:26 +02:00
|
|
|
file_id: FileId,
|
2020-06-08 13:56:31 +03:00
|
|
|
runnable: Runnable,
|
2020-06-02 16:30:26 +02:00
|
|
|
) -> Result<lsp_ext::Runnable> {
|
2020-06-03 11:16:08 +02:00
|
|
|
let spec = CargoTargetSpec::for_file(snap, file_id)?;
|
2020-06-02 16:30:26 +02:00
|
|
|
let target = spec.as_ref().map(|s| s.target.clone());
|
2020-06-02 17:22:23 +02:00
|
|
|
let (cargo_args, executable_args) =
|
2020-06-02 16:30:26 +02:00
|
|
|
CargoTargetSpec::runnable_args(spec, &runnable.kind, &runnable.cfg_exprs)?;
|
2020-06-06 12:00:46 +03:00
|
|
|
let label = runnable.label(target);
|
2020-06-08 13:56:31 +03:00
|
|
|
let location = location_link(snap, None, runnable.nav)?;
|
2020-06-02 16:30:26 +02:00
|
|
|
|
|
|
|
Ok(lsp_ext::Runnable {
|
|
|
|
label,
|
2020-06-02 17:22:23 +02:00
|
|
|
location: Some(location),
|
2020-06-02 16:30:26 +02:00
|
|
|
kind: lsp_ext::RunnableKind::Cargo,
|
2020-06-02 17:22:23 +02:00
|
|
|
args: lsp_ext::CargoRunnable {
|
2020-06-03 11:16:08 +02:00
|
|
|
workspace_root: snap.workspace_root_for(file_id).map(|root| root.to_owned()),
|
2020-06-02 17:22:23 +02:00
|
|
|
cargo_args,
|
|
|
|
executable_args,
|
2020-06-02 16:30:26 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2020-06-13 11:00:06 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use test_utils::extract_ranges;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conv_fold_line_folding_only_fixup() {
|
|
|
|
let text = r#"<fold>mod a;
|
|
|
|
mod b;
|
|
|
|
mod c;</fold>
|
|
|
|
|
|
|
|
fn main() <fold>{
|
|
|
|
if cond <fold>{
|
|
|
|
a::do_a();
|
|
|
|
}</fold> else <fold>{
|
|
|
|
b::do_b();
|
|
|
|
}</fold>
|
|
|
|
}</fold>"#;
|
|
|
|
|
|
|
|
let (ranges, text) = extract_ranges(text, "fold");
|
|
|
|
assert_eq!(ranges.len(), 4);
|
|
|
|
let folds = vec![
|
|
|
|
Fold { range: ranges[0], kind: FoldKind::Mods },
|
|
|
|
Fold { range: ranges[1], kind: FoldKind::Block },
|
|
|
|
Fold { range: ranges[2], kind: FoldKind::Block },
|
|
|
|
Fold { range: ranges[3], kind: FoldKind::Block },
|
|
|
|
];
|
|
|
|
|
|
|
|
let line_index = LineIndex::new(&text);
|
|
|
|
let converted: Vec<lsp_types::FoldingRange> =
|
|
|
|
folds.into_iter().map(|it| folding_range(&text, &line_index, true, it)).collect();
|
|
|
|
|
|
|
|
let expected_lines = [(0, 2), (4, 10), (5, 6), (7, 9)];
|
|
|
|
assert_eq!(converted.len(), expected_lines.len());
|
|
|
|
for (folding_range, (start_line, end_line)) in converted.iter().zip(expected_lines.iter()) {
|
|
|
|
assert_eq!(folding_range.start_line, *start_line);
|
|
|
|
assert_eq!(folding_range.start_character, None);
|
|
|
|
assert_eq!(folding_range.end_line, *end_line);
|
|
|
|
assert_eq!(folding_range.end_character, None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// `Url` is not able to parse windows paths on unix machines.
|
|
|
|
#[test]
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
fn test_lowercase_drive_letter_with_drive() {
|
|
|
|
let url = url_from_abs_path(Path::new("C:\\Test"));
|
|
|
|
assert_eq!(url.to_string(), "file:///c:/Test");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
fn test_drive_without_colon_passthrough() {
|
|
|
|
let url = url_from_abs_path(Path::new(r#"\\localhost\C$\my_dir"#));
|
|
|
|
assert_eq!(url.to_string(), "file://localhost/C$/my_dir");
|
|
|
|
}
|
|
|
|
}
|