Skip to content

Commit b75e0e7

Browse files
committed
Initial commit of highlight related configuration w/ implementation.
1 parent 1dd1814 commit b75e0e7

File tree

6 files changed

+78
-10
lines changed

6 files changed

+78
-10
lines changed

crates/ide/src/highlight_related.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ pub struct HighlightedRange {
1818
pub access: Option<ReferenceAccess>,
1919
}
2020

21+
#[derive(Default, Clone)]
22+
pub struct HighlightRelatedConfig {
23+
pub references: bool,
24+
pub exit_points: bool,
25+
pub break_points: bool,
26+
pub yield_points: bool,
27+
}
28+
2129
// Feature: Highlight Related
2230
//
2331
// Highlights constructs related to the thing under the cursor:
@@ -27,6 +35,7 @@ pub struct HighlightedRange {
2735
// - if on a `break`, `loop`, `while` or `for` token, highlights all break points for that loop or block context
2836
pub(crate) fn highlight_related(
2937
sema: &Semantics<RootDatabase>,
38+
config: HighlightRelatedConfig,
3039
position: FilePosition,
3140
) -> Option<Vec<HighlightedRange>> {
3241
let _p = profile::span("highlight_related");
@@ -46,10 +55,13 @@ pub(crate) fn highlight_related(
4655
})?;
4756

4857
match token.kind() {
49-
T![return] | T![?] | T![->] => highlight_exit_points(sema, token),
50-
T![await] | T![async] => highlight_yield_points(token),
51-
T![break] | T![loop] | T![for] | T![while] => highlight_break_points(token),
52-
_ => highlight_references(sema, &syntax, position),
58+
T![return] | T![?] | T![->] if config.exit_points => highlight_exit_points(sema, token),
59+
T![await] | T![async] if config.yield_points => highlight_yield_points(token),
60+
T![break] | T![loop] | T![for] | T![while] if config.break_points => {
61+
highlight_break_points(token)
62+
}
63+
_ if config.references => highlight_references(sema, &syntax, position),
64+
_ => None,
5365
}
5466
}
5567

