Skip to content

Commit a7c8aa7

Browse files
committed
add support of feature flag for runnables #4464
Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
2 parents c614374 + a4e6963 commit a7c8aa7

File tree

130 files changed

+2975
-1971
lines changed

Some content is hidden

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

130 files changed

+2975
-1971
lines changed

Cargo.lock

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! 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.
6+
7+
#[derive(Clone, Debug, PartialEq, Eq)]
8+
pub struct AssistConfig {
9+
pub snippet_cap: Option<SnippetCap>,
10+
}
11+
12+
impl AssistConfig {
13+
pub fn allow_snippets(&mut self, yes: bool) {
14+
self.snippet_cap = if yes { Some(SnippetCap { _private: () }) } else { None }
15+
}
16+
}
17+
18+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19+
pub struct SnippetCap {
20+
_private: (),
21+
}
22+
23+
impl Default for AssistConfig {
24+
fn default() -> Self {
25+
AssistConfig { snippet_cap: Some(SnippetCap { _private: () }) }
26+
}
27+
}

crates/ra_assists/src/assist_context.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use ra_syntax::{
1515
};
1616
use ra_text_edit::TextEditBuilder;
1717

18-
use crate::{Assist, AssistId, GroupLabel, ResolvedAssist};
18+
use crate::{
19+
assist_config::{AssistConfig, SnippetCap},
20+
Assist, AssistId, GroupLabel, ResolvedAssist,
21+
};
1922

2023
/// `AssistContext` allows to apply an assist or check if it could be applied.
2124
///
@@ -48,17 +51,22 @@ use crate::{Assist, AssistId, GroupLabel, ResolvedAssist};
4851
/// moment, because the LSP API is pretty awkward in this place, and it's much
4952
/// easier to just compute the edit eagerly :-)
5053
pub(crate) struct AssistContext<'a> {
54+
pub(crate) config: &'a AssistConfig,
5155
pub(crate) sema: Semantics<'a, RootDatabase>,
5256
pub(crate) db: &'a RootDatabase,
5357
pub(crate) frange: FileRange,
5458
source_file: SourceFile,
5559
}
5660

