Skip to content

Commit d011e79

Browse files
committed
Auto merge of #14162 - azriel91:bugfix/14161/generate-getter-snippet-cap-check, r=Veykril
Conditionally add snippet marker. Fixes #14161. Heya, I just added the code with some tests, but not sure if it's *the way* to do it -- I didn't refactor existing methods for the `check` test method, but added another calling layer.
2 parents 1f2d33f + a6f54d6 commit d011e79

File tree

2 files changed

+146
-3
lines changed

2 files changed

+146
-3
lines changed

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

+105-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ pub(crate) fn generate_getter_impl(
180180

181181
// Insert `$0` only for last getter we generate
182182
if i == record_fields_count - 1 {
183-
getter_buf = getter_buf.replacen("fn ", "fn $0", 1);
183+
if ctx.config.snippet_cap.is_some() {
184+
getter_buf = getter_buf.replacen("fn ", "fn $0", 1);
185+
}
184186
}
185187

186188
// For first element we do not merge with '\n', as
@@ -330,7 +332,7 @@ fn parse_record_field(record_field: ast::RecordField, mutable: bool) -> Option<R
330332

331333
#[cfg(test)]
332334
mod tests {
333-
use crate::tests::{check_assist, check_assist_not_applicable};
335+
use crate::tests::{check_assist, check_assist_no_snippet_cap, check_assist_not_applicable};
334336

335337
use super::*;
336338

@@ -377,6 +379,49 @@ impl Context {
377379
);
378380
}
379381

382+
#[test]
383+
fn test_generate_getter_from_field_no_snippet_cap() {
384+
check_assist_no_snippet_cap(
385+
generate_getter,
386+
r#"
387+
struct Context {
388+
dat$0a: Data,
389+
}
390+
"#,
391+
r#"
392+
struct Context {
393+
data: Data,
394+
}
395+
396+
impl Context {
397+
fn data(&self) -> &Data {
398+
&self.data
399+
}
400+
}
401+
"#,
402+
);
403+
404+
check_assist_no_snippet_cap(
405+
generate_getter_mut,
406+
r#"
407+
struct Context {
408+
dat$0a: Data,
409+
}
410+
"#,
411+
r#"
412+
struct Context {
413+
data: Data,
414+
}
415+
416+
impl Context {
417+
fn data_mut(&mut self) -> &mut Data {
418+
&mut self.data
419+
}
420+
}
421+
"#,
422+
);
423+
}
424+
380425
#[test]
381426
fn test_generate_getter_already_implemented() {
382427
check_assist_not_applicable(
@@ -433,6 +478,29 @@ impl Context {
433478
);
434479
}
435480

481+
#[test]
482+
fn test_generate_getter_from_field_with_visibility_marker_no_snippet_cap() {
483+
check_assist_no_snippet_cap(
484+
generate_getter,
485+
r#"
486+
pub(crate) struct Context {
487+
dat$0a: Data,
488+
}
489+
"#,
490+
r#"
491+
pub(crate) struct Context {
492+
data: Data,
493+
}
494+
495+
impl Context {
496+
pub(crate) fn data(&self) -> &Data {
497+
&self.data
498+
}
499+
}
500+
"#,
501+
);
502+
}
503+
436504
#[test]
437505
fn test_multiple_generate_getter() {
438506
check_assist(
@@ -468,6 +536,41 @@ impl Context {
468536
);
469537
}
470538

539+
#[test]
540+
fn test_multiple_generate_getter_no_snippet_cap() {
541+
check_assist_no_snippet_cap(
542+
generate_getter,
543+
r#"
544+
struct Context {
545+
data: Data,
546+
cou$0nt: usize,
547+
}
548+
549+
impl Context {
550+
fn data(&self) -> &Data {
551+
&self.data
552+
}
553+
}
554+
"#,
555+
r#"
556+
struct Context {
557+
data: Data,
558+
count: usize,
559+
}
560+
561+
impl Context {
562+
fn data(&self) -> &Data {
563+
&self.data
564+
}
565+
566+
fn count(&self) -> &usize {
567+
&self.count
568+
}
569+
}
570+
"#,
571+
);
572+
}
573+
471574
#[test]
472575
fn test_not_a_special_case() {
473576
cov_mark::check_count!(convert_reference_type, 0);

crates/ide-assists/src/tests.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
3333
assist_emit_must_use: false,
3434
};
3535

36+
pub(crate) const TEST_CONFIG_NO_SNIPPET_CAP: AssistConfig = AssistConfig {
37+
snippet_cap: None,
38+
allowed: None,
39+
insert_use: InsertUseConfig {
40+
granularity: ImportGranularity::Crate,
41+
prefix_kind: hir::PrefixKind::Plain,
42+
enforce_granularity: true,
43+
group: true,
44+
skip_glob_imports: true,
45+
},
46+
prefer_no_std: false,
47+
assist_emit_must_use: false,
48+
};
49+
3650
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
3751
RootDatabase::with_single_file(text)
3852
}
@@ -43,6 +57,22 @@ pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_
4357
check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), None);
4458
}
4559

60+
#[track_caller]
61+
pub(crate) fn check_assist_no_snippet_cap(
62+
assist: Handler,
63+
ra_fixture_before: &str,
64+
ra_fixture_after: &str,
65+
) {
66+
let ra_fixture_after = trim_indent(ra_fixture_after);
67+
check_with_config(
68+
TEST_CONFIG_NO_SNIPPET_CAP,
69+
assist,
70+
ra_fixture_before,
71+
ExpectedResult::After(&ra_fixture_after),
72+
None,
73+
);
74+
}
75+
4676
// There is no way to choose what assist within a group you want to test against,
4777
// so this is here to allow you choose.
4878
pub(crate) fn check_assist_by_label(
@@ -119,14 +149,24 @@ enum ExpectedResult<'a> {
119149

120150
#[track_caller]
121151
fn check(handler: Handler, before: &str, expected: ExpectedResult<'_>, assist_label: Option<&str>) {
152+
check_with_config(TEST_CONFIG, handler, before, expected, assist_label);
153+
}
154+
155+
#[track_caller]
156+
fn check_with_config(
157+
config: AssistConfig,
158+
handler: Handler,
159+
before: &str,
160+
expected: ExpectedResult<'_>,
161+
assist_label: Option<&str>,
162+
) {
122163
let (mut db, file_with_caret_id, range_or_offset) = RootDatabase::with_range_or_offset(before);
123164
db.set_enable_proc_attr_macros(true);
124165
let text_without_caret = db.file_text(file_with_caret_id).to_string();
125166

126167
let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() };
127168

128169
let sema = Semantics::new(&db);
129-
let config = TEST_CONFIG;
130170
let ctx = AssistContext::new(sema, &config, frange);
131171
let resolve = match expected {
132172
ExpectedResult::Unresolved => AssistResolveStrategy::None,

0 commit comments

Comments
 (0)