Skip to content

Commit 8489e20

Browse files
snippet edit support no longer experimental
1 parent e887c95 commit 8489e20

60 files changed

Lines changed: 483 additions & 421 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/ide-assists/src/assist_config.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
//! Settings for tweaking assists.
2-
//!
3-
//! The fun thing here is `SnippetCap` -- this type can only be created in this
4-
//! module, and we use to statically check that we only produce snippet
5-
//! assists if we are allowed to.
62
73
use hir::FindPathConfig;
84
use ide_db::{
9-
SnippetCap,
5+
WorkspaceSnippetCap,
106
assists::ExprFillDefaultMode,
117
imports::{import_assets::ImportPathConfig, insert_use::InsertUseConfig},
128
rename::RenameConfig,
@@ -16,7 +12,7 @@ use crate::AssistKind;
1612

1713
#[derive(Clone, Debug, PartialEq, Eq)]
1814
pub struct AssistConfig {
19-
pub snippet_cap: Option<SnippetCap>,
15+
pub workspace_snippet_cap: Option<WorkspaceSnippetCap>,
2016
pub allowed: Option<Vec<AssistKind>>,
2117
pub insert_use: InsertUseConfig,
2218
pub prefer_no_std: bool,

crates/ide-assists/src/handlers/add_label_to_loop.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ pub(crate) fn add_label_to_loop(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
5353
];
5454
editor.insert_all(Position::before(&loop_kw), elements);
5555

56-
if let Some(cap) = ctx.config.snippet_cap {
57-
editor.add_annotation(label.syntax(), builder.make_placeholder_snippet(cap));
56+
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
57+
editor.add_annotation(
58+
label.syntax(),
59+
builder.make_placeholder_snippet(workspace_snippet_cap),
60+
);
5861
}
5962

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

97-
if let Some(cap) = ctx.config.snippet_cap {
98-
editor.add_annotation(label.syntax(), builder.make_placeholder_snippet(cap));
100+
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
101+
editor.add_annotation(
102+
label.syntax(),
103+
builder.make_placeholder_snippet(workspace_snippet_cap),
104+
);
99105
}
100106
}
101107

crates/ide-assists/src/handlers/add_missing_impl_members.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ fn add_missing_impl_members_inner(
200200
first_new_item = assoc_item_list.assoc_items().next();
201201
}
202202

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

213213
if let Some(macro_call) = placeholder {
214-
let placeholder = edit.make_placeholder_snippet(cap);
214+
let placeholder = edit.make_placeholder_snippet(workspace_snippet_cap);
215215
editor.add_annotation(macro_call.syntax(), placeholder);
216216
} else if let Some(first_new_item) = first_new_item {
217-
let tabstop = edit.make_tabstop_before(cap);
217+
let tabstop = edit.make_tabstop_before(workspace_snippet_cap);
218218
editor.add_annotation(first_new_item.syntax(), tabstop);
219219
};
220220
};

crates/ide-assists/src/handlers/add_missing_match_arms.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,22 +278,31 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
278278
arms_edit.add_comma_after_last_arm(ctx, &make, &editor);
279279
arms_edit.append_arms(&missing_arms, &make, &editor);
280280

281-
if let Some(cap) = ctx.config.snippet_cap {
281+
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
282282
if let Some(it) = missing_arms
283283
.first()
284284
.and_then(|arm| arm.syntax().descendants().find_map(ast::WildcardPat::cast))
285285
{
286-
editor.add_annotation(it.syntax(), builder.make_placeholder_snippet(cap));
286+
editor.add_annotation(
287+
it.syntax(),
288+
builder.make_placeholder_snippet(workspace_snippet_cap),
289+
);
287290
}
288291

289292
for arm in &missing_arms {
290293
if let Some(expr) = arm.expr() {
291-
editor.add_annotation(expr.syntax(), builder.make_placeholder_snippet(cap));
294+
editor.add_annotation(
295+
expr.syntax(),
296+
builder.make_placeholder_snippet(workspace_snippet_cap),
297+
);
292298
}
293299
}
294300

295301
if let Some(arm) = missing_arms.last() {
296-
editor.add_annotation(arm.syntax(), builder.make_tabstop_after(cap));
302+
editor.add_annotation(
303+
arm.syntax(),
304+
builder.make_tabstop_after(workspace_snippet_cap),
305+
);
297306
}
298307
}
299308

crates/ide-assists/src/handlers/add_turbo_fish.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
112112
placeholder_ty.syntax().clone().into(),
113113
];
114114
editor.insert_all(Position::after(pat.syntax()), elements);
115-
if let Some(cap) = ctx.config.snippet_cap {
115+
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
116116
editor.add_annotation(
117117
placeholder_ty.syntax(),
118-
builder.make_placeholder_snippet(cap),
118+
builder.make_placeholder_snippet(workspace_snippet_cap),
119119
);
120120
}
121121
}
@@ -173,9 +173,12 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
173173
}
174174
};
175175

176-
if let Some(cap) = ctx.config.snippet_cap {
176+
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
177177
for arg in fish_head.generic_args() {
178-
editor.add_annotation(arg.syntax(), builder.make_placeholder_snippet(cap));
178+
editor.add_annotation(
179+
arg.syntax(),
180+
builder.make_placeholder_snippet(workspace_snippet_cap),
181+
);
179182
}
180183
}
181184
builder.add_file_edits(ctx.vfs_file_id(), editor);

