Skip to content

Commit f621722

Browse files
committed
Declare hover_actions_* as local
1 parent 3e17039 commit f621722

File tree

2 files changed

+106
-97
lines changed

2 files changed

+106
-97
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 85 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -298,42 +298,6 @@ config_data! {
298298
files_watcher: FilesWatcherDef = "\"client\"",
299299

300300

301-
/// Whether to show `Debug` action. Only applies when
302-
/// `#rust-analyzer.hover.actions.enable#` is set.
303-
hover_actions_debug_enable: bool = "true",
304-
/// Whether to show HoverActions in Rust files.
305-
hover_actions_enable: bool = "true",
306-
/// Whether to show `Go to Type Definition` action. Only applies when
307-
/// `#rust-analyzer.hover.actions.enable#` is set.
308-
hover_actions_gotoTypeDef_enable: bool = "true",
309-
/// Whether to show `Implementations` action. Only applies when
310-
/// `#rust-analyzer.hover.actions.enable#` is set.
311-
hover_actions_implementations_enable: bool = "true",
312-
/// Whether to show `References` action. Only applies when
313-
/// `#rust-analyzer.hover.actions.enable#` is set.
314-
hover_actions_references_enable: bool = "false",
315-
/// Whether to show `Run` action. Only applies when
316-
/// `#rust-analyzer.hover.actions.enable#` is set.
317-
hover_actions_run_enable: bool = "true",
318-
319-
/// Whether to show documentation on hover.
320-
hover_documentation_enable: bool = "true",
321-
/// Whether to show keyword hover popups. Only applies when
322-
/// `#rust-analyzer.hover.documentation.enable#` is set.
323-
hover_documentation_keywords_enable: bool = "true",
324-
/// Use markdown syntax for links on hover.
325-
hover_links_enable: bool = "true",
326-
/// How to render the align information in a memory layout hover.
327-
hover_memoryLayout_alignment: Option<MemoryLayoutHoverRenderKindDef> = "\"hexadecimal\"",
328-
/// Whether to show memory layout data on hover.
329-
hover_memoryLayout_enable: bool = "true",
330-
/// How to render the niche information in a memory layout hover.
331-
hover_memoryLayout_niches: Option<bool> = "false",
332-
/// How to render the offset information in a memory layout hover.
333-
hover_memoryLayout_offset: Option<MemoryLayoutHoverRenderKindDef> = "\"hexadecimal\"",
334-
/// How to render the size information in a memory layout hover.
335-
hover_memoryLayout_size: Option<MemoryLayoutHoverRenderKindDef> = "\"both\"",
336-
337301
/// Enables the experimental support for interpreting tests.
338302
interpret_tests: bool = "false",
339303

@@ -507,6 +471,42 @@ config_data! {
507471
/// Enables highlighting of all break points for a loop or block context while the cursor is on any `async` or `await` keywords.
508472
highlightRelated_yieldPoints_enable: bool = "true",
509473

474+
/// Whether to show `Debug` action. Only applies when
475+
/// `#rust-analyzer.hover.actions.enable#` is set.
476+
hover_actions_debug_enable: bool = "true",
477+
/// Whether to show HoverActions in Rust files.
478+
hover_actions_enable: bool = "true",
479+
/// Whether to show `Go to Type Definition` action. Only applies when
480+
/// `#rust-analyzer.hover.actions.enable#` is set.
481+
hover_actions_gotoTypeDef_enable: bool = "true",
482+
/// Whether to show `Implementations` action. Only applies when
483+
/// `#rust-analyzer.hover.actions.enable#` is set.
484+
hover_actions_implementations_enable: bool = "true",
485+
/// Whether to show `References` action. Only applies when
486+
/// `#rust-analyzer.hover.actions.enable#` is set.
487+
hover_actions_references_enable: bool = "false",
488+
/// Whether to show `Run` action. Only applies when
489+
/// `#rust-analyzer.hover.actions.enable#` is set.
490+
hover_actions_run_enable: bool = "true",
491+
492+
/// Whether to show documentation on hover.
493+
hover_documentation_enable: bool = "true",
494+
/// Whether to show keyword hover popups. Only applies when
495+
/// `#rust-analyzer.hover.documentation.enable#` is set.
496+
hover_documentation_keywords_enable: bool = "true",
497+
/// Use markdown syntax for links on hover.
498+
hover_links_enable: bool = "true",
499+
/// How to render the align information in a memory layout hover.
500+
hover_memoryLayout_alignment: Option<MemoryLayoutHoverRenderKindDef> = "\"hexadecimal\"",
501+
/// Whether to show memory layout data on hover.
502+
hover_memoryLayout_enable: bool = "true",
503+
/// How to render the niche information in a memory layout hover.
504+
hover_memoryLayout_niches: Option<bool> = "false",
505+
/// How to render the offset information in a memory layout hover.
506+
hover_memoryLayout_offset: Option<MemoryLayoutHoverRenderKindDef> = "\"hexadecimal\"",
507+
/// How to render the size information in a memory layout hover.
508+
hover_memoryLayout_size: Option<MemoryLayoutHoverRenderKindDef> = "\"both\"",
509+
510510
/// Whether to show inlay type hints for binding modes.
511511
inlayHints_bindingModeHints_enable: bool = "false",
512512
/// Whether to show inlay type hints for method chains.
@@ -743,6 +743,55 @@ impl<'a> LocalConfigView<'a> {
743743
}
744744
}
745745

746+
pub fn hover_actions(&self) -> HoverActionsConfig {
747+
let enable = self.experimental("hoverActions") && self.local.hover_actions_enable;
748+
HoverActionsConfig {
749+
implementations: enable && self.local.hover_actions_implementations_enable,
750+
references: enable && self.local.hover_actions_references_enable,
751+
run: enable && self.local.hover_actions_run_enable,
752+
debug: enable && self.local.hover_actions_debug_enable,
753+
goto_type_def: enable && self.local.hover_actions_gotoTypeDef_enable,
754+
}
755+
}
756+
757+
pub fn hover(&self) -> HoverConfig {
758+
let mem_kind = |kind| match kind {
759+
MemoryLayoutHoverRenderKindDef::Both => MemoryLayoutHoverRenderKind::Both,
760+
MemoryLayoutHoverRenderKindDef::Decimal => MemoryLayoutHoverRenderKind::Decimal,
761+
MemoryLayoutHoverRenderKindDef::Hexadecimal => MemoryLayoutHoverRenderKind::Hexadecimal,
762+
};
763+
HoverConfig {
764+
links_in_hover: self.local.hover_links_enable,
765+
memory_layout: self.local.hover_memoryLayout_enable.then_some(
766+
MemoryLayoutHoverConfig {
767+
size: self.local.hover_memoryLayout_size.map(mem_kind),
768+
offset: self.local.hover_memoryLayout_offset.map(mem_kind),
769+
alignment: self.local.hover_memoryLayout_alignment.map(mem_kind),
770+
niches: self.local.hover_memoryLayout_niches.unwrap_or_default(),
771+
},
772+
),
773+
documentation: self.local.hover_documentation_enable,
774+
format: {
775+
let is_markdown = try_or_def!(self
776+
.caps
777+
.text_document
778+
.as_ref()?
779+
.hover
780+
.as_ref()?
781+
.content_format
782+
.as_ref()?
783+
.as_slice())
784+
.contains(&MarkupKind::Markdown);
785+
if is_markdown {
786+
HoverDocFormat::Markdown
787+
} else {
788+
HoverDocFormat::PlainText
789+
}
790+
},
791+
keywords: self.local.hover_documentation_keywords_enable,
792+
}
793+
}
794+
746795
pub fn inlay_hints(&self) -> InlayHintsConfig {
747796
let client_capability_fields = self
748797
.caps
@@ -1742,19 +1791,6 @@ impl Config {
17421791
}
17431792
}
17441793

1745-
pub fn hover_actions(&self) -> HoverActionsConfig {
1746-
let enable =
1747-
self.experimental("hoverActions") && self.root_config.global.0.hover_actions_enable;
1748-
HoverActionsConfig {
1749-
implementations: enable
1750-
&& self.root_config.global.0.hover_actions_implementations_enable,
1751-
references: enable && self.root_config.global.0.hover_actions_references_enable,
1752-
run: enable && self.root_config.global.0.hover_actions_run_enable,
1753-
debug: enable && self.root_config.global.0.hover_actions_debug_enable,
1754-
goto_type_def: enable && self.root_config.global.0.hover_actions_gotoTypeDef_enable,
1755-
}
1756-
}
1757-
17581794
pub fn highlighting_non_standard_tokens(&self) -> bool {
17591795
self.root_config.global.0.semanticHighlighting_nonStandardTokens
17601796
}
@@ -1788,44 +1824,6 @@ impl Config {
17881824
}
17891825
}
17901826

