Skip to content

Commit

Permalink
Apply requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alibektas committed Mar 5, 2024
1 parent 6d5d76d commit fa7f873
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 101 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/base-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ salsa.workspace = true
rustc-hash.workspace = true
triomphe.workspace = true
semver.workspace = true
serde.workspace = true
tracing.workspace = true

# local deps
Expand Down
10 changes: 0 additions & 10 deletions crates/base-db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::{fmt, mem, ops, str::FromStr};
use cfg::CfgOptions;
use la_arena::{Arena, Idx, RawIdx};
use rustc_hash::{FxHashMap, FxHashSet};
use serde::Serialize;
use syntax::SmolStr;
use triomphe::Arc;
use vfs::{file_set::FileSet, AbsPathBuf, AnchoredPath, FileId, VfsPath};
Expand Down Expand Up @@ -102,15 +101,6 @@ pub type CrateId = Idx<CrateData>;
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct CrateName(SmolStr);

impl Serialize for CrateName {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self)
}
}

impl CrateName {
/// Creates a crate name, checking for dashes in the string provided.
/// Dashes are not allowed in the crate names,
Expand Down
8 changes: 8 additions & 0 deletions crates/project-model/src/project_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ struct DepData {
#[serde(rename = "crate")]
krate: usize,
#[serde(deserialize_with = "deserialize_crate_name")]
#[serde(serialize_with = "serialize_crate_name")]
name: CrateName,
}

Expand All @@ -249,3 +250,10 @@ where
let name = String::deserialize(de)?;
CrateName::new(&name).map_err(|err| de::Error::custom(format!("invalid crate name: {err:?}")))
}

