Skip to content

Commit 0df9ece

Browse files
bors[bot]matklad
andauthored
Merge #5798
5798: Introduce Label r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents e81c310 + aa1a7a5 commit 0df9ece

File tree

8 files changed

+68
-30
lines changed

8 files changed

+68
-30
lines changed

crates/assists/src/assist_context.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use algo::find_covering_element;
66
use base_db::{FileId, FileRange};
77
use hir::Semantics;
88
use ide_db::{
9+
label::Label,
910
source_change::{SourceChange, SourceFileEdit},
1011
RootDatabase,
1112
};
@@ -157,8 +158,9 @@ impl Assists {
157158
if !self.is_allowed(&id) {
158159
return None;
159160
}
160-
let label = Assist::new(id, label.into(), None, target);
161-
self.add_impl(label, f)
161+
let label = Label::new(label.into());
162+
let assist = Assist { id, label, group: None, target };
163+
self.add_impl(assist, f)
162164
}
163165

164166
pub(crate) fn add_group(
@@ -172,12 +174,12 @@ impl Assists {
172174
if !self.is_allowed(&id) {
173175
return None;
174176
}
175-
176-
let label = Assist::new(id, label.into(), Some(group.clone()), target);
177-
self.add_impl(label, f)
177+
let label = Label::new(label.into());
178+
let assist = Assist { id, label, group: Some(group.clone()), target };
179+
self.add_impl(assist, f)
178180
}
179181

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<()> {
181183
let source_change = if self.resolve {
182184
let mut builder = AssistBuilder::new(self.file);
183185
f(&mut builder);
@@ -186,7 +188,7 @@ impl Assists {
186188
None
187189
};
188190

189-
self.buf.push((label, source_change));
191+
self.buf.push((assist, source_change));
190192
Some(())
191193
}
192194

crates/assists/src/lib.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub mod ast_transform;
1919

2020
use base_db::FileRange;
2121
use hir::Semantics;
22-
use ide_db::{source_change::SourceChange, RootDatabase};
22+
use ide_db::{label::Label, source_change::SourceChange, RootDatabase};
2323
use syntax::TextRange;
2424

2525
pub(crate) use crate::assist_context::{AssistContext, Assists};
@@ -68,7 +68,7 @@ pub struct GroupLabel(pub String);
6868
pub struct Assist {
6969
pub id: AssistId,
7070
/// Short description of the assist, as shown in the UI.
71-
label: String,
71+
pub label: Label,
7272
pub group: Option<GroupLabel>,
7373
/// Target ranges are used to sort assists: the smaller the target range,
7474
/// the more specific assist is, and so it should be sorted first.
@@ -82,11 +82,6 @@ pub struct ResolvedAssist {
8282
}
8383

8484
impl 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-
9085
/// Return all the assists applicable at the given position.
9186
///
9287
/// Assists are returned in the "unresolved" state, that is only labels are
@@ -118,10 +113,6 @@ impl Assist {
118113
});
119114
acc.finish_resolved()
120115
}
121-
122-
pub fn label(&self) -> &str {
123-
self.label.as_str()
124-
}
125116
}
126117

127118
mod handlers {

crates/ide/src/diagnostics.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax::{
1919
};
2020
use text_edit::TextEdit;
2121

22-
use crate::{FileId, SourceChange, SourceFileEdit};
22+
use crate::{FileId, Label, SourceChange, SourceFileEdit};
2323

2424
use self::fixes::DiagnosticWithFix;
2525

@@ -34,20 +34,15 @@ pub struct Diagnostic {
3434

3535
#[derive(Debug)]
3636
pub struct Fix {
37-
pub label: String,
37+
pub label: Label,
3838
pub source_change: SourceChange,
3939
/// Allows to trigger the fix only when the caret is in the range given
4040
pub fix_trigger_range: TextRange,
4141
}
4242

4343
impl Fix {
44-
fn new(
45-
label: impl Into<String>,
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('.'));
44+
fn new(label: &str, source_change: SourceChange, fix_trigger_range: TextRange) -> Self {
45+
let label = Label::new(label);
5146
Self { label, source_change, fix_trigger_range }
5247
}
5348
}

crates/ide/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub use base_db::{
8888
pub use hir::{Documentation, Semantics};
8989
pub use ide_db::{
9090
change::AnalysisChange,
91+
label::Label,
9192
line_index::{LineCol, LineIndex},
9293
search::SearchScope,
9394
source_change::{FileSystemEdit, SourceChange, SourceFileEdit},

crates/ide_db/src/label.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! See `Label`
2+
use 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)]
11+
pub struct Label(String);
12+
13+
impl PartialEq<str> for Label {
14+
fn eq(&self, other: &str) -> bool {
15+
self.0 == other
16+
}
17+
}
18+
19+
impl PartialEq<&'_ str> for Label {
20+
fn eq(&self, other: &&str) -> bool {
21+
self == *other
22+
}
23+
}
24+
25+
impl From<Label> for String {
26+
fn from(label: Label) -> String {
27+
label.0
28+
}
29+
}
30+
31+
impl 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+
39+
impl fmt::Display for Label {
40+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41+
fmt::Display::fmt(&self.0, f)
42+
}
43+
}
44+
45+
impl fmt::Debug for Label {
46+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47+
fmt::Debug::fmt(&self.0, f)
48+
}
49+
}

crates/ide_db/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! It is mainly a `HirDatabase` for semantic analysis, plus a `SymbolsDatabase`, for fuzzy search.
44
5+
pub mod label;
56
pub mod line_index;
67
pub mod symbol_index;
78
pub mod change;

crates/rust-analyzer/src/handlers.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,10 +782,9 @@ fn handle_fixes(
782782
.filter_map(|d| d.fix)
783783
.filter(|fix| fix.fix_trigger_range.intersect(range).is_some())
784784
{
785-
let title = fix.label;
786785
let edit = to_proto::snippet_workspace_edit(&snap, fix.source_change)?;
787786
let action = lsp_ext::CodeAction {
788-
title,
787+
title: fix.label.to_string(),
789788
id: None,
790789
group: None,
791790
kind: Some(CodeActionKind::QUICKFIX),

crates/rust-analyzer/src/to_proto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ pub(crate) fn unresolved_code_action(
704704
index: usize,
705705
) -> Result<lsp_ext::CodeAction> {
706706
let res = lsp_ext::CodeAction {
707-
title: assist.label().to_string(),
707+
title: assist.label.to_string(),
708708
id: Some(format!("{}:{}", assist.id.0, index.to_string())),
709709
group: assist.group.filter(|_| snap.config.client_caps.code_action_group).map(|gr| gr.0),
710710
kind: Some(code_action_kind(assist.id.1)),

0 commit comments

Comments
 (0)