mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 05:38:46 +00:00
Move allow list into AssistConfig
This commit is contained in:
parent
6a7d1d4412
commit
21c1504ca9
6 changed files with 40 additions and 44 deletions
|
@ -4,9 +4,12 @@
|
|||
//! module, and we use to statically check that we only produce snippet
|
||||
//! assists if we are allowed to.
|
||||
|
||||
use crate::AssistKind;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct AssistConfig {
|
||||
pub snippet_cap: Option<SnippetCap>,
|
||||
pub allowed: Option<Vec<AssistKind>>,
|
||||
}
|
||||
|
||||
impl AssistConfig {
|
||||
|
@ -22,6 +25,6 @@ pub struct SnippetCap {
|
|||
|
||||
impl Default for AssistConfig {
|
||||
fn default() -> Self {
|
||||
AssistConfig { snippet_cap: Some(SnippetCap { _private: () }) }
|
||||
AssistConfig { snippet_cap: Some(SnippetCap { _private: () }), allowed: None }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,7 +57,6 @@ pub(crate) struct AssistContext<'a> {
|
|||
pub(crate) sema: Semantics<'a, RootDatabase>,
|
||||
pub(crate) frange: FileRange,
|
||||
source_file: SourceFile,
|
||||
allowed: Option<Vec<AssistKind>>,
|
||||
}
|
||||
|
||||
impl<'a> AssistContext<'a> {
|
||||
|
@ -65,10 +64,9 @@ impl<'a> AssistContext<'a> {
|
|||
sema: Semantics<'a, RootDatabase>,
|
||||
config: &'a AssistConfig,
|
||||
frange: FileRange,
|
||||
allowed: Option<Vec<AssistKind>>,
|
||||
) -> AssistContext<'a> {
|
||||
let source_file = sema.parse(frange.file_id);
|
||||
AssistContext { config, sema, frange, source_file, allowed }
|
||||
AssistContext { config, sema, frange, source_file }
|
||||
}
|
||||
|
||||
pub(crate) fn db(&self) -> &RootDatabase {
|
||||
|
@ -114,7 +112,7 @@ impl Assists {
|
|||
resolve: true,
|
||||
file: ctx.frange.file_id,
|
||||
buf: Vec::new(),
|
||||
allowed: ctx.allowed.clone(),
|
||||
allowed: ctx.config.allowed.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -123,7 +121,7 @@ impl Assists {
|
|||
resolve: false,
|
||||
file: ctx.frange.file_id,
|
||||
buf: Vec::new(),
|
||||
allowed: ctx.allowed.clone(),
|
||||
allowed: ctx.config.allowed.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -86,14 +86,9 @@ impl Assist {
|
|||
///
|
||||
/// Assists are returned in the "unresolved" state, that is only labels are
|
||||
/// returned, without actual edits.
|
||||
pub fn unresolved(
|
||||
db: &RootDatabase,
|
||||
config: &AssistConfig,
|
||||
range: FileRange,
|
||||
allowed: Option<Vec<AssistKind>>,
|
||||
) -> Vec<Assist> {
|
||||
pub fn unresolved(db: &RootDatabase, config: &AssistConfig, range: FileRange) -> Vec<Assist> {
|
||||
let sema = Semantics::new(db);
|
||||
let ctx = AssistContext::new(sema, config, range, allowed);
|
||||
let ctx = AssistContext::new(sema, config, range);
|
||||
let mut acc = Assists::new_unresolved(&ctx);
|
||||
handlers::all().iter().for_each(|handler| {
|
||||
handler(&mut acc, &ctx);
|
||||
|
@ -109,10 +104,9 @@ impl Assist {
|
|||
db: &RootDatabase,
|
||||
config: &AssistConfig,
|
||||
range: FileRange,
|
||||
allowed: Option<Vec<AssistKind>>,
|
||||
) -> Vec<ResolvedAssist> {
|
||||
let sema = Semantics::new(db);
|
||||
let ctx = AssistContext::new(sema, config, range, allowed);
|
||||
let ctx = AssistContext::new(sema, config, range);
|
||||
let mut acc = Assists::new_resolved(&ctx);
|
||||
handlers::all().iter().for_each(|handler| {
|
||||
handler(&mut acc, &ctx);
|
||||
|
|
|
@ -35,14 +35,14 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) {
|
|||
let before = db.file_text(file_id).to_string();
|
||||
let frange = FileRange { file_id, range: selection.into() };
|
||||
|
||||
let mut assist = Assist::resolved(&db, &AssistConfig::default(), frange, None)
|
||||
let mut assist = Assist::resolved(&db, &AssistConfig::default(), frange)
|
||||
.into_iter()
|
||||
.find(|assist| assist.assist.id.0 == assist_id)
|
||||
.unwrap_or_else(|| {
|
||||
panic!(
|
||||
"\n\nAssist is not applicable: {}\nAvailable assists: {}",
|
||||
assist_id,
|
||||
Assist::resolved(&db, &AssistConfig::default(), frange, None)
|
||||
Assist::resolved(&db, &AssistConfig::default(), frange)
|
||||
.into_iter()
|
||||
.map(|assist| assist.assist.id.0)
|
||||
.collect::<Vec<_>>()
|
||||
|
@ -73,7 +73,7 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult) {
|
|||
|
||||
let sema = Semantics::new(&db);
|
||||
let config = AssistConfig::default();
|
||||
let ctx = AssistContext::new(sema, &config, frange, None);
|
||||
let ctx = AssistContext::new(sema, &config, frange);
|
||||
let mut acc = Assists::new_resolved(&ctx);
|
||||
handler(&mut acc, &ctx);
|
||||
let mut res = acc.finish_resolved();
|
||||
|
@ -105,7 +105,7 @@ fn assist_order_field_struct() {
|
|||
let (before_cursor_pos, before) = extract_offset(before);
|
||||
let (db, file_id) = with_single_file(&before);
|
||||
let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) };
|
||||
let assists = Assist::resolved(&db, &AssistConfig::default(), frange, None);
|
||||
let assists = Assist::resolved(&db, &AssistConfig::default(), frange);
|
||||
let mut assists = assists.iter();
|
||||
|
||||
assert_eq!(
|
||||
|
@ -128,7 +128,7 @@ fn assist_order_if_expr() {
|
|||
let (range, before) = extract_range(before);
|
||||
let (db, file_id) = with_single_file(&before);
|
||||
let frange = FileRange { file_id, range };
|
||||
let assists = Assist::resolved(&db, &AssistConfig::default(), frange, None);
|
||||
let assists = Assist::resolved(&db, &AssistConfig::default(), frange);
|
||||
let mut assists = assists.iter();
|
||||
|
||||
assert_eq!(assists.next().expect("expected assist").assist.label, "Extract into variable");
|
||||
|
@ -150,9 +150,10 @@ fn assist_filter_works() {
|
|||
let frange = FileRange { file_id, range };
|
||||
|
||||
{
|
||||
let allowed = Some(vec![AssistKind::Refactor]);
|
||||
let mut cfg = AssistConfig::default();
|
||||
cfg.allowed = Some(vec![AssistKind::Refactor]);
|
||||
|
||||
let assists = Assist::resolved(&db, &AssistConfig::default(), frange, allowed);
|
||||
let assists = Assist::resolved(&db, &cfg, frange);
|
||||
let mut assists = assists.iter();
|
||||
|
||||
assert_eq!(assists.next().expect("expected assist").assist.label, "Extract into variable");
|
||||
|
@ -160,8 +161,9 @@ fn assist_filter_works() {
|
|||
}
|
||||
|
||||
{
|
||||
let allowed = Some(vec![AssistKind::RefactorExtract]);
|
||||
let assists = Assist::resolved(&db, &AssistConfig::default(), frange, allowed);
|
||||
let mut cfg = AssistConfig::default();
|
||||
cfg.allowed = Some(vec![AssistKind::RefactorExtract]);
|
||||
let assists = Assist::resolved(&db, &cfg, frange);
|
||||
assert_eq!(assists.len(), 1);
|
||||
|
||||
let mut assists = assists.iter();
|
||||
|
@ -169,8 +171,9 @@ fn assist_filter_works() {
|
|||
}
|
||||
|
||||
{
|
||||
let allowed = Some(vec![AssistKind::QuickFix]);
|
||||
let assists = Assist::resolved(&db, &AssistConfig::default(), frange, allowed);
|
||||
let mut cfg = AssistConfig::default();
|
||||
cfg.allowed = Some(vec![AssistKind::QuickFix]);
|
||||
let assists = Assist::resolved(&db, &cfg, frange);
|
||||
assert!(assists.is_empty(), "All asserts but quickfixes should be filtered out");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -476,9 +476,8 @@ impl Analysis {
|
|||
&self,
|
||||
config: &AssistConfig,
|
||||
frange: FileRange,
|
||||
allowed: Option<Vec<AssistKind>>,
|
||||
) -> Cancelable<Vec<ResolvedAssist>> {
|
||||
self.with_db(|db| ra_assists::Assist::resolved(db, config, frange, allowed))
|
||||
self.with_db(|db| ra_assists::Assist::resolved(db, config, frange))
|
||||
}
|
||||
|
||||
/// Computes unresolved assists (aka code actions aka intentions) for the given
|
||||
|
@ -487,9 +486,8 @@ impl Analysis {
|
|||
&self,
|
||||
config: &AssistConfig,
|
||||
frange: FileRange,
|
||||
allowed: Option<Vec<AssistKind>>,
|
||||
) -> Cancelable<Vec<Assist>> {
|
||||
self.with_db(|db| Assist::unresolved(db, config, frange, allowed))
|
||||
self.with_db(|db| Assist::unresolved(db, config, frange))
|
||||
}
|
||||
|
||||
/// Computes the set of diagnostics for the given file.
|
||||
|
|
|
@ -790,7 +790,7 @@ fn handle_fixes(
|
|||
}
|
||||
|
||||
pub(crate) fn handle_code_action(
|
||||
snap: GlobalStateSnapshot,
|
||||
mut snap: GlobalStateSnapshot,
|
||||
params: lsp_types::CodeActionParams,
|
||||
) -> Result<Option<Vec<lsp_ext::CodeAction>>> {
|
||||
let _p = profile("handle_code_action");
|
||||
|
@ -806,25 +806,24 @@ pub(crate) fn handle_code_action(
|
|||
let range = from_proto::text_range(&line_index, params.range);
|
||||
let frange = FileRange { file_id, range };
|
||||
|
||||
snap.config.assist.allowed = params
|
||||
.clone()
|
||||
.context
|
||||
.only
|
||||
.map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
|
||||
|
||||
let mut res: Vec<lsp_ext::CodeAction> = Vec::new();
|
||||
|
||||
handle_fixes(&snap, ¶ms, &mut res)?;
|
||||
|
||||
let only =
|
||||
params.context.only.map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
|
||||
|
||||
if snap.config.client_caps.resolve_code_action {
|
||||
for (index, assist) in snap
|
||||
.analysis
|
||||
.unresolved_assists(&snap.config.assist, frange, only)?
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
for (index, assist) in
|
||||
snap.analysis.unresolved_assists(&snap.config.assist, frange)?.into_iter().enumerate()
|
||||
{
|
||||
res.push(to_proto::unresolved_code_action(&snap, assist, index)?);
|
||||
}
|
||||
} else {
|
||||
for assist in snap.analysis.resolved_assists(&snap.config.assist, frange, only)?.into_iter()
|
||||
{
|
||||
for assist in snap.analysis.resolved_assists(&snap.config.assist, frange)?.into_iter() {
|
||||
res.push(to_proto::resolved_code_action(&snap, assist)?);
|
||||
}
|
||||
}
|
||||
|
@ -833,7 +832,7 @@ pub(crate) fn handle_code_action(
|
|||
}
|
||||
|
||||
pub(crate) fn handle_resolve_code_action(
|
||||
snap: GlobalStateSnapshot,
|
||||
mut snap: GlobalStateSnapshot,
|
||||
params: lsp_ext::ResolveCodeActionParams,
|
||||
) -> Result<Option<lsp_ext::SnippetWorkspaceEdit>> {
|
||||
let _p = profile("handle_resolve_code_action");
|
||||
|
@ -841,13 +840,14 @@ pub(crate) fn handle_resolve_code_action(
|
|||
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 only = params
|
||||
|
||||
snap.config.assist.allowed = params
|
||||
.code_action_params
|
||||
.context
|
||||
.only
|
||||
.map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
|
||||
|
||||
let assists = snap.analysis.resolved_assists(&snap.config.assist, frange, only)?;
|
||||
let assists = snap.analysis.resolved_assists(&snap.config.assist, frange)?;
|
||||
let (id_string, index) = split_delim(¶ms.id, ':').unwrap();
|
||||
let index = index.parse::<usize>().unwrap();
|
||||
let assist = &assists[index];
|
||||
|
|
Loading…
Reference in a new issue