close
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions crates/ide-assists/src/assist_config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
//! Settings for tweaking assists.
//!
//! The fun thing here is `SnippetCap` -- this type can only be created in this
//! module, and we use to statically check that we only produce snippet
//! assists if we are allowed to.

use hir::FindPathConfig;
use ide_db::{
SnippetCap,
WorkspaceSnippetCap,
assists::ExprFillDefaultMode,
imports::{import_assets::ImportPathConfig, insert_use::InsertUseConfig},
rename::RenameConfig,
Expand All @@ -16,7 +12,7 @@ use crate::AssistKind;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AssistConfig {
pub snippet_cap: Option<SnippetCap>,
pub workspace_snippet_cap: Option<WorkspaceSnippetCap>,
pub allowed: Option<Vec<AssistKind>>,
pub insert_use: InsertUseConfig,
pub prefer_no_std: bool,
Expand Down
14 changes: 10 additions & 4 deletions crates/ide-assists/src/handlers/add_label_to_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ pub(crate) fn add_label_to_loop(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
];
editor.insert_all(Position::before(&loop_kw), elements);

if let Some(cap) = ctx.config.snippet_cap {
editor.add_annotation(label.syntax(), builder.make_placeholder_snippet(cap));
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
editor.add_annotation(
label.syntax(),
builder.make_placeholder_snippet(workspace_snippet_cap),
);
}

let loop_body = loop_expr.loop_body().and_then(|it| it.stmt_list());
Expand Down Expand Up @@ -94,8 +97,11 @@ fn insert_label_after_token(
let elements = vec![make.whitespace(" ").into(), label.syntax().clone().into()];
editor.insert_all(Position::after(token), elements);

if let Some(cap) = ctx.config.snippet_cap {
editor.add_annotation(label.syntax(), builder.make_placeholder_snippet(cap));
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
editor.add_annotation(
label.syntax(),
builder.make_placeholder_snippet(workspace_snippet_cap),
);
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/ide-assists/src/handlers/add_missing_impl_members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fn add_missing_impl_members_inner(
first_new_item = assoc_item_list.assoc_items().next();
}

if let Some(cap) = ctx.config.snippet_cap {
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
let mut placeholder = None;
if let DefaultMethods::No = mode
&& let Some(ast::AssocItem::Fn(func)) = &first_new_item
Expand All @@ -211,10 +211,10 @@ fn add_missing_impl_members_inner(
}

if let Some(macro_call) = placeholder {
let placeholder = edit.make_placeholder_snippet(cap);
let placeholder = edit.make_placeholder_snippet(workspace_snippet_cap);
editor.add_annotation(macro_call.syntax(), placeholder);
} else if let Some(first_new_item) = first_new_item {
let tabstop = edit.make_tabstop_before(cap);
let tabstop = edit.make_tabstop_before(workspace_snippet_cap);
editor.add_annotation(first_new_item.syntax(), tabstop);
};
};
Expand Down
17 changes: 13 additions & 4 deletions crates/ide-assists/src/handlers/add_missing_match_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,22 +278,31 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
arms_edit.add_comma_after_last_arm(ctx, &make, &editor);
arms_edit.append_arms(&missing_arms, &make, &editor);

if let Some(cap) = ctx.config.snippet_cap {
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
if let Some(it) = missing_arms
.first()
.and_then(|arm| arm.syntax().descendants().find_map(ast::WildcardPat::cast))
{
editor.add_annotation(it.syntax(), builder.make_placeholder_snippet(cap));
editor.add_annotation(
it.syntax(),
builder.make_placeholder_snippet(workspace_snippet_cap),
);
}

for arm in &missing_arms {
if let Some(expr) = arm.expr() {
editor.add_annotation(expr.syntax(), builder.make_placeholder_snippet(cap));
editor.add_annotation(
expr.syntax(),
builder.make_placeholder_snippet(workspace_snippet_cap),
);
}
}

if let Some(arm) = missing_arms.last() {
editor.add_annotation(arm.syntax(), builder.make_tabstop_after(cap));
editor.add_annotation(
arm.syntax(),
builder.make_tabstop_after(workspace_snippet_cap),
);
}
}

Expand Down
11 changes: 7 additions & 4 deletions crates/ide-assists/src/handlers/add_turbo_fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
placeholder_ty.syntax().clone().into(),
];
editor.insert_all(Position::after(pat.syntax()), elements);
if let Some(cap) = ctx.config.snippet_cap {
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
editor.add_annotation(
placeholder_ty.syntax(),
builder.make_placeholder_snippet(cap),
builder.make_placeholder_snippet(workspace_snippet_cap),
);
}
}
Expand Down Expand Up @@ -173,9 +173,12 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
}
};

