diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-02-06 14:14:47 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-02-06 14:14:47 +0000 |
commit | ff2d77bde6acffc5e4c42878606b3d6d92300e11 (patch) | |
tree | 76b0a8bbf94b0c68dd3aa891e0d9ea5cdf067863 /crates/ra_ide/src/lib.rs | |
parent | 19de59a9233a09a9b70a96a6c49213b119819c46 (diff) | |
parent | 355c98fd0861acf0f0fddad08cbc923fee0698fb (diff) |
Merge #3029
3029: Docs r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide/src/lib.rs')
-rw-r--r-- | crates/ra_ide/src/lib.rs | 147 |
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 | ||
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; |
@@ -35,14 +31,11 @@ mod assists; | |||
35 | mod diagnostics; | 31 | mod diagnostics; |
36 | mod syntax_tree; | 32 | mod syntax_tree; |
37 | mod folding_ranges; | 33 | mod folding_ranges; |
38 | mod line_index; | ||
39 | mod line_index_utils; | ||
40 | mod join_lines; | 34 | mod join_lines; |
41 | mod typing; | 35 | mod typing; |
42 | mod matching_brace; | 36 | mod matching_brace; |
43 | mod display; | 37 | mod display; |
44 | mod inlay_hints; | 38 | mod inlay_hints; |
45 | mod wasm_shims; | ||
46 | mod expand; | 39 | mod expand; |
47 | mod expand_macro; | 40 | mod 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 | }; |
54 | use ra_ide_db::{ | ||
55 | symbol_index::{self, FileSymbol}, | ||
56 | LineIndexDatabase, | ||
57 | }; | ||
61 | use ra_syntax::{SourceFile, TextRange, TextUnit}; | 58 | use ra_syntax::{SourceFile, TextRange, TextUnit}; |
62 | 59 | ||
63 | use crate::{db::LineIndexDatabase, display::ToNav, symbol_index::FileSymbol}; | 60 | use crate::display::ToNav; |
64 | 61 | ||
65 | pub use crate::{ | 62 | pub 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; | |||
88 | pub use ra_db::{ | 81 | pub use ra_db::{ |
89 | Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRootId, | 82 | Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRootId, |
90 | }; | 83 | }; |
84 | pub 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 | ||
92 | pub type Cancelable<T> = Result<T, Canceled>; | 93 | pub 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)] | ||
103 | pub struct Query { | ||
104 | query: String, | ||
105 | lowercased: String, | ||
106 | only_types: bool, | ||
107 | libs: bool, | ||
108 | exact: bool, | ||
109 | limit: usize, | ||
110 | } | ||
111 | |||
112 | impl 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)] |
144 | pub struct RangeInfo<T> { | 105 | pub 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)] |
165 | pub struct AnalysisHost { | 126 | pub struct AnalysisHost { |
166 | db: db::RootDatabase, | 127 | db: RootDatabase, |
167 | } | 128 | } |
168 | 129 | ||
169 | impl Default for AnalysisHost { | 130 | impl Default for AnalysisHost { |
@@ -174,7 +135,7 @@ impl Default for AnalysisHost { | |||
174 | 135 | ||
175 | impl AnalysisHost { | 136 | impl 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)] |
226 | pub struct Analysis { | 187 | pub 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)] | ||
484 | mod 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#" | ||
508 | fn 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#" | ||
521 | mod 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#" | ||
537 | fn foo() {} | ||
538 | |||
539 | struct 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 | } | ||