Skip to content

Commit 04b5fcf

Browse files
committed
Ensure that listing&resolving code actions use the same set of actions
1 parent fe29a9e commit 04b5fcf

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

crates/ide/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,34 @@ impl Analysis {
531531
self.with_db(|db| diagnostics::diagnostics(db, config, file_id))
532532
}
533533

534+
/// Convenience function to return assists + quick fixes for diagnostics
535+
pub fn assists_with_fixes(
536+
&self,
537+
assist_config: &AssistConfig,
538+
diagnostics_config: &DiagnosticsConfig,
539+
resolve: bool,
540+
frange: FileRange,
541+
) -> Cancelable<Vec<Assist>> {
542+
let include_fixes = match &assist_config.allowed {
543+
Some(it) => it.iter().any(|&it| it == AssistKind::None || it == AssistKind::QuickFix),
544+
None => true,
545+
};
546+
547+
self.with_db(|db| {
548+
let mut res = Assist::get(db, assist_config, resolve, frange);
549+
ssr::add_ssr_assist(db, &mut res, resolve, frange);
550+
551+
if include_fixes {
552+
res.extend(
553+
diagnostics::diagnostics(db, diagnostics_config, frange.file_id)
554+
.into_iter()
555+
.filter_map(|it| it.fix),
556+
);
557+
}
558+
res
559+
})
560+
}
561+
534562
/// Returns the edit required to rename reference at the position to the new
535563
/// name.
536564
pub fn rename(

crates/rust-analyzer/src/handlers.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::{
88
};
99

1010
use ide::{
11-
AnnotationConfig, AssistKind, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData,
12-
Query, RangeInfo, Runnable, RunnableKind, SearchScope, SourceChange, TextEdit,
11+
AnnotationConfig, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, Query,
12+
RangeInfo, Runnable, RunnableKind, SearchScope, SourceChange, TextEdit,
1313
};
1414
use ide_db::SymbolKind;
1515
use itertools::Itertools;
@@ -1003,27 +1003,13 @@ pub(crate) fn handle_code_action(
10031003

10041004
let mut res: Vec<lsp_ext::CodeAction> = Vec::new();
10051005

1006-
let include_quick_fixes = match &assists_config.allowed {
1007-
Some(v) => v.iter().any(|it| it == &AssistKind::None || it == &AssistKind::QuickFix),
1008-
None => true,
1009-
};
10101006
let code_action_resolve_cap = snap.config.code_action_resolve();
1011-
1012-
let mut assists = Vec::new();
1013-
1014-
// Fixes from native diagnostics.
1015-
if include_quick_fixes {
1016-
let diagnostics = snap.analysis.diagnostics(&snap.config.diagnostics(), frange.file_id)?;
1017-
assists.extend(
1018-
diagnostics
1019-
.into_iter()
1020-
.filter_map(|d| d.fix)
1021-
.filter(|fix| fix.target.intersect(frange.range).is_some()),
1022-
)
1023-
}
1024-
1025-
// Assists proper.
1026-
assists.extend(snap.analysis.assists(&assists_config, !code_action_resolve_cap, frange)?);
1007+
let assists = snap.analysis.assists_with_fixes(
1008+
&assists_config,
1009+
&snap.config.diagnostics(),
1010+
!code_action_resolve_cap,
1011+
frange,
1012+
)?;
10271013
for (index, assist) in assists.into_iter().enumerate() {
10281014
let resolve_data =
10291015
if code_action_resolve_cap { Some((index, params.clone())) } else { None };
@@ -1066,7 +1052,13 @@ pub(crate) fn handle_code_action_resolve(
10661052
.only
10671053
.map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
10681054

1069-
let assists = snap.analysis.assists(&assists_config, true, frange)?;
1055+
let assists = snap.analysis.assists_with_fixes(
1056+
&assists_config,
1057+
&snap.config.diagnostics(),
1058+
true,
1059+
frange,
1060+
)?;
1061+
10701062
let (id, index) = split_once(&params.id, ':').unwrap();
10711063
let index = index.parse::<usize>().unwrap();
10721064
let assist = &assists[index];

0 commit comments

Comments
 (0)