2020-02-18 12:25:26 +01:00
|
|
|
//! This module is responsible for implementing handlers for Language Server
|
|
|
|
//! Protocol. The majority of requests are fulfilled by calling into the
|
|
|
|
//! `ra_ide` crate.
|
2019-09-30 11:58:53 +03:00
|
|
|
|
2020-01-31 13:34:44 +01:00
|
|
|
use std::{
|
|
|
|
io::Write as _,
|
|
|
|
process::{self, Stdio},
|
|
|
|
};
|
2019-05-29 15:42:14 +03:00
|
|
|
|
2019-08-30 17:24:11 +03:00
|
|
|
use lsp_server::ErrorCode;
|
2019-01-14 13:55:56 +03:00
|
|
|
use lsp_types::{
|
2019-12-30 09:12:06 -05:00
|
|
|
CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
|
|
|
|
CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
|
2020-05-18 00:11:40 +02:00
|
|
|
CodeLens, Command, CompletionItem, Diagnostic, DocumentFormattingParams, DocumentHighlight,
|
2020-06-03 14:15:54 +03:00
|
|
|
DocumentSymbol, FoldingRange, FoldingRangeParams, HoverContents, Location, MarkupContent,
|
|
|
|
MarkupKind, Position, PrepareRenameResponse, Range, RenameParams, SemanticTokensParams,
|
|
|
|
SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation,
|
|
|
|
TextDocumentIdentifier, Url, WorkspaceEdit,
|
2019-01-06 00:58:03 +01:00
|
|
|
};
|
2019-11-27 21:32:33 +03:00
|
|
|
use ra_ide::{
|
2020-06-10 21:24:36 +03:00
|
|
|
FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, NavigationTarget, Query,
|
|
|
|
RangeInfo, Runnable, RunnableKind, SearchScope, TextEdit,
|
2019-10-24 14:01:02 +03:00
|
|
|
};
|
2019-04-14 23:28:10 +03:00
|
|
|
use ra_prof::profile;
|
2020-05-10 13:55:24 +02:00
|
|
|
use ra_project_model::TargetKind;
|
2020-06-02 16:30:26 +02:00
|
|
|
use ra_syntax::{AstNode, SyntaxKind, TextRange, TextSize};
|
2019-07-04 23:05:17 +03:00
|
|
|
use serde::{Deserialize, Serialize};
|
2018-08-29 18:03:14 +03:00
|
|
|
use serde_json::to_value;
|
2020-06-03 19:33:57 +02:00
|
|
|
use stdx::{format_to, split1};
|
2018-08-10 23:30:11 +03:00
|
|
|
|
2018-10-15 20:15:53 +03:00
|
|
|
use crate::{
|
2020-02-18 12:17:47 +01:00
|
|
|
cargo_target_spec::CargoTargetSpec,
|
2020-04-01 14:32:04 +02:00
|
|
|
config::RustfmtConfig,
|
2020-01-31 19:23:25 +01:00
|
|
|
diagnostics::DiagnosticTask,
|
2020-05-10 13:55:24 +02:00
|
|
|
from_json, from_proto,
|
2020-06-03 11:16:08 +02:00
|
|
|
global_state::GlobalStateSnapshot,
|
2020-05-10 19:25:37 +02:00
|
|
|
lsp_ext::{self, InlayHint, InlayHintsParams},
|
2020-06-03 11:16:08 +02:00
|
|
|
to_proto, LspError, Result,
|
2018-08-11 00:12:31 +03:00
|
|
|
};
|
2018-08-10 21:13:39 +03:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
pub fn handle_analyzer_status(snap: GlobalStateSnapshot, _: ()) -> Result<String> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_analyzer_status");
|
2020-06-03 11:16:08 +02:00
|
|
|
let mut buf = snap.status();
|
2020-05-01 19:13:52 -04:00
|
|
|
format_to!(buf, "\n\nrequests:\n");
|
2020-06-03 11:16:08 +02:00
|
|
|
let requests = snap.latest_requests.read();
|
2019-05-31 20:14:54 +03:00
|
|
|
for (is_last, r) in requests.iter() {
|
|
|
|
let mark = if is_last { "*" } else { " " };
|
2020-05-01 19:13:52 -04:00
|
|
|
format_to!(buf, "{}{:4} {:<36}{}ms\n", mark, r.id, r.method, r.duration.as_millis());
|
2019-05-29 15:42:14 +03:00
|
|
|
}
|
|
|
|
Ok(buf)
|
2019-01-23 00:15:03 +03:00
|
|
|
}
|
|
|
|
|
2020-05-10 19:25:37 +02:00
|
|
|
pub fn handle_syntax_tree(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:25:37 +02:00
|
|
|
params: lsp_ext::SyntaxTreeParams,
|
|
|
|
) -> Result<String> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_syntax_tree");
|
2020-06-03 11:16:08 +02:00
|
|
|
let id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(id)?;
|
2020-05-10 13:55:24 +02:00
|
|
|
let text_range = params.range.map(|r| from_proto::text_range(&line_index, r));
|
2020-06-03 11:16:08 +02:00
|
|
|
let res = snap.analysis().syntax_tree(id, text_range)?;
|
2018-08-29 18:03:14 +03:00
|
|
|
Ok(res)
|
2018-08-10 21:13:39 +03:00
|
|
|
}
|
2018-08-10 22:23:17 +03:00
|
|
|
|
2019-11-18 02:47:50 +08:00
|
|
|
pub fn handle_expand_macro(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:25:37 +02:00
|
|
|
params: lsp_ext::ExpandMacroParams,
|
|
|
|
) -> Result<Option<lsp_ext::ExpandedMacro>> {
|
2019-11-18 02:47:50 +08:00
|
|
|
let _p = profile("handle_expand_macro");
|
2020-06-03 11:16:08 +02:00
|
|
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
2020-05-25 14:56:26 +02:00
|
|
|
let offset = from_proto::offset(&line_index, params.position);
|
2019-11-18 02:47:50 +08:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let res = snap.analysis().expand_macro(FilePosition { file_id, offset })?;
|
2020-05-25 14:56:26 +02:00
|
|
|
Ok(res.map(|it| lsp_ext::ExpandedMacro { name: it.name, expansion: it.expansion }))
|
2019-11-18 02:47:50 +08:00
|
|
|
}
|
|
|
|
|
2019-04-21 12:13:48 +03:00
|
|
|
pub fn handle_selection_range(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::SelectionRangeParams,
|
|
|
|
) -> Result<Option<Vec<lsp_types::SelectionRange>>> {
|
2019-05-27 11:48:23 +03:00
|
|
|
let _p = profile("handle_selection_range");
|
2020-06-03 11:16:08 +02:00
|
|
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
2020-05-10 19:24:02 +02:00
|
|
|
let res: Result<Vec<lsp_types::SelectionRange>> = params
|
2019-04-21 12:13:48 +03:00
|
|
|
.positions
|
|
|
|
.into_iter()
|
|
|
|
.map(|position| {
|
2020-05-10 13:55:24 +02:00
|
|
|
let offset = from_proto::offset(&line_index, position);
|
2019-04-21 12:13:48 +03:00
|
|
|
let mut ranges = Vec::new();
|
|
|
|
{
|
2020-05-10 13:55:24 +02:00
|
|
|
let mut range = TextRange::new(offset, offset);
|
2019-04-21 12:13:48 +03:00
|
|
|
loop {
|
|
|
|
ranges.push(range);
|
|
|
|
let frange = FileRange { file_id, range };
|
2020-06-03 11:16:08 +02:00
|
|
|
let next = snap.analysis().extend_selection(frange)?;
|
2019-04-21 12:13:48 +03:00
|
|
|
if next == range {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
range = next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 19:24:02 +02:00
|
|
|
let mut range = lsp_types::SelectionRange {
|
2020-05-10 13:55:24 +02:00
|
|
|
range: to_proto::range(&line_index, *ranges.last().unwrap()),
|
2019-04-21 12:13:48 +03:00
|
|
|
parent: None,
|
|
|
|
};
|
2020-05-10 13:55:24 +02:00
|
|
|
for &r in ranges.iter().rev().skip(1) {
|
2020-05-10 19:24:02 +02:00
|
|
|
range = lsp_types::SelectionRange {
|
2020-05-10 13:55:24 +02:00
|
|
|
range: to_proto::range(&line_index, r),
|
2019-04-21 12:13:48 +03:00
|
|
|
parent: Some(Box::new(range)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(range)
|
|
|
|
})
|
2020-03-09 10:17:16 -04:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(Some(res?))
|
2019-04-21 12:13:48 +03:00
|
|
|
}
|
|
|
|
|
2020-05-24 16:18:46 +02:00
|
|
|
pub fn handle_matching_brace(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-24 16:18:46 +02:00
|
|
|
params: lsp_ext::MatchingBraceParams,
|
2018-08-16 00:23:22 +03:00
|
|
|
) -> Result<Vec<Position>> {
|
2020-05-24 16:18:46 +02:00
|
|
|
let _p = profile("handle_matching_brace");
|
2020-06-03 11:16:08 +02:00
|
|
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
2018-10-15 17:44:23 -04:00
|
|
|
let res = params
|
2020-05-24 16:18:46 +02:00
|
|
|
.positions
|
2018-08-16 00:23:22 +03:00
|
|
|
.into_iter()
|
2020-05-10 13:55:24 +02:00
|
|
|
.map(|position| {
|
|
|
|
let offset = from_proto::offset(&line_index, position);
|
2020-06-03 11:16:08 +02:00
|
|
|
let offset = match snap.analysis().matching_brace(FilePosition { file_id, offset }) {
|
2020-05-10 13:55:24 +02:00
|
|
|
Ok(Some(matching_brace_offset)) => matching_brace_offset,
|
|
|
|
Err(_) | Ok(None) => offset,
|
|
|
|
};
|
|
|
|
to_proto::position(&line_index, offset)
|
2018-08-16 00:23:22 +03:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2018-08-23 22:14:51 +03:00
|
|
|
pub fn handle_join_lines(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:25:37 +02:00
|
|
|
params: lsp_ext::JoinLinesParams,
|
2020-05-21 19:50:23 +02:00
|
|
|
) -> Result<Vec<lsp_types::TextEdit>> {
|
2019-05-27 11:48:23 +03:00
|
|
|
let _p = profile("handle_join_lines");
|
2020-06-03 11:16:08 +02:00
|
|
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
|
|
|
let line_endings = snap.file_line_endings(file_id);
|
2020-05-21 19:50:23 +02:00
|
|
|
let mut res = TextEdit::default();
|
|
|
|
for range in params.ranges {
|
|
|
|
let range = from_proto::text_range(&line_index, range);
|
2020-06-03 11:16:08 +02:00
|
|
|
let edit = snap.analysis().join_lines(FileRange { file_id, range })?;
|
2020-05-21 19:50:23 +02:00
|
|
|
match res.union(edit) {
|
|
|
|
Ok(()) => (),
|
|
|
|
Err(_edit) => {
|
|
|
|
// just ignore overlapping edits
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let res = to_proto::text_edit_vec(&line_index, line_endings, res);
|
|
|
|
Ok(res)
|
2018-08-29 18:03:14 +03:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:00:20 +03:00
|
|
|
pub fn handle_on_enter(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::TextDocumentPositionParams,
|
2020-05-25 14:12:53 +02:00
|
|
|
) -> Result<Option<Vec<lsp_ext::SnippetTextEdit>>> {
|
2019-05-27 11:48:23 +03:00
|
|
|
let _p = profile("handle_on_enter");
|
2020-06-03 11:16:08 +02:00
|
|
|
let position = from_proto::file_position(&snap, params)?;
|
|
|
|
let edit = match snap.analysis().on_enter(position)? {
|
2020-05-25 14:12:53 +02:00
|
|
|
None => return Ok(None),
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
2020-06-03 11:16:08 +02:00
|
|
|
let line_index = snap.analysis().file_line_index(position.file_id)?;
|
|
|
|
let line_endings = snap.file_line_endings(position.file_id);
|
2020-05-25 14:12:53 +02:00
|
|
|
let edit = to_proto::snippet_text_edit_vec(&line_index, line_endings, true, edit);
|
|
|
|
Ok(Some(edit))
|
2018-10-09 16:00:20 +03:00
|
|
|
}
|
|
|
|
|
2019-10-25 12:16:56 +03:00
|
|
|
// Don't forget to add new trigger characters to `ServerCapabilities` in `caps.rs`.
|
2018-08-29 18:03:14 +03:00
|
|
|
pub fn handle_on_type_formatting(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::DocumentOnTypeFormattingParams,
|
2020-05-21 19:50:23 +02:00
|
|
|
) -> Result<Option<Vec<lsp_types::TextEdit>>> {
|
2019-05-27 11:48:23 +03:00
|
|
|
let _p = profile("handle_on_type_formatting");
|
2020-06-03 11:16:08 +02:00
|
|
|
let mut position = from_proto::file_position(&snap, params.text_document_position)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(position.file_id)?;
|
|
|
|
let line_endings = snap.file_line_endings(position.file_id);
|
2019-07-07 14:13:13 -04:00
|
|
|
|
2019-11-27 21:32:33 +03:00
|
|
|
// in `ra_ide`, the `on_type` invariant is that
|
2019-07-07 14:13:13 -04:00
|
|
|
// `text.char_at(position) == typed_char`.
|
2020-04-24 23:40:41 +02:00
|
|
|
position.offset -= TextSize::of('.');
|
2019-10-25 11:19:26 +03:00
|
|
|
let char_typed = params.ch.chars().next().unwrap_or('\0');
|
2020-03-02 13:45:26 +01:00
|
|
|
assert!({
|
2020-06-03 11:16:08 +02:00
|
|
|
let text = snap.analysis().file_text(position.file_id)?;
|
2020-04-24 23:40:41 +02:00
|
|
|
text[usize::from(position.offset)..].starts_with(char_typed)
|
2020-03-02 13:45:26 +01:00
|
|
|
});
|
2019-10-25 13:03:57 +03:00
|
|
|
|
|
|
|
// We have an assist that inserts ` ` after typing `->` in `fn foo() ->{`,
|
|
|
|
// but it requires precise cursor positioning to work, and one can't
|
|
|
|
// position the cursor with on_type formatting. So, let's just toggle this
|
|
|
|
// feature off here, hoping that we'll enable it one day, 😿.
|
|
|
|
if char_typed == '>' {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let edit = snap.analysis().on_char_typed(position, char_typed)?;
|
2019-01-11 15:05:40 +03:00
|
|
|
let mut edit = match edit {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2019-01-06 00:58:03 +01:00
|
|
|
|
2019-01-11 15:05:40 +03:00
|
|
|
// This should be a single-file edit
|
|
|
|
let edit = edit.source_file_edits.pop().unwrap();
|
2019-01-06 00:58:03 +01:00
|
|
|
|
2020-05-10 13:55:24 +02:00
|
|
|
let change = to_proto::text_edit_vec(&line_index, line_endings, edit.edit);
|
2019-02-06 15:50:26 -05:00
|
|
|
Ok(Some(change))
|
2018-08-23 22:14:51 +03:00
|
|
|
}
|
|
|
|
|
2018-08-11 14:44:12 +03:00
|
|
|
pub fn handle_document_symbol(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::DocumentSymbolParams,
|
|
|
|
) -> Result<Option<lsp_types::DocumentSymbolResponse>> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_document_symbol");
|
2020-06-03 11:16:08 +02:00
|
|
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
2018-08-11 14:44:12 +03:00
|
|
|
|
2018-08-14 11:20:09 +03:00
|
|
|
let mut parents: Vec<(DocumentSymbol, Option<usize>)> = Vec::new();
|
2018-08-11 14:44:12 +03:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
for symbol in snap.analysis().file_structure(file_id)? {
|
2018-08-11 14:44:12 +03:00
|
|
|
let doc_symbol = DocumentSymbol {
|
2018-08-14 11:20:09 +03:00
|
|
|
name: symbol.label,
|
2019-01-24 18:21:17 +01:00
|
|
|
detail: symbol.detail,
|
2020-05-10 13:55:24 +02:00
|
|
|
kind: to_proto::symbol_kind(symbol.kind),
|
2019-02-05 17:05:46 -05:00
|
|
|
deprecated: Some(symbol.deprecated),
|
2020-05-10 13:55:24 +02:00
|
|
|
range: to_proto::range(&line_index, symbol.node_range),
|
|
|
|
selection_range: to_proto::range(&line_index, symbol.navigation_range),
|
2018-08-11 14:44:12 +03:00
|
|
|
children: None,
|
|
|
|
};
|
2018-08-14 11:20:09 +03:00
|
|
|
parents.push((doc_symbol, symbol.parent));
|
|
|
|
}
|
2020-04-24 10:11:57 -04:00
|
|
|
let mut document_symbols = Vec::new();
|
2018-08-14 11:20:09 +03:00
|
|
|
while let Some((node, parent)) = parents.pop() {
|
|
|
|
match parent {
|
2020-04-24 10:11:57 -04:00
|
|
|
None => document_symbols.push(node),
|
2018-08-14 11:20:09 +03:00
|
|
|
Some(i) => {
|
|
|
|
let children = &mut parents[i].0.children;
|
|
|
|
if children.is_none() {
|
|
|
|
*children = Some(Vec::new());
|
|
|
|
}
|
|
|
|
children.as_mut().unwrap().push(node);
|
2018-08-11 14:44:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-14 11:20:09 +03:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let res = if snap.config.client_caps.hierarchical_symbols {
|
2020-05-10 13:55:24 +02:00
|
|
|
document_symbols.into()
|
2020-04-24 10:11:57 -04:00
|
|
|
} else {
|
2020-06-13 11:00:06 +02:00
|
|
|
let url = to_proto::url(&snap, file_id);
|
2020-04-24 10:11:57 -04:00
|
|
|
let mut symbol_information = Vec::<SymbolInformation>::new();
|
|
|
|
for symbol in document_symbols {
|
|
|
|
flatten_document_symbol(&symbol, None, &url, &mut symbol_information);
|
|
|
|
}
|
2020-05-10 13:55:24 +02:00
|
|
|
symbol_information.into()
|
|
|
|
};
|
|
|
|
return Ok(Some(res));
|
2020-04-24 10:11:57 -04:00
|
|
|
|
2020-05-10 13:55:24 +02:00
|
|
|
fn flatten_document_symbol(
|
|
|
|
symbol: &DocumentSymbol,
|
|
|
|
container_name: Option<String>,
|
|
|
|
url: &Url,
|
|
|
|
res: &mut Vec<SymbolInformation>,
|
|
|
|
) {
|
|
|
|
res.push(SymbolInformation {
|
|
|
|
name: symbol.name.clone(),
|
|
|
|
kind: symbol.kind,
|
|
|
|
deprecated: symbol.deprecated,
|
|
|
|
location: Location::new(url.clone(), symbol.range),
|
2020-05-22 17:26:31 -04:00
|
|
|
container_name,
|
2020-05-10 13:55:24 +02:00
|
|
|
});
|
2020-04-24 10:11:57 -04:00
|
|
|
|
2020-05-10 13:55:24 +02:00
|
|
|
for child in symbol.children.iter().flatten() {
|
|
|
|
flatten_document_symbol(child, Some(symbol.name.clone()), url, res);
|
|
|
|
}
|
2020-04-24 10:11:57 -04:00
|
|
|
}
|
2018-08-11 14:44:12 +03:00
|
|
|
}
|
|
|
|
|
2018-08-13 15:35:53 +03:00
|
|
|
pub fn handle_workspace_symbol(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::WorkspaceSymbolParams,
|
2018-08-13 15:35:53 +03:00
|
|
|
) -> Result<Option<Vec<SymbolInformation>>> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_workspace_symbol");
|
2018-10-16 11:45:10 -04:00
|
|
|
let all_symbols = params.query.contains('#');
|
|
|
|
let libs = params.query.contains('*');
|
2018-08-13 16:07:05 +03:00
|
|
|
let query = {
|
2019-02-08 14:49:43 +03:00
|
|
|
let query: String = params.query.chars().filter(|&c| c != '#' && c != '*').collect();
|
2018-08-13 16:07:05 +03:00
|
|
|
let mut q = Query::new(query);
|
|
|
|
if !all_symbols {
|
|
|
|
q.only_types();
|
|
|
|
}
|
2018-09-03 21:03:37 +03:00
|
|
|
if libs {
|
|
|
|
q.libs();
|
|
|
|
}
|
2018-08-13 17:19:27 +03:00
|
|
|
q.limit(128);
|
2018-08-13 16:07:05 +03:00
|
|
|
q
|
|
|
|
};
|
2020-06-03 11:16:08 +02:00
|
|
|
let mut res = exec_query(&snap, query)?;
|
2018-08-14 13:33:44 +03:00
|
|
|
if res.is_empty() && !all_symbols {
|
|
|
|
let mut query = Query::new(params.query);
|
|
|
|
query.limit(128);
|
2020-06-03 11:16:08 +02:00
|
|
|
res = exec_query(&snap, query)?;
|
2018-08-14 13:33:44 +03:00
|
|
|
}
|
2018-08-13 16:07:05 +03:00
|
|
|
|
2018-08-14 13:33:44 +03:00
|
|
|
return Ok(Some(res));
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
fn exec_query(snap: &GlobalStateSnapshot, query: Query) -> Result<Vec<SymbolInformation>> {
|
2018-08-14 13:33:44 +03:00
|
|
|
let mut res = Vec::new();
|
2020-06-03 11:16:08 +02:00
|
|
|
for nav in snap.analysis().symbol_search(query)? {
|
2018-08-14 13:33:44 +03:00
|
|
|
let info = SymbolInformation {
|
2019-01-02 23:35:51 +03:00
|
|
|
name: nav.name().to_string(),
|
2020-05-10 13:55:24 +02:00
|
|
|
kind: to_proto::symbol_kind(nav.kind()),
|
2020-06-03 11:16:08 +02:00
|
|
|
location: to_proto::location(snap, nav.file_range())?,
|
2019-02-12 21:47:51 +02:00
|
|
|
container_name: nav.container_name().map(|v| v.to_string()),
|
2018-09-23 11:13:27 -04:00
|
|
|
deprecated: None,
|
2018-08-14 13:33:44 +03:00
|
|
|
};
|
|
|
|
res.push(info);
|
2018-10-15 17:44:23 -04:00
|
|
|
}
|
2018-08-14 13:33:44 +03:00
|
|
|
Ok(res)
|
|
|
|
}
|
2018-08-13 15:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:17 +03:00
|
|
|
pub fn handle_goto_definition(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::GotoDefinitionParams,
|
|
|
|
) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_goto_definition");
|
2020-06-03 11:16:08 +02:00
|
|
|
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
|
|
|
|
let nav_info = match snap.analysis().goto_definition(position)? {
|
2018-12-08 16:02:23 +00:00
|
|
|
None => return Ok(None),
|
2018-12-08 21:18:29 +03:00
|
|
|
Some(it) => it,
|
|
|
|
};
|
2020-05-25 15:55:25 +02:00
|
|
|
let src = FileRange { file_id: position.file_id, range: nav_info.range };
|
2020-06-03 11:16:08 +02:00
|
|
|
let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
|
2019-07-08 13:47:02 +03:00
|
|
|
Ok(Some(res))
|
2018-08-13 16:35:17 +03:00
|
|
|
}
|
|
|
|
|
2019-01-28 09:26:32 -05:00
|
|
|
pub fn handle_goto_implementation(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::request::GotoImplementationParams,
|
|
|
|
) -> Result<Option<lsp_types::request::GotoImplementationResponse>> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_goto_implementation");
|
2020-06-03 11:16:08 +02:00
|
|
|
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
|
|
|
|
let nav_info = match snap.analysis().goto_implementation(position)? {
|
2019-01-28 09:26:32 -05:00
|
|
|
None => return Ok(None),
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
2020-05-25 15:55:25 +02:00
|
|
|
let src = FileRange { file_id: position.file_id, range: nav_info.range };
|
2020-06-03 11:16:08 +02:00
|
|
|
let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
|
2019-07-08 13:47:02 +03:00
|
|
|
Ok(Some(res))
|
2019-04-23 14:11:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_goto_type_definition(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::request::GotoTypeDefinitionParams,
|
|
|
|
) -> Result<Option<lsp_types::request::GotoTypeDefinitionResponse>> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_goto_type_definition");
|
2020-06-03 11:16:08 +02:00
|
|
|
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
|
|
|
|
let nav_info = match snap.analysis().goto_type_definition(position)? {
|
2019-04-23 14:11:27 -04:00
|
|
|
None => return Ok(None),
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
2020-05-25 15:55:25 +02:00
|
|
|
let src = FileRange { file_id: position.file_id, range: nav_info.range };
|
2020-06-03 11:16:08 +02:00
|
|
|
let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
|
2019-07-08 13:47:02 +03:00
|
|
|
Ok(Some(res))
|
2018-08-13 16:35:17 +03:00
|
|
|
}
|
|
|
|
|
2018-08-22 10:18:58 +03:00
|
|
|
pub fn handle_parent_module(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::TextDocumentPositionParams,
|
2020-05-25 15:55:25 +02:00
|
|
|
) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_parent_module");
|
2020-06-03 11:16:08 +02:00
|
|
|
let position = from_proto::file_position(&snap, params)?;
|
|
|
|
let navs = snap.analysis().parent_module(position)?;
|
|
|
|
let res = to_proto::goto_definition_response(&snap, None, navs)?;
|
2020-05-25 15:55:25 +02:00
|
|
|
Ok(Some(res))
|
2018-08-22 10:18:58 +03:00
|
|
|
}
|
|
|
|
|
2018-08-29 18:03:14 +03:00
|
|
|
pub fn handle_runnables(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:25:37 +02:00
|
|
|
params: lsp_ext::RunnablesParams,
|
|
|
|
) -> Result<Vec<lsp_ext::Runnable>> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_runnables");
|
2020-06-03 11:16:08 +02:00
|
|
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
2020-05-10 13:55:24 +02:00
|
|
|
let offset = params.position.map(|it| from_proto::offset(&line_index, it));
|
2018-08-29 18:03:14 +03:00
|
|
|
let mut res = Vec::new();
|
2020-06-03 11:16:08 +02:00
|
|
|
let workspace_root = snap.workspace_root_for(file_id);
|
|
|
|
let cargo_spec = CargoTargetSpec::for_file(&snap, file_id)?;
|
|
|
|
for runnable in snap.analysis().runnables(file_id)? {
|
2018-08-29 18:03:14 +03:00
|
|
|
if let Some(offset) = offset {
|
2020-06-02 17:22:23 +02:00
|
|
|
if !runnable.nav.full_range().contains_inclusive(offset) {
|
2018-08-29 18:03:14 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-06 14:30:29 +03:00
|
|
|
}
|
2020-06-06 22:11:17 +03:00
|
|
|
if should_skip_target(&runnable, cargo_spec.as_ref()) {
|
2020-06-06 12:00:46 +03:00
|
|
|
continue;
|
2018-08-29 18:03:14 +03:00
|
|
|
}
|
2020-06-06 12:00:46 +03:00
|
|
|
|
2020-06-08 13:56:31 +03:00
|
|
|
res.push(to_proto::runnable(&snap, file_id, runnable)?);
|
2018-08-29 18:03:14 +03:00
|
|
|
}
|
2020-05-07 16:52:14 +02:00
|
|
|
|
2020-04-26 10:40:13 +02:00
|
|
|
// Add `cargo check` and `cargo test` for the whole package
|
2020-05-07 16:52:14 +02:00
|
|
|
match cargo_spec {
|
2018-10-25 11:01:37 +03:00
|
|
|
Some(spec) => {
|
2020-04-26 10:40:13 +02:00
|
|
|
for &cmd in ["check", "test"].iter() {
|
2020-05-10 19:25:37 +02:00
|
|
|
res.push(lsp_ext::Runnable {
|
2020-04-26 10:40:13 +02:00
|
|
|
label: format!("cargo {} -p {}", cmd, spec.package),
|
2020-06-02 17:22:23 +02:00
|
|
|
location: None,
|
2020-05-31 05:13:08 +03:00
|
|
|
kind: lsp_ext::RunnableKind::Cargo,
|
2020-06-02 17:22:23 +02:00
|
|
|
args: lsp_ext::CargoRunnable {
|
|
|
|
workspace_root: workspace_root.map(|root| root.to_owned()),
|
|
|
|
cargo_args: vec![
|
|
|
|
cmd.to_string(),
|
|
|
|
"--package".to_string(),
|
|
|
|
spec.package.clone(),
|
|
|
|
],
|
|
|
|
executable_args: Vec::new(),
|
|
|
|
},
|
2020-04-26 10:40:13 +02:00
|
|
|
})
|
|
|
|
}
|
2018-10-25 11:01:37 +03:00
|
|
|
}
|
|
|
|
None => {
|
2020-05-10 19:25:37 +02:00
|
|
|
res.push(lsp_ext::Runnable {
|
2020-04-26 10:40:13 +02:00
|
|
|
label: "cargo check --workspace".to_string(),
|
2020-06-02 17:22:23 +02:00
|
|
|
location: None,
|
2020-05-31 05:13:08 +03:00
|
|
|
kind: lsp_ext::RunnableKind::Cargo,
|
2020-06-02 17:22:23 +02:00
|
|
|
args: lsp_ext::CargoRunnable {
|
|
|
|
workspace_root: workspace_root.map(|root| root.to_owned()),
|
|
|
|
cargo_args: vec!["check".to_string(), "--workspace".to_string()],
|
|
|
|
executable_args: Vec::new(),
|
|
|
|
},
|
2020-04-26 10:40:13 +02:00
|
|
|
});
|
2018-10-25 11:01:37 +03:00
|
|
|
}
|
2018-10-25 10:56:12 +03:00
|
|
|
}
|
2019-02-06 15:50:26 -05:00
|
|
|
Ok(res)
|
2018-08-29 18:03:14 +03:00
|
|
|
}
|
|
|
|
|
2018-08-26 12:51:45 +03:00
|
|
|
pub fn handle_completion(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::CompletionParams,
|
|
|
|
) -> Result<Option<lsp_types::CompletionResponse>> {
|
2019-04-14 23:28:10 +03:00
|
|
|
let _p = profile("handle_completion");
|
2020-06-03 11:16:08 +02:00
|
|
|
let position = from_proto::file_position(&snap, params.text_document_position)?;
|
2018-11-21 19:04:33 +03:00
|
|
|
let completion_triggered_after_single_colon = {
|
|
|
|
let mut res = false;
|
|
|
|
if let Some(ctx) = params.context {
|
2018-11-29 15:30:49 -05:00
|
|
|
if ctx.trigger_character.unwrap_or_default() == ":" {
|
2020-06-03 11:16:08 +02:00
|
|
|
let source_file = snap.analysis().parse(position.file_id)?;
|
2018-11-21 19:04:33 +03:00
|
|
|
let syntax = source_file.syntax();
|
|
|
|
let text = syntax.text();
|
|
|
|
if let Some(next_char) = text.char_at(position.offset) {
|
2020-04-24 23:40:41 +02:00
|
|
|
let diff = TextSize::of(next_char) + TextSize::of(':');
|
2018-11-21 19:04:33 +03:00
|
|
|
let prev_char = position.offset - diff;
|
|
|
|
if text.char_at(prev_char) != Some(':') {
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res
|
|
|
|
};
|
|
|
|
if completion_triggered_after_single_colon {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let items = match snap.analysis().completions(&snap.config.completion, position)? {
|
2018-08-26 12:51:45 +03:00
|
|
|
None => return Ok(None),
|
|
|
|
Some(items) => items,
|
|
|
|
};
|
2020-06-03 11:16:08 +02:00
|
|
|
let line_index = snap.analysis().file_line_index(position.file_id)?;
|
|
|
|
let line_endings = snap.file_line_endings(position.file_id);
|
2020-05-10 13:55:24 +02:00
|
|
|
let items: Vec<CompletionItem> = items
|
|
|
|
.into_iter()
|
|
|
|
.map(|item| to_proto::completion_item(&line_index, line_endings, item))
|
|
|
|
.collect();
|
2018-08-26 12:51:45 +03:00
|
|
|
|
2019-07-07 17:28:21 -04:00
|
|
|
Ok(Some(items.into()))
|
2018-08-26 12:51:45 +03:00
|
|
|
}
|
|
|
|
|
2018-09-23 11:13:27 -04:00
|
|
|
pub fn handle_folding_range(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2018-09-23 11:13:27 -04:00
|
|
|
params: FoldingRangeParams,
|
|
|
|
) -> Result<Option<Vec<FoldingRange>>> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_folding_range");
|
2020-06-03 11:16:08 +02:00
|
|
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
|
|
|
let folds = snap.analysis().folding_ranges(file_id)?;
|
|
|
|
let text = snap.analysis().file_text(file_id)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
|
|
|
let line_folding_only = snap.config.client_caps.line_folding_only;
|
2020-05-10 13:55:24 +02:00
|
|
|
let res = folds
|
|
|
|
.into_iter()
|
|
|
|
.map(|it| to_proto::folding_range(&*text, &line_index, line_folding_only, it))
|
|
|
|
.collect();
|
|
|
|
Ok(Some(res))
|
2018-09-23 11:13:27 -04:00
|
|
|
}
|
|
|
|
|
2018-10-09 10:08:17 -04:00
|
|
|
pub fn handle_signature_help(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::SignatureHelpParams,
|
|
|
|
) -> Result<Option<lsp_types::SignatureHelp>> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_signature_help");
|
2020-06-03 11:16:08 +02:00
|
|
|
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
|
|
|
|
let call_info = match snap.analysis().call_info(position)? {
|
2020-05-10 13:55:24 +02:00
|
|
|
None => return Ok(None),
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
2020-06-03 11:16:08 +02:00
|
|
|
let concise = !snap.config.call_info_full;
|
2020-05-10 13:55:24 +02:00
|
|
|
let mut active_parameter = call_info.active_parameter.map(|it| it as i64);
|
|
|
|
if concise && call_info.signature.has_self_param {
|
|
|
|
active_parameter = active_parameter.map(|it| it.saturating_sub(1));
|
2018-10-09 10:08:17 -04:00
|
|
|
}
|
2020-05-10 13:55:24 +02:00
|
|
|
let sig_info = to_proto::signature_information(call_info.signature, concise);
|
|
|
|
|
2020-05-10 19:24:02 +02:00
|
|
|
Ok(Some(lsp_types::SignatureHelp {
|
2020-05-10 13:55:24 +02:00
|
|
|
signatures: vec![sig_info],
|
|
|
|
active_signature: Some(0),
|
|
|
|
active_parameter,
|
|
|
|
}))
|
2018-10-09 10:08:17 -04:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
pub fn handle_hover(
|
|
|
|
snap: GlobalStateSnapshot,
|
|
|
|
params: lsp_types::HoverParams,
|
2020-06-03 14:15:54 +03:00
|
|
|
) -> Result<Option<lsp_ext::Hover>> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_hover");
|
2020-06-03 11:16:08 +02:00
|
|
|
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
|
|
|
|
let info = match snap.analysis().hover(position)? {
|
2018-12-08 16:02:23 +00:00
|
|
|
None => return Ok(None),
|
2019-01-05 17:22:41 +03:00
|
|
|
Some(info) => info,
|
2018-12-08 16:02:23 +00:00
|
|
|
};
|
2020-06-03 11:16:08 +02:00
|
|
|
let line_index = snap.analysis.file_line_index(position.file_id)?;
|
2020-05-10 13:55:24 +02:00
|
|
|
let range = to_proto::range(&line_index, info.range);
|
2020-06-03 16:39:32 +03:00
|
|
|
let hover = lsp_ext::Hover {
|
|
|
|
hover: lsp_types::Hover {
|
|
|
|
contents: HoverContents::Markup(MarkupContent {
|
|
|
|
kind: MarkupKind::Markdown,
|
|
|
|
value: crate::markdown::format_docs(&info.info.to_markup()),
|
|
|
|
}),
|
|
|
|
range: Some(range),
|
|
|
|
},
|
2020-06-06 14:30:29 +03:00
|
|
|
actions: prepare_hover_actions(&snap, position.file_id, info.info.actions()),
|
2019-01-04 19:06:46 +09:00
|
|
|
};
|
2020-06-03 16:39:32 +03:00
|
|
|
|
|
|
|
Ok(Some(hover))
|
2018-11-05 16:37:27 -05:00
|
|
|
}
|
|
|
|
|
2018-10-19 15:25:10 -04:00
|
|
|
pub fn handle_prepare_rename(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::TextDocumentPositionParams,
|
2018-10-19 15:25:10 -04:00
|
|
|
) -> Result<Option<PrepareRenameResponse>> {
|
2019-10-23 18:26:43 +03:00
|
|
|
let _p = profile("handle_prepare_rename");
|
2020-06-03 11:16:08 +02:00
|
|
|
let position = from_proto::file_position(&snap, params)?;
|
2018-10-19 15:25:10 -04:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let optional_change = snap.analysis().rename(position, "dummy")?;
|
2019-09-05 21:36:40 +03:00
|
|
|
let range = match optional_change {
|
2018-11-05 14:57:41 +03:00
|
|
|
None => return Ok(None),
|
2019-09-05 21:36:40 +03:00
|
|
|
Some(it) => it.range,
|
2018-11-05 14:57:41 +03:00
|
|
|
};
|
2019-02-17 13:38:32 +02:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let line_index = snap.analysis().file_line_index(position.file_id)?;
|
2020-05-10 13:55:24 +02:00
|
|
|
let range = to_proto::range(&line_index, range);
|
2019-09-05 21:36:40 +03:00
|
|
|
Ok(Some(PrepareRenameResponse::Range(range)))
|
2018-10-19 15:25:10 -04:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
pub fn handle_rename(
|
|
|
|
snap: GlobalStateSnapshot,
|
|
|
|
params: RenameParams,
|
|
|
|
) -> Result<Option<WorkspaceEdit>> {
|
2019-10-23 18:26:43 +03:00
|
|
|
let _p = profile("handle_rename");
|
2020-06-03 11:16:08 +02:00
|
|
|
let position = from_proto::file_position(&snap, params.text_document_position)?;
|
2018-10-18 17:56:22 -04:00
|
|
|
|
|
|
|
if params.new_name.is_empty() {
|
2018-10-31 23:41:43 +03:00
|
|
|
return Err(LspError::new(
|
|
|
|
ErrorCode::InvalidParams as i32,
|
|
|
|
"New Name cannot be empty".into(),
|
|
|
|
)
|
|
|
|
.into());
|
2018-10-18 17:56:22 -04:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let optional_change = snap.analysis().rename(position, &*params.new_name)?;
|
2020-05-10 13:55:24 +02:00
|
|
|
let source_change = match optional_change {
|
2019-01-18 16:32:36 +08:00
|
|
|
None => return Ok(None),
|
2019-09-05 21:36:40 +03:00
|
|
|
Some(it) => it.info,
|
2019-01-18 16:32:36 +08:00
|
|
|
};
|
2020-06-03 11:16:08 +02:00
|
|
|
let workspace_edit = to_proto::workspace_edit(&snap, source_change)?;
|
2020-05-18 00:11:40 +02:00
|
|
|
Ok(Some(workspace_edit))
|
2018-10-18 17:56:22 -04:00
|
|
|
}
|
|
|
|
|
2018-10-18 13:40:12 -04:00
|
|
|
pub fn handle_references(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::ReferenceParams,
|
2018-10-18 13:40:12 -04:00
|
|
|
) -> Result<Option<Vec<Location>>> {
|
2019-10-23 18:26:43 +03:00
|
|
|
let _p = profile("handle_references");
|
2020-06-03 11:16:08 +02:00
|
|
|
let position = from_proto::file_position(&snap, params.text_document_position)?;
|
2019-10-12 18:47:17 +03:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let refs = match snap.analysis().find_all_refs(position, None)? {
|
2019-02-17 13:38:32 +02:00
|
|
|
None => return Ok(None),
|
|
|
|
Some(refs) => refs,
|
|
|
|
};
|
2018-10-18 13:40:12 -04:00
|
|
|
|
2019-02-17 13:38:32 +02:00
|
|
|
let locations = if params.context.include_declaration {
|
|
|
|
refs.into_iter()
|
2020-06-03 11:16:08 +02:00
|
|
|
.filter_map(|reference| to_proto::location(&snap, reference.file_range).ok())
|
2019-02-17 13:38:32 +02:00
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
// Only iterate over the references if include_declaration was false
|
|
|
|
refs.references()
|
|
|
|
.iter()
|
2020-06-03 11:16:08 +02:00
|
|
|
.filter_map(|reference| to_proto::location(&snap, reference.file_range).ok())
|
2019-02-17 13:38:32 +02:00
|
|
|
.collect()
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Some(locations))
|
2018-10-18 13:40:12 -04:00
|
|
|
}
|
|
|
|
|
2018-12-29 20:09:42 +01:00
|
|
|
pub fn handle_formatting(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2018-12-29 20:09:42 +01:00
|
|
|
params: DocumentFormattingParams,
|
2020-05-21 19:50:23 +02:00
|
|
|
) -> Result<Option<Vec<lsp_types::TextEdit>>> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_formatting");
|
2020-06-03 11:16:08 +02:00
|
|
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
|
|
|
let file = snap.analysis().file_text(file_id)?;
|
|
|
|
let crate_ids = snap.analysis().crate_for(file_id)?;
|
2018-12-29 20:09:42 +01:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let file_line_index = snap.analysis().file_line_index(file_id)?;
|
2020-05-10 13:55:24 +02:00
|
|
|
let end_position = to_proto::position(&file_line_index, TextSize::of(file.as_str()));
|
2018-12-29 20:09:42 +01:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let mut rustfmt = match &snap.config.rustfmt {
|
2020-04-01 13:31:12 +02:00
|
|
|
RustfmtConfig::Rustfmt { extra_args } => {
|
|
|
|
let mut cmd = process::Command::new("rustfmt");
|
|
|
|
cmd.args(extra_args);
|
|
|
|
if let Some(&crate_id) = crate_ids.first() {
|
|
|
|
// Assume all crates are in the same edition
|
2020-06-03 11:16:08 +02:00
|
|
|
let edition = snap.analysis().crate_edition(crate_id)?;
|
2020-04-01 13:31:12 +02:00
|
|
|
cmd.arg("--edition");
|
|
|
|
cmd.arg(edition.to_string());
|
|
|
|
}
|
|
|
|
cmd
|
|
|
|
}
|
|
|
|
RustfmtConfig::CustomCommand { command, args } => {
|
|
|
|
let mut cmd = process::Command::new(command);
|
|
|
|
cmd.args(args);
|
|
|
|
cmd
|
|
|
|
}
|
|
|
|
};
|
2019-01-26 20:16:15 +00:00
|
|
|
|
|
|
|
if let Ok(path) = params.text_document.uri.to_file_path() {
|
|
|
|
if let Some(parent) = path.parent() {
|
|
|
|
rustfmt.current_dir(parent);
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 13:34:44 +01:00
|
|
|
let mut rustfmt = rustfmt.stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?;
|
2018-12-29 20:09:42 +01:00
|
|
|
|
|
|
|
rustfmt.stdin.as_mut().unwrap().write_all(file.as_bytes())?;
|
|
|
|
|
|
|
|
let output = rustfmt.wait_with_output()?;
|
|
|
|
let captured_stdout = String::from_utf8(output.stdout)?;
|
2019-06-27 07:52:19 +10:00
|
|
|
|
2018-12-29 20:09:42 +01:00
|
|
|
if !output.status.success() {
|
2019-06-27 07:52:19 +10:00
|
|
|
match output.status.code() {
|
|
|
|
Some(1) => {
|
|
|
|
// While `rustfmt` doesn't have a specific exit code for parse errors this is the
|
|
|
|
// likely cause exiting with 1. Most Language Servers swallow parse errors on
|
|
|
|
// formatting because otherwise an error is surfaced to the user on top of the
|
|
|
|
// syntax error diagnostics they're already receiving. This is especially jarring
|
|
|
|
// if they have format on save enabled.
|
|
|
|
log::info!("rustfmt exited with status 1, assuming parse error and ignoring");
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Something else happened - e.g. `rustfmt` is missing or caught a signal
|
|
|
|
return Err(LspError::new(
|
|
|
|
-32900,
|
|
|
|
format!(
|
|
|
|
r#"rustfmt exited with:
|
|
|
|
Status: {}
|
|
|
|
stdout: {}"#,
|
|
|
|
output.status, captured_stdout,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 20:09:42 +01:00
|
|
|
}
|
|
|
|
|
2020-05-21 19:50:23 +02:00
|
|
|
Ok(Some(vec![lsp_types::TextEdit {
|
2018-12-29 20:09:42 +01:00
|
|
|
range: Range::new(Position::new(0, 0), end_position),
|
|
|
|
new_text: captured_stdout,
|
|
|
|
}]))
|
|
|
|
}
|
|
|
|
|
2020-06-02 22:21:48 +02:00
|
|
|
fn handle_fixes(
|
2020-06-03 19:26:01 +02:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-06-02 22:21:48 +02:00
|
|
|
params: &lsp_types::CodeActionParams,
|
|
|
|
res: &mut Vec<lsp_ext::CodeAction>,
|
|
|
|
) -> Result<()> {
|
2020-06-03 11:16:08 +02:00
|
|
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
2020-05-10 13:55:24 +02:00
|
|
|
let range = from_proto::text_range(&line_index, params.range);
|
2020-06-03 11:16:08 +02:00
|
|
|
let diagnostics = snap.analysis().diagnostics(file_id)?;
|
2019-02-24 13:53:35 +03:00
|
|
|
|
|
|
|
let fixes_from_diagnostics = diagnostics
|
2018-10-15 17:44:23 -04:00
|
|
|
.into_iter()
|
2018-08-29 18:03:14 +03:00
|
|
|
.filter_map(|d| Some((d.range, d.fix?)))
|
2020-04-24 23:40:41 +02:00
|
|
|
.filter(|(diag_range, _fix)| diag_range.intersect(range).is_some())
|
2018-08-29 18:03:14 +03:00
|
|
|
.map(|(_range, fix)| fix);
|
2020-05-22 18:03:08 +02:00
|
|
|
for fix in fixes_from_diagnostics {
|
|
|
|
let title = fix.label;
|
2020-06-03 11:16:08 +02:00
|
|
|
let edit = to_proto::snippet_workspace_edit(&snap, fix.source_change)?;
|
2020-06-02 22:21:48 +02:00
|
|
|
let action = lsp_ext::CodeAction {
|
|
|
|
title,
|
|
|
|
id: None,
|
|
|
|
group: None,
|
2020-05-17 21:03:40 -04:00
|
|
|
kind: Some(lsp_types::code_action_kind::QUICKFIX.into()),
|
2020-06-02 22:21:48 +02:00
|
|
|
edit: Some(edit),
|
|
|
|
command: None,
|
|
|
|
};
|
2020-05-18 00:11:40 +02:00
|
|
|
res.push(action);
|
2019-02-24 13:53:35 +03:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
for fix in snap.check_fixes.get(&file_id).into_iter().flatten() {
|
2020-05-10 13:55:24 +02:00
|
|
|
let fix_range = from_proto::text_range(&line_index, fix.range);
|
2020-04-24 23:40:41 +02:00
|
|
|
if fix_range.intersect(range).is_none() {
|
2019-12-25 12:21:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-01-31 19:23:25 +01:00
|
|
|
res.push(fix.action.clone());
|
2019-12-25 12:21:38 +01:00
|
|
|
}
|
2020-06-02 22:21:48 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-12-25 12:21:38 +01:00
|
|
|
|
2020-06-02 22:21:48 +02:00
|
|
|
pub fn handle_code_action(
|
2020-06-03 19:26:01 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-06-02 22:21:48 +02:00
|
|
|
params: lsp_types::CodeActionParams,
|
|
|
|
) -> Result<Option<Vec<lsp_ext::CodeAction>>> {
|
|
|
|
let _p = profile("handle_code_action");
|
|
|
|
// We intentionally don't support command-based actions, as those either
|
|
|
|
// requires custom client-code anyway, or requires server-initiated edits.
|
|
|
|
// Server initiated edits break causality, so we avoid those as well.
|
2020-06-03 19:26:01 +02:00
|
|
|
if !snap.config.client_caps.code_action_literals {
|
2020-06-02 22:21:48 +02:00
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2020-06-03 19:26:01 +02:00
|
|
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
2020-06-02 22:21:48 +02:00
|
|
|
let range = from_proto::text_range(&line_index, params.range);
|
|
|
|
let frange = FileRange { file_id, range };
|
|
|
|
let mut res: Vec<lsp_ext::CodeAction> = Vec::new();
|
|
|
|
|
2020-06-03 19:26:01 +02:00
|
|
|
handle_fixes(&snap, ¶ms, &mut res)?;
|
2020-06-02 22:21:48 +02:00
|
|
|
|
2020-06-03 19:26:01 +02:00
|
|
|
if snap.config.client_caps.resolve_code_action {
|
|
|
|
for (index, assist) in
|
|
|
|
snap.analysis().unresolved_assists(&snap.config.assist, frange)?.into_iter().enumerate()
|
2020-06-02 22:21:48 +02:00
|
|
|
{
|
2020-06-03 19:26:01 +02:00
|
|
|
res.push(to_proto::unresolved_code_action(&snap, assist, index)?);
|
2020-06-02 22:21:48 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-03 19:26:01 +02:00
|
|
|
for assist in snap.analysis().resolved_assists(&snap.config.assist, frange)?.into_iter() {
|
|
|
|
res.push(to_proto::resolved_code_action(&snap, assist)?);
|
2020-06-02 22:21:48 +02:00
|
|
|
}
|
2020-02-26 13:24:00 +03:00
|
|
|
}
|
2020-06-02 22:21:48 +02:00
|
|
|
|
2019-07-04 14:57:14 -04:00
|
|
|
Ok(Some(res))
|
2018-08-11 14:44:12 +03:00
|
|
|
}
|
|
|
|
|
2020-06-02 22:21:48 +02:00
|
|
|
pub fn handle_resolve_code_action(
|
2020-06-03 19:26:01 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-06-02 22:21:48 +02:00
|
|
|
params: lsp_ext::ResolveCodeActionParams,
|
|
|
|
) -> Result<Option<lsp_ext::SnippetWorkspaceEdit>> {
|
|
|
|
let _p = profile("handle_resolve_code_action");
|
2020-06-03 19:26:01 +02:00
|
|
|
let file_id = from_proto::file_id(&snap, ¶ms.code_action_params.text_document.uri)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
2020-06-02 22:21:48 +02:00
|
|
|
let range = from_proto::text_range(&line_index, params.code_action_params.range);
|
|
|
|
let frange = FileRange { file_id, range };
|
|
|
|
|
2020-06-03 19:26:01 +02:00
|
|
|
let assists = snap.analysis().resolved_assists(&snap.config.assist, frange)?;
|
2020-06-03 19:33:57 +02:00
|
|
|
let (id_string, index) = split1(¶ms.id, ':').unwrap();
|
|
|
|
let index = index.parse::<usize>().unwrap();
|
2020-06-03 18:39:01 +02:00
|
|
|
let assist = &assists[index];
|
2020-06-03 19:33:57 +02:00
|
|
|
assert!(assist.assist.id.0 == id_string);
|
2020-06-03 19:26:01 +02:00
|
|
|
Ok(to_proto::resolved_code_action(&snap, assist.clone())?.edit)
|
2020-06-02 22:21:48 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 15:16:55 -05:00
|
|
|
pub fn handle_code_lens(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::CodeLensParams,
|
2019-01-11 15:16:55 -05:00
|
|
|
) -> Result<Option<Vec<CodeLens>>> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_code_lens");
|
2019-01-11 15:16:55 -05:00
|
|
|
let mut lenses: Vec<CodeLens> = Default::default();
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
if snap.config.lens.none() {
|
2020-05-17 19:51:44 +03:00
|
|
|
// early return before any db query!
|
|
|
|
return Ok(Some(lenses));
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
|
|
|
let cargo_spec = CargoTargetSpec::for_file(&snap, file_id)?;
|
2020-05-17 19:51:44 +03:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
if snap.config.lens.runnable() {
|
2020-05-17 19:51:44 +03:00
|
|
|
// Gather runnables
|
2020-06-03 11:16:08 +02:00
|
|
|
for runnable in snap.analysis().runnables(file_id)? {
|
2020-06-06 22:11:17 +03:00
|
|
|
if should_skip_target(&runnable, cargo_spec.as_ref()) {
|
2020-06-06 12:00:46 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-05-17 19:51:44 +03:00
|
|
|
|
2020-06-06 12:00:46 +03:00
|
|
|
let action = runnable.action();
|
2020-06-02 17:22:23 +02:00
|
|
|
let range = to_proto::range(&line_index, runnable.nav.range());
|
2020-06-08 13:56:31 +03:00
|
|
|
let r = to_proto::runnable(&snap, file_id, runnable)?;
|
2020-06-03 11:16:08 +02:00
|
|
|
if snap.config.lens.run {
|
2020-05-17 19:51:44 +03:00
|
|
|
let lens = CodeLens {
|
2020-06-02 17:22:23 +02:00
|
|
|
range,
|
2020-06-06 12:00:46 +03:00
|
|
|
command: Some(run_single_command(&r, action.run_title)),
|
2020-05-17 19:51:44 +03:00
|
|
|
data: None,
|
|
|
|
};
|
|
|
|
lenses.push(lens);
|
2020-05-07 16:52:14 +02:00
|
|
|
}
|
2020-03-12 22:31:47 +01:00
|
|
|
|
2020-06-06 12:00:46 +03:00
|
|
|
if action.debugee && snap.config.lens.debug {
|
2020-06-06 14:30:29 +03:00
|
|
|
let debug_lens =
|
2020-06-08 13:56:31 +03:00
|
|
|
CodeLens { range, command: Some(debug_single_command(&r)), data: None };
|
2020-05-17 19:51:44 +03:00
|
|
|
lenses.push(debug_lens);
|
|
|
|
}
|
2020-03-12 22:31:47 +01:00
|
|
|
}
|
2019-01-11 15:16:55 -05:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
if snap.config.lens.impementations {
|
2020-05-17 19:51:44 +03:00
|
|
|
// Handle impls
|
|
|
|
lenses.extend(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap.analysis()
|
2020-05-17 19:51:44 +03:00
|
|
|
.file_structure(file_id)?
|
|
|
|
.into_iter()
|
|
|
|
.filter(|it| match it.kind {
|
|
|
|
SyntaxKind::TRAIT_DEF | SyntaxKind::STRUCT_DEF | SyntaxKind::ENUM_DEF => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
.map(|it| {
|
|
|
|
let range = to_proto::range(&line_index, it.node_range);
|
|
|
|
let pos = range.start;
|
|
|
|
let lens_params = lsp_types::request::GotoImplementationParams {
|
|
|
|
text_document_position_params: lsp_types::TextDocumentPositionParams::new(
|
|
|
|
params.text_document.clone(),
|
|
|
|
pos,
|
|
|
|
),
|
|
|
|
work_done_progress_params: Default::default(),
|
|
|
|
partial_result_params: Default::default(),
|
|
|
|
};
|
|
|
|
CodeLens {
|
|
|
|
range,
|
|
|
|
command: None,
|
|
|
|
data: Some(to_value(CodeLensResolveData::Impls(lens_params)).unwrap()),
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2019-02-06 15:50:26 -05:00
|
|
|
Ok(Some(lenses))
|
2019-01-11 15:16:55 -05:00
|
|
|
}
|
|
|
|
|
2019-02-01 08:44:23 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
enum CodeLensResolveData {
|
2020-05-10 19:24:02 +02:00
|
|
|
Impls(lsp_types::request::GotoImplementationParams),
|
2019-02-01 08:44:23 -05:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
pub fn handle_code_lens_resolve(
|
|
|
|
snap: GlobalStateSnapshot,
|
|
|
|
code_lens: CodeLens,
|
|
|
|
) -> Result<CodeLens> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_code_lens_resolve");
|
2019-02-01 08:44:23 -05:00
|
|
|
let data = code_lens.data.unwrap();
|
2020-02-11 09:46:45 +01:00
|
|
|
let resolve = from_json::<Option<CodeLensResolveData>>("CodeLensResolveData", data)?;
|
2019-02-01 08:44:23 -05:00
|
|
|
match resolve {
|
|
|
|
Some(CodeLensResolveData::Impls(lens_params)) => {
|
|
|
|
let locations: Vec<Location> =
|
2020-06-03 11:16:08 +02:00
|
|
|
match handle_goto_implementation(snap, lens_params.clone())? {
|
2020-05-10 19:24:02 +02:00
|
|
|
Some(lsp_types::GotoDefinitionResponse::Scalar(loc)) => vec![loc],
|
|
|
|
Some(lsp_types::GotoDefinitionResponse::Array(locs)) => locs,
|
|
|
|
Some(lsp_types::GotoDefinitionResponse::Link(links)) => links
|
2019-02-01 08:44:23 -05:00
|
|
|
.into_iter()
|
|
|
|
.map(|link| Location::new(link.target_uri, link.target_selection_range))
|
|
|
|
.collect(),
|
|
|
|
_ => vec![],
|
|
|
|
};
|
|
|
|
|
2020-06-03 14:15:54 +03:00
|
|
|
let title = implementation_title(locations.len());
|
|
|
|
let cmd = show_references_command(
|
2019-02-04 16:34:02 -05:00
|
|
|
title,
|
2020-06-03 14:15:54 +03:00
|
|
|
&lens_params.text_document_position_params.text_document.uri,
|
|
|
|
code_lens.range.start,
|
|
|
|
locations,
|
|
|
|
);
|
2019-02-08 14:49:43 +03:00
|
|
|
Ok(CodeLens { range: code_lens.range, command: Some(cmd), data: None })
|
2019-02-01 08:44:23 -05:00
|
|
|
}
|
2019-02-06 15:50:26 -05:00
|
|
|
None => Ok(CodeLens {
|
|
|
|
range: code_lens.range,
|
2019-02-08 14:49:43 +03:00
|
|
|
command: Some(Command { title: "Error".into(), ..Default::default() }),
|
2019-02-06 15:50:26 -05:00
|
|
|
data: None,
|
|
|
|
}),
|
2019-02-01 08:44:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 11:08:44 +00:00
|
|
|
pub fn handle_document_highlight(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:24:02 +02:00
|
|
|
params: lsp_types::DocumentHighlightParams,
|
2018-12-31 11:08:44 +00:00
|
|
|
) -> Result<Option<Vec<DocumentHighlight>>> {
|
2019-10-23 18:26:43 +03:00
|
|
|
let _p = profile("handle_document_highlight");
|
2020-06-03 11:16:08 +02:00
|
|
|
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(position.file_id)?;
|
2018-12-31 11:08:44 +00:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let refs = match snap
|
2020-05-10 13:55:24 +02:00
|
|
|
.analysis()
|
|
|
|
.find_all_refs(position, Some(SearchScope::single_file(position.file_id)))?
|
|
|
|
{
|
2019-02-17 13:38:32 +02:00
|
|
|
None => return Ok(None),
|
|
|
|
Some(refs) => refs,
|
|
|
|
};
|
2018-12-31 11:08:44 +00:00
|
|
|
|
2020-05-10 13:55:24 +02:00
|
|
|
let res = refs
|
|
|
|
.into_iter()
|
|
|
|
.filter(|reference| reference.file_range.file_id == position.file_id)
|
|
|
|
.map(|reference| DocumentHighlight {
|
|
|
|
range: to_proto::range(&line_index, reference.file_range.range),
|
|
|
|
kind: reference.access.map(to_proto::document_highlight_kind),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
Ok(Some(res))
|
2018-12-31 11:08:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 19:25:37 +02:00
|
|
|
pub fn handle_ssr(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-05-10 19:25:37 +02:00
|
|
|
params: lsp_ext::SsrParams,
|
2020-05-22 00:28:49 +02:00
|
|
|
) -> Result<lsp_types::WorkspaceEdit> {
|
2020-02-10 22:45:38 +00:00
|
|
|
let _p = profile("handle_ssr");
|
2020-05-10 13:55:24 +02:00
|
|
|
let source_change =
|
2020-06-03 11:16:08 +02:00
|
|
|
snap.analysis().structural_search_replace(¶ms.query, params.parse_only)??;
|
|
|
|
to_proto::workspace_edit(&snap, source_change)
|
2020-02-10 22:45:38 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
pub fn publish_diagnostics(snap: &GlobalStateSnapshot, file_id: FileId) -> Result<DiagnosticTask> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("publish_diagnostics");
|
2020-06-03 11:16:08 +02:00
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
|
|
|
let diagnostics: Vec<Diagnostic> = snap
|
2018-10-15 17:44:23 -04:00
|
|
|
.analysis()
|
2018-10-20 22:02:41 +03:00
|
|
|
.diagnostics(file_id)?
|
2018-08-10 23:30:11 +03:00
|
|
|
.into_iter()
|
2018-08-29 18:03:14 +03:00
|
|
|
.map(|d| Diagnostic {
|
2020-05-10 13:55:24 +02:00
|
|
|
range: to_proto::range(&line_index, d.range),
|
|
|
|
severity: Some(to_proto::diagnostic_severity(d.severity)),
|
2018-08-10 23:30:11 +03:00
|
|
|
code: None,
|
2018-09-18 17:40:33 -04:00
|
|
|
source: Some("rust-analyzer".to_string()),
|
2018-08-29 18:03:14 +03:00
|
|
|
message: d.message,
|
2018-08-10 23:30:11 +03:00
|
|
|
related_information: None,
|
2019-12-11 18:34:01 +01:00
|
|
|
tags: None,
|
2018-10-15 17:44:23 -04:00
|
|
|
})
|
|
|
|
.collect();
|
2020-01-31 19:23:25 +01:00
|
|
|
Ok(DiagnosticTask::SetNative(file_id, diagnostics))
|
2018-08-10 23:30:11 +03:00
|
|
|
}
|
|
|
|
|
2019-07-22 21:52:47 +03:00
|
|
|
pub fn handle_inlay_hints(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2019-07-22 21:52:47 +03:00
|
|
|
params: InlayHintsParams,
|
|
|
|
) -> Result<Vec<InlayHint>> {
|
2019-10-29 16:08:36 -04:00
|
|
|
let _p = profile("handle_inlay_hints");
|
2020-06-03 11:16:08 +02:00
|
|
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
|
|
|
let analysis = snap.analysis();
|
2019-07-25 20:22:41 +03:00
|
|
|
let line_index = analysis.file_line_index(file_id)?;
|
2019-07-22 21:52:47 +03:00
|
|
|
Ok(analysis
|
2020-06-03 11:16:08 +02:00
|
|
|
.inlay_hints(file_id, &snap.config.inlay_hints)?
|
2019-07-22 21:52:47 +03:00
|
|
|
.into_iter()
|
2020-05-10 13:55:24 +02:00
|
|
|
.map(|it| to_proto::inlay_int(&line_index, it))
|
2019-07-22 21:52:47 +03:00
|
|
|
.collect())
|
|
|
|
}
|
2019-12-30 09:12:06 -05:00
|
|
|
|
|
|
|
pub fn handle_call_hierarchy_prepare(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2019-12-30 09:12:06 -05:00
|
|
|
params: CallHierarchyPrepareParams,
|
|
|
|
) -> Result<Option<Vec<CallHierarchyItem>>> {
|
|
|
|
let _p = profile("handle_call_hierarchy_prepare");
|
2020-06-03 11:16:08 +02:00
|
|
|
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
|
2019-12-30 09:12:06 -05:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let nav_info = match snap.analysis().call_hierarchy(position)? {
|
2019-12-30 09:12:06 -05:00
|
|
|
None => return Ok(None),
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
|
|
|
|
2020-05-10 13:55:24 +02:00
|
|
|
let RangeInfo { range: _, info: navs } = nav_info;
|
2019-12-30 09:12:06 -05:00
|
|
|
let res = navs
|
|
|
|
.into_iter()
|
|
|
|
.filter(|it| it.kind() == SyntaxKind::FN_DEF)
|
2020-06-03 11:16:08 +02:00
|
|
|
.map(|it| to_proto::call_hierarchy_item(&snap, it))
|
2020-05-10 13:55:24 +02:00
|
|
|
.collect::<Result<Vec<_>>>()?;
|
2019-12-30 09:12:06 -05:00
|
|
|
|
|
|
|
Ok(Some(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_call_hierarchy_incoming(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2019-12-30 09:12:06 -05:00
|
|
|
params: CallHierarchyIncomingCallsParams,
|
|
|
|
) -> Result<Option<Vec<CallHierarchyIncomingCall>>> {
|
|
|
|
let _p = profile("handle_call_hierarchy_incoming");
|
|
|
|
let item = params.item;
|
|
|
|
|
|
|
|
let doc = TextDocumentIdentifier::new(item.uri);
|
2020-06-03 11:16:08 +02:00
|
|
|
let frange = from_proto::file_range(&snap, doc, item.range)?;
|
2019-12-30 09:12:06 -05:00
|
|
|
let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let call_items = match snap.analysis().incoming_calls(fpos)? {
|
2019-12-30 09:12:06 -05:00
|
|
|
None => return Ok(None),
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut res = vec![];
|
|
|
|
|
|
|
|
for call_item in call_items.into_iter() {
|
|
|
|
let file_id = call_item.target.file_id();
|
2020-06-03 11:16:08 +02:00
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
|
|
|
let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
|
2019-12-30 09:12:06 -05:00
|
|
|
res.push(CallHierarchyIncomingCall {
|
|
|
|
from: item,
|
2020-05-10 13:55:24 +02:00
|
|
|
from_ranges: call_item
|
|
|
|
.ranges
|
|
|
|
.into_iter()
|
|
|
|
.map(|it| to_proto::range(&line_index, it))
|
|
|
|
.collect(),
|
2019-12-30 09:12:06 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Some(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_call_hierarchy_outgoing(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2019-12-30 09:12:06 -05:00
|
|
|
params: CallHierarchyOutgoingCallsParams,
|
|
|
|
) -> Result<Option<Vec<CallHierarchyOutgoingCall>>> {
|
|
|
|
let _p = profile("handle_call_hierarchy_outgoing");
|
|
|
|
let item = params.item;
|
|
|
|
|
|
|
|
let doc = TextDocumentIdentifier::new(item.uri);
|
2020-06-03 11:16:08 +02:00
|
|
|
let frange = from_proto::file_range(&snap, doc, item.range)?;
|
2019-12-30 09:12:06 -05:00
|
|
|
let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let call_items = match snap.analysis().outgoing_calls(fpos)? {
|
2019-12-30 09:12:06 -05:00
|
|
|
None => return Ok(None),
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut res = vec![];
|
|
|
|
|
|
|
|
for call_item in call_items.into_iter() {
|
|
|
|
let file_id = call_item.target.file_id();
|
2020-06-03 11:16:08 +02:00
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
|
|
|
let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
|
2019-12-30 09:12:06 -05:00
|
|
|
res.push(CallHierarchyOutgoingCall {
|
|
|
|
to: item,
|
2020-05-10 13:55:24 +02:00
|
|
|
from_ranges: call_item
|
|
|
|
.ranges
|
|
|
|
.into_iter()
|
|
|
|
.map(|it| to_proto::range(&line_index, it))
|
|
|
|
.collect(),
|
2019-12-30 09:12:06 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Some(res))
|
|
|
|
}
|
2020-02-14 17:56:28 -05:00
|
|
|
|
|
|
|
pub fn handle_semantic_tokens(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-02-14 17:56:28 -05:00
|
|
|
params: SemanticTokensParams,
|
|
|
|
) -> Result<Option<SemanticTokensResult>> {
|
|
|
|
let _p = profile("handle_semantic_tokens");
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
|
|
|
let text = snap.analysis().file_text(file_id)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
2020-02-14 17:56:28 -05:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let highlights = snap.analysis().highlight(file_id)?;
|
2020-05-10 19:09:22 +02:00
|
|
|
let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights);
|
|
|
|
Ok(Some(semantic_tokens.into()))
|
2020-02-14 17:56:28 -05:00
|
|
|
}
|
2020-02-25 08:38:50 -05:00
|
|
|
|
|
|
|
pub fn handle_semantic_tokens_range(
|
2020-06-03 11:16:08 +02:00
|
|
|
snap: GlobalStateSnapshot,
|
2020-02-25 08:38:50 -05:00
|
|
|
params: SemanticTokensRangeParams,
|
|
|
|
) -> Result<Option<SemanticTokensRangeResult>> {
|
|
|
|
let _p = profile("handle_semantic_tokens_range");
|
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let frange = from_proto::file_range(&snap, params.text_document, params.range)?;
|
|
|
|
let text = snap.analysis().file_text(frange.file_id)?;
|
|
|
|
let line_index = snap.analysis().file_line_index(frange.file_id)?;
|
2020-02-25 08:38:50 -05:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
let highlights = snap.analysis().highlight_range(frange)?;
|
2020-05-10 19:09:22 +02:00
|
|
|
let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights);
|
|
|
|
Ok(Some(semantic_tokens.into()))
|
2020-02-25 08:38:50 -05:00
|
|
|
}
|
2020-06-03 14:15:54 +03:00
|
|
|
|
|
|
|
fn implementation_title(count: usize) -> String {
|
|
|
|
if count == 1 {
|
|
|
|
"1 implementation".into()
|
|
|
|
} else {
|
|
|
|
format!("{} implementations", count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show_references_command(
|
|
|
|
title: String,
|
|
|
|
uri: &lsp_types::Url,
|
|
|
|
position: lsp_types::Position,
|
|
|
|
locations: Vec<lsp_types::Location>,
|
|
|
|
) -> Command {
|
|
|
|
// We cannot use the 'editor.action.showReferences' command directly
|
|
|
|
// because that command requires vscode types which we convert in the handler
|
|
|
|
// on the client side.
|
|
|
|
|
|
|
|
Command {
|
|
|
|
title,
|
|
|
|
command: "rust-analyzer.showReferences".into(),
|
|
|
|
arguments: Some(vec![
|
|
|
|
to_value(uri).unwrap(),
|
|
|
|
to_value(position).unwrap(),
|
|
|
|
to_value(locations).unwrap(),
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 12:00:46 +03:00
|
|
|
fn run_single_command(runnable: &lsp_ext::Runnable, title: &str) -> Command {
|
|
|
|
Command {
|
|
|
|
title: title.to_string(),
|
|
|
|
command: "rust-analyzer.runSingle".into(),
|
|
|
|
arguments: Some(vec![to_value(runnable).unwrap()]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 13:56:31 +03:00
|
|
|
fn debug_single_command(runnable: &lsp_ext::Runnable) -> Command {
|
2020-06-06 12:00:46 +03:00
|
|
|
Command {
|
|
|
|
title: "Debug".into(),
|
|
|
|
command: "rust-analyzer.debugSingle".into(),
|
|
|
|
arguments: Some(vec![to_value(runnable).unwrap()]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 21:24:36 +03:00
|
|
|
fn goto_location_command(snap: &GlobalStateSnapshot, nav: &NavigationTarget) -> Option<Command> {
|
|
|
|
let value = if snap.config.client_caps.location_link {
|
|
|
|
let link = to_proto::location_link(snap, None, nav.clone()).ok()?;
|
|
|
|
to_value(link).ok()?
|
|
|
|
} else {
|
|
|
|
let range = FileRange { file_id: nav.file_id(), range: nav.range() };
|
|
|
|
let location = to_proto::location(snap, range).ok()?;
|
|
|
|
to_value(location).ok()?
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(Command {
|
|
|
|
title: nav.name().to_string(),
|
|
|
|
command: "rust-analyzer.gotoLocation".into(),
|
|
|
|
arguments: Some(vec![value]),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-03 14:15:54 +03:00
|
|
|
fn to_command_link(command: Command, tooltip: String) -> lsp_ext::CommandLink {
|
2020-06-03 17:35:26 +03:00
|
|
|
lsp_ext::CommandLink { tooltip: Some(tooltip), command }
|
2020-06-03 14:15:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn show_impl_command_link(
|
2020-06-03 15:13:26 +03:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-06-03 14:15:54 +03:00
|
|
|
position: &FilePosition,
|
|
|
|
) -> Option<lsp_ext::CommandLinkGroup> {
|
2020-06-03 15:13:26 +03:00
|
|
|
if snap.config.hover.implementations {
|
|
|
|
if let Some(nav_data) = snap.analysis().goto_implementation(*position).unwrap_or(None) {
|
2020-06-13 11:00:06 +02:00
|
|
|
let uri = to_proto::url(snap, position.file_id);
|
2020-06-03 15:13:26 +03:00
|
|
|
let line_index = snap.analysis().file_line_index(position.file_id).ok()?;
|
2020-06-03 14:15:54 +03:00
|
|
|
let position = to_proto::position(&line_index, position.offset);
|
|
|
|
let locations: Vec<_> = nav_data
|
|
|
|
.info
|
|
|
|
.iter()
|
2020-06-03 15:13:26 +03:00
|
|
|
.filter_map(|it| to_proto::location(snap, it.file_range()).ok())
|
2020-06-03 14:15:54 +03:00
|
|
|
.collect();
|
|
|
|
let title = implementation_title(locations.len());
|
|
|
|
let command = show_references_command(title, &uri, position, locations);
|
|
|
|
|
|
|
|
return Some(lsp_ext::CommandLinkGroup {
|
|
|
|
commands: vec![to_command_link(command, "Go to implementations".into())],
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-06-10 21:24:36 +03:00
|
|
|
fn runnable_action_links(
|
2020-06-06 14:30:29 +03:00
|
|
|
snap: &GlobalStateSnapshot,
|
|
|
|
file_id: FileId,
|
2020-06-08 13:56:31 +03:00
|
|
|
runnable: Runnable,
|
2020-06-06 14:30:29 +03:00
|
|
|
) -> Option<lsp_ext::CommandLinkGroup> {
|
2020-06-06 22:11:17 +03:00
|
|
|
let cargo_spec = CargoTargetSpec::for_file(&snap, file_id).ok()?;
|
2020-06-10 14:23:06 +03:00
|
|
|
if !snap.config.hover.runnable() || should_skip_target(&runnable, cargo_spec.as_ref()) {
|
2020-06-06 22:11:17 +03:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-06-08 13:56:31 +03:00
|
|
|
let action: &'static _ = runnable.action();
|
2020-06-06 14:30:29 +03:00
|
|
|
to_proto::runnable(snap, file_id, runnable).ok().map(|r| {
|
|
|
|
let mut group = lsp_ext::CommandLinkGroup::default();
|
|
|
|
|
|
|
|
if snap.config.hover.run {
|
|
|
|
let run_command = run_single_command(&r, action.run_title);
|
|
|
|
group.commands.push(to_command_link(run_command, r.label.clone()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if snap.config.hover.debug {
|
2020-06-08 13:56:31 +03:00
|
|
|
let dbg_command = debug_single_command(&r);
|
|
|
|
group.commands.push(to_command_link(dbg_command, r.label));
|
2020-06-06 14:30:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
group
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-10 21:24:36 +03:00
|
|
|
fn goto_type_action_links(
|
|
|
|
snap: &GlobalStateSnapshot,
|
|
|
|
nav_targets: &[HoverGotoTypeData],
|
|
|
|
) -> Option<lsp_ext::CommandLinkGroup> {
|
|
|
|
if !snap.config.hover.goto_type_def || nav_targets.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(lsp_ext::CommandLinkGroup {
|
|
|
|
title: Some("Go to ".into()),
|
|
|
|
commands: nav_targets
|
|
|
|
.iter()
|
|
|
|
.filter_map(|it| {
|
|
|
|
goto_location_command(snap, &it.nav)
|
|
|
|
.map(|cmd| to_command_link(cmd, it.mod_path.clone()))
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-03 14:15:54 +03:00
|
|
|
fn prepare_hover_actions(
|
2020-06-03 15:13:26 +03:00
|
|
|
snap: &GlobalStateSnapshot,
|
2020-06-06 14:30:29 +03:00
|
|
|
file_id: FileId,
|
2020-06-03 14:15:54 +03:00
|
|
|
actions: &[HoverAction],
|
|
|
|
) -> Vec<lsp_ext::CommandLinkGroup> {
|
2020-06-03 15:13:26 +03:00
|
|
|
if snap.config.hover.none() || !snap.config.client_caps.hover_actions {
|
2020-06-03 14:15:54 +03:00
|
|
|
return Vec::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
actions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|it| match it {
|
2020-06-03 15:13:26 +03:00
|
|
|
HoverAction::Implementaion(position) => show_impl_command_link(snap, position),
|
2020-06-10 21:24:36 +03:00
|
|
|
HoverAction::Runnable(r) => runnable_action_links(snap, file_id, r.clone()),
|
|
|
|
HoverAction::GoToType(targets) => goto_type_action_links(snap, targets),
|
2020-06-03 14:15:54 +03:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2020-06-06 12:00:46 +03:00
|
|
|
|
2020-06-06 22:11:17 +03:00
|
|
|
fn should_skip_target(runnable: &Runnable, cargo_spec: Option<&CargoTargetSpec>) -> bool {
|
|
|
|
match runnable.kind {
|
|
|
|
RunnableKind::Bin => {
|
|
|
|
// Do not suggest binary run on other target than binary
|
|
|
|
match &cargo_spec {
|
2020-06-08 13:56:31 +03:00
|
|
|
Some(spec) => spec.target_kind != TargetKind::Bin,
|
2020-06-06 22:11:17 +03:00
|
|
|
None => true,
|
2020-06-06 12:00:46 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-06 22:11:17 +03:00
|
|
|
_ => false,
|
2020-06-06 12:00:46 +03:00
|
|
|
}
|
2020-06-06 14:30:29 +03:00
|
|
|
}
|