Initial commit of highlight related configuration w/ implementation.

This commit is contained in:
Kevin DeLorey
2021-07-21 19:44:16 -06:00
parent 1dd1814100
commit b75e0e7bb1
6 changed files with 78 additions and 10 deletions

View File

@@ -18,6 +18,14 @@ pub struct HighlightedRange {
pub access: Option<ReferenceAccess>, pub access: Option<ReferenceAccess>,
} }
#[derive(Default, Clone)]
pub struct HighlightRelatedConfig {
pub references: bool,
pub exit_points: bool,
pub break_points: bool,
pub yield_points: bool,
}
// Feature: Highlight Related // Feature: Highlight Related
// //
// Highlights constructs related to the thing under the cursor: // Highlights constructs related to the thing under the cursor:
@@ -27,6 +35,7 @@ pub struct HighlightedRange {
// - if on a `break`, `loop`, `while` or `for` token, highlights all break points for that loop or block context // - if on a `break`, `loop`, `while` or `for` token, highlights all break points for that loop or block context
pub(crate) fn highlight_related( pub(crate) fn highlight_related(
sema: &Semantics<RootDatabase>, sema: &Semantics<RootDatabase>,
config: HighlightRelatedConfig,
position: FilePosition, position: FilePosition,
) -> Option<Vec<HighlightedRange>> { ) -> Option<Vec<HighlightedRange>> {
let _p = profile::span("highlight_related"); let _p = profile::span("highlight_related");
@@ -46,10 +55,13 @@ pub(crate) fn highlight_related(
})?; })?;
match token.kind() { match token.kind() {
T![return] | T![?] | T![->] => highlight_exit_points(sema, token), T![return] | T![?] | T![->] if config.exit_points => highlight_exit_points(sema, token),
T![await] | T![async] => highlight_yield_points(token), T![await] | T![async] if config.yield_points => highlight_yield_points(token),
T![break] | T![loop] | T![for] | T![while] => highlight_break_points(token), T![break] | T![loop] | T![for] | T![while] if config.break_points => {
_ => highlight_references(sema, &syntax, position), highlight_break_points(token)
}
_ if config.references => highlight_references(sema, &syntax, position),
_ => None,
} }
} }
@@ -261,7 +273,14 @@ mod tests {
fn check(ra_fixture: &str) { fn check(ra_fixture: &str) {
let (analysis, pos, annotations) = fixture::annotations(ra_fixture); let (analysis, pos, annotations) = fixture::annotations(ra_fixture);
let hls = analysis.highlight_related(pos).unwrap().unwrap(); let config = HighlightRelatedConfig {
break_points: true,
exit_points: true,
references: true,
yield_points: true,
};
let hls = analysis.highlight_related(config, pos).unwrap().unwrap();
let mut expected = annotations let mut expected = annotations
.into_iter() .into_iter()

View File

@@ -76,7 +76,7 @@ pub use crate::{
expand_macro::ExpandedMacro, expand_macro::ExpandedMacro,
file_structure::{StructureNode, StructureNodeKind}, file_structure::{StructureNode, StructureNodeKind},
folding_ranges::{Fold, FoldKind}, folding_ranges::{Fold, FoldKind},
highlight_related::HighlightedRange, highlight_related::{HighlightRelatedConfig, HighlightedRange},
hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult}, hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult},
inlay_hints::{InlayHint, InlayHintsConfig, InlayKind}, inlay_hints::{InlayHint, InlayHintsConfig, InlayKind},
join_lines::JoinLinesConfig, join_lines::JoinLinesConfig,
@@ -496,9 +496,12 @@ impl Analysis {
/// Computes all ranges to highlight for a given item in a file. /// Computes all ranges to highlight for a given item in a file.
pub fn highlight_related( pub fn highlight_related(
&self, &self,
config: HighlightRelatedConfig,
position: FilePosition, position: FilePosition,
) -> Cancellable<Option<Vec<HighlightedRange>>> { ) -> Cancellable<Option<Vec<HighlightedRange>>> {
self.with_db(|db| highlight_related::highlight_related(&Semantics::new(db), position)) self.with_db(|db| {
highlight_related::highlight_related(&Semantics::new(db), config, position)
})
} }
/// Computes syntax highlighting for the given file range. /// Computes syntax highlighting for the given file range.

View File

@@ -11,8 +11,8 @@ use std::{ffi::OsString, iter, path::PathBuf};
use flycheck::FlycheckConfig; use flycheck::FlycheckConfig;
use ide::{ use ide::{
AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, HoverDocFormat, AssistConfig, CompletionConfig, DiagnosticsConfig, HighlightRelatedConfig, HoverConfig,
InlayHintsConfig, JoinLinesConfig, HoverDocFormat, InlayHintsConfig, JoinLinesConfig,
}; };
use ide_db::helpers::{ use ide_db::helpers::{
insert_use::{ImportGranularity, InsertUseConfig, PrefixKind}, insert_use::{ImportGranularity, InsertUseConfig, PrefixKind},
@@ -261,6 +261,14 @@ config_data! {
workspace_symbol_search_scope: WorskpaceSymbolSearchScopeDef = "\"workspace\"", workspace_symbol_search_scope: WorskpaceSymbolSearchScopeDef = "\"workspace\"",
/// Workspace symbol search kind. /// Workspace symbol search kind.
workspace_symbol_search_kind: WorskpaceSymbolSearchKindDef = "\"only_types\"", workspace_symbol_search_kind: WorskpaceSymbolSearchKindDef = "\"only_types\"",
highlightRelated_references: bool = "true",
highlightRelated_exitPoints: bool = "true",
highlightRelated_breakPoints: bool = "true",
highlightRelated_yieldPoints: bool = "true",
} }
} }
@@ -852,6 +860,15 @@ impl Config {
false false
) )
} }
pub fn highlight_related(&self) -> HighlightRelatedConfig {
HighlightRelatedConfig {
references: self.data.highlightRelated_references,
break_points: self.data.highlightRelated_breakPoints,
exit_points: self.data.highlightRelated_exitPoints,
yield_points: self.data.highlightRelated_yieldPoints,
}
}
} }
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]

