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 f582c5b
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 104 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)
}
147 changes: 76 additions & 71 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use project_model::{
use rustc_hash::{FxHashMap, FxHashSet};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use stdx::format_to_acc;
use toml;
use vfs::{AbsPath, AbsPathBuf};

use crate::{
Expand Down Expand Up @@ -63,6 +62,13 @@ mod patch_old_style;
// To deprecate an option by replacing it with another name use `new_name | `old_name` so that we keep
// parsing the old name.
config_data! {
/// Configs that apply on workspace-wide level. There are 3 levels on which a global configuration can be configured
///
/// 1. `rust-analyzer.toml` file under user's config directory (e.g ~/.config/rust-analyzer.toml)
/// 2. Client's own configurations (e.g `settings.json` on VS Code)
/// 3. `rust-analyzer.toml` file located at the workspace root
///
/// A config is searched for in the reversed order. First found declaration is chosen.
global: struct GlobalConfigData <- GlobalConfigInput -> {
/// Whether to insert #[must_use] when generating `as_` methods
/// for enum variants.
Expand Down Expand Up @@ -268,6 +274,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 @@ -392,6 +433,9 @@ config_data! {
}

config_data! {
/// Local configurations can be overridden for every crate by placing a `rust-analyzer.toml` on crate root.
/// Starting from the nearest `SourceRoot` (i.e the crate itself for which a configuration is called.) a tree of configuration files is traversed and
/// first occurrence is used.
local: struct LocalConfigData <- LocalConfigInput -> {
/// Toggles the additional completions that automatically add imports when completed.
/// Note that your client must specify the `additionalTextEdits` LSP client capability to truly have this feature enabled.
Expand Down Expand Up @@ -466,42 +510,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 @@ -617,6 +625,8 @@ config_data! {
}

config_data! {
/// Configs that only make sense when they are set by a client. As such they can only be defined
/// by setting them using client's settings (e.g `settings.json` on VS Code).
client: struct ClientConfigData <- ClientConfigInput -> {}
}

Expand All @@ -633,8 +643,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 +877,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,9 +915,8 @@ 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;
tracing::debug!("deserialized config data: {:#?}", self.client_config);
self.client_config = ConfigInput::from_json(json, &mut errors);
tracing::debug!(?self.client_config, "deserialized config data");
self.snippets.clear();

let snips = self.completion_snippets_custom(None).to_owned();
Expand Down Expand Up @@ -1051,36 +1060,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 +1103,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 +2201,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 +2265,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 +2288,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 @@ -2315,7 +2320,7 @@ macro_rules! _impl_for_config_data {

macro_rules! _config_data {
// modname is for the tests
($modname:ident: struct $name:ident <- $input:ident -> {
($(#[doc=$dox:literal])* $modname:ident: struct $name:ident <- $input:ident -> {
$(
$(#[doc=$doc:literal])*
$field:ident $(| $alias:ident)*: $ty:ty = $(@$marker:ident: )? $default:expr,
Expand Down Expand Up @@ -2382,7 +2387,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 +2399,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
Loading

0 comments on commit f582c5b

Please sign in to comment.