diff options
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/assist_ctx.rs | 10 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/add_explicit_type.rs | 2 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/add_missing_impl_members.rs | 3 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/add_new.rs | 69 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/fill_match_arms.rs | 3 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/inline_local_variable.rs | 2 |
6 files changed, 76 insertions, 13 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index 1908bdec9..0ea84d548 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs | |||
@@ -1,6 +1,5 @@ | |||
1 | //! This module defines `AssistCtx` -- the API surface that is exposed to assists. | 1 | //! This module defines `AssistCtx` -- the API surface that is exposed to assists. |
2 | 2 | use hir::{db::HirDatabase, SourceAnalyzer}; | |
3 | use hir::db::HirDatabase; | ||
4 | use ra_db::FileRange; | 3 | use ra_db::FileRange; |
5 | use ra_fmt::{leading_indent, reindent}; | 4 | use ra_fmt::{leading_indent, reindent}; |
6 | use ra_syntax::{ | 5 | use ra_syntax::{ |
@@ -113,6 +112,13 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { | |||
113 | pub(crate) fn covering_element(&self) -> SyntaxElement { | 112 | pub(crate) fn covering_element(&self) -> SyntaxElement { |
114 | find_covering_element(self.source_file.syntax(), self.frange.range) | 113 | find_covering_element(self.source_file.syntax(), self.frange.range) |
115 | } | 114 | } |
115 | pub(crate) fn source_analyzer( | ||
116 | &self, | ||
117 | node: &SyntaxNode, | ||
118 | offset: Option<TextUnit>, | ||
119 | ) -> SourceAnalyzer { | ||
120 | SourceAnalyzer::new(self.db, hir::Source::new(self.frange.file_id.into(), node), offset) | ||
121 | } | ||
116 | 122 | ||
117 | pub(crate) fn covering_node_for_range(&self, range: TextRange) -> SyntaxElement { | 123 | pub(crate) fn covering_node_for_range(&self, range: TextRange) -> SyntaxElement { |
118 | find_covering_element(self.source_file.syntax(), range) | 124 | find_covering_element(self.source_file.syntax(), range) |
diff --git a/crates/ra_assists/src/assists/add_explicit_type.rs b/crates/ra_assists/src/assists/add_explicit_type.rs index ddda1a0f2..562a09685 100644 --- a/crates/ra_assists/src/assists/add_explicit_type.rs +++ b/crates/ra_assists/src/assists/add_explicit_type.rs | |||
@@ -40,7 +40,7 @@ pub(crate) fn add_explicit_type(ctx: AssistCtx<impl HirDatabase>) -> Option<Assi | |||
40 | } | 40 | } |
41 | // Infer type | 41 | // Infer type |
42 | let db = ctx.db; | 42 | let db = ctx.db; |
43 | let analyzer = hir::SourceAnalyzer::new(db, ctx.frange.file_id, stmt.syntax(), None); | 43 | let analyzer = ctx.source_analyzer(stmt.syntax(), None); |
44 | let ty = analyzer.type_of(db, &expr)?; | 44 | let ty = analyzer.type_of(db, &expr)?; |
45 | // Assist not applicable if the type is unknown | 45 | // Assist not applicable if the type is unknown |
46 | if is_unknown(&ty) { | 46 | if is_unknown(&ty) { |
diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs index 41de23921..91af161ee 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs | |||
@@ -100,8 +100,7 @@ fn add_missing_impl_members_inner( | |||
100 | let impl_item_list = impl_node.item_list()?; | 100 | let impl_item_list = impl_node.item_list()?; |
101 | 101 | ||
102 | let trait_def = { | 102 | let trait_def = { |
103 | let file_id = ctx.frange.file_id; | 103 | let analyzer = ctx.source_analyzer(impl_node.syntax(), None); |
104 | let analyzer = hir::SourceAnalyzer::new(ctx.db, file_id, impl_node.syntax(), None); | ||
105 | 104 | ||
106 | resolve_target_trait_def(ctx.db, &analyzer, &impl_node)? | 105 | resolve_target_trait_def(ctx.db, &analyzer, &impl_node)? |
107 | }; | 106 | }; |
diff --git a/crates/ra_assists/src/assists/add_new.rs b/crates/ra_assists/src/assists/add_new.rs index a8839cfba..2038afdc6 100644 --- a/crates/ra_assists/src/assists/add_new.rs +++ b/crates/ra_assists/src/assists/add_new.rs | |||
@@ -158,9 +158,12 @@ fn find_struct_impl( | |||
158 | let same_ty = blk.target_ty(db) == struct_ty; | 158 | let same_ty = blk.target_ty(db) == struct_ty; |
159 | let not_trait_impl = blk.target_trait(db).is_none(); | 159 | let not_trait_impl = blk.target_trait(db).is_none(); |
160 | 160 | ||
161 | found_new_fn = has_new_fn(impl_blk); | 161 | if !(same_ty && not_trait_impl) { |
162 | return false; | ||
163 | } | ||
162 | 164 | ||
163 | same_ty && not_trait_impl | 165 | found_new_fn = has_new_fn(impl_blk); |
166 | true | ||
164 | }); | 167 | }); |
165 | 168 | ||
166 | if found_new_fn { | 169 | if found_new_fn { |
@@ -186,9 +189,10 @@ fn has_new_fn(imp: &ast::ImplBlock) -> bool { | |||
186 | 189 | ||
187 | #[cfg(test)] | 190 | #[cfg(test)] |
188 | mod tests { | 191 | mod tests { |
189 | use super::*; | ||
190 | use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; | 192 | use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; |
191 | 193 | ||
194 | use super::*; | ||
195 | |||
192 | #[test] | 196 | #[test] |
193 | #[rustfmt::skip] | 197 | #[rustfmt::skip] |
194 | fn test_add_new() { | 198 | fn test_add_new() { |
@@ -345,7 +349,7 @@ struct Foo {<|>} | |||
345 | impl Foo { | 349 | impl Foo { |
346 | fn new() -> Self { | 350 | fn new() -> Self { |
347 | Self | 351 | Self |
348 | } | 352 | } |
349 | }", | 353 | }", |
350 | ); | 354 | ); |
351 | 355 | ||
@@ -357,7 +361,7 @@ struct Foo {<|>} | |||
357 | impl Foo { | 361 | impl Foo { |
358 | fn New() -> Self { | 362 | fn New() -> Self { |
359 | Self | 363 | Self |
360 | } | 364 | } |
361 | }", | 365 | }", |
362 | ); | 366 | ); |
363 | } | 367 | } |
@@ -376,4 +380,59 @@ struct EvenMoreIrrelevant; | |||
376 | struct Foo<'a, T: Foo<'a>> {}", | 380 | struct Foo<'a, T: Foo<'a>> {}", |
377 | ); | 381 | ); |
378 | } | 382 | } |
383 | |||
384 | #[test] | ||
385 | fn test_unrelated_new() { | ||
386 | check_assist( | ||
387 | add_new, | ||
388 | r##" | ||
389 | pub struct AstId<N: AstNode> { | ||
390 | file_id: HirFileId, | ||
391 | file_ast_id: FileAstId<N>, | ||
392 | } | ||
393 | |||
394 | impl<N: AstNode> AstId<N> { | ||
395 | pub fn new(file_id: HirFileId, file_ast_id: FileAstId<N>) -> AstId<N> { | ||
396 | AstId { file_id, file_ast_id } | ||
397 | } | ||
398 | } | ||
399 | |||
400 | pub struct Source<T> { | ||
401 | pub file_id: HirFileId,<|> | ||
402 | pub ast: T, | ||
403 | } | ||
404 | |||
405 | impl<T> Source<T> { | ||
406 | pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> { | ||
407 | Source { file_id: self.file_id, ast: f(self.ast) } | ||
408 | } | ||
409 | } | ||
410 | "##, | ||
411 | r##" | ||
412 | pub struct AstId<N: AstNode> { | ||
413 | file_id: HirFileId, | ||
414 | file_ast_id: FileAstId<N>, | ||
415 | } | ||
416 | |||
417 | impl<N: AstNode> AstId<N> { | ||
418 | pub fn new(file_id: HirFileId, file_ast_id: FileAstId<N>) -> AstId<N> { | ||
419 | AstId { file_id, file_ast_id } | ||
420 | } | ||
421 | } | ||
422 | |||
423 | pub struct Source<T> { | ||
424 | pub file_id: HirFileId, | ||
425 | pub ast: T, | ||
426 | } | ||
427 | |||
428 | impl<T> Source<T> { | ||
429 | pub fn new(file_id: HirFileId, ast: T) -> Self { Self { file_id, ast } }<|> | ||
430 | |||
431 | pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> { | ||
432 | Source { file_id: self.file_id, ast: f(self.ast) } | ||
433 | } | ||
434 | } | ||
435 | "##, | ||
436 | ); | ||
437 | } | ||
379 | } | 438 | } |
diff --git a/crates/ra_assists/src/assists/fill_match_arms.rs b/crates/ra_assists/src/assists/fill_match_arms.rs index 2b74f355c..b851c2082 100644 --- a/crates/ra_assists/src/assists/fill_match_arms.rs +++ b/crates/ra_assists/src/assists/fill_match_arms.rs | |||
@@ -47,8 +47,7 @@ pub(crate) fn fill_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist | |||
47 | 47 | ||
48 | let expr = match_expr.expr()?; | 48 | let expr = match_expr.expr()?; |
49 | let enum_def = { | 49 | let enum_def = { |
50 | let file_id = ctx.frange.file_id; | 50 | let analyzer = ctx.source_analyzer(expr.syntax(), None); |
51 | let analyzer = hir::SourceAnalyzer::new(ctx.db, file_id, expr.syntax(), None); | ||
52 | resolve_enum_def(ctx.db, &analyzer, &expr)? | 51 | resolve_enum_def(ctx.db, &analyzer, &expr)? |
53 | }; | 52 | }; |
54 | let variant_list = enum_def.variant_list()?; | 53 | let variant_list = enum_def.variant_list()?; |
diff --git a/crates/ra_assists/src/assists/inline_local_variable.rs b/crates/ra_assists/src/assists/inline_local_variable.rs index a7fd9b6d2..18a34502c 100644 --- a/crates/ra_assists/src/assists/inline_local_variable.rs +++ b/crates/ra_assists/src/assists/inline_local_variable.rs | |||
@@ -45,7 +45,7 @@ pub(crate) fn inline_local_varialbe(ctx: AssistCtx<impl HirDatabase>) -> Option< | |||
45 | } else { | 45 | } else { |
46 | let_stmt.syntax().text_range() | 46 | let_stmt.syntax().text_range() |
47 | }; | 47 | }; |
48 | let analyzer = hir::SourceAnalyzer::new(ctx.db, ctx.frange.file_id, bind_pat.syntax(), None); | 48 | let analyzer = ctx.source_analyzer(bind_pat.syntax(), None); |
49 | let refs = analyzer.find_all_refs(&bind_pat); | 49 | let refs = analyzer.find_all_refs(&bind_pat); |
50 | 50 | ||
51 | let mut wrap_in_parens = vec![true; refs.len()]; | 51 | let mut wrap_in_parens = vec![true; refs.len()]; |