Skip to content

Commit 5397f05

Browse files
bors[bot]matklad
andauthored
Merge #3049
3049: Introduce assists utils r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 1996762 + d00add1 commit 5397f05

30 files changed

+97
-109
lines changed

crates/ra_assists/src/assist_ctx.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub(crate) enum Assist {
1919
Resolved { assist: ResolvedAssist },
2020
}
2121

22+
pub(crate) type AssistHandler = fn(AssistCtx) -> Option<Assist>;
23+
2224
/// `AssistCtx` allows to apply an assist or check if it could be applied.
2325
///
2426
/// Assists use a somewhat over-engineered approach, given the current needs. The
@@ -57,7 +59,7 @@ pub(crate) struct AssistCtx<'a> {
5759
should_compute_edit: bool,
5860
}
5961

60-
impl<'a> Clone for AssistCtx<'a> {
62+
impl Clone for AssistCtx<'_> {
6163
fn clone(&self) -> Self {
6264
AssistCtx {
6365
db: self.db,
@@ -69,31 +71,18 @@ impl<'a> Clone for AssistCtx<'a> {
6971
}
7072

7173
impl<'a> AssistCtx<'a> {
72-
pub(crate) fn with_ctx<F, T>(
73-
db: &RootDatabase,
74-
frange: FileRange,
75-
should_compute_edit: bool,
76-
f: F,
77-
) -> T
78-
where
79-
F: FnOnce(AssistCtx) -> T,
80-
{
74+
pub fn new(db: &RootDatabase, frange: FileRange, should_compute_edit: bool) -> AssistCtx {
8175
let parse = db.parse(frange.file_id);
82-
83-
let ctx = AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit };
84-
f(ctx)
76+
AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit }
8577
}
86-
}
8778

88-
impl<'a> AssistCtx<'a> {
8979
pub(crate) fn add_assist(
9080
self,
9181
id: AssistId,
9282
label: impl Into<String>,
9383
f: impl FnOnce(&mut ActionBuilder),
9484
) -> Option<Assist> {
95-
let label = AssistLabel { label: label.into(), id };
96-
assert!(label.label.chars().nth(0).unwrap().is_uppercase());
85+
let label = AssistLabel::new(label.into(), id);
9786

9887
let assist = if self.should_compute_edit {
9988
let action = {
@@ -115,7 +104,7 @@ impl<'a> AssistCtx<'a> {
115104
label: impl Into<String>,
116105
f: impl FnOnce() -> Vec<ActionBuilder>,
117106
) -> Option<Assist> {
118-
let label = AssistLabel { label: label.into(), id };
107+
let label = AssistLabel::new(label.into(), id);
119108
let assist = if self.should_compute_edit {
120109
let actions = f();
121110
assert!(!actions.is_empty(), "Assist cannot have no");

crates/ra_assists/src/assists/apply_demorgan.rs renamed to crates/ra_assists/src/handlers/apply_demorgan.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use super::invert_if::invert_boolean_expression;
21
use ra_syntax::ast::{self, AstNode};
32

4-
use crate::{Assist, AssistCtx, AssistId};
3+
use crate::{utils::invert_boolean_expression, Assist, AssistCtx, AssistId};
54

65
// Assist: apply_demorgan
76
//

crates/ra_assists/src/assists/auto_import.rs renamed to crates/ra_assists/src/handlers/auto_import.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use hir::ModPath;
2+
use ra_ide_db::imports_locator::ImportsLocator;
23
use ra_syntax::{
34
ast::{self, AstNode},
45
SyntaxNode,
@@ -8,7 +9,7 @@ use crate::{
89
assist_ctx::{ActionBuilder, Assist, AssistCtx},
910
auto_import_text_edit, AssistId,
1011
};
11-
use ra_ide_db::imports_locator::ImportsLocator;
12+
use std::collections::BTreeSet;
1213

1314
// Assist: auto_import
1415
//
@@ -60,7 +61,8 @@ pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> {
6061
.filter_map(|module_def| module_with_name_to_import.find_use_path(ctx.db, module_def))
6162
.filter(|use_path| !use_path.segments.is_empty())
6263
.take(20)
63-
.collect::<std::collections::BTreeSet<_>>();
64+
.collect::<BTreeSet<_>>();
65+
6466
if proposed_imports.is_empty() {
6567
return None;
6668
}
@@ -82,9 +84,10 @@ fn import_to_action(import: ModPath, position: &SyntaxNode, anchor: &SyntaxNode)
8284

8385
#[cfg(test)]
8486
mod tests {
85-
use super::*;
8687
use crate::helpers::{check_assist, check_assist_not_applicable};
8788

89+
use super::*;
90+
8891
#[test]
8992
fn applicable_when_found_an_import() {
9093
check_assist(

0 commit comments

Comments
 (0)