@@ -68,22 +68,29 @@ pub struct FirstSegmentUnresolved {
6868pub enum NameToImport {
6969 /// Requires items with names that exactly match the given string, bool indicates case-sensitivity.
7070 Exact ( String , bool ) ,
71- /// Requires items with names that case-insensitively contain all letters from the string,
71+ /// Requires items with names that match the given string by prefix, bool indicates case-sensitivity.
72+ Prefix ( String , bool ) ,
73+ /// Requires items with names contain all letters from the string,
7274 /// in the same order, but not necessary adjacent.
73- Fuzzy ( String ) ,
75+ Fuzzy ( String , bool ) ,
7476}
7577
7678impl NameToImport {
7779 pub fn exact_case_sensitive ( s : String ) -> NameToImport {
7880 NameToImport :: Exact ( s, true )
7981 }
80- }
8182
82- impl NameToImport {
83+ pub fn fuzzy ( s : String ) -> NameToImport {
84+ // unless all chars are lowercase, we do a case sensitive search
85+ let case_sensitive = s. chars ( ) . any ( |c| c. is_uppercase ( ) ) ;
86+ NameToImport :: Fuzzy ( s, case_sensitive)
87+ }
88+
8389 pub fn text ( & self ) -> & str {
8490 match self {
85- NameToImport :: Exact ( text, _) => text. as_str ( ) ,
86- NameToImport :: Fuzzy ( text) => text. as_str ( ) ,
91+ NameToImport :: Prefix ( text, _)
92+ | NameToImport :: Exact ( text, _)
93+ | NameToImport :: Fuzzy ( text, _) => text. as_str ( ) ,
8794 }
8895 }
8996}
@@ -165,7 +172,7 @@ impl ImportAssets {
165172 Some ( Self {
166173 import_candidate : ImportCandidate :: TraitMethod ( TraitImportCandidate {
167174 receiver_ty,
168- assoc_item_name : NameToImport :: Fuzzy ( fuzzy_method_name) ,
175+ assoc_item_name : NameToImport :: fuzzy ( fuzzy_method_name) ,
169176 } ) ,
170177 module_with_candidate : module_with_method_call,
171178 candidate_node,
@@ -228,12 +235,30 @@ impl ImportAssets {
228235 self . search_for ( sema, None , prefer_no_std)
229236 }
230237
231- pub fn path_fuzzy_name_to_exact ( & mut self , case_sensitive : bool ) {
238+ /// Requires imports to by prefix instead of fuzzily.
239+ pub fn path_fuzzy_name_to_prefix ( & mut self ) {
240+ if let ImportCandidate :: Path ( PathImportCandidate { name : to_import, .. } ) =
241+ & mut self . import_candidate
242+ {
243+ let ( name, case_sensitive) = match to_import {
244+ NameToImport :: Fuzzy ( name, case_sensitive) => {
245+ ( std:: mem:: take ( name) , * case_sensitive)
246+ }
247+ _ => return ,
248+ } ;
249+ * to_import = NameToImport :: Prefix ( name, case_sensitive) ;
250+ }
251+ }
252+
253+ /// Requires imports to match exactly instead of fuzzily.
254+ pub fn path_fuzzy_name_to_exact ( & mut self ) {
232255 if let ImportCandidate :: Path ( PathImportCandidate { name : to_import, .. } ) =
233256 & mut self . import_candidate
234257 {
235- let name = match to_import {
236- NameToImport :: Fuzzy ( name) => std:: mem:: take ( name) ,
258+ let ( name, case_sensitive) = match to_import {
259+ NameToImport :: Fuzzy ( name, case_sensitive) => {
260+ ( std:: mem:: take ( name) , * case_sensitive)
261+ }
237262 _ => return ,
238263 } ;
239264 * to_import = NameToImport :: Exact ( name, case_sensitive) ;
@@ -623,7 +648,7 @@ impl ImportCandidate {
623648 fuzzy_name : String ,
624649 sema : & Semantics < ' _ , RootDatabase > ,
625650 ) -> Option < Self > {
626- path_import_candidate ( sema, qualifier, NameToImport :: Fuzzy ( fuzzy_name) )
651+ path_import_candidate ( sema, qualifier, NameToImport :: fuzzy ( fuzzy_name) )
627652 }
628653}
629654
0 commit comments