crates/ide-assists/src/handlers/convert_from_to_tryfrom.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ pub(crate) fn convert_from_to_tryfrom(acc: &mut Assists, ctx: &AssistContext<'_>
9494
make.ty_alias(None, "Error", None, None, None, Some((make.ty("()"), None)));
9595
let error_type = ast::AssocItem::TypeAlias(error_type_alias);
9696

97-
if let Some(cap) = ctx.config.snippet_cap
97+
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap
9898
&& let ast::AssocItem::TypeAlias(type_alias) = &error_type
9999
&& let Some(ty) = type_alias.ty()
100100
{
101-
let placeholder = builder.make_placeholder_snippet(cap);
101+
let placeholder = builder.make_placeholder_snippet(workspace_snippet_cap);
102102
editor.add_annotation(ty.syntax(), placeholder);
103103
}
104104

crates/ide-assists/src/handlers/destructure_tuple_binding.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,10 @@ fn edit_tuple_assignment(
192192
.and_then(ast::RecordPatField::for_field_name)
193193
.is_some_and(|field| field.colon_token().is_none());
194194

195-
if let Some(cap) = ctx.config.snippet_cap {
195+
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
196196
// place cursor on first tuple name
197197
if let Some(ast::Pat::IdentPat(first_pat)) = tuple_pat.fields().next() {
198-
let annotation = edit.make_tabstop_before(cap);
198+
let annotation = edit.make_tabstop_before(workspace_snippet_cap);
199199
editor.add_annotation(
200200
first_pat.name().expect("first ident pattern should have a name").syntax(),
201201
annotation,

crates/ide-assists/src/handlers/extract_expressions_from_format_string.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub(crate) fn extract_expressions_from_format_string(
134134
let new_tt = make.token_tree(tt_delimiter, new_tt_bits);
135135
editor.replace(tt.syntax(), new_tt.syntax());
136136

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

147147
if stdx::always!(placeholder.kind() == T![_]) {
148-
let annotation = edit.make_placeholder_snippet(cap);
148+
let annotation = edit.make_placeholder_snippet(workspace_snippet_cap);
149149
editor.add_annotation(placeholder, annotation);
150150
}
151151
}
@@ -154,7 +154,7 @@ pub(crate) fn extract_expressions_from_format_string(
154154
if let Some(NodeOrToken::Token(literal)) =
155155
new_tt.token_trees_and_tokens().nth(1 + format_string_index)
156156
{
157-
let annotation = edit.make_tabstop_after(cap);
157+
let annotation = edit.make_tabstop_after(workspace_snippet_cap);
158158
editor.add_annotation(literal, annotation);
159159
}
160160
}
@@ -179,7 +179,7 @@ fn format_str_index(
179179
#[cfg(test)]
180180
mod tests {
181181
use super::*;
182-
use crate::tests::{check_assist, check_assist_no_snippet_cap};
182+
use crate::tests::{check_assist, check_assist_no_workspace_snippet_cap};
183183

184184
#[test]
185185
fn multiple_middle_arg() {
@@ -347,7 +347,7 @@ fn main() {
347347

348348
#[test]
349349
fn without_snippets() {
350-
check_assist_no_snippet_cap(
350+
check_assist_no_workspace_snippet_cap(
351351
extract_expressions_from_format_string,
352352
r#"
353353
//- minicore: fmt

crates/ide-assists/src/handlers/extract_function.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,21 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
168168
}
169169
_ => fn_def.indent_with_mapping(new_indent, make).syntax().clone(),
170170
};
171-
if let Some(cap) = ctx.config.snippet_cap {
171+
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap {
172172
let extracted_fn = fn_def.descendants().find_map(ast::Fn::cast);
173173
if let Some(fn_) = extracted_fn {
174174
if let Some(ws) = fn_
175175
.fn_token()
176176
.and_then(|tok| tok.next_token())
177177
.filter(|tok| tok.kind() == SyntaxKind::WHITESPACE)
178178
{
179-
editor.add_annotation(ws, builder.make_tabstop_after(cap));
179+
editor
180+
.add_annotation(ws, builder.make_tabstop_after(workspace_snippet_cap));
180181
} else if let Some(name) = fn_.name() {
181-
editor.add_annotation(name.syntax(), builder.make_tabstop_before(cap));
182+
editor.add_annotation(
183+
name.syntax(),
184+
builder.make_tabstop_before(workspace_snippet_cap),
185+
);
182186
}
183187
}
184188
}

crates/ide-assists/src/handlers/extract_type_alias.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
8787
let ty_alias =
8888
make.ty_alias(None, name, generic_params, None, None, Some((resolved_ty, None)));
8989

90-
if let Some(cap) = ctx.config.snippet_cap
90+
if let Some(workspace_snippet_cap) = ctx.config.workspace_snippet_cap
9191
&& let Some(name) = ty_alias.name()
9292
{
93-
editor.add_annotation(name.syntax(), builder.make_tabstop_before(cap));
93+
editor.add_annotation(
94+
name.syntax(),
95+
builder.make_tabstop_before(workspace_snippet_cap),
96+
);
9497
}
9598

9699
let indent = IndentLevel::from_node(node);

0 commit comments

Comments
 (0)