mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-26 11:55:04 +00:00
Merge #4717
4717: Implementation of lazy assits r=matklad a=mcrakhman Co-authored-by: Mikhail Rakhmanov <rakhmanov.m@gmail.com>
This commit is contained in:
commit
65a3cc21ed
14 changed files with 220 additions and 95 deletions
|
@ -77,7 +77,7 @@ pub use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use hir::Documentation;
|
pub use hir::Documentation;
|
||||||
pub use ra_assists::{AssistConfig, AssistId};
|
pub use ra_assists::{Assist, AssistConfig, AssistId, ResolvedAssist};
|
||||||
pub use ra_db::{
|
pub use ra_db::{
|
||||||
Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRootId,
|
Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRootId,
|
||||||
};
|
};
|
||||||
|
@ -142,14 +142,6 @@ pub struct AnalysisHost {
|
||||||
db: RootDatabase,
|
db: RootDatabase,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Assist {
|
|
||||||
pub id: AssistId,
|
|
||||||
pub label: String,
|
|
||||||
pub group_label: Option<String>,
|
|
||||||
pub source_change: SourceChange,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AnalysisHost {
|
impl AnalysisHost {
|
||||||
pub fn new(lru_capacity: Option<usize>) -> AnalysisHost {
|
pub fn new(lru_capacity: Option<usize>) -> AnalysisHost {
|
||||||
AnalysisHost { db: RootDatabase::new(lru_capacity) }
|
AnalysisHost { db: RootDatabase::new(lru_capacity) }
|
||||||
|
@ -470,20 +462,23 @@ impl Analysis {
|
||||||
self.with_db(|db| completion::completions(db, config, position).map(Into::into))
|
self.with_db(|db| completion::completions(db, config, position).map(Into::into))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes assists (aka code actions aka intentions) for the given
|
/// Computes resolved assists with source changes for the given position.
|
||||||
|
pub fn resolved_assists(
|
||||||
|
&self,
|
||||||
|
config: &AssistConfig,
|
||||||
|
frange: FileRange,
|
||||||
|
) -> Cancelable<Vec<ResolvedAssist>> {
|
||||||
|
self.with_db(|db| ra_assists::Assist::resolved(db, config, frange))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Computes unresolved assists (aka code actions aka intentions) for the given
|
||||||
/// position.
|
/// position.
|
||||||
pub fn assists(&self, config: &AssistConfig, frange: FileRange) -> Cancelable<Vec<Assist>> {
|
pub fn unresolved_assists(
|
||||||
self.with_db(|db| {
|
&self,
|
||||||
ra_assists::Assist::resolved(db, config, frange)
|
config: &AssistConfig,
|
||||||
.into_iter()
|
frange: FileRange,
|
||||||
.map(|assist| Assist {
|
) -> Cancelable<Vec<Assist>> {
|
||||||
id: assist.assist.id,
|
self.with_db(|db| Assist::unresolved(db, config, frange))
|
||||||
label: assist.assist.label,
|
|
||||||
group_label: assist.assist.group.map(|it| it.0),
|
|
||||||
source_change: assist.source_change,
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes the set of diagnostics for the given file.
|
/// Computes the set of diagnostics for the given file.
|
||||||
|
|
|
@ -123,6 +123,7 @@ pub struct ClientCapsConfig {
|
||||||
pub code_action_literals: bool,
|
pub code_action_literals: bool,
|
||||||
pub work_done_progress: bool,
|
pub work_done_progress: bool,
|
||||||
pub code_action_group: bool,
|
pub code_action_group: bool,
|
||||||
|
pub resolve_code_action: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
|
@ -336,7 +337,11 @@ impl Config {
|
||||||
|
|
||||||
let code_action_group =
|
let code_action_group =
|
||||||
experimental.get("codeActionGroup").and_then(|it| it.as_bool()) == Some(true);
|
experimental.get("codeActionGroup").and_then(|it| it.as_bool()) == Some(true);
|
||||||
self.client_caps.code_action_group = code_action_group
|
self.client_caps.code_action_group = code_action_group;
|
||||||
|
|
||||||
|
let resolve_code_action =
|
||||||
|
experimental.get("resolveCodeAction").and_then(|it| it.as_bool()) == Some(true);
|
||||||
|
self.client_caps.resolve_code_action = resolve_code_action;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,6 +65,7 @@ expression: diag
|
||||||
fixes: [
|
fixes: [
|
||||||
CodeAction {
|
CodeAction {
|
||||||
title: "return the expression directly",
|
title: "return the expression directly",
|
||||||
|
id: None,
|
||||||
group: None,
|
group: None,
|
||||||
kind: Some(
|
kind: Some(
|
||||||
"quickfix",
|
"quickfix",
|
||||||
|
|
|
@ -50,6 +50,7 @@ expression: diag
|
||||||
fixes: [
|
fixes: [
|
||||||
CodeAction {
|
CodeAction {
|
||||||
title: "consider prefixing with an underscore",
|
title: "consider prefixing with an underscore",
|
||||||
|
id: None,
|
||||||
group: None,
|
group: None,
|
||||||
kind: Some(
|
kind: Some(
|
||||||
"quickfix",
|
"quickfix",
|
||||||
|
|
|
@ -145,6 +145,7 @@ fn map_rust_child_diagnostic(
|
||||||
} else {
|
} else {
|
||||||
MappedRustChildDiagnostic::SuggestedFix(lsp_ext::CodeAction {
|
MappedRustChildDiagnostic::SuggestedFix(lsp_ext::CodeAction {
|
||||||
title: rd.message.clone(),
|
title: rd.message.clone(),
|
||||||
|
id: None,
|
||||||
group: None,
|
group: None,
|
||||||
kind: Some("quickfix".to_string()),
|
kind: Some("quickfix".to_string()),
|
||||||
edit: Some(lsp_ext::SnippetWorkspaceEdit {
|
edit: Some(lsp_ext::SnippetWorkspaceEdit {
|
||||||
|
|
|
@ -97,6 +97,22 @@ pub struct JoinLinesParams {
|
||||||
pub ranges: Vec<Range>,
|
pub ranges: Vec<Range>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum ResolveCodeActionRequest {}
|
||||||
|
|
||||||
|
impl Request for ResolveCodeActionRequest {
|
||||||
|
type Params = ResolveCodeActionParams;
|
||||||
|
type Result = Option<SnippetWorkspaceEdit>;
|
||||||
|
const METHOD: &'static str = "experimental/resolveCodeAction";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Params for the ResolveCodeActionRequest
|
||||||
|
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct ResolveCodeActionParams {
|
||||||
|
pub code_action_params: lsp_types::CodeActionParams,
|
||||||
|
pub id: String,
|
||||||
|
}
|
||||||
|
|
||||||
pub enum OnEnter {}
|
pub enum OnEnter {}
|
||||||
|
|
||||||
impl Request for OnEnter {
|
impl Request for OnEnter {
|
||||||
|
@ -202,6 +218,8 @@ impl Request for CodeActionRequest {
|
||||||
pub struct CodeAction {
|
pub struct CodeAction {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub id: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub group: Option<String>,
|
pub group: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub kind: Option<String>,
|
pub kind: Option<String>,
|
||||||
|
|
|
@ -509,6 +509,7 @@ fn on_request(
|
||||||
.on::<lsp_ext::Runnables>(handlers::handle_runnables)?
|
.on::<lsp_ext::Runnables>(handlers::handle_runnables)?
|
||||||
.on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints)?
|
.on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints)?
|
||||||
.on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)?
|
.on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)?
|
||||||
|
.on::<lsp_ext::ResolveCodeActionRequest>(handlers::handle_resolve_code_action)?
|
||||||
.on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting)?
|
.on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting)?
|
||||||
.on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol)?
|
.on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol)?
|
||||||
.on::<lsp_types::request::WorkspaceSymbol>(handlers::handle_workspace_symbol)?
|
.on::<lsp_types::request::WorkspaceSymbol>(handlers::handle_workspace_symbol)?
|
||||||
|
|
|
@ -25,7 +25,7 @@ use ra_project_model::TargetKind;
|
||||||
use ra_syntax::{AstNode, SyntaxKind, TextRange, TextSize};
|
use ra_syntax::{AstNode, SyntaxKind, TextRange, TextSize};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::to_value;
|
use serde_json::to_value;
|
||||||
use stdx::format_to;
|
use stdx::{format_to, split1};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
cargo_target_spec::CargoTargetSpec,
|
cargo_target_spec::CargoTargetSpec,
|
||||||
|
@ -701,6 +701,45 @@ pub fn handle_formatting(
|
||||||
}]))
|
}]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handle_fixes(
|
||||||
|
snap: &GlobalStateSnapshot,
|
||||||
|
params: &lsp_types::CodeActionParams,
|
||||||
|
res: &mut Vec<lsp_ext::CodeAction>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
||||||
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
||||||
|
let range = from_proto::text_range(&line_index, params.range);
|
||||||
|
let diagnostics = snap.analysis().diagnostics(file_id)?;
|
||||||
|
|
||||||
|
let fixes_from_diagnostics = diagnostics
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|d| Some((d.range, d.fix?)))
|
||||||
|
.filter(|(diag_range, _fix)| diag_range.intersect(range).is_some())
|
||||||
|
.map(|(_range, fix)| fix);
|
||||||
|
for fix in fixes_from_diagnostics {
|
||||||
|
let title = fix.label;
|
||||||
|
let edit = to_proto::snippet_workspace_edit(&snap, fix.source_change)?;
|
||||||
|
let action = lsp_ext::CodeAction {
|
||||||
|
title,
|
||||||
|
id: None,
|
||||||
|
group: None,
|
||||||
|
kind: Some(lsp_types::code_action_kind::QUICKFIX.into()),
|
||||||
|
edit: Some(edit),
|
||||||
|
command: None,
|
||||||
|
};
|
||||||
|
res.push(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
for fix in snap.check_fixes.get(&file_id).into_iter().flatten() {
|
||||||
|
let fix_range = from_proto::text_range(&line_index, fix.range);
|
||||||
|
if fix_range.intersect(range).is_none() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
res.push(fix.action.clone());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn handle_code_action(
|
pub fn handle_code_action(
|
||||||
snap: GlobalStateSnapshot,
|
snap: GlobalStateSnapshot,
|
||||||
params: lsp_types::CodeActionParams,
|
params: lsp_types::CodeActionParams,
|
||||||
|
@ -717,43 +756,43 @@ pub fn handle_code_action(
|
||||||
let line_index = snap.analysis().file_line_index(file_id)?;
|
let line_index = snap.analysis().file_line_index(file_id)?;
|
||||||
let range = from_proto::text_range(&line_index, params.range);
|
let range = from_proto::text_range(&line_index, params.range);
|
||||||
let frange = FileRange { file_id, range };
|
let frange = FileRange { file_id, range };
|
||||||
|
|
||||||
let diagnostics = snap.analysis().diagnostics(file_id)?;
|
|
||||||
let mut res: Vec<lsp_ext::CodeAction> = Vec::new();
|
let mut res: Vec<lsp_ext::CodeAction> = Vec::new();
|
||||||
|
|
||||||
let fixes_from_diagnostics = diagnostics
|
handle_fixes(&snap, ¶ms, &mut res)?;
|
||||||
.into_iter()
|
|
||||||
.filter_map(|d| Some((d.range, d.fix?)))
|
|
||||||
.filter(|(diag_range, _fix)| diag_range.intersect(range).is_some())
|
|
||||||
.map(|(_range, fix)| fix);
|
|
||||||
|
|
||||||
for fix in fixes_from_diagnostics {
|
if snap.config.client_caps.resolve_code_action {
|
||||||
let title = fix.label;
|
for (index, assist) in
|
||||||
let edit = to_proto::snippet_workspace_edit(&snap, fix.source_change)?;
|
snap.analysis().unresolved_assists(&snap.config.assist, frange)?.into_iter().enumerate()
|
||||||
let action = lsp_ext::CodeAction {
|
{
|
||||||
title,
|
res.push(to_proto::unresolved_code_action(&snap, assist, index)?);
|
||||||
group: None,
|
}
|
||||||
kind: Some(lsp_types::code_action_kind::QUICKFIX.into()),
|
} else {
|
||||||
edit: Some(edit),
|
for assist in snap.analysis().resolved_assists(&snap.config.assist, frange)?.into_iter() {
|
||||||
command: None,
|
res.push(to_proto::resolved_code_action(&snap, assist)?);
|
||||||
};
|
|
||||||
res.push(action);
|
|
||||||
}
|
|
||||||
|
|
||||||
for fix in snap.check_fixes.get(&file_id).into_iter().flatten() {
|
|
||||||
let fix_range = from_proto::text_range(&line_index, fix.range);
|
|
||||||
if fix_range.intersect(range).is_none() {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
res.push(fix.action.clone());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for assist in snap.analysis().assists(&snap.config.assist, frange)?.into_iter() {
|
|
||||||
res.push(to_proto::code_action(&snap, assist)?.into());
|
|
||||||
}
|
|
||||||
Ok(Some(res))
|
Ok(Some(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn handle_resolve_code_action(
|
||||||
|
snap: GlobalStateSnapshot,
|
||||||
|
params: lsp_ext::ResolveCodeActionParams,
|
||||||
|
) -> Result<Option<lsp_ext::SnippetWorkspaceEdit>> {
|
||||||
|
let _p = profile("handle_resolve_code_action");
|
||||||
|
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)?;
|
||||||
|
let range = from_proto::text_range(&line_index, params.code_action_params.range);
|
||||||
|
let frange = FileRange { file_id, range };
|
||||||
|
|
||||||
|
let assists = snap.analysis().resolved_assists(&snap.config.assist, frange)?;
|
||||||
|
let (id_string, index) = split1(¶ms.id, ':').unwrap();
|
||||||
|
let index = index.parse::<usize>().unwrap();
|
||||||
|
let assist = &assists[index];
|
||||||
|
assert!(assist.assist.id.0 == id_string);
|
||||||
|
Ok(to_proto::resolved_code_action(&snap, assist.clone())?.edit)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn handle_code_lens(
|
pub fn handle_code_lens(
|
||||||
snap: GlobalStateSnapshot,
|
snap: GlobalStateSnapshot,
|
||||||
params: lsp_types::CodeLensParams,
|
params: lsp_types::CodeLensParams,
|
||||||
|
|
|
@ -3,8 +3,8 @@ use ra_db::{FileId, FileRange};
|
||||||
use ra_ide::{
|
use ra_ide::{
|
||||||
Assist, CompletionItem, CompletionItemKind, Documentation, FileSystemEdit, Fold, FoldKind,
|
Assist, CompletionItem, CompletionItemKind, Documentation, FileSystemEdit, Fold, FoldKind,
|
||||||
FunctionSignature, Highlight, HighlightModifier, HighlightTag, HighlightedRange, Indel,
|
FunctionSignature, Highlight, HighlightModifier, HighlightTag, HighlightedRange, Indel,
|
||||||
InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget, ReferenceAccess, Runnable,
|
InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget, ReferenceAccess,
|
||||||
RunnableKind, Severity, SourceChange, SourceFileEdit, TextEdit,
|
ResolvedAssist, Runnable, RunnableKind, Severity, SourceChange, SourceFileEdit, TextEdit,
|
||||||
};
|
};
|
||||||
use ra_syntax::{SyntaxKind, TextRange, TextSize};
|
use ra_syntax::{SyntaxKind, TextRange, TextSize};
|
||||||
use ra_vfs::LineEndings;
|
use ra_vfs::LineEndings;
|
||||||
|
@ -623,20 +623,36 @@ fn main() <fold>{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn code_action(
|
pub(crate) fn unresolved_code_action(
|
||||||
snap: &GlobalStateSnapshot,
|
snap: &GlobalStateSnapshot,
|
||||||
assist: Assist,
|
assist: Assist,
|
||||||
|
index: usize,
|
||||||
) -> Result<lsp_ext::CodeAction> {
|
) -> Result<lsp_ext::CodeAction> {
|
||||||
let res = lsp_ext::CodeAction {
|
let res = lsp_ext::CodeAction {
|
||||||
title: assist.label,
|
title: assist.label,
|
||||||
group: if snap.config.client_caps.code_action_group { assist.group_label } else { None },
|
id: Some(format!("{}:{}", assist.id.0.to_owned(), index.to_string())),
|
||||||
|
group: assist.group.filter(|_| snap.config.client_caps.code_action_group).map(|gr| gr.0),
|
||||||
kind: Some(String::new()),
|
kind: Some(String::new()),
|
||||||
edit: Some(snippet_workspace_edit(snap, assist.source_change)?),
|
edit: None,
|
||||||
command: None,
|
command: None,
|
||||||
};
|
};
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn resolved_code_action(
|
||||||
|
snap: &GlobalStateSnapshot,
|
||||||
|
assist: ResolvedAssist,
|
||||||
|
) -> Result<lsp_ext::CodeAction> {
|
||||||
|
let change = assist.source_change;
|
||||||
|
unresolved_code_action(snap, assist.assist, 0).and_then(|it| {
|
||||||
|
Ok(lsp_ext::CodeAction {
|
||||||
|
id: None,
|
||||||
|
edit: Some(snippet_workspace_edit(snap, change)?),
|
||||||
|
..it
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn runnable(
|
pub(crate) fn runnable(
|
||||||
snap: &GlobalStateSnapshot,
|
snap: &GlobalStateSnapshot,
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
|
|
|
@ -97,6 +97,30 @@ Invoking code action at this position will yield two code actions for importing
|
||||||
* Is a fixed two-level structure enough?
|
* Is a fixed two-level structure enough?
|
||||||
* Should we devise a general way to encode custom interaction protocols for GUI refactorings?
|
* Should we devise a general way to encode custom interaction protocols for GUI refactorings?
|
||||||
|
|
||||||
|
## Lazy assists with `ResolveCodeAction`
|
||||||
|
|
||||||
|
**Issue:** https://github.com/microsoft/language-server-protocol/issues/787
|
||||||
|
|
||||||
|
**Client Capability** `{ "resolveCodeAction": boolean }`
|
||||||
|
|
||||||
|
If this capability is set, the assists will be computed lazily. Thus `CodeAction` returned from the server will only contain `id` but not `edit` or `command` fields. The only exclusion from the rule is the diagnostic edits.
|
||||||
|
|
||||||
|
After the client got the id, it should then call `experimental/resolveCodeAction` command on the server and provide the following payload:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
interface ResolveCodeActionParams {
|
||||||
|
id: string;
|
||||||
|
codeActionParams: lc.CodeActionParams;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
As a result of the command call the client will get the respective workspace edit (`lc.WorkspaceEdit`).
|
||||||
|
|
||||||
|
### Unresolved Questions
|
||||||
|
|
||||||
|
* Apply smarter filtering for ids?
|
||||||
|
* Upon `resolveCodeAction` command only call the assits which should be resolved and not all of them?
|
||||||
|
|
||||||
## Parent Module
|
## Parent Module
|
||||||
|
|
||||||
**Issue:** https://github.com/microsoft/language-server-protocol/issues/1002
|
**Issue:** https://github.com/microsoft/language-server-protocol/issues/1002
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
import * as lc from 'vscode-languageclient';
|
import * as lc from 'vscode-languageclient';
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
|
import * as ra from '../src/lsp_ext';
|
||||||
|
import * as Is from 'vscode-languageclient/lib/utils/is';
|
||||||
|
|
||||||
import { CallHierarchyFeature } from 'vscode-languageclient/lib/callHierarchy.proposed';
|
import { CallHierarchyFeature } from 'vscode-languageclient/lib/callHierarchy.proposed';
|
||||||
import { SemanticTokensFeature, DocumentSemanticsTokensSignature } from 'vscode-languageclient/lib/semanticTokens.proposed';
|
import { SemanticTokensFeature, DocumentSemanticsTokensSignature } from 'vscode-languageclient/lib/semanticTokens.proposed';
|
||||||
|
import { assert } from './util';
|
||||||
|
|
||||||
export function createClient(serverPath: string, cwd: string): lc.LanguageClient {
|
export function createClient(serverPath: string, cwd: string): lc.LanguageClient {
|
||||||
// '.' Is the fallback if no folder is open
|
// '.' Is the fallback if no folder is open
|
||||||
|
@ -32,6 +35,8 @@ export function createClient(serverPath: string, cwd: string): lc.LanguageClient
|
||||||
if (res === undefined) throw new Error('busy');
|
if (res === undefined) throw new Error('busy');
|
||||||
return res;
|
return res;
|
||||||
},
|
},
|
||||||
|
// Using custom handling of CodeActions where each code action is resloved lazily
|
||||||
|
// That's why we are not waiting for any command or edits
|
||||||
async provideCodeActions(document: vscode.TextDocument, range: vscode.Range, context: vscode.CodeActionContext, token: vscode.CancellationToken, _next: lc.ProvideCodeActionsSignature) {
|
async provideCodeActions(document: vscode.TextDocument, range: vscode.Range, context: vscode.CodeActionContext, token: vscode.CancellationToken, _next: lc.ProvideCodeActionsSignature) {
|
||||||
const params: lc.CodeActionParams = {
|
const params: lc.CodeActionParams = {
|
||||||
textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document),
|
textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document),
|
||||||
|
@ -43,32 +48,36 @@ export function createClient(serverPath: string, cwd: string): lc.LanguageClient
|
||||||
const result: (vscode.CodeAction | vscode.Command)[] = [];
|
const result: (vscode.CodeAction | vscode.Command)[] = [];
|
||||||
const groups = new Map<string, { index: number; items: vscode.CodeAction[] }>();
|
const groups = new Map<string, { index: number; items: vscode.CodeAction[] }>();
|
||||||
for (const item of values) {
|
for (const item of values) {
|
||||||
|
// In our case we expect to get code edits only from diagnostics
|
||||||
if (lc.CodeAction.is(item)) {
|
if (lc.CodeAction.is(item)) {
|
||||||
|
assert(!item.command, "We don't expect to receive commands in CodeActions");
|
||||||
const action = client.protocol2CodeConverter.asCodeAction(item);
|
const action = client.protocol2CodeConverter.asCodeAction(item);
|
||||||
const group = actionGroup(item);
|
result.push(action);
|
||||||
if (isSnippetEdit(item) || group) {
|
continue;
|
||||||
action.command = {
|
}
|
||||||
command: "rust-analyzer.applySnippetWorkspaceEdit",
|
assert(isCodeActionWithoutEditsAndCommands(item), "We don't expect edits or commands here");
|
||||||
title: "",
|
const action = new vscode.CodeAction(item.title);
|
||||||
arguments: [action.edit],
|
const group = (item as any).group;
|
||||||
};
|
const id = (item as any).id;
|
||||||
action.edit = undefined;
|
const resolveParams: ra.ResolveCodeActionParams = {
|
||||||
}
|
id: id,
|
||||||
|
codeActionParams: params
|
||||||
if (group) {
|
};
|
||||||
let entry = groups.get(group);
|
action.command = {
|
||||||
if (!entry) {
|
command: "rust-analyzer.resolveCodeAction",
|
||||||
entry = { index: result.length, items: [] };
|
title: item.title,
|
||||||
groups.set(group, entry);
|
arguments: [resolveParams],
|
||||||
result.push(action);
|
};
|
||||||
}
|
if (group) {
|
||||||
entry.items.push(action);
|
let entry = groups.get(group);
|
||||||
} else {
|
if (!entry) {
|
||||||
|
entry = { index: result.length, items: [] };
|
||||||
|
groups.set(group, entry);
|
||||||
result.push(action);
|
result.push(action);
|
||||||
}
|
}
|
||||||
|
entry.items.push(action);
|
||||||
} else {
|
} else {
|
||||||
const command = client.protocol2CodeConverter.asCommand(item);
|
result.push(action);
|
||||||
result.push(command);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const [group, { index, items }] of groups) {
|
for (const [group, { index, items }] of groups) {
|
||||||
|
@ -80,7 +89,7 @@ export function createClient(serverPath: string, cwd: string): lc.LanguageClient
|
||||||
command: "rust-analyzer.applyActionGroup",
|
command: "rust-analyzer.applyActionGroup",
|
||||||
title: "",
|
title: "",
|
||||||
arguments: [items.map((item) => {
|
arguments: [items.map((item) => {
|
||||||
return { label: item.title, edit: item.command!!.arguments!![0] };
|
return { label: item.title, arguments: item.command!!.arguments!![0] };
|
||||||
})],
|
})],
|
||||||
};
|
};
|
||||||
result[index] = action;
|
result[index] = action;
|
||||||
|
@ -119,24 +128,17 @@ class ExperimentalFeatures implements lc.StaticFeature {
|
||||||
const caps: any = capabilities.experimental ?? {};
|
const caps: any = capabilities.experimental ?? {};
|
||||||
caps.snippetTextEdit = true;
|
caps.snippetTextEdit = true;
|
||||||
caps.codeActionGroup = true;
|
caps.codeActionGroup = true;
|
||||||
|
caps.resolveCodeAction = true;
|
||||||
capabilities.experimental = caps;
|
capabilities.experimental = caps;
|
||||||
}
|
}
|
||||||
initialize(_capabilities: lc.ServerCapabilities<any>, _documentSelector: lc.DocumentSelector | undefined): void {
|
initialize(_capabilities: lc.ServerCapabilities<any>, _documentSelector: lc.DocumentSelector | undefined): void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isSnippetEdit(action: lc.CodeAction): boolean {
|
function isCodeActionWithoutEditsAndCommands(value: any): boolean {
|
||||||
const documentChanges = action.edit?.documentChanges ?? [];
|
const candidate: lc.CodeAction = value;
|
||||||
for (const edit of documentChanges) {
|
return candidate && Is.string(candidate.title) &&
|
||||||
if (lc.TextDocumentEdit.is(edit)) {
|
(candidate.diagnostics === void 0 || Is.typedArray(candidate.diagnostics, lc.Diagnostic.is)) &&
|
||||||
if (edit.edits.some((indel) => (indel as any).insertTextFormat === lc.InsertTextFormat.Snippet)) {
|
(candidate.kind === void 0 || Is.string(candidate.kind)) &&
|
||||||
return true;
|
(candidate.edit === void 0 && candidate.command === void 0);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function actionGroup(action: lc.CodeAction): string | undefined {
|
|
||||||
return (action as any).group;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -343,10 +343,25 @@ export function showReferences(ctx: Ctx): Cmd {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function applyActionGroup(_ctx: Ctx): Cmd {
|
export function applyActionGroup(_ctx: Ctx): Cmd {
|
||||||
return async (actions: { label: string; edit: vscode.WorkspaceEdit }[]) => {
|
return async (actions: { label: string; arguments: ra.ResolveCodeActionParams }[]) => {
|
||||||
const selectedAction = await vscode.window.showQuickPick(actions);
|
const selectedAction = await vscode.window.showQuickPick(actions);
|
||||||
if (!selectedAction) return;
|
if (!selectedAction) return;
|
||||||
await applySnippetWorkspaceEdit(selectedAction.edit);
|
vscode.commands.executeCommand(
|
||||||
|
'rust-analyzer.resolveCodeAction',
|
||||||
|
selectedAction.arguments,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveCodeAction(ctx: Ctx): Cmd {
|
||||||
|
const client = ctx.client;
|
||||||
|
return async (params: ra.ResolveCodeActionParams) => {
|
||||||
|
const item: lc.WorkspaceEdit = await client.sendRequest(ra.resolveCodeAction, params);
|
||||||
|
if (!item) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const edit = client.protocol2CodeConverter.asWorkspaceEdit(item);
|
||||||
|
await applySnippetWorkspaceEdit(edit);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,12 @@ export const matchingBrace = new lc.RequestType<MatchingBraceParams, lc.Position
|
||||||
|
|
||||||
export const parentModule = new lc.RequestType<lc.TextDocumentPositionParams, lc.LocationLink[], void>("experimental/parentModule");
|
export const parentModule = new lc.RequestType<lc.TextDocumentPositionParams, lc.LocationLink[], void>("experimental/parentModule");
|
||||||
|
|
||||||
|
export interface ResolveCodeActionParams {
|
||||||
|
id: string;
|
||||||
|
codeActionParams: lc.CodeActionParams;
|
||||||
|
}
|
||||||
|
export const resolveCodeAction = new lc.RequestType<ResolveCodeActionParams, lc.WorkspaceEdit, unknown>('experimental/resolveCodeAction');
|
||||||
|
|
||||||
export interface JoinLinesParams {
|
export interface JoinLinesParams {
|
||||||
textDocument: lc.TextDocumentIdentifier;
|
textDocument: lc.TextDocumentIdentifier;
|
||||||
ranges: lc.Range[];
|
ranges: lc.Range[];
|
||||||
|
|
|
@ -98,6 +98,7 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||||
ctx.registerCommand('debugSingle', commands.debugSingle);
|
ctx.registerCommand('debugSingle', commands.debugSingle);
|
||||||
ctx.registerCommand('showReferences', commands.showReferences);
|
ctx.registerCommand('showReferences', commands.showReferences);
|
||||||
ctx.registerCommand('applySnippetWorkspaceEdit', commands.applySnippetWorkspaceEditCommand);
|
ctx.registerCommand('applySnippetWorkspaceEdit', commands.applySnippetWorkspaceEditCommand);
|
||||||
|
ctx.registerCommand('resolveCodeAction', commands.resolveCodeAction);
|
||||||
ctx.registerCommand('applyActionGroup', commands.applyActionGroup);
|
ctx.registerCommand('applyActionGroup', commands.applyActionGroup);
|
||||||
|
|
||||||
ctx.pushCleanup(activateTaskProvider(workspaceFolder));
|
ctx.pushCleanup(activateTaskProvider(workspaceFolder));
|
||||||
|
|
Loading…
Reference in a new issue