fn serialize_crate_name<S>(name: &CrateName, se: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
se.serialize_str(&name)
}
130 changes: 62 additions & 68 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,41 @@ config_data! {
/// Controls file watching implementation.
files_watcher: FilesWatcherDef = FilesWatcherDef::Client,

/// Whether to show `Debug` action. Only applies when
/// `#rust-analyzer.hover.actions.enable#` is set.
hover_actions_debug_enable: bool = true,
/// Whether to show HoverActions in Rust files.
hover_actions_enable: bool = true,
/// Whether to show `Go to Type Definition` action. Only applies when
/// `#rust-analyzer.hover.actions.enable#` is set.
hover_actions_gotoTypeDef_enable: bool = true,
/// Whether to show `Implementations` action. Only applies when
/// `#rust-analyzer.hover.actions.enable#` is set.
hover_actions_implementations_enable: bool = true,
/// Whether to show `References` action. Only applies when
/// `#rust-analyzer.hover.actions.enable#` is set.
hover_actions_references_enable: bool = false,
/// Whether to show `Run` action. Only applies when
/// `#rust-analyzer.hover.actions.enable#` is set.
hover_actions_run_enable: bool = true,

/// Whether to show documentation on hover.
hover_documentation_enable: bool = true,
/// Whether to show keyword hover popups. Only applies when
/// `#rust-analyzer.hover.documentation.enable#` is set.
hover_documentation_keywords_enable: bool = true,
/// Use markdown syntax for links on hover.
hover_links_enable: bool = true,
/// How to render the align information in a memory layout hover.
hover_memoryLayout_alignment: Option<MemoryLayoutHoverRenderKindDef> = Some(MemoryLayoutHoverRenderKindDef::Hexadecimal),
/// Whether to show memory layout data on hover.
hover_memoryLayout_enable: bool = true,
/// How to render the niche information in a memory layout hover.
hover_memoryLayout_niches: Option<bool> = Some(false),
/// How to render the offset information in a memory layout hover.
hover_memoryLayout_offset: Option<MemoryLayoutHoverRenderKindDef> = Some(MemoryLayoutHoverRenderKindDef::Hexadecimal),
/// How to render the size information in a memory layout hover.
hover_memoryLayout_size: Option<MemoryLayoutHoverRenderKindDef> = Some(MemoryLayoutHoverRenderKindDef::Both),

/// Enables the experimental support for interpreting tests.
interpret_tests: bool = false,
Expand Down Expand Up @@ -466,42 +501,6 @@ config_data! {
/// Enables highlighting of all break points for a loop or block context while the cursor is on any `async` or `await` keywords.
highlightRelated_yieldPoints_enable: bool = true,

/// Whether to show `Debug` action. Only applies when
/// `#rust-analyzer.hover.actions.enable#` is set.
hover_actions_debug_enable: bool = true,
/// Whether to show HoverActions in Rust files.
hover_actions_enable: bool = true,
/// Whether to show `Go to Type Definition` action. Only applies when
/// `#rust-analyzer.hover.actions.enable#` is set.
hover_actions_gotoTypeDef_enable: bool = true,
/// Whether to show `Implementations` action. Only applies when
/// `#rust-analyzer.hover.actions.enable#` is set.
hover_actions_implementations_enable: bool = true,
/// Whether to show `References` action. Only applies when
/// `#rust-analyzer.hover.actions.enable#` is set.
hover_actions_references_enable: bool = false,
/// Whether to show `Run` action. Only applies when
/// `#rust-analyzer.hover.actions.enable#` is set.
hover_actions_run_enable: bool = true,

/// Whether to show documentation on hover.
hover_documentation_enable: bool = true,
/// Whether to show keyword hover popups. Only applies when
/// `#rust-analyzer.hover.documentation.enable#` is set.
hover_documentation_keywords_enable: bool = true,
/// Use markdown syntax for links on hover.
hover_links_enable: bool = true,
/// How to render the align information in a memory layout hover.
hover_memoryLayout_alignment: Option<MemoryLayoutHoverRenderKindDef> = Some(MemoryLayoutHoverRenderKindDef::Hexadecimal),
/// Whether to show memory layout data on hover.
hover_memoryLayout_enable: bool = true,
/// How to render the niche information in a memory layout hover.
hover_memoryLayout_niches: Option<bool> = Some(false),
/// How to render the offset information in a memory layout hover.
hover_memoryLayout_offset: Option<MemoryLayoutHoverRenderKindDef> = Some(MemoryLayoutHoverRenderKindDef::Hexadecimal),
/// How to render the size information in a memory layout hover.
hover_memoryLayout_size: Option<MemoryLayoutHoverRenderKindDef> = Some(MemoryLayoutHoverRenderKindDef::Both),

/// Whether to enforce the import granularity setting for all files. If set to false rust-analyzer will try to keep import styles consistent per file.
imports_granularity_enforce: bool = false,
/// How imports should be grouped into use statements.
Expand Down Expand Up @@ -633,8 +632,8 @@ pub struct Config {

default_config: ConfigData,
client_config: ConfigInput,
xdg_config: ConfigInput,
ratoml_arena: FxHashMap<SourceRootId, RatomlNode>,
user_config: ConfigInput,
ratoml_files: FxHashMap<SourceRootId, RatomlNode>,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -867,8 +866,8 @@ impl Config {
workspace_roots,
is_visual_studio_code,
client_config: ConfigInput::default(),
xdg_config: ConfigInput::default(),
ratoml_arena: FxHashMap::default(),
user_config: ConfigInput::default(),
ratoml_files: FxHashMap::default(),
default_config: ConfigData::default(),
}
}
Expand Down Expand Up @@ -905,8 +904,7 @@ impl Config {
.map(AbsPathBuf::assert)
.collect();
patch_old_style::patch_json_for_outdated_configs(&mut json);
let input = ConfigInput::from_json(json, &mut errors);
self.client_config = input;
self.client_config = ConfigInput::from_json(json, &mut errors);
tracing::debug!("deserialized config data: {:#?}", self.client_config);
self.snippets.clear();

Expand Down Expand Up @@ -1051,36 +1049,32 @@ impl Config {
}
}

pub fn hover_actions(&self, source_root: Option<SourceRootId>) -> HoverActionsConfig {
let enable =
self.experimental("hoverActions") && self.hover_actions_enable(source_root).to_owned();
pub fn hover_actions(&self) -> HoverActionsConfig {
let enable = self.experimental("hoverActions") && self.hover_actions_enable().to_owned();
HoverActionsConfig {
implementations: enable
&& self.hover_actions_implementations_enable(source_root).to_owned(),
references: enable && self.hover_actions_references_enable(source_root).to_owned(),
run: enable && self.hover_actions_run_enable(source_root).to_owned(),
debug: enable && self.hover_actions_debug_enable(source_root).to_owned(),
goto_type_def: enable && self.hover_actions_gotoTypeDef_enable(source_root).to_owned(),
implementations: enable && self.hover_actions_implementations_enable().to_owned(),
references: enable && self.hover_actions_references_enable().to_owned(),
run: enable && self.hover_actions_run_enable().to_owned(),
debug: enable && self.hover_actions_debug_enable().to_owned(),
goto_type_def: enable && self.hover_actions_gotoTypeDef_enable().to_owned(),
}
}

pub fn hover(&self, source_root: Option<SourceRootId>) -> HoverConfig {
pub fn hover(&self) -> HoverConfig {
let mem_kind = |kind| match kind {
MemoryLayoutHoverRenderKindDef::Both => MemoryLayoutHoverRenderKind::Both,
MemoryLayoutHoverRenderKindDef::Decimal => MemoryLayoutHoverRenderKind::Decimal,
MemoryLayoutHoverRenderKindDef::Hexadecimal => MemoryLayoutHoverRenderKind::Hexadecimal,
};
HoverConfig {
links_in_hover: self.hover_links_enable(source_root).to_owned(),
memory_layout: self.hover_memoryLayout_enable(source_root).then_some(
MemoryLayoutHoverConfig {
size: self.hover_memoryLayout_size(source_root).map(mem_kind),
offset: self.hover_memoryLayout_offset(source_root).map(mem_kind),
alignment: self.hover_memoryLayout_alignment(source_root).map(mem_kind),
niches: self.hover_memoryLayout_niches(source_root).unwrap_or_default(),
},
),
documentation: self.hover_documentation_enable(source_root).to_owned(),
links_in_hover: self.hover_links_enable().to_owned(),
memory_layout: self.hover_memoryLayout_enable().then_some(MemoryLayoutHoverConfig {
size: self.hover_memoryLayout_size().map(mem_kind),
offset: self.hover_memoryLayout_offset().map(mem_kind),
alignment: self.hover_memoryLayout_alignment().map(mem_kind),
niches: self.hover_memoryLayout_niches().unwrap_or_default(),
}),
documentation: self.hover_documentation_enable().to_owned(),
format: {
let is_markdown = try_or_def!(self
.caps
Expand All @@ -1098,7 +1092,7 @@ impl Config {
HoverDocFormat::PlainText
}
},
keywords: self.hover_documentation_keywords_enable(source_root).to_owned(),
keywords: self.hover_documentation_keywords_enable().to_owned(),
}
}

Expand Down Expand Up @@ -2196,7 +2190,7 @@ pub(crate) enum WorkspaceSymbolSearchKindDef {
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
#[serde(untagged)]
enum MemoryLayoutHoverRenderKindDef {
pub(crate) enum MemoryLayoutHoverRenderKindDef {
#[serde(with = "unit_v::decimal")]
Decimal,
#[serde(with = "unit_v::hexadecimal")]
Expand Down Expand Up @@ -2260,7 +2254,7 @@ macro_rules! _impl_for_config_data {
return &v;
}

if let Some(v) = self.xdg_config.local.$field.as_ref() {
if let Some(v) = self.user_config.local.$field.as_ref() {
return &v;
}

Expand All @@ -2283,7 +2277,7 @@ macro_rules! _impl_for_config_data {
return &v;
}

if let Some(v) = self.xdg_config.global.$field.as_ref() {
if let Some(v) = self.user_config.global.$field.as_ref() {
return &v;
}

Expand Down Expand Up @@ -2382,7 +2376,7 @@ macro_rules! _config_data {
}

impl $input {
#[allow(unused)]
#[allow(unused, clippy::ptr_arg)]
fn from_json(json: &mut serde_json::Value, error_sink: &mut Vec<(String, serde_json::Error)>) -> Self {
Self {$(
$field: get_field(
Expand All @@ -2394,7 +2388,7 @@ macro_rules! _config_data {
)*}
}

#[allow(unused)]
#[allow(unused, clippy::ptr_arg)]
fn from_toml(toml: &mut toml::Table , error_sink: &mut Vec<(String, toml::de::Error)>) -> Self {
Self {$(
$field: get_field_toml::<$ty>(
Expand Down
28 changes: 7 additions & 21 deletions crates/rust-analyzer/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,9 +973,8 @@ pub(crate) fn handle_hover(
PositionOrRange::Range(range) => range,
};
let file_range = from_proto::file_range(&snap, &params.text_document, range)?;
let source_root = snap.analysis.source_root(file_range.file_id)?;

let hover = snap.config.hover(Some(source_root));
let hover = snap.config.hover();
let info = match snap.analysis.hover(&hover, file_range)? {
None => return Ok(None),
Some(info) => info,
Expand All @@ -992,7 +991,7 @@ pub(crate) fn handle_hover(
)),
range: Some(range),
},
actions: if snap.config.hover_actions(Some(source_root)).none() {
actions: if snap.config.hover_actions().none() {
Vec::new()
} else {
prepare_hover_actions(&snap, &info.info.actions)
Expand Down Expand Up @@ -1786,11 +1785,7 @@ fn show_impl_command_link(
snap: &GlobalStateSnapshot,
position: &FilePosition,
) -> Option<lsp_ext::CommandLinkGroup> {
let source_root = snap.analysis.source_root(position.file_id).ok()?;

if snap.config.hover_actions(Some(source_root)).implementations
&& snap.config.client_commands().show_reference
{
if snap.config.hover_actions().implementations && snap.config.client_commands().show_reference {
if let Some(nav_data) = snap.analysis.goto_implementation(*position).unwrap_or(None) {
let uri = to_proto::url(snap, position.file_id);
let line_index = snap.file_line_index(position.file_id).ok()?;
Expand All @@ -1816,11 +1811,7 @@ fn show_ref_command_link(
snap: &GlobalStateSnapshot,
position: &FilePosition,
) -> Option<lsp_ext::CommandLinkGroup> {
let source_root = snap.analysis.source_root(position.file_id).ok()?;

if snap.config.hover_actions(Some(source_root)).references
&& snap.config.client_commands().show_reference
{
if snap.config.hover_actions().references && snap.config.client_commands().show_reference {
if let Some(ref_search_res) = snap.analysis.find_all_refs(*position, None).unwrap_or(None) {
let uri = to_proto::url(snap, position.file_id);
let line_index = snap.file_line_index(position.file_id).ok()?;
Expand Down Expand Up @@ -1850,14 +1841,12 @@ fn runnable_action_links(
snap: &GlobalStateSnapshot,
runnable: Runnable,
) -> Option<lsp_ext::CommandLinkGroup> {
let cargo_spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id).ok()?;
let source_root = snap.analysis.source_root(runnable.nav.file_id).ok()?;

let hover_actions_config = snap.config.hover_actions(Some(source_root));
let hover_actions_config = snap.config.hover_actions();
if !hover_actions_config.runnable() {
return None;
}

let cargo_spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id).ok()?;
if should_skip_target(&runnable, cargo_spec.as_ref()) {
return None;
}
Expand Down Expand Up @@ -1889,11 +1878,8 @@ fn goto_type_action_links(
snap: &GlobalStateSnapshot,
nav_targets: &[HoverGotoTypeData],
) -> Option<lsp_ext::CommandLinkGroup> {
// FIXME @alibektas : Is it justified/logical to base this function's progress on a single nav target?
let source_root = snap.analysis.source_root(nav_targets[0].nav.file_id).ok()?;

if nav_targets.is_empty()
|| !snap.config.hover_actions(Some(source_root)).goto_type_def
|| !snap.config.hover_actions().goto_type_def
|| !snap.config.client_commands().goto_location
{
return None;
Expand Down

0 comments on commit fa7f873

Please sign in to comment.