@@ -261,7 +273,14 @@ mod tests {
261273

262274
fn check(ra_fixture: &str) {
263275
let (analysis, pos, annotations) = fixture::annotations(ra_fixture);
264-
let hls = analysis.highlight_related(pos).unwrap().unwrap();
276+
let config = HighlightRelatedConfig {
277+
break_points: true,
278+
exit_points: true,
279+
references: true,
280+
yield_points: true,
281+
};
282+
283+
let hls = analysis.highlight_related(config, pos).unwrap().unwrap();
265284

266285
let mut expected = annotations
267286
.into_iter()

crates/ide/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub use crate::{
7676
expand_macro::ExpandedMacro,
7777
file_structure::{StructureNode, StructureNodeKind},
7878
folding_ranges::{Fold, FoldKind},
79-
highlight_related::HighlightedRange,
79+
highlight_related::{HighlightRelatedConfig, HighlightedRange},
8080
hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult},
8181
inlay_hints::{InlayHint, InlayHintsConfig, InlayKind},
8282
join_lines::JoinLinesConfig,
@@ -496,9 +496,12 @@ impl Analysis {
496496
/// Computes all ranges to highlight for a given item in a file.
497497
pub fn highlight_related(
498498
&self,
499+
config: HighlightRelatedConfig,
499500
position: FilePosition,
500501
) -> Cancellable<Option<Vec<HighlightedRange>>> {
501-
self.with_db(|db| highlight_related::highlight_related(&Semantics::new(db), position))
502+
self.with_db(|db| {
503+
highlight_related::highlight_related(&Semantics::new(db), config, position)
504+
})
502505
}
503506

504507
/// Computes syntax highlighting for the given file range.

crates/rust-analyzer/src/config.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use std::{ffi::OsString, iter, path::PathBuf};
1111

1212
use flycheck::FlycheckConfig;
1313
use ide::{
14-
AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, HoverDocFormat,
15-
InlayHintsConfig, JoinLinesConfig,
14+
AssistConfig, CompletionConfig, DiagnosticsConfig, HighlightRelatedConfig, HoverConfig,
15+
HoverDocFormat, InlayHintsConfig, JoinLinesConfig,
1616
};
1717
use ide_db::helpers::{
1818
insert_use::{ImportGranularity, InsertUseConfig, PrefixKind},
@@ -261,6 +261,14 @@ config_data! {
261261
workspace_symbol_search_scope: WorskpaceSymbolSearchScopeDef = "\"workspace\"",
262262
/// Workspace symbol search kind.
263263
workspace_symbol_search_kind: WorskpaceSymbolSearchKindDef = "\"only_types\"",
264+
265+
highlightRelated_references: bool = "true",
266+
267+
highlightRelated_exitPoints: bool = "true",
268+
269+
highlightRelated_breakPoints: bool = "true",
270+
271+
highlightRelated_yieldPoints: bool = "true",
264272
}
265273
}
266274

@@ -852,6 +860,15 @@ impl Config {
852860
false
853861
)
854862
}
863+
864+
pub fn highlight_related(&self) -> HighlightRelatedConfig {
865+
HighlightRelatedConfig {
866+
references: self.data.highlightRelated_references,
867+
break_points: self.data.highlightRelated_breakPoints,
868+
exit_points: self.data.highlightRelated_exitPoints,
869+
yield_points: self.data.highlightRelated_yieldPoints,
870+
}
871+
}
855872
}
856873

857874
#[derive(Deserialize, Debug, Clone)]

crates/rust-analyzer/src/handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ pub(crate) fn handle_document_highlight(
11881188
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
11891189
let line_index = snap.file_line_index(position.file_id)?;
11901190

1191-
let refs = match snap.analysis.highlight_related(position)? {
1191+
let refs = match snap.analysis.highlight_related(snap.config.highlight_related(), position)? {
11921192
None => return Ok(None),
11931193
Some(refs) => refs,
11941194
};

editors/code/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,26 @@
881881
"Search for all symbols kinds"
882882
]
883883
},
884+
"rust-analyzer.highlightRelated.references": {
885+
"markdownDescription": "Enables highlighting of related references while hovering your mouse above any identifier.",
886+
"default": true,
887+
"type": "boolean"
888+
},
889+
"rust-analyzer.highlightRelated.exitPoints": {
890+
"markdownDescription": "Enables highlighting of all exit points while hovering your mouse above any `return`, `?`, or return type arrow (`->`).",
891+
"default": true,
892+
"type": "boolean"
893+
},
894+
"rust-analyzer.highlightRelated.breakPoints": {
895+
"markdownDescription": "Enables highlighting of related references while hovering your mouse `break`, `loop`, `while`, or `for` keywords.",
896+
"default": true,
897+
"type": "boolean"
898+
},
899+
"rust-analyzer.highlightRelated.yieldPoints": {
900+
"markdownDescription": "Enables highlighting of all break points for a loop or block context while hovering your mouse above any `async` or `await` keywords.",
901+
"default": true,
902+
"type": "boolean"
903+
},
884904
"$generated-end": {}
885905
}
886906
},

editors/code/src/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ export class Config {
172172
};
173173
}
174174

175+
get highlightRelated() {
176+
return {
177+
references: this.get<boolean>("highlightRelated.references"),
178+
exitPoints: this.get<boolean>("highlightRelated.exit_points"),
179+
breakPoints: this.get<boolean>("highlightRelated.exit_points"),
180+
yieldPoints: this.get<boolean>("highlightRelated.yield_points")
181+
};
182+
}
183+
175184
get currentExtensionIsNightly() {
176185
return this.package.releaseTag === NIGHTLY_TAG;
177186
}

0 commit comments

Comments
 (0)