5761
impl<'a> AssistContext<'a> {
58-
pub fn new(sema: Semantics<'a, RootDatabase>, frange: FileRange) -> AssistContext<'a> {
62+
pub(crate) fn new(
63+
sema: Semantics<'a, RootDatabase>,
64+
config: &'a AssistConfig,
65+
frange: FileRange,
66+
) -> AssistContext<'a> {
5967
let source_file = sema.parse(frange.file_id);
6068
let db = sema.db;
61-
AssistContext { sema, db, frange, source_file }
69+
AssistContext { config, sema, db, frange, source_file }
6270
}
6371

6472
// NB, this ignores active selection.
@@ -163,13 +171,13 @@ impl Assists {
163171

164172
pub(crate) struct AssistBuilder {
165173
edit: TextEditBuilder,
166-
cursor_position: Option<TextSize>,
167174
file: FileId,
175+
is_snippet: bool,
168176
}
169177

170178
impl AssistBuilder {
171179
pub(crate) fn new(file: FileId) -> AssistBuilder {
172-
AssistBuilder { edit: TextEditBuilder::default(), cursor_position: None, file }
180+
AssistBuilder { edit: TextEditBuilder::default(), file, is_snippet: false }
173181
}
174182

175183
/// Remove specified `range` of text.
@@ -180,10 +188,30 @@ impl AssistBuilder {
180188
pub(crate) fn insert(&mut self, offset: TextSize, text: impl Into<String>) {
181189
self.edit.insert(offset, text.into())
182190
}
191+
/// Append specified `snippet` at the given `offset`
192+
pub(crate) fn insert_snippet(
193+
&mut self,
194+
_cap: SnippetCap,
195+
offset: TextSize,
196+
snippet: impl Into<String>,
197+
) {
198+
self.is_snippet = true;
199+
self.insert(offset, snippet);
200+
}
183201
/// Replaces specified `range` of text with a given string.
184202
pub(crate) fn replace(&mut self, range: TextRange, replace_with: impl Into<String>) {
185203
self.edit.replace(range, replace_with.into())
186204
}
205+
/// Replaces specified `range` of text with a given `snippet`.
206+
pub(crate) fn replace_snippet(
207+
&mut self,
208+
_cap: SnippetCap,
209+
range: TextRange,
210+
snippet: impl Into<String>,
211+
) {
212+
self.is_snippet = true;
213+
self.replace(range, snippet);
214+
}
187215
pub(crate) fn replace_ast<N: AstNode>(&mut self, old: N, new: N) {
188216
algo::diff(old.syntax(), new.syntax()).into_text_edit(&mut self.edit)
189217
}
@@ -207,10 +235,6 @@ impl AssistBuilder {
207235
algo::diff(&node, &new).into_text_edit(&mut self.edit)
208236
}
209237

210-
/// Specify desired position of the cursor after the assist is applied.
211-
pub(crate) fn set_cursor(&mut self, offset: TextSize) {
212-
self.cursor_position = Some(offset)
213-
}
214238
// FIXME: better API
215239
pub(crate) fn set_file(&mut self, assist_file: FileId) {
216240
self.file = assist_file;
@@ -224,10 +248,10 @@ impl AssistBuilder {
224248

225249
fn finish(self, change_label: String) -> SourceChange {
226250
let edit = self.edit.finish();
227-
if edit.is_empty() && self.cursor_position.is_none() {
228-
panic!("Only call `add_assist` if the assist can be applied")
251+
let mut res = SingleFileChange { label: change_label, edit }.into_source_change(self.file);
252+
if self.is_snippet {
253+
res.is_snippet = true;
229254
}
230-
SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position }
231-
.into_source_change(self.file)
255+
res
232256
}
233257
}

crates/ra_assists/src/handlers/add_custom_impl.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
// struct S;
2626
//
2727
// impl Debug for S {
28-
//
28+
// $0
2929
// }
3030
// ```
3131
pub(crate) fn add_custom_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
@@ -52,7 +52,7 @@ pub(crate) fn add_custom_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<
5252
format!("Add custom impl `{}` for `{}`", trait_token.text().as_str(), annotated_name);
5353

5454
let target = attr.syntax().text_range();
55-
acc.add(AssistId("add_custom_impl"), label, target, |edit| {
55+
acc.add(AssistId("add_custom_impl"), label, target, |builder| {
5656
let new_attr_input = input
5757
.syntax()
5858
.descendants_with_tokens()
@@ -63,35 +63,36 @@ pub(crate) fn add_custom_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<
6363
let has_more_derives = !new_attr_input.is_empty();
6464
let new_attr_input = new_attr_input.iter().sep_by(", ").surround_with("(", ")").to_string();
6565

66-
let mut buf = String::new();
67-
buf.push_str("\n\nimpl ");
68-
buf.push_str(trait_token.text().as_str());
69-
buf.push_str(" for ");
70-
buf.push_str(annotated_name.as_str());
71-
buf.push_str(" {\n");
72-
73-
let cursor_delta = if has_more_derives {
74-
let delta = input.syntax().text_range().len() - TextSize::of(&new_attr_input);
75-
edit.replace(input.syntax().text_range(), new_attr_input);
76-
delta
66+
if has_more_derives {
67+
builder.replace(input.syntax().text_range(), new_attr_input);
7768
} else {
7869
let attr_range = attr.syntax().text_range();
79-
edit.delete(attr_range);
70+
builder.delete(attr_range);
8071

8172
let line_break_range = attr
8273
.syntax()
8374
.next_sibling_or_token()
8475
.filter(|t| t.kind() == WHITESPACE)
8576
.map(|t| t.text_range())
8677
.unwrap_or_else(|| TextRange::new(TextSize::from(0), TextSize::from(0)));
87-
edit.delete(line_break_range);
88-
89-
attr_range.len() + line_break_range.len()
90-
};
91-
92-
edit.set_cursor(start_offset + TextSize::of(&buf) - cursor_delta);
93-
buf.push_str("\n}");
94-
edit.insert(start_offset, buf);
78+
builder.delete(line_break_range);
79+
}
80+
81+
match ctx.config.snippet_cap {
82+
Some(cap) => {
83+
builder.insert_snippet(
84+
cap,
85+
start_offset,
86+
format!("\n\nimpl {} for {} {{\n $0\n}}", trait_token, annotated_name),
87+
);
88+
}
89+
None => {
90+
builder.insert(
91+
start_offset,
92+
format!("\n\nimpl {} for {} {{\n\n}}", trait_token, annotated_name),
93+
);
94+
}
95+
}
9596
})
9697
}
9798

@@ -117,7 +118,7 @@ struct Foo {
117118
}
118119
119120
impl Debug for Foo {
120-
<|>
121+
$0
121122
}
122123
",
123124
)
@@ -139,7 +140,7 @@ pub struct Foo {
139140
}
140141
141142
impl Debug for Foo {
142-
<|>
143+
$0
143144
}
144145
",
145146
)
@@ -158,7 +159,7 @@ struct Foo {}
158159
struct Foo {}
159160
160161
impl Debug for Foo {
161-
<|>
162+
$0
162163
}
163164
",
164165
)

0 commit comments

Comments
 (0)