if let Some(cap) = ctx.config.snippet_cap {
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
for arg in fish_head.generic_args() {
editor.add_annotation(arg.syntax(), builder.make_placeholder_snippet(cap));
editor.add_annotation(
arg.syntax(),
builder.make_placeholder_snippet(workspace_snippet_cap),
);
}
}
builder.add_file_edits(ctx.vfs_file_id(), editor);
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-assists/src/handlers/convert_from_to_tryfrom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ pub(crate) fn convert_from_to_tryfrom(acc: &mut Assists, ctx: &AssistContext<'_>
make.ty_alias(None, "Error", None, None, None, Some((make.ty("()"), None)));
let error_type = ast::AssocItem::TypeAlias(error_type_alias);

if let Some(cap) = ctx.config.snippet_cap
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap
&& let ast::AssocItem::TypeAlias(type_alias) = &error_type
&& let Some(ty) = type_alias.ty()
{
let placeholder = builder.make_placeholder_snippet(cap);
let placeholder = builder.make_placeholder_snippet(workspace_snippet_cap);
editor.add_annotation(ty.syntax(), placeholder);
}

Expand Down
4 changes: 2 additions & 2 deletions crates/ide-assists/src/handlers/destructure_tuple_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ fn edit_tuple_assignment(
.and_then(ast::RecordPatField::for_field_name)
.is_some_and(|field| field.colon_token().is_none());

if let Some(cap) = ctx.config.snippet_cap {
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
// place cursor on first tuple name
if let Some(ast::Pat::IdentPat(first_pat)) = tuple_pat.fields().next() {
let annotation = edit.make_tabstop_before(cap);
let annotation = edit.make_tabstop_before(workspace_snippet_cap);
editor.add_annotation(
first_pat.name().expect("first ident pattern should have a name").syntax(),
annotation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub(crate) fn extract_expressions_from_format_string(
let new_tt = make.token_tree(tt_delimiter, new_tt_bits);
editor.replace(tt.syntax(), new_tt.syntax());

if let Some(cap) = ctx.config.snippet_cap {
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
// Add placeholder snippets over placeholder args
for pos in placeholder_indexes {
// Skip the opening delimiter
Expand All @@ -145,7 +145,7 @@ pub(crate) fn extract_expressions_from_format_string(
};

if stdx::always!(placeholder.kind() == T![_]) {
let annotation = edit.make_placeholder_snippet(cap);
let annotation = edit.make_placeholder_snippet(workspace_snippet_cap);
editor.add_annotation(placeholder, annotation);
}
}
Expand All @@ -154,7 +154,7 @@ pub(crate) fn extract_expressions_from_format_string(
if let Some(NodeOrToken::Token(literal)) =
new_tt.token_trees_and_tokens().nth(1 + format_string_index)
{
let annotation = edit.make_tabstop_after(cap);
let annotation = edit.make_tabstop_after(workspace_snippet_cap);
editor.add_annotation(literal, annotation);
}
}
Expand All @@ -179,7 +179,7 @@ fn format_str_index(
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::{check_assist, check_assist_no_snippet_cap};
use crate::tests::{check_assist, check_assist_no_workspace_snippet_cap};

#[test]
fn multiple_middle_arg() {
Expand Down Expand Up @@ -347,7 +347,7 @@ fn main() {

#[test]
fn without_snippets() {
check_assist_no_snippet_cap(
check_assist_no_workspace_snippet_cap(
extract_expressions_from_format_string,
r#"
//- minicore: fmt
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-assists/src/handlers/extract_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op

let fn_def = format_function(ctx, module, &fun, old_indent).clone_for_update();

if let Some(cap) = ctx.config.snippet_cap
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap
&& let Some(name) = fn_def.name()
{
builder.add_tabstop_before(cap, name);
builder.add_tabstop_before(workspace_snippet_cap, name);
}

// FIXME: wrap non-adt types
Expand Down
7 changes: 5 additions & 2 deletions crates/ide-assists/src/handlers/extract_type_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,13 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
let ty_alias =
make.ty_alias(None, name, generic_params, None, None, Some((resolved_ty, None)));

if let Some(cap) = ctx.config.snippet_cap
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap
&& let Some(name) = ty_alias.name()
{
editor.add_annotation(name.syntax(), builder.make_tabstop_before(cap));
editor.add_annotation(
name.syntax(),
builder.make_tabstop_before(workspace_snippet_cap),
);
}

let indent = IndentLevel::from_node(node);
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-assists/src/handlers/extract_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
let pat_name = make.name(&var_name);
let name_expr = make.expr_path(make.ident_path(&var_name));

if let Some(cap) = ctx.config.snippet_cap {
let tabstop = edit.make_tabstop_before(cap);
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
let tabstop = edit.make_tabstop_before(workspace_snippet_cap);
editor.add_annotation(pat_name.syntax().clone(), tabstop);
}

Expand Down
7 changes: 5 additions & 2 deletions crates/ide-assists/src/handlers/fix_visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ fn add_vis_to_referenced_module_def(acc: &mut Assists, ctx: &AssistContext<'_>)
);
}

if let Some(cap) = ctx.config.snippet_cap {
editor.add_annotation(missing_visibility.syntax(), builder.make_tabstop_before(cap));
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
editor.add_annotation(
missing_visibility.syntax(),
builder.make_tabstop_before(workspace_snippet_cap),
);
}

builder.add_file_edits(target_file, editor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ pub(crate) fn generate_blanket_trait_impl(
],
);

if let Some(cap) = ctx.config.snippet_cap
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap
&& let Some(self_ty) = impl_.self_ty()
{
builder.add_tabstop_before(cap, self_ty);
builder.add_tabstop_before(workspace_snippet_cap, self_ty);
}
builder.add_file_edits(ctx.vfs_file_id(), editor);
},
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-assists/src/handlers/generate_delegate_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
}
};

if let Some(cap) = ctx.config.snippet_cap
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap
&& let Some(fn_) = fn_
{
let tabstop = edit.make_tabstop_before(cap);
let tabstop = edit.make_tabstop_before(workspace_snippet_cap);
editor.add_annotation(fn_.syntax(), tabstop);
}
edit.add_file_edits(ctx.vfs_file_id(), editor);
Expand Down
6 changes: 3 additions & 3 deletions crates/ide-assists/src/handlers/generate_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{AssistContext, AssistId, Assists};
// }
// ```
pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let cap = ctx.config.snippet_cap?;
let workspace_snippet_cap = ctx.config.workspace_snippet_cap?;
let nominal = ctx.find_node_at_offset::<ast::Adt>()?;
let target = nominal.syntax().text_range();
let derive_attr = nominal
Expand Down Expand Up @@ -76,15 +76,15 @@ pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
.r_paren_token()
.expect("make::attr_outer was expected to have a R_PAREN");

let tabstop_before = edit.make_tabstop_before(cap);
let tabstop_before = edit.make_tabstop_before(workspace_snippet_cap);

editor.add_annotation(delimiter, tabstop_before);
edit.add_file_edits(ctx.vfs_file_id(), editor);
}
Some(_) => {
// Just move the cursor.
edit.add_tabstop_before_token(
cap,
workspace_snippet_cap,
delimiter.expect("Right delim token could not be found."),
);
}
Expand Down
7 changes: 5 additions & 2 deletions crates/ide-assists/src/handlers/generate_fn_type_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,13 @@ pub(crate) fn generate_fn_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>)
],
);

if let Some(cap) = ctx.config.snippet_cap
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap
&& let Some(name) = ty_alias.name()
{
editor.add_annotation(name.syntax(), builder.make_placeholder_snippet(cap));
editor.add_annotation(
name.syntax(),
builder.make_placeholder_snippet(workspace_snippet_cap),
);
}

builder.add_file_edits(ctx.vfs_file_id(), editor);
Expand Down
Loading
Loading