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