View File

@@ -1188,7 +1188,7 @@ pub(crate) fn handle_document_highlight(
let position = from_proto::file_position(&snap, params.text_document_position_params)?; let position = from_proto::file_position(&snap, params.text_document_position_params)?;
let line_index = snap.file_line_index(position.file_id)?; let line_index = snap.file_line_index(position.file_id)?;
let refs = match snap.analysis.highlight_related(position)? { let refs = match snap.analysis.highlight_related(snap.config.highlight_related(), position)? {
None => return Ok(None), None => return Ok(None),
Some(refs) => refs, Some(refs) => refs,
}; };

View File

@@ -881,6 +881,26 @@
"Search for all symbols kinds" "Search for all symbols kinds"
] ]
}, },
"rust-analyzer.highlightRelated.references": {
"markdownDescription": "Enables highlighting of related references while hovering your mouse above any identifier.",
"default": true,
"type": "boolean"
},
"rust-analyzer.highlightRelated.exitPoints": {
"markdownDescription": "Enables highlighting of all exit points while hovering your mouse above any `return`, `?`, or return type arrow (`->`).",
"default": true,
"type": "boolean"
},
"rust-analyzer.highlightRelated.breakPoints": {
"markdownDescription": "Enables highlighting of related references while hovering your mouse `break`, `loop`, `while`, or `for` keywords.",
"default": true,
"type": "boolean"
},
"rust-analyzer.highlightRelated.yieldPoints": {
"markdownDescription": "Enables highlighting of all break points for a loop or block context while hovering your mouse above any `async` or `await` keywords.",
"default": true,
"type": "boolean"
},
"$generated-end": {} "$generated-end": {}
} }
}, },

View File

@@ -172,6 +172,15 @@ export class Config {
}; };
} }
get highlightRelated() {
return {
references: this.get<boolean>("highlightRelated.references"),
exitPoints: this.get<boolean>("highlightRelated.exit_points"),
breakPoints: this.get<boolean>("highlightRelated.exit_points"),
yieldPoints: this.get<boolean>("highlightRelated.yield_points")
};
}
get currentExtensionIsNightly() { get currentExtensionIsNightly() {
return this.package.releaseTag === NIGHTLY_TAG; return this.package.releaseTag === NIGHTLY_TAG;
} }