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.rs88
1 files changed, 53 insertions, 35 deletions
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index 09f602fe1..12d5716e8 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -16,7 +16,6 @@ macro_rules! eprintln {
16} 16}
17 17
18pub mod mock_analysis; 18pub mod mock_analysis;
19mod source_change;
20 19
21mod prime_caches; 20mod prime_caches;
22mod status; 21mod status;
@@ -24,6 +23,7 @@ mod completion;
24mod runnables; 23mod runnables;
25mod goto_definition; 24mod goto_definition;
26mod goto_type_definition; 25mod goto_type_definition;
26mod goto_implementation;
27mod extend_selection; 27mod extend_selection;
28mod hover; 28mod hover;
29mod call_hierarchy; 29mod call_hierarchy;
@@ -31,8 +31,6 @@ mod call_info;
31mod syntax_highlighting; 31mod syntax_highlighting;
32mod parent_module; 32mod parent_module;
33mod references; 33mod references;
34mod impls;
35mod assists;
36mod diagnostics; 34mod diagnostics;
37mod syntax_tree; 35mod syntax_tree;
38mod folding_ranges; 36mod folding_ranges;
@@ -44,11 +42,6 @@ mod inlay_hints;
44mod expand_macro; 42mod expand_macro;
45mod ssr; 43mod ssr;
46 44
47#[cfg(test)]
48mod marks;
49#[cfg(test)]
50mod test_utils;
51
52use std::sync::Arc; 45use std::sync::Arc;
53 46
54use ra_cfg::CfgOptions; 47use ra_cfg::CfgOptions;
@@ -65,7 +58,6 @@ use ra_syntax::{SourceFile, TextRange, TextSize};
65use crate::display::ToNav; 58use crate::display::ToNav;
66 59
67pub use crate::{ 60pub use crate::{
68 assists::{Assist, AssistId},
69 call_hierarchy::CallItem, 61 call_hierarchy::CallItem,
70 completion::{ 62 completion::{
71 CompletionConfig, CompletionItem, CompletionItemKind, CompletionScore, InsertTextFormat, 63 CompletionConfig, CompletionItem, CompletionItemKind, CompletionScore, InsertTextFormat,
@@ -78,7 +70,6 @@ pub use crate::{
78 inlay_hints::{InlayHint, InlayHintsConfig, InlayKind}, 70 inlay_hints::{InlayHint, InlayHintsConfig, InlayKind},
79 references::{Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult}, 71 references::{Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult},
80 runnables::{Runnable, RunnableKind, TestId}, 72 runnables::{Runnable, RunnableKind, TestId},
81 source_change::{FileSystemEdit, SourceChange, SourceFileEdit},
82 ssr::SsrError, 73 ssr::SsrError,
83 syntax_highlighting::{ 74 syntax_highlighting::{
84 Highlight, HighlightModifier, HighlightModifiers, HighlightTag, HighlightedRange, 75 Highlight, HighlightModifier, HighlightModifiers, HighlightTag, HighlightedRange,
@@ -86,17 +77,19 @@ pub use crate::{
86}; 77};
87 78
88pub use hir::Documentation; 79pub use hir::Documentation;
80pub use ra_assists::{AssistConfig, AssistId};
89pub use ra_db::{ 81pub use ra_db::{
90 Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRootId, 82 Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRootId,
91}; 83};
92pub use ra_ide_db::{ 84pub use ra_ide_db::{
93 change::{AnalysisChange, LibraryData}, 85 change::{AnalysisChange, LibraryData},
94 line_index::{LineCol, LineIndex}, 86 line_index::{LineCol, LineIndex},
95 line_index_utils::translate_offset_with_edit,
96 search::SearchScope, 87 search::SearchScope,
88 source_change::{FileSystemEdit, SourceChange, SourceFileEdit},
97 symbol_index::Query, 89 symbol_index::Query,
98 RootDatabase, 90 RootDatabase,
99}; 91};
92pub use ra_text_edit::{Indel, TextEdit};
100 93
101pub type Cancelable<T> = Result<T, Canceled>; 94pub type Cancelable<T> = Result<T, Canceled>;
102 95
@@ -104,8 +97,22 @@ pub type Cancelable<T> = Result<T, Canceled>;
104pub struct Diagnostic { 97pub struct Diagnostic {
105 pub message: String, 98 pub message: String,
106 pub range: TextRange, 99 pub range: TextRange,
107 pub fix: Option<SourceChange>,
108 pub severity: Severity, 100 pub severity: Severity,
101 pub fix: Option<Fix>,
102}
103
104#[derive(Debug)]
105pub struct Fix {
106 pub label: String,
107 pub source_change: SourceChange,
108}
109
110impl Fix {
111 pub fn new(label: impl Into<String>, source_change: SourceChange) -> Self {
112 let label = label.into();
113 assert!(label.starts_with(char::is_uppercase) && !label.ends_with('.'));
114 Self { label, source_change }
115 }
109} 116}
110 117
111/// Info associated with a text range. 118/// Info associated with a text range.
@@ -135,10 +142,12 @@ pub struct AnalysisHost {
135 db: RootDatabase, 142 db: RootDatabase,
136} 143}
137 144
138impl Default for AnalysisHost { 145#[derive(Debug)]
139 fn default() -> AnalysisHost { 146pub struct Assist {
140 AnalysisHost::new(None) 147 pub id: AssistId,
141 } 148 pub label: String,
149 pub group_label: Option<String>,
150 pub source_change: SourceChange,
142} 151}
143 152
144impl AnalysisHost { 153impl AnalysisHost {
@@ -176,18 +185,20 @@ impl AnalysisHost {
176 pub fn request_cancellation(&mut self) { 185 pub fn request_cancellation(&mut self) {
177 self.db.request_cancellation(); 186 self.db.request_cancellation();
178 } 187 }
179 pub fn raw_database( 188 pub fn raw_database(&self) -> &RootDatabase {
180 &self,
181 ) -> &(impl hir::db::HirDatabase + salsa::Database + ra_db::SourceDatabaseExt) {
182 &self.db 189 &self.db
183 } 190 }
184 pub fn raw_database_mut( 191 pub fn raw_database_mut(&mut self) -> &mut RootDatabase {
185 &mut self,
186 ) -> &mut (impl hir::db::HirDatabase + salsa::Database + ra_db::SourceDatabaseExt) {
187 &mut self.db 192 &mut self.db
188 } 193 }
189} 194}
190 195
196impl Default for AnalysisHost {
197 fn default() -> AnalysisHost {
198 AnalysisHost::new(None)
199 }
200}
201
191/// Analysis is a snapshot of a world state at a moment in time. It is the main 202/// Analysis is a snapshot of a world state at a moment in time. It is the main
192/// entry point for asking semantic information about the world. When the world 203/// entry point for asking semantic information about the world. When the world
193/// state is advanced using `AnalysisHost::apply_change` method, all existing 204/// state is advanced using `AnalysisHost::apply_change` method, all existing
@@ -289,20 +300,17 @@ impl Analysis {
289 300
290 /// Returns an edit to remove all newlines in the range, cleaning up minor 301 /// Returns an edit to remove all newlines in the range, cleaning up minor
291 /// stuff like trailing commas. 302 /// stuff like trailing commas.
292 pub fn join_lines(&self, frange: FileRange) -> Cancelable<SourceChange> { 303 pub fn join_lines(&self, frange: FileRange) -> Cancelable<TextEdit> {
293 self.with_db(|db| { 304 self.with_db(|db| {
294 let parse = db.parse(frange.file_id); 305 let parse = db.parse(frange.file_id);
295 let file_edit = SourceFileEdit { 306 join_lines::join_lines(&parse.tree(), frange.range)
296 file_id: frange.file_id,
297 edit: join_lines::join_lines(&parse.tree(), frange.range),
298 };
299 SourceChange::source_file_edit("join lines", file_edit)
300 }) 307 })
301 } 308 }
302 309
303 /// Returns an edit which should be applied when opening a new line, fixing 310 /// Returns an edit which should be applied when opening a new line, fixing
304 /// up minor stuff like continuing the comment. 311 /// up minor stuff like continuing the comment.
305 pub fn on_enter(&self, position: FilePosition) -> Cancelable<Option<SourceChange>> { 312 /// The edit will be a snippet (with `$0`).
313 pub fn on_enter(&self, position: FilePosition) -> Cancelable<Option<TextEdit>> {
306 self.with_db(|db| typing::on_enter(&db, position)) 314 self.with_db(|db| typing::on_enter(&db, position))
307 } 315 }
308 316
@@ -365,7 +373,7 @@ impl Analysis {
365 &self, 373 &self,
366 position: FilePosition, 374 position: FilePosition,
367 ) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> { 375 ) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
368 self.with_db(|db| impls::goto_implementation(db, position)) 376 self.with_db(|db| goto_implementation::goto_implementation(db, position))
369 } 377 }
370 378
371 /// Returns the type definitions for the symbol at `position`. 379 /// Returns the type definitions for the symbol at `position`.
@@ -456,16 +464,26 @@ impl Analysis {
456 /// Computes completions at the given position. 464 /// Computes completions at the given position.
457 pub fn completions( 465 pub fn completions(
458 &self, 466 &self,
459 position: FilePosition,
460 config: &CompletionConfig, 467 config: &CompletionConfig,
468 position: FilePosition,
461 ) -> Cancelable<Option<Vec<CompletionItem>>> { 469 ) -> Cancelable<Option<Vec<CompletionItem>>> {
462 self.with_db(|db| completion::completions(db, position, config).map(Into::into)) 470 self.with_db(|db| completion::completions(db, config, position).map(Into::into))
463 } 471 }
464 472
465 /// Computes assists (aka code actions aka intentions) for the given 473 /// Computes assists (aka code actions aka intentions) for the given
466 /// position. 474 /// position.
467 pub fn assists(&self, frange: FileRange) -> Cancelable<Vec<Assist>> { 475 pub fn assists(&self, config: &AssistConfig, frange: FileRange) -> Cancelable<Vec<Assist>> {
468 self.with_db(|db| assists::assists(db, frange)) 476 self.with_db(|db| {
477 ra_assists::Assist::resolved(db, config, frange)
478 .into_iter()
479 .map(|assist| Assist {
480 id: assist.assist.id,
481 label: assist.assist.label,
482 group_label: assist.assist.group.map(|it| it.0),
483 source_change: assist.source_change,
484 })
485 .collect()
486 })
469 } 487 }
470 488
471 /// Computes the set of diagnostics for the given file. 489 /// Computes the set of diagnostics for the given file.
@@ -490,7 +508,7 @@ impl Analysis {
490 ) -> Cancelable<Result<SourceChange, SsrError>> { 508 ) -> Cancelable<Result<SourceChange, SsrError>> {
491 self.with_db(|db| { 509 self.with_db(|db| {
492 let edits = ssr::parse_search_replace(query, parse_only, db)?; 510 let edits = ssr::parse_search_replace(query, parse_only, db)?;
493 Ok(SourceChange::source_file_edits("ssr", edits)) 511 Ok(SourceChange::source_file_edits(edits))
494 }) 512 })
495 } 513 }
496 514