aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-08-18 15:41:21 +0100
committerAleksey Kladov <[email protected]>2020-08-18 15:50:07 +0100
commitaa1a7a5414e59c7f1c6e74002df1b6a04937459e (patch)
tree6d112b6a8614138ebffa8bef0b27d90f8ef2cef9 /crates
parenteb81731600d1bc50efc00e32b9fc2244a2af52ad (diff)
Introduce Label
Diffstat (limited to 'crates')
-rw-r--r--crates/assists/src/assist_context.rs16
-rw-r--r--crates/assists/src/lib.rs13
-rw-r--r--crates/ide/src/diagnostics.rs13
-rw-r--r--crates/ide/src/lib.rs1
-rw-r--r--crates/ide_db/src/label.rs49
-rw-r--r--crates/ide_db/src/lib.rs1
-rw-r--r--crates/rust-analyzer/src/handlers.rs3
-rw-r--r--crates/rust-analyzer/src/to_proto.rs2
8 files changed, 68 insertions, 30 deletions
diff --git a/crates/assists/src/assist_context.rs b/crates/assists/src/assist_context.rs
index 79574b9ac..11c171fc2 100644
--- a/crates/assists/src/assist_context.rs
+++ b/crates/assists/src/assist_context.rs
@@ -6,6 +6,7 @@ use algo::find_covering_element;
6use base_db::{FileId, FileRange}; 6use base_db::{FileId, FileRange};
7use hir::Semantics; 7use hir::Semantics;
8use ide_db::{ 8use ide_db::{
9 label::Label,
9 source_change::{SourceChange, SourceFileEdit}, 10 source_change::{SourceChange, SourceFileEdit},
10 RootDatabase, 11 RootDatabase,
11}; 12};
@@ -157,8 +158,9 @@ impl Assists {
157 if !self.is_allowed(&id) { 158 if !self.is_allowed(&id) {
158 return None; 159 return None;
159 } 160 }
160 let label = Assist::new(id, label.into(), None, target); 161 let label = Label::new(label.into());
161 self.add_impl(label, f) 162 let assist = Assist { id, label, group: None, target };
163 self.add_impl(assist, f)
162 } 164 }
163 165
164 pub(crate) fn add_group( 166 pub(crate) fn add_group(
@@ -172,12 +174,12 @@ impl Assists {
172 if !self.is_allowed(&id) { 174 if !self.is_allowed(&id) {
173 return None; 175 return None;
174 } 176 }
175 177 let label = Label::new(label.into());
176 let label = Assist::new(id, label.into(), Some(group.clone()), target); 178 let assist = Assist { id, label, group: Some(group.clone()), target };
177 self.add_impl(label, f) 179 self.add_impl(assist, f)
178 } 180 }
179 181
180 fn add_impl(&mut self, label: Assist, f: impl FnOnce(&mut AssistBuilder)) -> Option<()> { 182 fn add_impl(&mut self, assist: Assist, f: impl FnOnce(&mut AssistBuilder)) -> Option<()> {
181 let source_change = if self.resolve { 183 let source_change = if self.resolve {
182 let mut builder = AssistBuilder::new(self.file); 184 let mut builder = AssistBuilder::new(self.file);
183 f(&mut builder); 185 f(&mut builder);
@@ -186,7 +188,7 @@ impl Assists {
186 None 188 None
187 }; 189 };
188 190
189 self.buf.push((label, source_change)); 191 self.buf.push((assist, source_change));
190 Some(()) 192 Some(())
191 } 193 }
192 194
diff --git a/crates/assists/src/lib.rs b/crates/assists/src/lib.rs
index c589b08dc..14834480a 100644
--- a/crates/assists/src/lib.rs
+++ b/crates/assists/src/lib.rs
@@ -19,7 +19,7 @@ pub mod ast_transform;
19 19
20use base_db::FileRange; 20use base_db::FileRange;
21use hir::Semantics; 21use hir::Semantics;
22use ide_db::{source_change::SourceChange, RootDatabase}; 22use ide_db::{label::Label, source_change::SourceChange, RootDatabase};
23use syntax::TextRange; 23use syntax::TextRange;
24 24
25pub(crate) use crate::assist_context::{AssistContext, Assists}; 25pub(crate) use crate::assist_context::{AssistContext, Assists};
@@ -68,7 +68,7 @@ pub struct GroupLabel(pub String);
68pub struct Assist { 68pub struct Assist {
69 pub id: AssistId, 69 pub id: AssistId,
70 /// Short description of the assist, as shown in the UI. 70 /// Short description of the assist, as shown in the UI.
71 label: String, 71 pub label: Label,
72 pub group: Option<GroupLabel>, 72 pub group: Option<GroupLabel>,
73 /// Target ranges are used to sort assists: the smaller the target range, 73 /// Target ranges are used to sort assists: the smaller the target range,
74 /// the more specific assist is, and so it should be sorted first. 74 /// the more specific assist is, and so it should be sorted first.
@@ -82,11 +82,6 @@ pub struct ResolvedAssist {
82} 82}
83 83
84impl Assist { 84impl Assist {
85 fn new(id: AssistId, label: String, group: Option<GroupLabel>, target: TextRange) -> Assist {
86 assert!(label.starts_with(char::is_uppercase));
87 Assist { id, label, group, target }
88 }
89
90 /// Return all the assists applicable at the given position. 85 /// Return all the assists applicable at the given position.
91 /// 86 ///
92 /// Assists are returned in the "unresolved" state, that is only labels are 87 /// Assists are returned in the "unresolved" state, that is only labels are
@@ -118,10 +113,6 @@ impl Assist {
118 }); 113 });
119 acc.finish_resolved() 114 acc.finish_resolved()
120 } 115 }
121
122 pub fn label(&self) -> &str {
123 self.label.as_str()
124 }
125} 116}
126 117
127mod handlers { 118mod handlers {
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
index 1f85805d2..3c36c0e15 100644
--- a/crates/ide/src/diagnostics.rs
+++ b/crates/ide/src/diagnostics.rs
@@ -19,7 +19,7 @@ use syntax::{
19}; 19};
20use text_edit::TextEdit; 20use text_edit::TextEdit;
21 21
22use crate::{FileId, SourceChange, SourceFileEdit}; 22use crate::{FileId, Label, SourceChange, SourceFileEdit};
23 23
24use self::fixes::DiagnosticWithFix; 24use self::fixes::DiagnosticWithFix;
25 25
@@ -34,20 +34,15 @@ pub struct Diagnostic {
34 34
35#[derive(Debug)] 35#[derive(Debug)]
36pub struct Fix { 36pub struct Fix {
37 pub label: String, 37 pub label: Label,
38 pub source_change: SourceChange, 38 pub source_change: SourceChange,
39 /// Allows to trigger the fix only when the caret is in the range given 39 /// Allows to trigger the fix only when the caret is in the range given
40 pub fix_trigger_range: TextRange, 40 pub fix_trigger_range: TextRange,
41} 41}
42 42
43impl Fix { 43impl Fix {
44 fn new( 44 fn new(label: &str, source_change: SourceChange, fix_trigger_range: TextRange) -> Self {
45 label: impl Into<String>, 45 let label = Label::new(label);
46 source_change: SourceChange,
47 fix_trigger_range: TextRange,
48 ) -> Self {
49 let label = label.into();
50 assert!(label.starts_with(char::is_uppercase) && !label.ends_with('.'));
51 Self { label, source_change, fix_trigger_range } 46 Self { label, source_change, fix_trigger_range }
52 } 47 }
53} 48}
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs
index f37119e28..e3af6d5bc 100644
--- a/crates/ide/src/lib.rs
+++ b/crates/ide/src/lib.rs
@@ -88,6 +88,7 @@ pub use base_db::{
88pub use hir::{Documentation, Semantics}; 88pub use hir::{Documentation, Semantics};
89pub use ide_db::{ 89pub use ide_db::{
90 change::AnalysisChange, 90 change::AnalysisChange,
91 label::Label,
91 line_index::{LineCol, LineIndex}, 92 line_index::{LineCol, LineIndex},
92 search::SearchScope, 93 search::SearchScope,
93 source_change::{FileSystemEdit, SourceChange, SourceFileEdit}, 94 source_change::{FileSystemEdit, SourceChange, SourceFileEdit},
diff --git a/crates/ide_db/src/label.rs b/crates/ide_db/src/label.rs
new file mode 100644
index 000000000..c0e89e72f
--- /dev/null
+++ b/crates/ide_db/src/label.rs
@@ -0,0 +1,49 @@
1//! See `Label`
2use std::fmt;
3
4/// A type to specify UI label, like an entry in the list of assists. Enforces
5/// proper casing:
6///
7/// Frobnicate bar
8///
9/// Note the upper-case first letter and the absence of `.` at the end.
10#[derive(Clone)]
11pub struct Label(String);
12
13impl PartialEq<str> for Label {
14 fn eq(&self, other: &str) -> bool {
15 self.0 == other
16 }
17}
18
19impl PartialEq<&'_ str> for Label {
20 fn eq(&self, other: &&str) -> bool {
21 self == *other
22 }
23}
24
25impl From<Label> for String {
26 fn from(label: Label) -> String {
27 label.0
28 }
29}
30
31impl Label {
32 pub fn new(label: impl Into<String>) -> Label {
33 let label = label.into();
34 assert!(label.starts_with(char::is_uppercase) && !label.ends_with('.'));
35 Label(label)
36 }
37}
38
39impl fmt::Display for Label {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 fmt::Display::fmt(&self.0, f)
42 }
43}
44
45impl fmt::Debug for Label {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 fmt::Debug::fmt(&self.0, f)
48 }
49}
diff --git a/crates/ide_db/src/lib.rs b/crates/ide_db/src/lib.rs
index fd474cd0f..70ada02f3 100644
--- a/crates/ide_db/src/lib.rs
+++ b/crates/ide_db/src/lib.rs
@@ -2,6 +2,7 @@
2//! 2//!
3//! It is mainly a `HirDatabase` for semantic analysis, plus a `SymbolsDatabase`, for fuzzy search. 3//! It is mainly a `HirDatabase` for semantic analysis, plus a `SymbolsDatabase`, for fuzzy search.
4 4
5pub mod label;
5pub mod line_index; 6pub mod line_index;
6pub mod symbol_index; 7pub mod symbol_index;
7pub mod change; 8pub mod change;
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs
index 69ab1b3b1..f8159d0f4 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -782,10 +782,9 @@ fn handle_fixes(
782 .filter_map(|d| d.fix) 782 .filter_map(|d| d.fix)
783 .filter(|fix| fix.fix_trigger_range.intersect(range).is_some()) 783 .filter(|fix| fix.fix_trigger_range.intersect(range).is_some())
784 { 784 {
785 let title = fix.label;
786 let edit = to_proto::snippet_workspace_edit(&snap, fix.source_change)?; 785 let edit = to_proto::snippet_workspace_edit(&snap, fix.source_change)?;
787 let action = lsp_ext::CodeAction { 786 let action = lsp_ext::CodeAction {
788 title, 787 title: fix.label.to_string(),
789 id: None, 788 id: None,
790 group: None, 789 group: None,
791 kind: Some(CodeActionKind::QUICKFIX), 790 kind: Some(CodeActionKind::QUICKFIX),
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index 535de2f71..643dcb4fc 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -704,7 +704,7 @@ pub(crate) fn unresolved_code_action(
704 index: usize, 704 index: usize,
705) -> Result<lsp_ext::CodeAction> { 705) -> Result<lsp_ext::CodeAction> {
706 let res = lsp_ext::CodeAction { 706 let res = lsp_ext::CodeAction {
707 title: assist.label().to_string(), 707 title: assist.label.to_string(),
708 id: Some(format!("{}:{}", assist.id.0, index.to_string())), 708 id: Some(format!("{}:{}", assist.id.0, index.to_string())),
709 group: assist.group.filter(|_| snap.config.client_caps.code_action_group).map(|gr| gr.0), 709 group: assist.group.filter(|_| snap.config.client_caps.code_action_group).map(|gr| gr.0),
710 kind: Some(code_action_kind(assist.id.1)), 710 kind: Some(code_action_kind(assist.id.1)),