1791-
pub fn hover(&self) -> HoverConfig {
1792-
let mem_kind = |kind| match kind {
1793-
MemoryLayoutHoverRenderKindDef::Both => MemoryLayoutHoverRenderKind::Both,
1794-
MemoryLayoutHoverRenderKindDef::Decimal => MemoryLayoutHoverRenderKind::Decimal,
1795-
MemoryLayoutHoverRenderKindDef::Hexadecimal => MemoryLayoutHoverRenderKind::Hexadecimal,
1796-
};
1797-
HoverConfig {
1798-
links_in_hover: self.root_config.global.0.hover_links_enable,
1799-
memory_layout: self.root_config.global.0.hover_memoryLayout_enable.then_some(
1800-
MemoryLayoutHoverConfig {
1801-
size: self.root_config.global.0.hover_memoryLayout_size.map(mem_kind),
1802-
offset: self.root_config.global.0.hover_memoryLayout_offset.map(mem_kind),
1803-
alignment: self.root_config.global.0.hover_memoryLayout_alignment.map(mem_kind),
1804-
niches: self.root_config.global.0.hover_memoryLayout_niches.unwrap_or_default(),
1805-
},
1806-
),
1807-
documentation: self.root_config.global.0.hover_documentation_enable,
1808-
format: {
1809-
let is_markdown = try_or_def!(self
1810-
.caps
1811-
.text_document
1812-
.as_ref()?
1813-
.hover
1814-
.as_ref()?
1815-
.content_format
1816-
.as_ref()?
1817-
.as_slice())
1818-
.contains(&MarkupKind::Markdown);
1819-
if is_markdown {
1820-
HoverDocFormat::Markdown
1821-
} else {
1822-
HoverDocFormat::PlainText
1823-
}
1824-
},
1825-
keywords: self.root_config.global.0.hover_documentation_keywords_enable,
1826-
}
1827-
}
1828-
18291827
pub fn workspace_symbol(&self) -> WorkspaceSymbolConfig {
18301828
WorkspaceSymbolConfig {
18311829
search_scope: match self.root_config.global.0.workspace_symbol_search_scope {

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -974,16 +974,17 @@ pub(crate) fn handle_hover(
974974
PositionOrRange::Position(position) => Range::new(position, position),
975975
PositionOrRange::Range(range) => range,
976976
};
977-
978977
let file_range = from_proto::file_range(&snap, &params.text_document, range)?;
979-
let info = match snap.analysis.hover(&snap.config.hover(), file_range)? {
978+
let local_conf = snap.config.localize_by_file_id(file_range.file_id);
979+
let hover = local_conf.hover();
980+
let info = match snap.analysis.hover(&hover, file_range)? {
980981
None => return Ok(None),
981982
Some(info) => info,
982983
};
983984

984985
let line_index = snap.file_line_index(file_range.file_id)?;
985986
let range = to_proto::range(&line_index, info.range);
986-
let markup_kind = snap.config.hover().format;
987+
let markup_kind = hover.format;
987988
let hover = lsp_ext::Hover {
988989
hover: lsp_types::Hover {
989990
contents: HoverContents::Markup(to_proto::markup_content(
@@ -992,7 +993,7 @@ pub(crate) fn handle_hover(
992993
)),
993994
range: Some(range),
994995
},
995-
actions: if snap.config.hover_actions().none() {
996+
actions: if local_conf.hover_actions().none() {
996997
Vec::new()
997998
} else {
998999
prepare_hover_actions(&snap, &info.info.actions)
@@ -1795,7 +1796,9 @@ fn show_impl_command_link(
17951796
snap: &GlobalStateSnapshot,
17961797
position: &FilePosition,
17971798
) -> Option<lsp_ext::CommandLinkGroup> {
1798-
if snap.config.hover_actions().implementations && snap.config.client_commands().show_reference {
1799+
if snap.config.localize_by_file_id(position.file_id).hover_actions().implementations
1800+
&& snap.config.client_commands().show_reference
1801+
{
17991802
if let Some(nav_data) = snap.analysis.goto_implementation(*position).unwrap_or(None) {
18001803
let uri = to_proto::url(snap, position.file_id);
18011804
let line_index = snap.file_line_index(position.file_id).ok()?;
@@ -1821,7 +1824,9 @@ fn show_ref_command_link(
18211824
snap: &GlobalStateSnapshot,
18221825
position: &FilePosition,
18231826
) -> Option<lsp_ext::CommandLinkGroup> {
1824-
if snap.config.hover_actions().references && snap.config.client_commands().show_reference {
1827+
if snap.config.localize_by_file_id(position.file_id).hover_actions().references
1828+
&& snap.config.client_commands().show_reference
1829+
{
18251830
if let Some(ref_search_res) = snap.analysis.find_all_refs(*position, None).unwrap_or(None) {
18261831
let uri = to_proto::url(snap, position.file_id);
18271832
let line_index = snap.file_line_index(position.file_id).ok()?;
@@ -1851,12 +1856,14 @@ fn runnable_action_links(
18511856
snap: &GlobalStateSnapshot,
18521857
runnable: Runnable,
18531858
) -> Option<lsp_ext::CommandLinkGroup> {
1854-
let hover_actions_config = snap.config.hover_actions();
1859+
let cargo_spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id).ok()?;
1860+
1861+
let hover_actions_config =
1862+
snap.config.localize_by_file_id(runnable.nav.file_id).hover_actions();
18551863
if !hover_actions_config.runnable() {
18561864
return None;
18571865
}
18581866

1859-
let cargo_spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id).ok()?;
18601867
if should_skip_target(&runnable, cargo_spec.as_ref()) {
18611868
return None;
18621869
}
@@ -1888,8 +1895,12 @@ fn goto_type_action_links(
18881895
snap: &GlobalStateSnapshot,
18891896
nav_targets: &[HoverGotoTypeData],
18901897
) -> Option<lsp_ext::CommandLinkGroup> {
1891-
if !snap.config.hover_actions().goto_type_def
1892-
|| nav_targets.is_empty()
1898+
if nav_targets.is_empty()
1899+
|| !snap
1900+
.config
1901+
.localize_by_file_id(nav_targets[0].nav.file_id)
1902+
.hover_actions()
1903+
.goto_type_def
18931904
|| !snap.config.client_commands().goto_location
18941905
{
18951906
return None;

0 commit comments

Comments
 (0)