aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/lib.rs')
-rw-r--r--crates/ra_ide/src/lib.rs147
1 files changed, 91 insertions, 56 deletions
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index 03ad6b2c1..5fb111a90 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -10,12 +10,8 @@
10// For proving that RootDatabase is RefUnwindSafe. 10// For proving that RootDatabase is RefUnwindSafe.
11#![recursion_limit = "128"] 11#![recursion_limit = "128"]
12 12
13mod db;
14pub mod mock_analysis; 13pub mod mock_analysis;
15mod symbol_index;
16mod change;
17mod source_change; 14mod source_change;
18mod feature_flags;
19 15
20mod status; 16mod status;
21mod completion; 17mod completion;
@@ -35,14 +31,11 @@ mod assists;
35mod diagnostics; 31mod diagnostics;
36mod syntax_tree; 32mod syntax_tree;
37mod folding_ranges; 33mod folding_ranges;
38mod line_index;
39mod line_index_utils;
40mod join_lines; 34mod join_lines;
41mod typing; 35mod typing;
42mod matching_brace; 36mod matching_brace;
43mod display; 37mod display;
44mod inlay_hints; 38mod inlay_hints;
45mod wasm_shims;
46mod expand; 39mod expand;
47mod expand_macro; 40mod expand_macro;
48 41
@@ -58,24 +51,24 @@ use ra_db::{
58 salsa::{self, ParallelDatabase}, 51 salsa::{self, ParallelDatabase},
59 CheckCanceled, Env, FileLoader, SourceDatabase, 52 CheckCanceled, Env, FileLoader, SourceDatabase,
60}; 53};
54use ra_ide_db::{
55 symbol_index::{self, FileSymbol},
56 LineIndexDatabase,
57};
61use ra_syntax::{SourceFile, TextRange, TextUnit}; 58use ra_syntax::{SourceFile, TextRange, TextUnit};
62 59
63use crate::{db::LineIndexDatabase, display::ToNav, symbol_index::FileSymbol}; 60use crate::display::ToNav;
64 61
65pub use crate::{ 62pub use crate::{
66 assists::{Assist, AssistId}, 63 assists::{Assist, AssistId},
67 call_hierarchy::CallItem, 64 call_hierarchy::CallItem,
68 change::{AnalysisChange, LibraryData},
69 completion::{CompletionItem, CompletionItemKind, InsertTextFormat}, 65 completion::{CompletionItem, CompletionItemKind, InsertTextFormat},
70 diagnostics::Severity, 66 diagnostics::Severity,
71 display::{file_structure, FunctionSignature, NavigationTarget, StructureNode}, 67 display::{file_structure, FunctionSignature, NavigationTarget, StructureNode},
72 expand_macro::ExpandedMacro, 68 expand_macro::ExpandedMacro,
73 feature_flags::FeatureFlags,
74 folding_ranges::{Fold, FoldKind}, 69 folding_ranges::{Fold, FoldKind},
75 hover::HoverResult, 70 hover::HoverResult,
76 inlay_hints::{InlayHint, InlayKind}, 71 inlay_hints::{InlayHint, InlayKind},
77 line_index::{LineCol, LineIndex},
78 line_index_utils::translate_offset_with_edit,
79 references::{ 72 references::{
80 Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult, SearchScope, 73 Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult, SearchScope,
81 }, 74 },
@@ -88,6 +81,14 @@ pub use hir::Documentation;
88pub use ra_db::{ 81pub use ra_db::{
89 Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRootId, 82 Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRootId,
90}; 83};
84pub use ra_ide_db::{
85 change::{AnalysisChange, LibraryData},
86 feature_flags::FeatureFlags,
87 line_index::{LineCol, LineIndex},
88 line_index_utils::translate_offset_with_edit,
89 symbol_index::Query,
90 RootDatabase,
91};
91 92
92pub type Cancelable<T> = Result<T, Canceled>; 93pub type Cancelable<T> = Result<T, Canceled>;
93 94
@@ -99,46 +100,6 @@ pub struct Diagnostic {
99 pub severity: Severity, 100 pub severity: Severity,
100} 101}
101 102
102#[derive(Debug)]
103pub struct Query {
104 query: String,
105 lowercased: String,
106 only_types: bool,
107 libs: bool,
108 exact: bool,
109 limit: usize,
110}
111
112impl Query {
113 pub fn new(query: String) -> Query {
114 let lowercased = query.to_lowercase();
115 Query {
116 query,
117 lowercased,
118 only_types: false,
119 libs: false,
120 exact: false,
121 limit: usize::max_value(),
122 }
123 }
124
125 pub fn only_types(&mut self) {
126 self.only_types = true;
127 }
128
129 pub fn libs(&mut self) {
130 self.libs = true;
131 }
132
133 pub fn exact(&mut self) {
134 self.exact = true;
135 }
136
137 pub fn limit(&mut self, limit: usize) {
138 self.limit = limit
139 }
140}
141
142/// Info associated with a text range. 103/// Info associated with a text range.
143#[derive(Debug)] 104#[derive(Debug)]
144pub struct RangeInfo<T> { 105pub struct RangeInfo<T> {
@@ -163,7 +124,7 @@ pub struct CallInfo {
163/// `AnalysisHost` stores the current state of the world. 124/// `AnalysisHost` stores the current state of the world.
164#[derive(Debug)] 125#[derive(Debug)]
165pub struct AnalysisHost { 126pub struct AnalysisHost {
166 db: db::RootDatabase, 127 db: RootDatabase,
167} 128}
168 129
169impl Default for AnalysisHost { 130impl Default for AnalysisHost {
@@ -174,7 +135,7 @@ impl Default for AnalysisHost {
174 135
175impl AnalysisHost { 136impl AnalysisHost {
176 pub fn new(lru_capcity: Option<usize>, feature_flags: FeatureFlags) -> AnalysisHost { 137 pub fn new(lru_capcity: Option<usize>, feature_flags: FeatureFlags) -> AnalysisHost {
177 AnalysisHost { db: db::RootDatabase::new(lru_capcity, feature_flags) } 138 AnalysisHost { db: RootDatabase::new(lru_capcity, feature_flags) }
178 } 139 }
179 /// Returns a snapshot of the current state, which you can query for 140 /// Returns a snapshot of the current state, which you can query for
180 /// semantic information. 141 /// semantic information.
@@ -224,7 +185,7 @@ impl AnalysisHost {
224/// `Analysis` are canceled (most method return `Err(Canceled)`). 185/// `Analysis` are canceled (most method return `Err(Canceled)`).
225#[derive(Debug)] 186#[derive(Debug)]
226pub struct Analysis { 187pub struct Analysis {
227 db: salsa::Snapshot<db::RootDatabase>, 188 db: salsa::Snapshot<RootDatabase>,
228} 189}
229 190
230// As a general design guideline, `Analysis` API are intended to be independent 191// As a general design guideline, `Analysis` API are intended to be independent
@@ -505,7 +466,7 @@ impl Analysis {
505 } 466 }
506 467
507 /// Performs an operation on that may be Canceled. 468 /// Performs an operation on that may be Canceled.
508 fn with_db<F: FnOnce(&db::RootDatabase) -> T + std::panic::UnwindSafe, T>( 469 fn with_db<F: FnOnce(&RootDatabase) -> T + std::panic::UnwindSafe, T>(
509 &self, 470 &self,
510 f: F, 471 f: F,
511 ) -> Cancelable<T> { 472 ) -> Cancelable<T> {
@@ -518,3 +479,77 @@ fn analysis_is_send() {
518 fn is_send<T: Send>() {} 479 fn is_send<T: Send>() {}
519 is_send::<Analysis>(); 480 is_send::<Analysis>();
520} 481}
482
483#[cfg(test)]
484mod tests {
485 use crate::{display::NavigationTarget, mock_analysis::single_file, Query};
486 use ra_syntax::{
487 SmolStr,
488 SyntaxKind::{FN_DEF, STRUCT_DEF},
489 };
490
491 #[test]
492 fn test_world_symbols_with_no_container() {
493 let code = r#"
494 enum FooInner { }
495 "#;
496
497 let mut symbols = get_symbols_matching(code, "FooInner");
498
499 let s = symbols.pop().unwrap();
500
501 assert_eq!(s.name(), "FooInner");
502 assert!(s.container_name().is_none());
503 }
504
505 #[test]
506 fn test_world_symbols_include_container_name() {
507 let code = r#"
508fn foo() {
509 enum FooInner { }
510}
511 "#;
512
513 let mut symbols = get_symbols_matching(code, "FooInner");
514
515 let s = symbols.pop().unwrap();
516
517 assert_eq!(s.name(), "FooInner");
518 assert_eq!(s.container_name(), Some(&SmolStr::new("foo")));
519
520 let code = r#"
521mod foo {
522 struct FooInner;
523}
524 "#;
525
526 let mut symbols = get_symbols_matching(code, "FooInner");
527
528 let s = symbols.pop().unwrap();
529
530 assert_eq!(s.name(), "FooInner");
531 assert_eq!(s.container_name(), Some(&SmolStr::new("foo")));
532 }
533
534 #[test]
535 fn test_world_symbols_are_case_sensitive() {
536 let code = r#"
537fn foo() {}
538
539struct Foo;
540 "#;
541
542 let symbols = get_symbols_matching(code, "Foo");
543
544 let fn_match = symbols.iter().find(|s| s.name() == "foo").map(|s| s.kind());
545 let struct_match = symbols.iter().find(|s| s.name() == "Foo").map(|s| s.kind());
546
547 assert_eq!(fn_match, Some(FN_DEF));
548 assert_eq!(struct_match, Some(STRUCT_DEF));
549 }
550
551 fn get_symbols_matching(text: &str, query: &str) -> Vec<NavigationTarget> {
552 let (analysis, _) = single_file(text);
553 analysis.symbol_search(Query::new(query.into())).unwrap()
554 }
555}