2019-10-25 06:00:30 +00:00
//! Config used by the language server.
//!
//! We currently get this config from `initialize` LSP request, which is not the
//! best way to do it, but was the simplest thing we could implement.
//!
//! Of particular interest is the `feature_flags` hash map: while other fields
//! configure the server itself, feature flags are passed into analysis, and
//! tweak things like automatic insertion of `()` in completions.
2019-09-30 08:58:53 +00:00
2023-03-25 22:03:22 +00:00
use std ::{ fmt , iter , ops ::Not , path ::PathBuf } ;
2020-04-23 16:50:25 +00:00
2020-06-25 07:13:46 +00:00
use flycheck ::FlycheckConfig ;
2021-06-21 19:57:01 +00:00
use ide ::{
2022-05-13 17:52:44 +00:00
AssistConfig , CallableSnippets , CompletionConfig , DiagnosticsConfig , ExprFillDefaultMode ,
2022-08-22 11:38:35 +00:00
HighlightConfig , HighlightRelatedConfig , HoverConfig , HoverDocFormat , InlayHintsConfig ,
JoinLinesConfig , Snippet , SnippetScope ,
2021-06-21 19:57:01 +00:00
} ;
2022-03-06 18:01:30 +00:00
use ide_db ::{
imports ::insert_use ::{ ImportGranularity , InsertUseConfig , PrefixKind } ,
2021-01-16 17:33:36 +00:00
SnippetCap ,
2021-01-06 17:43:46 +00:00
} ;
2022-04-11 11:05:34 +00:00
use itertools ::Itertools ;
2023-01-16 15:04:38 +00:00
use lsp_types ::{ ClientCapabilities , MarkupKind } ;
2021-08-23 17:18:11 +00:00
use project_model ::{
2023-03-15 10:35:34 +00:00
CargoConfig , CargoFeatures , ProjectJson , ProjectJsonData , ProjectManifest , RustLibSource ,
2022-09-19 14:40:43 +00:00
UnsetTestCrates ,
2021-08-23 17:18:11 +00:00
} ;
2021-04-21 03:03:35 +00:00
use rustc_hash ::{ FxHashMap , FxHashSet } ;
2020-12-02 14:31:24 +00:00
use serde ::{ de ::DeserializeOwned , Deserialize } ;
2023-04-18 11:25:54 +00:00
use vfs ::{ AbsPath , AbsPathBuf } ;
2019-03-07 20:06:25 +00:00
2021-02-12 22:26:01 +00:00
use crate ::{
feat: gate custom clint-side commands behind capabilities
Some features of rust-analyzer requires support for custom commands on
the client side. Specifically, hover & code lens need this.
Stock LSP doesn't have a way for the server to know which client-side
commands are available. For that reason, we historically were just
sending the commands, not worrying whether the client supports then or
not.
That's not really great though, so in this PR we add infrastructure for
the client to explicitly opt-into custom commands, via `extensions`
field of the ClientCapabilities.
To preserve backwards compatability, if the client doesn't set the
field, we assume that it does support all custom commands. In the
future, we'll start treating that case as if the client doesn't support
commands.
So, if you maintain a rust-analyzer client and implement
`rust-analyzer/runSingle` and such, please also advertise this via a
capability.
2021-07-30 16:16:33 +00:00
caps ::completion_item_edit_resolve ,
diagnostics ::DiagnosticsMapConfig ,
2022-10-25 11:43:26 +00:00
line_index ::PositionEncoding ,
2023-02-14 00:56:28 +00:00
lsp_ext ::{ self , negotiated_encoding , WorkspaceSymbolSearchKind , WorkspaceSymbolSearchScope } ,
2021-02-12 22:26:01 +00:00
} ;
2020-07-03 15:19:00 +00:00
2022-05-04 13:19:00 +00:00
mod patch_old_style ;
2022-04-26 11:00:45 +00:00
// Conventions for configuration keys to preserve maximal extendability without breakage:
2022-04-26 11:18:51 +00:00
// - Toggles (be it binary true/false or with more options in-between) should almost always suffix as `_enable`
2022-04-29 08:56:32 +00:00
// This has the benefit of namespaces being extensible, and if the suffix doesn't fit later it can be changed without breakage.
2022-04-26 11:00:45 +00:00
// - In general be wary of using the namespace of something verbatim, it prevents us from adding subkeys in the future
// - Don't use abbreviations unless really necessary
2022-04-26 12:39:01 +00:00
// - foo_command = overrides the subcommand, foo_overrideCommand allows full overwriting, extra args only applies for foo_command
2022-04-26 11:00:45 +00:00
2021-04-28 10:06:46 +00:00
// Defines the server-side configuration of the rust-analyzer. We generate
2022-08-19 12:58:08 +00:00
// *parts* of VS Code's `package.json` config from this. Run `cargo test` to
// re-generate that file.
2021-04-28 10:06:46 +00:00
//
// However, editor specific config, which the server doesn't know about, should
// be specified directly in `package.json`.
2021-06-21 19:41:06 +00:00
//
// To deprecate an option by replacing it with another name use `new_name | `old_name` so that we keep
// parsing the old name.
2020-12-02 14:31:24 +00:00
config_data! {
struct ConfigData {
2022-10-24 15:36:32 +00:00
/// Whether to insert #[must_use] when generating `as_` methods
2022-10-06 18:41:02 +00:00
/// for enum variants.
assist_emitMustUse : bool = " false " ,
2022-04-29 13:48:48 +00:00
/// Placeholder expression to use for missing expressions in assists.
2022-04-26 11:00:45 +00:00
assist_expressionFillDefault : ExprFillDefaultDef = " \" todo \" " ,
2020-12-02 14:31:24 +00:00
2022-05-12 10:29:40 +00:00
/// Warm up caches on project load.
cachePriming_enable : bool = " true " ,
2022-05-19 14:04:02 +00:00
/// How many worker threads to handle priming caches. The default `0` means to pick automatically.
2022-05-12 10:29:40 +00:00
cachePriming_numThreads : ParallelCachePrimingNumThreads = " 0 " ,
2020-12-02 14:31:24 +00:00
/// Automatically refresh project info via `cargo metadata` on
2022-04-26 22:02:45 +00:00
/// `Cargo.toml` or `.cargo/config.toml` changes.
2020-12-02 14:31:24 +00:00
cargo_autoreload : bool = " true " ,
2021-03-04 11:52:36 +00:00
/// Run build scripts (`build.rs`) for more precise code analysis.
2022-04-29 08:56:32 +00:00
cargo_buildScripts_enable : bool = " true " ,
2022-10-22 21:02:59 +00:00
/// Specifies the working directory for running build scripts.
/// - "workspace": run build scripts for a workspace in the workspace's root directory.
/// This is incompatible with `#rust-analyzer.cargo.buildScripts.invocationStrategy#` set to `once`.
/// - "root": run build scripts in the project's root directory.
/// This config only has an effect when `#rust-analyzer.cargo.buildScripts.overrideCommand#`
/// is set.
cargo_buildScripts_invocationLocation : InvocationLocation = " \" workspace \" " ,
2022-08-27 16:28:09 +00:00
/// Specifies the invocation strategy to use when running the build scripts command.
2022-10-22 21:02:59 +00:00
/// If `per_workspace` is set, the command will be executed for each workspace.
/// If `once` is set, the command will be executed once.
2022-09-26 13:58:55 +00:00
/// This config only has an effect when `#rust-analyzer.cargo.buildScripts.overrideCommand#`
/// is set.
2022-08-27 16:28:09 +00:00
cargo_buildScripts_invocationStrategy : InvocationStrategy = " \" per_workspace \" " ,
2022-04-29 13:48:48 +00:00
/// Override the command rust-analyzer uses to run build scripts and
/// build procedural macros. The command is required to output json
2022-07-04 16:45:54 +00:00
/// and should therefore include `--message-format=json` or a similar
2022-04-29 13:48:48 +00:00
/// option.
///
/// By default, a cargo invocation will be constructed for the configured
/// targets and features, with the following base command line:
///
/// ```bash
/// cargo check --quiet --workspace --message-format=json --all-targets
/// ```
/// .
2022-04-26 11:00:45 +00:00
cargo_buildScripts_overrideCommand : Option < Vec < String > > = " null " ,
2021-04-12 08:04:36 +00:00
/// Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to
2022-07-04 16:45:54 +00:00
/// avoid checking unnecessary things.
2022-04-26 11:00:45 +00:00
cargo_buildScripts_useRustcWrapper : bool = " true " ,
2023-03-12 10:59:57 +00:00
/// Extra arguments that are passed to every cargo invocation.
cargo_extraArgs : Vec < String > = " [] " ,
2022-08-18 21:41:17 +00:00
/// Extra environment variables that will be set when running cargo, rustc
/// or other commands within the workspace. Useful for setting RUSTFLAGS.
cargo_extraEnv : FxHashMap < String , String > = " {} " ,
2022-04-29 13:48:48 +00:00
/// List of features to activate.
///
/// Set this to `"all"` to pass `--all-features` to cargo.
2022-09-19 14:40:43 +00:00
cargo_features : CargoFeaturesDef = " [] " ,
2022-04-29 13:48:48 +00:00
/// Whether to pass `--no-default-features` to cargo.
2020-12-02 14:31:24 +00:00
cargo_noDefaultFeatures : bool = " false " ,
2022-10-01 18:47:31 +00:00
/// Relative path to the sysroot, or "discover" to try to automatically find it via
/// "rustc --print sysroot".
///
/// Unsetting this disables sysroot loading.
///
/// This option does not take effect until rust-analyzer is restarted.
cargo_sysroot : Option < String > = " \" discover \" " ,
2023-02-06 11:07:33 +00:00
/// Relative path to the sysroot library sources. If left unset, this will default to
/// `{cargo.sysroot}/lib/rustlib/src/rust/library`.
///
/// This option does not take effect until rust-analyzer is restarted.
cargo_sysrootSrc : Option < String > = " null " ,
2022-04-26 11:18:51 +00:00
/// Compilation target override (target triple).
2022-09-24 23:22:27 +00:00
// FIXME(@poliorcetics): move to multiple targets here too, but this will need more work
// than `checkOnSave_target`
2022-04-26 11:18:51 +00:00
cargo_target : Option < String > = " null " ,
/// Unsets `#[cfg(test)]` for the specified crates.
2022-09-24 23:23:20 +00:00
cargo_unsetTest : Vec < String > = " [ \" core \" ] " ,
2020-12-02 14:31:24 +00:00
2023-01-09 13:15:13 +00:00
/// Run the check command for diagnostics on save.
2022-12-20 10:31:07 +00:00
checkOnSave | checkOnSave_enable : bool = " true " ,
2020-12-02 14:31:24 +00:00
2021-01-26 13:03:24 +00:00
/// Check all targets and tests (`--all-targets`).
2023-01-09 13:15:13 +00:00
check_allTargets | checkOnSave_allTargets : bool = " true " ,
2020-12-02 14:31:24 +00:00
/// Cargo command to use for `cargo check`.
2023-01-09 13:15:13 +00:00
check_command | checkOnSave_command : String = " \" check \" " ,
2020-12-02 14:31:24 +00:00
/// Extra arguments for `cargo check`.
2023-01-09 13:15:13 +00:00
check_extraArgs | checkOnSave_extraArgs : Vec < String > = " [] " ,
2022-08-18 21:41:17 +00:00
/// Extra environment variables that will be set when running `cargo check`.
2022-09-19 15:31:08 +00:00
/// Extends `#rust-analyzer.cargo.extraEnv#`.
2023-01-09 13:15:13 +00:00
check_extraEnv | checkOnSave_extraEnv : FxHashMap < String , String > = " {} " ,
2020-12-02 14:31:24 +00:00
/// List of features to activate. Defaults to
2020-12-22 00:15:50 +00:00
/// `#rust-analyzer.cargo.features#`.
2022-04-29 13:48:48 +00:00
///
2022-07-04 16:45:54 +00:00
/// Set to `"all"` to pass `--all-features` to Cargo.
2023-01-09 13:15:13 +00:00
check_features | checkOnSave_features : Option < CargoFeaturesDef > = " null " ,
2022-10-22 21:02:59 +00:00
/// Specifies the working directory for running checks.
/// - "workspace": run checks for workspaces in the corresponding workspaces' root directories.
// FIXME: Ideally we would support this in some way
/// This falls back to "root" if `#rust-analyzer.cargo.checkOnSave.invocationStrategy#` is set to `once`.
/// - "root": run checks in the project's root directory.
/// This config only has an effect when `#rust-analyzer.cargo.buildScripts.overrideCommand#`
/// is set.
2023-01-09 13:15:13 +00:00
check_invocationLocation | checkOnSave_invocationLocation : InvocationLocation = " \" workspace \" " ,
2022-09-15 11:28:09 +00:00
/// Specifies the invocation strategy to use when running the checkOnSave command.
2022-10-22 21:02:59 +00:00
/// If `per_workspace` is set, the command will be executed for each workspace.
/// If `once` is set, the command will be executed once.
2022-09-26 13:58:55 +00:00
/// This config only has an effect when `#rust-analyzer.cargo.buildScripts.overrideCommand#`
/// is set.
2023-01-09 13:15:13 +00:00
check_invocationStrategy | checkOnSave_invocationStrategy : InvocationStrategy = " \" per_workspace \" " ,
2022-07-04 16:45:54 +00:00
/// Whether to pass `--no-default-features` to Cargo. Defaults to
2022-05-14 11:53:41 +00:00
/// `#rust-analyzer.cargo.noDefaultFeatures#`.
2023-01-09 13:15:13 +00:00
check_noDefaultFeatures | checkOnSave_noDefaultFeatures : Option < bool > = " null " ,
2022-07-04 16:45:54 +00:00
/// Override the command rust-analyzer uses instead of `cargo check` for
/// diagnostics on save. The command is required to output json and
2022-12-25 18:52:42 +00:00
/// should therefore include `--message-format=json` or a similar option
2023-01-04 17:04:45 +00:00
/// (if your client supports the `colorDiagnosticOutput` experimental
/// capability, you can use `--message-format=json-diagnostic-rendered-ansi`).
2022-07-04 16:45:54 +00:00
///
/// If you're changing this because you're using some tool wrapping
/// Cargo, you might also want to change
/// `#rust-analyzer.cargo.buildScripts.overrideCommand#`.
2022-04-29 13:48:48 +00:00
///
2022-08-19 12:23:58 +00:00
/// If there are multiple linked projects, this command is invoked for
/// each of them, with the working directory being the project root
/// (i.e., the folder containing the `Cargo.toml`).
///
2022-04-29 13:48:48 +00:00
/// An example command would be:
///
/// ```bash
/// cargo check --workspace --message-format=json --all-targets
/// ```
/// .
2023-01-09 13:15:13 +00:00
check_overrideCommand | checkOnSave_overrideCommand : Option < Vec < String > > = " null " ,
2022-09-24 23:22:27 +00:00
/// Check for specific targets. Defaults to `#rust-analyzer.cargo.target#` if empty.
///
/// Can be a single target, e.g. `"x86_64-unknown-linux-gnu"` or a list of targets, e.g.
/// `["aarch64-apple-darwin", "x86_64-apple-darwin"]`.
///
/// Aliased as `"checkOnSave.targets"`.
2023-01-09 13:15:13 +00:00
check_targets | checkOnSave_targets | checkOnSave_target : Option < CheckOnSaveTargets > = " null " ,
2020-12-02 14:31:24 +00:00
2022-04-26 11:18:51 +00:00
/// 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.
completion_autoimport_enable : bool = " true " ,
/// Toggles the additional completions that automatically show method calls and field accesses
/// with `self` prefixed to them when inside a method.
completion_autoself_enable : bool = " true " ,
2022-04-26 11:00:45 +00:00
/// Whether to add parenthesis and argument snippets when completing function.
2022-05-14 11:53:41 +00:00
completion_callable_snippets : CallableCompletionDef = " \" fill_arguments \" " ,
2023-01-20 02:34:01 +00:00
/// Maximum number of completions to return. If `None`, the limit is infinite.
completion_limit : Option < usize > = " null " ,
2022-04-26 11:18:51 +00:00
/// Whether to show postfix snippets like `dbg`, `if`, `not`, etc.
completion_postfix_enable : bool = " true " ,
/// Enables completions of private items and fields that are defined in the current workspace even if they are not visible at the current position.
completion_privateEditable_enable : bool = " false " ,
2021-10-04 17:22:41 +00:00
/// Custom completion snippets.
2021-10-12 10:14:24 +00:00
// NOTE: Keep this list in sync with the feature docs of user snippets.
2022-04-26 11:00:45 +00:00
completion_snippets_custom : FxHashMap < String , SnippetDef > = r #" {
2021-10-12 10:14:24 +00:00
" Arc::new " : {
" postfix " : " arc " ,
" body " : " Arc::new(${receiver}) " ,
" requires " : " std::sync::Arc " ,
" description " : " Put the expression into an `Arc` " ,
" scope " : " expr "
} ,
" Rc::new " : {
" postfix " : " rc " ,
" body " : " Rc::new(${receiver}) " ,
" requires " : " std::rc::Rc " ,
" description " : " Put the expression into an `Rc` " ,
" scope " : " expr "
} ,
" Box::pin " : {
" postfix " : " pinbox " ,
" body " : " Box::pin(${receiver}) " ,
" requires " : " std::boxed::Box " ,
" description " : " Put the expression into a pinned `Box` " ,
" scope " : " expr "
} ,
" Ok " : {
" postfix " : " ok " ,
" body " : " Ok(${receiver}) " ,
" description " : " Wrap the expression in a `Result::Ok` " ,
" scope " : " expr "
} ,
" Err " : {
" postfix " : " err " ,
" body " : " Err(${receiver}) " ,
" description " : " Wrap the expression in a `Result::Err` " ,
" scope " : " expr "
} ,
" Some " : {
" postfix " : " some " ,
" body " : " Some(${receiver}) " ,
" description " : " Wrap the expression in an `Option::Some` " ,
" scope " : " expr "
}
} " #,
2020-12-02 14:31:24 +00:00
2022-04-26 11:18:51 +00:00
/// List of rust-analyzer diagnostics to disable.
diagnostics_disabled : FxHashSet < String > = " [] " ,
2020-12-02 14:31:24 +00:00
/// Whether to show native rust-analyzer diagnostics.
diagnostics_enable : bool = " true " ,
/// Whether to show experimental rust-analyzer diagnostics that might
/// have more false positives than usual.
2022-04-26 11:00:45 +00:00
diagnostics_experimental_enable : bool = " false " ,
2021-04-21 22:09:37 +00:00
/// Map of prefixes to be substituted when parsing diagnostic file paths.
2021-04-21 03:03:35 +00:00
/// This should be the reverse mapping of what is passed to `rustc` as `--remap-path-prefix`.
2021-04-21 22:09:37 +00:00
diagnostics_remapPrefix : FxHashMap < String , String > = " {} " ,
2021-03-09 11:43:05 +00:00
/// List of warnings that should be displayed with hint severity.
///
/// The warnings will be indicated by faded text or three dots in code
/// and will not show up in the `Problems Panel`.
2021-05-29 16:08:14 +00:00
diagnostics_warningsAsHint : Vec < String > = " [] " ,
/// List of warnings that should be displayed with info severity.
///
/// The warnings will be indicated by a blue squiggly underline in code
/// and a blue icon in the `Problems Panel`.
2020-12-02 14:31:24 +00:00
diagnostics_warningsAsInfo : Vec < String > = " [] " ,
2021-07-19 13:09:29 +00:00
/// These directories will be ignored by rust-analyzer. They are
/// relative to the workspace root, and globs are not supported. You may
2021-07-22 11:03:06 +00:00
/// also need to add the folders to Code's `files.watcherExclude`.
2021-01-26 13:18:01 +00:00
files_excludeDirs : Vec < PathBuf > = " [] " ,
2022-04-26 11:18:51 +00:00
/// Controls file watching implementation.
2022-07-18 15:50:56 +00:00
files_watcher : FilesWatcherDef = " \" client \" " ,
2022-11-04 16:11:15 +00:00
2022-04-29 13:48:48 +00:00
/// Enables highlighting of related references while the cursor is on `break`, `loop`, `while`, or `for` keywords.
2022-04-26 11:00:45 +00:00
highlightRelated_breakPoints_enable : bool = " true " ,
2022-04-29 13:48:48 +00:00
/// Enables highlighting of all exit points while the cursor is on any `return`, `?`, `fn`, or return type arrow (`->`).
2022-04-26 11:18:51 +00:00
highlightRelated_exitPoints_enable : bool = " true " ,
2022-04-29 13:48:48 +00:00
/// Enables highlighting of related references while the cursor is on any identifier.
2022-04-26 11:18:51 +00:00
highlightRelated_references_enable : bool = " true " ,
2022-04-29 13:48:48 +00:00
/// Enables highlighting of all break points for a loop or block context while the cursor is on any `async` or `await` keywords.
2022-04-26 11:00:45 +00:00
highlightRelated_yieldPoints_enable : bool = " true " ,
2021-06-14 13:25:10 +00:00
2020-12-02 14:31:24 +00:00
/// Whether to show `Debug` action. Only applies when
2022-05-13 19:17:03 +00:00
/// `#rust-analyzer.hover.actions.enable#` is set.
2022-04-26 11:00:45 +00:00
hover_actions_debug_enable : bool = " true " ,
2020-12-02 14:31:24 +00:00
/// Whether to show HoverActions in Rust files.
2022-04-26 11:00:45 +00:00
hover_actions_enable : bool = " true " ,
2020-12-02 14:31:24 +00:00
/// Whether to show `Go to Type Definition` action. Only applies when
2022-05-13 19:17:03 +00:00
/// `#rust-analyzer.hover.actions.enable#` is set.
2022-04-26 11:00:45 +00:00
hover_actions_gotoTypeDef_enable : bool = " true " ,
2020-12-02 14:31:24 +00:00
/// Whether to show `Implementations` action. Only applies when
2022-05-13 19:17:03 +00:00
/// `#rust-analyzer.hover.actions.enable#` is set.
2022-04-26 11:00:45 +00:00
hover_actions_implementations_enable : bool = " true " ,
2021-06-04 13:49:43 +00:00
/// Whether to show `References` action. Only applies when
2022-05-13 19:17:03 +00:00
/// `#rust-analyzer.hover.actions.enable#` is set.
2022-04-26 11:00:45 +00:00
hover_actions_references_enable : bool = " false " ,
2020-12-02 14:31:24 +00:00
/// Whether to show `Run` action. Only applies when
2022-05-13 19:17:03 +00:00
/// `#rust-analyzer.hover.actions.enable#` is set.
2022-04-26 11:00:45 +00:00
hover_actions_run_enable : bool = " true " ,
2020-12-02 14:31:24 +00:00
2022-04-26 11:18:51 +00:00
/// Whether to show documentation on hover.
2022-08-16 16:12:15 +00:00
hover_documentation_enable : bool = " true " ,
2022-08-16 14:51:40 +00:00
/// Whether to show keyword hover popups. Only applies when
/// `#rust-analyzer.hover.documentation.enable#` is set.
2022-08-16 16:12:15 +00:00
hover_documentation_keywords_enable : bool = " true " ,
2022-04-26 11:18:51 +00:00
/// Use markdown syntax for links in hover.
hover_links_enable : bool = " true " ,
/// 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.
2022-04-29 08:56:32 +00:00
imports_granularity_enforce : bool = " false " ,
2022-04-26 11:18:51 +00:00
/// How imports should be grouped into use statements.
2022-04-29 08:56:32 +00:00
imports_granularity_group : ImportGranularityDef = " \" crate \" " ,
2022-04-26 11:18:51 +00:00
/// Group inserted imports by the https://rust-analyzer.github.io/manual.html#auto-import[following order]. Groups are separated by newlines.
2022-04-29 08:56:32 +00:00
imports_group_enable : bool = " true " ,
2022-04-26 11:18:51 +00:00
/// Whether to allow import insertion to merge new imports into single path glob imports like `use std::fmt::*;`.
2022-04-29 08:56:32 +00:00
imports_merge_glob : bool = " true " ,
2022-09-13 13:09:40 +00:00
/// Prefer to unconditionally use imports of the core and alloc crate, over the std crate.
imports_prefer_no_std : bool = " false " ,
2022-04-26 11:18:51 +00:00
/// The path structure for newly inserted paths to use.
imports_prefix : ImportPrefixDef = " \" plain \" " ,
2020-12-02 14:31:24 +00:00
2022-05-14 12:26:08 +00:00
/// Whether to show inlay type hints for binding modes.
inlayHints_bindingModeHints_enable : bool = " false " ,
2022-03-16 20:16:55 +00:00
/// Whether to show inlay type hints for method chains.
2022-04-26 11:18:51 +00:00
inlayHints_chainingHints_enable : bool = " true " ,
2022-05-13 17:42:59 +00:00
/// Whether to show inlay hints after a closing `}` to indicate what item it belongs to.
inlayHints_closingBraceHints_enable : bool = " true " ,
/// Minimum number of lines required before the `}` until the hint is shown (set to 0 or 1
/// to always show them).
inlayHints_closingBraceHints_minLines : usize = " 25 " ,
2022-05-28 12:13:25 +00:00
/// Whether to show inlay type hints for return types of closures.
inlayHints_closureReturnTypeHints_enable : ClosureReturnTypeHintsDef = " \" never \" " ,
2023-04-13 22:35:00 +00:00
/// Closure notation in type and chaining inlay hints.
2023-04-06 12:44:38 +00:00
inlayHints_closureStyle : ClosureStyle = " \" impl_fn \" " ,
2022-12-23 10:28:46 +00:00
/// Whether to show enum variant discriminant hints.
inlayHints_discriminantHints_enable : DiscriminantHintsDef = " \" never \" " ,
2022-11-04 21:59:07 +00:00
/// Whether to show inlay hints for type adjustments.
inlayHints_expressionAdjustmentHints_enable : AdjustmentHintsDef = " \" never \" " ,
2022-12-21 18:18:12 +00:00
/// Whether to hide inlay hints for type adjustments outside of `unsafe` blocks.
inlayHints_expressionAdjustmentHints_hideOutsideUnsafe : bool = " false " ,
2022-12-21 15:00:05 +00:00
/// Whether to show inlay hints as postfix ops (`.*` instead of `*`, etc).
inlayHints_expressionAdjustmentHints_mode : AdjustmentHintsModeDef = " \" prefix \" " ,
2022-03-18 17:11:16 +00:00
/// Whether to show inlay type hints for elided lifetimes in function signatures.
2022-03-22 15:27:59 +00:00
inlayHints_lifetimeElisionHints_enable : LifetimeElisionDef = " \" never \" " ,
2022-03-19 19:12:14 +00:00
/// Whether to prefer using parameter names as the name for elided lifetime hints if possible.
2022-04-29 13:48:48 +00:00
inlayHints_lifetimeElisionHints_useParameterNames : bool = " false " ,
2021-03-23 17:04:48 +00:00
/// Maximum length for inlay hints. Set to null to have an unlimited length.
2022-04-29 13:48:48 +00:00
inlayHints_maxLength : Option < usize > = " 25 " ,
2020-12-02 14:31:24 +00:00
/// Whether to show function parameter name inlay hints at the call
/// site.
2022-04-26 11:00:45 +00:00
inlayHints_parameterHints_enable : bool = " true " ,
2022-11-04 21:59:07 +00:00
/// Whether to show inlay hints for compiler inserted reborrows.
/// This setting is deprecated in favor of #rust-analyzer.inlayHints.expressionAdjustmentHints.enable#.
2022-05-12 11:39:32 +00:00
inlayHints_reborrowHints_enable : ReborrowHintsDef = " \" never \" " ,
2022-05-11 02:15:07 +00:00
/// Whether to render leading colons for type hints, and trailing colons for parameter hints.
2022-04-29 13:48:48 +00:00
inlayHints_renderColons : bool = " true " ,
2020-12-02 14:31:24 +00:00
/// Whether to show inlay type hints for variables.
2022-04-26 11:00:45 +00:00
inlayHints_typeHints_enable : bool = " true " ,
2022-05-15 11:17:52 +00:00
/// Whether to hide inlay type hints for `let` statements that initialize to a closure.
/// Only applies to closures with blocks, same as `#rust-analyzer.inlayHints.closureReturnTypeHints.enable#`.
inlayHints_typeHints_hideClosureInitialization : bool = " false " ,
2022-04-29 13:48:48 +00:00
/// Whether to hide inlay type hints for constructors.
inlayHints_typeHints_hideNamedConstructor : bool = " false " ,
2023-02-03 11:16:25 +00:00
/// Enables the experimental support for interpreting tests.
interpret_tests : bool = " false " ,
2020-12-02 14:31:24 +00:00
2022-04-26 11:18:51 +00:00
/// Join lines merges consecutive declaration and initialization of an assignment.
joinLines_joinAssignments : bool = " true " ,
2021-07-05 20:31:44 +00:00
/// Join lines inserts else between consecutive ifs.
joinLines_joinElseIf : bool = " true " ,
/// Join lines removes trailing commas.
joinLines_removeTrailingComma : bool = " true " ,
/// Join lines unwraps trivial blocks.
joinLines_unwrapTrivialBlock : bool = " true " ,
2022-09-12 21:34:13 +00:00
2020-12-02 14:31:24 +00:00
/// Whether to show `Debug` lens. Only applies when
/// `#rust-analyzer.lens.enable#` is set.
2022-04-26 11:00:45 +00:00
lens_debug_enable : bool = " true " ,
2020-12-02 14:31:24 +00:00
/// Whether to show CodeLens in Rust files.
lens_enable : bool = " true " ,
2022-04-26 11:18:51 +00:00
/// Internal config: use custom client-side commands even when the
/// client doesn't set the corresponding capability.
lens_forceCustomCommands : bool = " true " ,
2020-12-02 14:31:24 +00:00
/// Whether to show `Implementations` lens. Only applies when
/// `#rust-analyzer.lens.enable#` is set.
2022-04-26 11:00:45 +00:00
lens_implementations_enable : bool = " true " ,
2022-09-12 21:34:13 +00:00
/// Where to render annotations.
lens_location : AnnotationLocation = " \" above_name \" " ,
2022-04-26 11:00:45 +00:00
/// Whether to show `References` lens for Struct, Enum, and Union.
2021-10-04 07:18:31 +00:00
/// Only applies when `#rust-analyzer.lens.enable#` is set.
2022-04-26 11:00:45 +00:00
lens_references_adt_enable : bool = " false " ,
2021-10-04 07:18:31 +00:00
/// Whether to show `References` lens for Enum Variants.
/// Only applies when `#rust-analyzer.lens.enable#` is set.
2022-04-27 15:51:44 +00:00
lens_references_enumVariant_enable : bool = " false " ,
2022-04-26 11:18:51 +00:00
/// Whether to show `Method References` lens. Only applies when
/// `#rust-analyzer.lens.enable#` is set.
lens_references_method_enable : bool = " false " ,
/// Whether to show `References` lens for Trait.
/// Only applies when `#rust-analyzer.lens.enable#` is set.
lens_references_trait_enable : bool = " false " ,
/// Whether to show `Run` lens. Only applies when
/// `#rust-analyzer.lens.enable#` is set.
lens_run_enable : bool = " true " ,
2020-12-02 14:31:24 +00:00
/// Disable project auto-discovery in favor of explicitly specified set
2021-03-09 11:43:05 +00:00
/// of projects.
///
/// Elements must be paths pointing to `Cargo.toml`,
2020-12-22 00:15:50 +00:00
/// `rust-project.json`, or JSON objects in `rust-project.json` format.
2020-12-02 14:31:24 +00:00
linkedProjects : Vec < ManifestOrProjectJson > = " [] " ,
2021-01-06 10:54:28 +00:00
2021-01-26 13:03:24 +00:00
/// Number of syntax trees rust-analyzer keeps in memory. Defaults to 128.
2022-04-26 11:00:45 +00:00
lru_capacity : Option < usize > = " null " ,
2023-03-25 22:03:22 +00:00
/// Sets the LRU capacity of the specified queries.
lru_query_capacities : FxHashMap < Box < str > , usize > = " {} " ,
2021-01-06 10:54:28 +00:00
2020-12-02 14:31:24 +00:00
/// Whether to show `can't find Cargo.toml` error message.
notifications_cargoTomlNotFound : bool = " true " ,
2021-01-06 10:54:28 +00:00
2022-12-09 13:11:46 +00:00
/// How many worker threads in the main loop. The default `null` means to pick automatically.
numThreads : Option < usize > = " null " ,
2022-04-26 11:18:51 +00:00
/// Expand attribute macros. Requires `#rust-analyzer.procMacro.enable#` to be set.
procMacro_attributes_enable : bool = " true " ,
2022-04-29 13:48:48 +00:00
/// Enable support for procedural macros, implies `#rust-analyzer.cargo.buildScripts.enable#`.
2021-03-15 15:19:08 +00:00
procMacro_enable : bool = " true " ,
2022-01-06 12:44:21 +00:00
/// These proc-macros will be ignored when trying to expand them.
///
/// This config takes a map of crate names with the exported proc-macro names to ignore as values.
procMacro_ignored : FxHashMap < Box < str > , Box < [ Box < str > ] > > = " {} " ,
2023-04-26 06:06:15 +00:00
/// Internal config, path to proc-macro server executable.
2022-04-26 11:18:51 +00:00
procMacro_server : Option < PathBuf > = " null " ,
2020-12-02 14:31:24 +00:00
2022-09-09 17:58:06 +00:00
/// Exclude imports from find-all-references.
references_excludeImports : bool = " false " ,
2020-12-02 14:31:24 +00:00
/// Command to be executed instead of 'cargo' for runnables.
2022-04-26 11:00:45 +00:00
runnables_command : Option < String > = " null " ,
2020-12-02 14:31:24 +00:00
/// Additional arguments to be passed to cargo for runnables such as
2021-03-09 11:43:05 +00:00
/// tests or binaries. For example, it may be `--release`.
2022-04-26 11:00:45 +00:00
runnables_extraArgs : Vec < String > = " [] " ,
2020-12-02 14:31:24 +00:00
2021-03-08 21:56:42 +00:00
/// Path to the Cargo.toml of the rust compiler workspace, for usage in rustc_private
2021-10-14 05:49:22 +00:00
/// projects, or "discover" to try to automatically find it if the `rustc-dev` component
/// is installed.
2021-03-08 21:56:42 +00:00
///
/// Any project which uses rust-analyzer with the rustcPrivate
2021-03-08 16:47:40 +00:00
/// crates must set `[package.metadata.rust-analyzer] rustc_private=true` to use it.
2021-03-08 21:56:42 +00:00
///
2021-10-14 05:49:22 +00:00
/// This option does not take effect until rust-analyzer is restarted.
2022-04-26 11:00:45 +00:00
rustc_source : Option < String > = " null " ,
2020-12-02 14:31:24 +00:00
2020-12-22 00:15:50 +00:00
/// Additional arguments to `rustfmt`.
2020-12-02 14:31:24 +00:00
rustfmt_extraArgs : Vec < String > = " [] " ,
/// Advanced option, fully override the command rust-analyzer uses for
2023-02-21 10:22:38 +00:00
/// formatting. This should be the equivalent of `rustfmt` here, and
/// not that of `cargo fmt`. The file contents will be passed on the
/// standard input and the formatted result will be read from the
/// standard output.
2020-12-02 14:31:24 +00:00
rustfmt_overrideCommand : Option < Vec < String > > = " null " ,
2021-05-04 21:13:51 +00:00
/// Enables the use of rustfmt's unstable range formatting command for the
/// `textDocument/rangeFormatting` request. The rustfmt option is unstable and only
/// available on a nightly build.
2022-04-26 11:00:45 +00:00
rustfmt_rangeFormatting_enable : bool = " false " ,
2021-02-23 12:03:31 +00:00
2022-08-22 12:15:09 +00:00
/// Inject additional highlighting into doc comments.
2022-08-22 11:15:42 +00:00
///
2022-08-22 12:15:09 +00:00
/// When enabled, rust-analyzer will highlight rust source in doc comments as well as intra
/// doc links.
semanticHighlighting_doc_comment_inject_enable : bool = " true " ,
2022-08-22 11:21:30 +00:00
/// Use semantic tokens for operators.
///
/// When disabled, rust-analyzer will emit semantic tokens only for operator tokens when
/// they are tagged with modifiers.
semanticHighlighting_operator_enable : bool = " true " ,
/// Use specialized semantic tokens for operators.
///
/// When enabled, rust-analyzer will emit special token types for operator tokens instead
/// of the generic `operator` token type.
semanticHighlighting_operator_specialization_enable : bool = " false " ,
2023-04-13 22:35:00 +00:00
/// Use semantic tokens for punctuation.
2022-08-22 11:44:07 +00:00
///
2022-08-22 12:15:09 +00:00
/// When disabled, rust-analyzer will emit semantic tokens only for punctuation tokens when
/// they are tagged with modifiers or have a special role.
semanticHighlighting_punctuation_enable : bool = " false " ,
/// When enabled, rust-analyzer will emit a punctuation semantic token for the `!` of macro
/// calls.
semanticHighlighting_punctuation_separate_macro_bang : bool = " false " ,
2023-04-13 22:35:00 +00:00
/// Use specialized semantic tokens for punctuation.
2022-08-22 12:15:09 +00:00
///
/// When enabled, rust-analyzer will emit special token types for punctuation tokens instead
/// of the generic `punctuation` token type.
semanticHighlighting_punctuation_specialization_enable : bool = " false " ,
/// Use semantic tokens for strings.
///
/// In some editors (e.g. vscode) semantic tokens override other highlighting grammars.
/// By disabling semantic tokens for strings, other grammars can be used to highlight
/// their contents.
semanticHighlighting_strings_enable : bool = " true " ,
2022-04-26 11:18:51 +00:00
2022-04-27 15:51:44 +00:00
/// Show full signature of the callable. Only shows parameters if disabled.
signatureInfo_detail : SignatureDetail = " \" full \" " ,
2022-04-26 11:18:51 +00:00
/// Show documentation.
signatureInfo_documentation_enable : bool = " true " ,
2021-02-23 12:03:31 +00:00
2022-05-25 10:15:36 +00:00
/// Whether to insert closing angle brackets when typing an opening angle bracket of a generic argument list.
typing_autoClosingAngleBrackets_enable : bool = " false " ,
2021-02-23 12:03:31 +00:00
/// Workspace symbol search kind.
2021-10-04 20:13:12 +00:00
workspace_symbol_search_kind : WorkspaceSymbolSearchKindDef = " \" only_types \" " ,
2021-11-18 16:30:36 +00:00
/// Limits the number of items returned from a workspace symbol search (Defaults to 128).
/// Some clients like vs-code issue new searches on result filtering and don't require all results to be returned in the initial search.
/// Other clients requires all results upfront and might require a higher limit.
workspace_symbol_search_limit : usize = " 128 " ,
2022-04-26 11:18:51 +00:00
/// Workspace symbol search scope.
workspace_symbol_search_scope : WorkspaceSymbolSearchScopeDef = " \" workspace \" " ,
2020-12-02 14:31:24 +00:00
}
}
2021-01-06 10:54:28 +00:00
impl Default for ConfigData {
fn default ( ) -> Self {
2022-01-06 13:23:35 +00:00
ConfigData ::from_json ( serde_json ::Value ::Null , & mut Vec ::new ( ) )
2021-01-06 10:54:28 +00:00
}
}
2020-04-01 12:32:04 +00:00
#[ derive(Debug, Clone) ]
pub struct Config {
2023-04-18 12:27:01 +00:00
discovered_projects : Vec < ProjectManifest > ,
2023-04-18 11:25:54 +00:00
/// The workspace roots as registered by the LSP client
workspace_roots : Vec < AbsPathBuf > ,
2022-04-11 11:05:34 +00:00
caps : lsp_types ::ClientCapabilities ,
root_path : AbsPathBuf ,
2021-01-06 10:54:28 +00:00
data : ConfigData ,
2021-05-23 17:56:54 +00:00
detached_files : Vec < AbsPathBuf > ,
2021-10-04 17:22:41 +00:00
snippets : Vec < Snippet > ,
2020-08-18 10:35:36 +00:00
}
2022-05-12 10:29:40 +00:00
type ParallelCachePrimingNumThreads = u8 ;
2022-04-14 10:15:58 +00:00
2020-07-01 14:42:14 +00:00
#[ derive(Debug, Clone, Eq, PartialEq) ]
2020-06-03 10:22:01 +00:00
pub enum LinkedProject {
ProjectManifest ( ProjectManifest ) ,
2020-06-24 12:57:37 +00:00
InlineJsonProject ( ProjectJson ) ,
2020-06-03 10:22:01 +00:00
}
impl From < ProjectManifest > for LinkedProject {
fn from ( v : ProjectManifest ) -> Self {
LinkedProject ::ProjectManifest ( v )
}
}
2020-06-24 12:57:37 +00:00
impl From < ProjectJson > for LinkedProject {
fn from ( v : ProjectJson ) -> Self {
2020-06-09 19:26:42 +00:00
LinkedProject ::InlineJsonProject ( v )
2020-06-03 10:22:01 +00:00
}
2020-05-17 16:51:44 +00:00
}
2022-04-26 11:00:45 +00:00
pub struct CallInfoConfig {
pub params_only : bool ,
pub docs : bool ,
}
2020-05-17 16:51:44 +00:00
#[ derive(Clone, Debug, PartialEq, Eq) ]
pub struct LensConfig {
2022-04-14 10:15:58 +00:00
// runnables
2020-05-17 16:51:44 +00:00
pub run : bool ,
pub debug : bool ,
2023-04-28 17:14:30 +00:00
pub interpret : bool ,
2022-04-14 10:15:58 +00:00
// implementations
2020-07-05 09:19:16 +00:00
pub implementations : bool ,
2022-04-14 10:15:58 +00:00
// references
2020-09-01 13:33:02 +00:00
pub method_refs : bool ,
2022-04-26 11:00:45 +00:00
pub refs_adt : bool , // for Struct, Enum, Union and Trait
pub refs_trait : bool , // for Struct, Enum, Union and Trait
2021-10-04 07:18:31 +00:00
pub enum_variant_refs : bool ,
2022-09-12 03:40:33 +00:00
// annotations
2022-09-12 21:34:13 +00:00
pub location : AnnotationLocation ,
2022-09-12 03:40:33 +00:00
}
#[ derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize) ]
#[ serde(rename_all = " snake_case " ) ]
pub enum AnnotationLocation {
AboveName ,
AboveWholeItem ,
}
impl From < AnnotationLocation > for ide ::AnnotationLocation {
fn from ( location : AnnotationLocation ) -> Self {
match location {
AnnotationLocation ::AboveName = > ide ::AnnotationLocation ::AboveName ,
AnnotationLocation ::AboveWholeItem = > ide ::AnnotationLocation ::AboveWholeItem ,
}
}
2020-05-17 16:51:44 +00:00
}
impl LensConfig {
pub fn any ( & self ) -> bool {
2022-04-14 10:15:58 +00:00
self . run
| | self . debug
| | self . implementations
| | self . method_refs
2022-04-26 11:00:45 +00:00
| | self . refs_adt
| | self . refs_trait
2022-04-14 10:15:58 +00:00
| | self . enum_variant_refs
2020-05-17 16:51:44 +00:00
}
pub fn none ( & self ) -> bool {
! self . any ( )
}
pub fn runnable ( & self ) -> bool {
self . run | | self . debug
}
2020-09-01 13:33:02 +00:00
pub fn references ( & self ) -> bool {
2022-04-26 11:00:45 +00:00
self . method_refs | | self . refs_adt | | self . refs_trait | | self . enum_variant_refs
2020-09-01 13:33:02 +00:00
}
2020-04-01 12:32:04 +00:00
}
2021-06-21 19:41:06 +00:00
#[ derive(Clone, Debug, PartialEq, Eq) ]
pub struct HoverActionsConfig {
pub implementations : bool ,
pub references : bool ,
pub run : bool ,
pub debug : bool ,
pub goto_type_def : bool ,
}
impl HoverActionsConfig {
pub const NO_ACTIONS : Self = Self {
implementations : false ,
references : false ,
run : false ,
debug : false ,
goto_type_def : false ,
} ;
pub fn any ( & self ) -> bool {
self . implementations | | self . references | | self . runnable ( ) | | self . goto_type_def
}
pub fn none ( & self ) -> bool {
! self . any ( )
}
pub fn runnable ( & self ) -> bool {
self . run | | self . debug
}
}
2020-04-02 09:55:04 +00:00
#[ derive(Debug, Clone) ]
pub struct FilesConfig {
2020-04-02 10:47:58 +00:00
pub watcher : FilesWatcher ,
2021-01-26 13:18:01 +00:00
pub exclude : Vec < AbsPathBuf > ,
2020-04-02 09:55:04 +00:00
}
#[ derive(Debug, Clone) ]
2020-04-02 10:47:58 +00:00
pub enum FilesWatcher {
2020-04-02 09:55:04 +00:00
Client ,
2022-07-18 15:50:56 +00:00
Server ,
2020-04-02 09:55:04 +00:00
}
2020-04-01 15:00:37 +00:00
#[ derive(Debug, Clone) ]
pub struct NotificationsConfig {
pub cargo_toml_not_found : bool ,
}
2020-04-01 12:32:04 +00:00
#[ derive(Debug, Clone) ]
pub enum RustfmtConfig {
2021-05-04 21:13:51 +00:00
Rustfmt { extra_args : Vec < String > , enable_range_formatting : bool } ,
2020-07-09 22:28:12 +00:00
CustomCommand { command : String , args : Vec < String > } ,
2020-04-01 12:32:04 +00:00
}
2020-09-05 09:52:27 +00:00
/// Configuration for runnable items, such as `main` function or tests.
2021-01-06 10:54:28 +00:00
#[ derive(Debug, Clone) ]
2020-09-05 09:52:27 +00:00
pub struct RunnablesConfig {
2020-09-05 13:20:33 +00:00
/// Custom command to be executed instead of `cargo` for runnables.
pub override_cargo : Option < String > ,
2020-09-05 09:52:27 +00:00
/// Additional arguments for the `cargo`, e.g. `--release`.
pub cargo_extra_args : Vec < String > ,
}
2021-02-23 12:03:31 +00:00
/// Configuration for workspace symbol search requests.
#[ derive(Debug, Clone) ]
pub struct WorkspaceSymbolConfig {
/// In what scope should the symbol be searched in.
pub search_scope : WorkspaceSymbolSearchScope ,
2021-11-18 16:30:36 +00:00
/// What kind of symbol is being searched for.
2021-02-23 12:03:31 +00:00
pub search_kind : WorkspaceSymbolSearchKind ,
2021-11-18 16:30:36 +00:00
/// How many items are returned at most.
pub search_limit : usize ,
2021-02-23 12:03:31 +00:00
}
feat: gate custom clint-side commands behind capabilities
Some features of rust-analyzer requires support for custom commands on
the client side. Specifically, hover & code lens need this.
Stock LSP doesn't have a way for the server to know which client-side
commands are available. For that reason, we historically were just
sending the commands, not worrying whether the client supports then or
not.
That's not really great though, so in this PR we add infrastructure for
the client to explicitly opt-into custom commands, via `extensions`
field of the ClientCapabilities.
To preserve backwards compatability, if the client doesn't set the
field, we assume that it does support all custom commands. In the
future, we'll start treating that case as if the client doesn't support
commands.
So, if you maintain a rust-analyzer client and implement
`rust-analyzer/runSingle` and such, please also advertise this via a
capability.
2021-07-30 16:16:33 +00:00
pub struct ClientCommandsConfig {
pub run_single : bool ,
pub debug_single : bool ,
pub show_reference : bool ,
pub goto_location : bool ,
pub trigger_parameter_hints : bool ,
}
2022-04-27 16:25:12 +00:00
#[ derive(Debug) ]
2022-04-11 11:05:34 +00:00
pub struct ConfigUpdateError {
errors : Vec < ( String , serde_json ::Error ) > ,
}
impl fmt ::Display for ConfigUpdateError {
fn fmt ( & self , f : & mut fmt ::Formatter < '_ > ) -> fmt ::Result {
let errors = self . errors . iter ( ) . format_with ( " \n " , | ( key , e ) , f | {
f ( key ) ? ;
f ( & " : " ) ? ;
f ( e )
} ) ;
write! (
f ,
" rust-analyzer found {} invalid config value{}: \n {} " ,
self . errors . len ( ) ,
if self . errors . len ( ) = = 1 { " " } else { " s " } ,
errors
)
}
}
2020-06-24 11:34:24 +00:00
impl Config {
2023-02-07 21:36:44 +00:00
pub fn new (
root_path : AbsPathBuf ,
caps : ClientCapabilities ,
workspace_roots : Vec < AbsPathBuf > ,
) -> Self {
2021-05-23 17:32:22 +00:00
Config {
caps ,
data : ConfigData ::default ( ) ,
detached_files : Vec ::new ( ) ,
2023-04-18 12:27:01 +00:00
discovered_projects : Vec ::new ( ) ,
2021-05-23 17:32:22 +00:00
root_path ,
2021-10-04 17:22:41 +00:00
snippets : Default ::default ( ) ,
2023-02-07 21:36:44 +00:00
workspace_roots ,
2021-05-23 17:32:22 +00:00
}
2020-04-01 15:00:37 +00:00
}
2022-04-11 11:05:34 +00:00
2023-02-07 21:36:44 +00:00
pub fn rediscover_workspaces ( & mut self ) {
let discovered = ProjectManifest ::discover_all ( & self . workspace_roots ) ;
tracing ::info! ( " discovered projects: {:?} " , discovered ) ;
if discovered . is_empty ( ) {
tracing ::error! ( " failed to find any projects in {:?} " , & self . workspace_roots ) ;
}
2023-04-18 12:27:01 +00:00
self . discovered_projects = discovered ;
2023-02-07 21:36:44 +00:00
}
2023-04-18 11:25:54 +00:00
pub fn remove_workspace ( & mut self , path : & AbsPath ) {
if let Some ( position ) = self . workspace_roots . iter ( ) . position ( | it | it = = path ) {
self . workspace_roots . remove ( position ) ;
}
}
pub fn add_workspaces ( & mut self , paths : impl Iterator < Item = AbsPathBuf > ) {
self . workspace_roots . extend ( paths ) ;
}
2022-04-11 11:05:34 +00:00
pub fn update ( & mut self , mut json : serde_json ::Value ) -> Result < ( ) , ConfigUpdateError > {
2021-08-15 12:46:13 +00:00
tracing ::info! ( " updating config from JSON: {:#} " , json ) ;
2020-07-20 22:11:32 +00:00
if json . is_null ( ) | | json . as_object ( ) . map_or ( false , | it | it . is_empty ( ) ) {
2022-01-06 13:23:35 +00:00
return Ok ( ( ) ) ;
2020-07-20 21:25:48 +00:00
}
2022-01-06 13:23:35 +00:00
let mut errors = Vec ::new ( ) ;
self . detached_files =
get_field ::< Vec < PathBuf > > ( & mut json , & mut errors , " detachedFiles " , None , " [] " )
. into_iter ( )
. map ( AbsPathBuf ::assert )
. collect ( ) ;
2022-05-04 13:19:00 +00:00
patch_old_style ::patch_json_for_outdated_configs ( & mut json ) ;
2022-01-06 13:23:35 +00:00
self . data = ConfigData ::from_json ( json , & mut errors ) ;
2022-05-12 15:55:25 +00:00
tracing ::debug! ( " deserialized config data: {:#?} " , self . data ) ;
2021-10-05 15:18:40 +00:00
self . snippets . clear ( ) ;
2022-04-26 11:00:45 +00:00
for ( name , def ) in self . data . completion_snippets_custom . iter ( ) {
2021-10-05 15:18:40 +00:00
if def . prefix . is_empty ( ) & & def . postfix . is_empty ( ) {
continue ;
}
let scope = match def . scope {
SnippetScopeDef ::Expr = > SnippetScope ::Expr ,
SnippetScopeDef ::Type = > SnippetScope ::Type ,
SnippetScopeDef ::Item = > SnippetScope ::Item ,
} ;
match Snippet ::new (
& def . prefix ,
& def . postfix ,
& def . body ,
def . description . as_ref ( ) . unwrap_or ( name ) ,
& def . requires ,
scope ,
) {
Some ( snippet ) = > self . snippets . push ( snippet ) ,
2022-04-28 13:17:44 +00:00
None = > errors . push ( (
format! ( " snippet {name} is invalid " ) ,
< serde_json ::Error as serde ::de ::Error > ::custom (
" snippet path is invalid or triggers are missing " ,
) ,
) ) ,
2021-10-05 15:18:40 +00:00
}
}
2022-04-11 11:10:43 +00:00
self . validate ( & mut errors ) ;
2022-01-06 13:23:35 +00:00
if errors . is_empty ( ) {
Ok ( ( ) )
} else {
2022-04-11 11:05:34 +00:00
Err ( ConfigUpdateError { errors } )
2022-01-06 13:23:35 +00:00
}
2019-03-07 20:06:25 +00:00
}
2020-12-02 14:31:24 +00:00
2022-04-11 11:10:43 +00:00
fn validate ( & self , error_sink : & mut Vec < ( String , serde_json ::Error ) > ) {
use serde ::de ::Error ;
2023-01-09 13:15:13 +00:00
if self . data . check_command . is_empty ( ) {
2022-04-11 11:10:43 +00:00
error_sink . push ( (
2023-01-09 13:15:13 +00:00
" /check/command " . to_string ( ) ,
2022-04-11 11:10:43 +00:00
serde_json ::Error ::custom ( " expected a non-empty string " ) ,
) ) ;
}
}
2020-12-02 14:31:24 +00:00
pub fn json_schema ( ) -> serde_json ::Value {
ConfigData ::json_schema ( )
}
2022-04-11 11:05:34 +00:00
pub fn root_path ( & self ) -> & AbsPathBuf {
& self . root_path
}
pub fn caps ( & self ) -> & lsp_types ::ClientCapabilities {
& self . caps
}
pub fn detached_files ( & self ) -> & [ AbsPathBuf ] {
& self . detached_files
}
2019-03-07 20:06:25 +00:00
}
2020-06-03 12:48:38 +00:00
2021-01-05 13:57:05 +00:00
macro_rules ! try_ {
( $expr :expr ) = > {
| | -> _ { Some ( $expr ) } ( )
} ;
}
macro_rules ! try_or {
( $expr :expr , $or :expr ) = > {
try_! ( $expr ) . unwrap_or ( $or )
} ;
}
2022-04-14 10:15:58 +00:00
macro_rules ! try_or_def {
( $expr :expr ) = > {
try_! ( $expr ) . unwrap_or_default ( )
} ;
}
2021-01-05 13:57:05 +00:00
impl Config {
2023-02-07 21:36:44 +00:00
pub fn has_linked_projects ( & self ) -> bool {
! self . data . linkedProjects . is_empty ( )
}
2021-01-06 10:54:28 +00:00
pub fn linked_projects ( & self ) -> Vec < LinkedProject > {
2022-04-11 11:05:34 +00:00
match self . data . linkedProjects . as_slice ( ) {
2023-02-28 11:08:23 +00:00
[ ] = > {
2023-04-18 12:27:01 +00:00
let exclude_dirs : Vec < _ > =
self . data . files_excludeDirs . iter ( ) . map ( | p | self . root_path . join ( p ) ) . collect ( ) ;
self . discovered_projects
. iter ( )
. filter (
| ( ProjectManifest ::ProjectJson ( path )
| ProjectManifest ::CargoToml ( path ) ) | {
2022-05-21 14:18:55 +00:00
! exclude_dirs . iter ( ) . any ( | p | path . starts_with ( p ) )
2023-04-18 12:27:01 +00:00
} ,
)
. cloned ( )
. map ( LinkedProject ::from )
. collect ( )
2023-02-28 11:08:23 +00:00
}
2022-04-11 11:05:34 +00:00
linked_projects = > linked_projects
2021-01-06 10:54:28 +00:00
. iter ( )
2022-04-11 11:05:34 +00:00
. filter_map ( | linked_project | match linked_project {
ManifestOrProjectJson ::Manifest ( it ) = > {
let path = self . root_path . join ( it ) ;
ProjectManifest ::from_manifest_file ( path )
. map_err ( | e | tracing ::error! ( " failed to load linked project: {} " , e ) )
. ok ( )
. map ( Into ::into )
}
ManifestOrProjectJson ::ProjectJson ( it ) = > {
Some ( ProjectJson ::new ( & self . root_path , it . clone ( ) ) . into ( ) )
}
2021-01-06 10:54:28 +00:00
} )
2022-04-11 11:05:34 +00:00
. collect ( ) ,
2021-01-06 10:54:28 +00:00
}
}
2023-03-09 20:06:26 +00:00
pub fn add_linked_projects ( & mut self , linked_projects : Vec < ProjectJsonData > ) {
let mut linked_projects = linked_projects
. into_iter ( )
. map ( ManifestOrProjectJson ::ProjectJson )
. collect ::< Vec < ManifestOrProjectJson > > ( ) ;
self . data . linkedProjects . append ( & mut linked_projects ) ;
}
2021-01-10 19:38:35 +00:00
pub fn did_save_text_document_dynamic_registration ( & self ) -> bool {
2022-04-14 10:15:58 +00:00
let caps = try_or_def! ( self . caps . text_document . as_ref ( ) ? . synchronization . clone ( ) ? ) ;
2021-01-10 19:38:35 +00:00
caps . did_save = = Some ( true ) & & caps . dynamic_registration = = Some ( true )
}
2022-04-14 10:15:58 +00:00
2021-01-10 19:38:35 +00:00
pub fn did_change_watched_files_dynamic_registration ( & self ) -> bool {
2022-04-14 10:15:58 +00:00
try_or_def! (
self . caps . workspace . as_ref ( ) ? . did_change_watched_files . as_ref ( ) ? . dynamic_registration ?
2021-01-10 19:38:35 +00:00
)
}
2021-11-19 15:56:46 +00:00
pub fn prefill_caches ( & self ) -> bool {
2022-05-12 10:29:40 +00:00
self . data . cachePriming_enable
2021-11-19 15:56:46 +00:00
}
2021-01-05 13:57:05 +00:00
pub fn location_link ( & self ) -> bool {
2022-04-14 10:15:58 +00:00
try_or_def! ( self . caps . text_document . as_ref ( ) ? . definition ? . link_support ? )
2021-01-05 13:57:05 +00:00
}
2022-04-14 10:15:58 +00:00
2021-01-05 13:57:05 +00:00
pub fn line_folding_only ( & self ) -> bool {
2022-04-14 10:15:58 +00:00
try_or_def! ( self . caps . text_document . as_ref ( ) ? . folding_range . as_ref ( ) ? . line_folding_only ? )
2021-01-05 13:57:05 +00:00
}
2022-04-14 10:15:58 +00:00
2021-01-05 13:57:05 +00:00
pub fn hierarchical_symbols ( & self ) -> bool {
2022-04-14 10:15:58 +00:00
try_or_def! (
2021-01-05 13:57:05 +00:00
self . caps
. text_document
. as_ref ( ) ?
. document_symbol
. as_ref ( ) ?
2022-04-14 10:15:58 +00:00
. hierarchical_document_symbol_support ?
2021-01-05 13:57:05 +00:00
)
}
2022-04-14 10:15:58 +00:00
2021-01-05 13:57:05 +00:00
pub fn code_action_literals ( & self ) -> bool {
try_! ( self
. caps
. text_document
. as_ref ( ) ?
. code_action
. as_ref ( ) ?
. code_action_literal_support
. as_ref ( ) ? )
. is_some ( )
}
2022-04-14 10:15:58 +00:00
2021-01-05 13:57:05 +00:00
pub fn work_done_progress ( & self ) -> bool {
2022-04-14 10:15:58 +00:00
try_or_def! ( self . caps . window . as_ref ( ) ? . work_done_progress ? )
2021-01-05 13:57:05 +00:00
}
2022-04-14 10:15:58 +00:00
2021-03-10 14:49:01 +00:00
pub fn will_rename ( & self ) -> bool {
2022-04-14 10:15:58 +00:00
try_or_def! ( self . caps . workspace . as_ref ( ) ? . file_operations . as_ref ( ) ? . will_rename ? )
2021-03-10 14:49:01 +00:00
}
2022-04-14 10:15:58 +00:00
2021-04-16 15:31:47 +00:00
pub fn change_annotation_support ( & self ) -> bool {
try_! ( self
. caps
. workspace
. as_ref ( ) ?
. workspace_edit
. as_ref ( ) ?
. change_annotation_support
. as_ref ( ) ? )
. is_some ( )
}
2022-04-14 10:15:58 +00:00
2021-01-05 13:57:05 +00:00
pub fn code_action_resolve ( & self ) -> bool {
2022-04-14 10:15:58 +00:00
try_or_def! ( self
. caps
. text_document
. as_ref ( ) ?
. code_action
. as_ref ( ) ?
. resolve_support
. as_ref ( ) ?
. properties
. as_slice ( ) )
2021-01-05 13:57:05 +00:00
. iter ( )
. any ( | it | it = = " edit " )
}
2022-04-14 10:15:58 +00:00
2021-01-05 13:57:05 +00:00
pub fn signature_help_label_offsets ( & self ) -> bool {
2022-04-14 10:15:58 +00:00
try_or_def! (
2021-01-05 13:57:05 +00:00
self . caps
. text_document
. as_ref ( ) ?
. signature_help
. as_ref ( ) ?
. signature_information
. as_ref ( ) ?
. parameter_information
. as_ref ( ) ?
2022-04-14 10:15:58 +00:00
. label_offset_support ?
2021-01-05 13:57:05 +00:00
)
}
2022-04-14 10:15:58 +00:00
2022-07-18 17:25:43 +00:00
pub fn completion_label_details_support ( & self ) -> bool {
try_! ( self
. caps
. text_document
. as_ref ( ) ?
. completion
. as_ref ( ) ?
. completion_item
. as_ref ( ) ?
. label_details_support
. as_ref ( ) ? )
. is_some ( )
}
2022-10-25 11:43:26 +00:00
pub fn position_encoding ( & self ) -> PositionEncoding {
2023-02-14 00:56:28 +00:00
negotiated_encoding ( & self . caps )
2021-02-12 22:26:01 +00:00
}
2021-01-05 13:57:05 +00:00
fn experimental ( & self , index : & 'static str ) -> bool {
2022-04-14 10:15:58 +00:00
try_or_def! ( self . caps . experimental . as_ref ( ) ? . get ( index ) ? . as_bool ( ) ? )
2021-01-05 13:57:05 +00:00
}
2022-04-14 10:15:58 +00:00
2021-01-05 13:57:05 +00:00
pub fn code_action_group ( & self ) -> bool {
self . experimental ( " codeActionGroup " )
}
2022-04-14 10:15:58 +00:00
2023-01-23 12:10:25 +00:00
pub fn open_server_logs ( & self ) -> bool {
self . experimental ( " openServerLogs " )
}
2021-04-06 11:16:35 +00:00
pub fn server_status_notification ( & self ) -> bool {
self . experimental ( " serverStatusNotification " )
2021-01-05 13:57:05 +00:00
}
2021-01-06 10:54:28 +00:00
2023-01-04 17:04:45 +00:00
/// Whether the client supports colored output for full diagnostics from `checkOnSave`.
pub fn color_diagnostic_output ( & self ) -> bool {
self . experimental ( " colorDiagnosticOutput " )
}
2021-01-06 10:54:28 +00:00
pub fn publish_diagnostics ( & self ) -> bool {
self . data . diagnostics_enable
}
2022-04-14 10:15:58 +00:00
2021-01-06 10:54:28 +00:00
pub fn diagnostics ( & self ) -> DiagnosticsConfig {
DiagnosticsConfig {
2022-06-14 08:40:57 +00:00
proc_attr_macros_enabled : self . expand_proc_attr_macros ( ) ,
proc_macros_enabled : self . data . procMacro_enable ,
2022-04-26 11:00:45 +00:00
disable_experimental : ! self . data . diagnostics_experimental_enable ,
2021-01-06 10:54:28 +00:00
disabled : self . data . diagnostics_disabled . clone ( ) ,
2022-04-26 11:00:45 +00:00
expr_fill_default : match self . data . assist_expressionFillDefault {
2021-12-31 15:11:17 +00:00
ExprFillDefaultDef ::Todo = > ExprFillDefaultMode ::Todo ,
2022-01-07 13:13:34 +00:00
ExprFillDefaultDef ::Default = > ExprFillDefaultMode ::Default ,
2021-12-31 15:11:17 +00:00
} ,
2022-08-06 14:51:51 +00:00
insert_use : self . insert_use_config ( ) ,
2022-09-13 13:09:40 +00:00
prefer_no_std : self . data . imports_prefer_no_std ,
2021-01-06 10:54:28 +00:00
}
}
2022-04-14 10:15:58 +00:00
2021-01-06 10:54:28 +00:00
pub fn diagnostics_map ( & self ) -> DiagnosticsMapConfig {
DiagnosticsMapConfig {
2021-04-21 22:09:37 +00:00
remap_prefix : self . data . diagnostics_remapPrefix . clone ( ) ,
2021-01-06 10:54:28 +00:00
warnings_as_info : self . data . diagnostics_warningsAsInfo . clone ( ) ,
warnings_as_hint : self . data . diagnostics_warningsAsHint . clone ( ) ,
}
}
2022-04-14 10:15:58 +00:00
2023-03-12 10:59:57 +00:00
pub fn extra_args ( & self ) -> & Vec < String > {
& self . data . cargo_extraArgs
}
2022-08-18 21:41:17 +00:00
pub fn extra_env ( & self ) -> & FxHashMap < String , String > {
& self . data . cargo_extraEnv
}
2023-03-12 10:59:57 +00:00
pub fn check_extra_args ( & self ) -> Vec < String > {
let mut extra_args = self . extra_args ( ) . clone ( ) ;
extra_args . extend_from_slice ( & self . data . check_extraArgs ) ;
extra_args
}
2023-01-11 16:10:04 +00:00
pub fn check_extra_env ( & self ) -> FxHashMap < String , String > {
2022-08-18 21:41:17 +00:00
let mut extra_env = self . data . cargo_extraEnv . clone ( ) ;
2023-01-09 13:15:13 +00:00
extra_env . extend ( self . data . check_extraEnv . clone ( ) ) ;
2022-08-18 21:41:17 +00:00
extra_env
}
2023-03-25 22:03:22 +00:00
pub fn lru_parse_query_capacity ( & self ) -> Option < usize > {
2022-04-26 11:00:45 +00:00
self . data . lru_capacity
2021-01-06 10:54:28 +00:00
}
2022-04-14 10:15:58 +00:00
2023-03-25 22:03:22 +00:00
pub fn lru_query_capacities ( & self ) -> Option < & FxHashMap < Box < str > , usize > > {
self . data . lru_query_capacities . is_empty ( ) . not ( ) . then ( | | & self . data . lru_query_capacities )
}
2023-04-26 06:06:15 +00:00
pub fn proc_macro_srv ( & self ) -> Option < AbsPathBuf > {
2023-04-28 06:30:41 +00:00
let path = self . data . procMacro_server . clone ( ) ? ;
Some ( AbsPathBuf ::try_from ( path ) . unwrap_or_else ( | path | self . root_path . join ( & path ) ) )
2021-01-06 10:54:28 +00:00
}
2022-04-14 10:15:58 +00:00
2022-01-04 19:40:16 +00:00
pub fn dummy_replacements ( & self ) -> & FxHashMap < Box < str > , Box < [ Box < str > ] > > {
2022-01-06 12:44:21 +00:00
& self . data . procMacro_ignored
2022-01-04 19:40:16 +00:00
}
2022-04-14 10:15:58 +00:00
2023-03-26 06:39:28 +00:00
pub fn expand_proc_macros ( & self ) -> bool {
self . data . procMacro_enable
}
2021-06-03 14:11:20 +00:00
pub fn expand_proc_attr_macros ( & self ) -> bool {
2022-06-12 16:44:46 +00:00
self . data . procMacro_enable & & self . data . procMacro_attributes_enable
2021-06-03 14:11:20 +00:00
}
2022-04-14 10:15:58 +00:00
2021-01-06 10:54:28 +00:00
pub fn files ( & self ) -> FilesConfig {
FilesConfig {
2022-07-18 15:50:56 +00:00
watcher : match self . data . files_watcher {
FilesWatcherDef ::Client if self . did_change_watched_files_dynamic_registration ( ) = > {
2022-01-01 14:26:54 +00:00
FilesWatcher ::Client
}
2022-07-18 15:50:56 +00:00
_ = > FilesWatcher ::Server ,
2021-01-06 10:54:28 +00:00
} ,
2021-01-26 13:18:01 +00:00
exclude : self . data . files_excludeDirs . iter ( ) . map ( | it | self . root_path . join ( it ) ) . collect ( ) ,
2021-01-06 10:54:28 +00:00
}
}
2022-04-14 10:15:58 +00:00
2021-01-06 10:54:28 +00:00
pub fn notifications ( & self ) -> NotificationsConfig {
NotificationsConfig { cargo_toml_not_found : self . data . notifications_cargoTomlNotFound }
}
2022-04-14 10:15:58 +00:00
2021-01-06 10:54:28 +00:00
pub fn cargo_autoreload ( & self ) -> bool {
self . data . cargo_autoreload
}
2022-04-14 10:15:58 +00:00
2021-03-04 11:52:36 +00:00
pub fn run_build_scripts ( & self ) -> bool {
2022-04-26 11:00:45 +00:00
self . data . cargo_buildScripts_enable | | self . data . procMacro_enable
2021-01-28 15:33:02 +00:00
}
2022-04-14 10:15:58 +00:00
2021-01-06 10:54:28 +00:00
pub fn cargo ( & self ) -> CargoConfig {
2022-04-26 11:00:45 +00:00
let rustc_source = self . data . rustc_source . as_ref ( ) . map ( | rustc_src | {
2021-02-11 16:34:56 +00:00
if rustc_src = = " discover " {
2023-03-15 10:35:34 +00:00
RustLibSource ::Discover
2021-02-11 16:34:56 +00:00
} else {
2023-03-15 10:35:34 +00:00
RustLibSource ::Path ( self . root_path . join ( rustc_src ) )
2021-02-11 16:34:56 +00:00
}
} ) ;
2022-10-01 18:47:31 +00:00
let sysroot = self . data . cargo_sysroot . as_ref ( ) . map ( | sysroot | {
if sysroot = = " discover " {
2023-03-15 10:35:34 +00:00
RustLibSource ::Discover
2022-10-01 18:47:31 +00:00
} else {
2023-03-15 10:35:34 +00:00
RustLibSource ::Path ( self . root_path . join ( sysroot ) )
2022-10-01 18:47:31 +00:00
}
} ) ;
2023-02-06 11:07:33 +00:00
let sysroot_src =
self . data . cargo_sysrootSrc . as_ref ( ) . map ( | sysroot | self . root_path . join ( sysroot ) ) ;
2021-01-06 10:54:28 +00:00
CargoConfig {
2022-04-26 11:00:45 +00:00
features : match & self . data . cargo_features {
2022-09-19 14:40:43 +00:00
CargoFeaturesDef ::All = > CargoFeatures ::All ,
CargoFeaturesDef ::Selected ( features ) = > CargoFeatures ::Selected {
features : features . clone ( ) ,
no_default_features : self . data . cargo_noDefaultFeatures ,
} ,
2022-04-26 11:00:45 +00:00
} ,
2021-01-06 10:54:28 +00:00
target : self . data . cargo_target . clone ( ) ,
2022-10-01 18:47:31 +00:00
sysroot ,
2023-02-06 11:07:33 +00:00
sysroot_src ,
2021-08-23 17:18:11 +00:00
rustc_source ,
unset_test_crates : UnsetTestCrates ::Only ( self . data . cargo_unsetTest . clone ( ) ) ,
2022-04-26 11:00:45 +00:00
wrap_rustc_in_build_scripts : self . data . cargo_buildScripts_useRustcWrapper ,
2022-08-27 16:28:09 +00:00
invocation_strategy : match self . data . cargo_buildScripts_invocationStrategy {
2022-10-19 21:34:36 +00:00
InvocationStrategy ::Once = > project_model ::InvocationStrategy ::Once ,
2022-08-27 16:28:09 +00:00
InvocationStrategy ::PerWorkspace = > project_model ::InvocationStrategy ::PerWorkspace ,
} ,
2022-10-22 21:02:59 +00:00
invocation_location : match self . data . cargo_buildScripts_invocationLocation {
InvocationLocation ::Root = > {
project_model ::InvocationLocation ::Root ( self . root_path . clone ( ) )
}
InvocationLocation ::Workspace = > project_model ::InvocationLocation ::Workspace ,
} ,
2022-04-26 11:00:45 +00:00
run_build_script_command : self . data . cargo_buildScripts_overrideCommand . clone ( ) ,
2023-03-12 10:59:57 +00:00
extra_args : self . data . cargo_extraArgs . clone ( ) ,
2022-08-18 21:41:17 +00:00
extra_env : self . data . cargo_extraEnv . clone ( ) ,
2021-01-06 10:54:28 +00:00
}
}
2021-06-14 04:41:46 +00:00
2021-01-06 10:54:28 +00:00
pub fn rustfmt ( & self ) -> RustfmtConfig {
match & self . data . rustfmt_overrideCommand {
Some ( args ) if ! args . is_empty ( ) = > {
let mut args = args . clone ( ) ;
let command = args . remove ( 0 ) ;
RustfmtConfig ::CustomCommand { command , args }
}
2021-05-04 21:13:51 +00:00
Some ( _ ) | None = > RustfmtConfig ::Rustfmt {
extra_args : self . data . rustfmt_extraArgs . clone ( ) ,
2022-04-26 11:00:45 +00:00
enable_range_formatting : self . data . rustfmt_rangeFormatting_enable ,
2021-05-04 21:13:51 +00:00
} ,
2021-01-06 10:54:28 +00:00
}
}
2022-04-14 10:15:58 +00:00
2022-12-17 22:26:54 +00:00
pub fn flycheck ( & self ) -> FlycheckConfig {
2023-01-09 13:15:13 +00:00
match & self . data . check_overrideCommand {
2021-01-06 10:54:28 +00:00
Some ( args ) if ! args . is_empty ( ) = > {
let mut args = args . clone ( ) ;
let command = args . remove ( 0 ) ;
2022-08-18 21:41:17 +00:00
FlycheckConfig ::CustomCommand {
command ,
args ,
2023-01-11 16:10:04 +00:00
extra_env : self . check_extra_env ( ) ,
2023-01-09 13:15:13 +00:00
invocation_strategy : match self . data . check_invocationStrategy {
2022-10-22 21:02:59 +00:00
InvocationStrategy ::Once = > flycheck ::InvocationStrategy ::Once ,
InvocationStrategy ::PerWorkspace = > {
flycheck ::InvocationStrategy ::PerWorkspace
}
} ,
2023-01-09 13:15:13 +00:00
invocation_location : match self . data . check_invocationLocation {
2022-10-22 21:02:59 +00:00
InvocationLocation ::Root = > {
flycheck ::InvocationLocation ::Root ( self . root_path . clone ( ) )
}
InvocationLocation ::Workspace = > flycheck ::InvocationLocation ::Workspace ,
} ,
2022-08-18 21:41:17 +00:00
}
2021-01-06 10:54:28 +00:00
}
Some ( _ ) | None = > FlycheckConfig ::CargoCommand {
2023-01-09 13:15:13 +00:00
command : self . data . check_command . clone ( ) ,
2022-11-21 21:40:32 +00:00
target_triples : self
. data
2023-01-09 13:15:13 +00:00
. check_targets
2022-11-21 21:40:32 +00:00
. clone ( )
. and_then ( | targets | match & targets . 0 [ .. ] {
[ ] = > None ,
targets = > Some ( targets . into ( ) ) ,
} )
. unwrap_or_else ( | | self . data . cargo_target . clone ( ) . into_iter ( ) . collect ( ) ) ,
2023-01-09 13:15:13 +00:00
all_targets : self . data . check_allTargets ,
2021-01-06 10:54:28 +00:00
no_default_features : self
. data
2023-01-09 13:15:13 +00:00
. check_noDefaultFeatures
2021-01-06 10:54:28 +00:00
. unwrap_or ( self . data . cargo_noDefaultFeatures ) ,
2022-04-26 11:00:45 +00:00
all_features : matches ! (
2023-01-09 13:15:13 +00:00
self . data . check_features . as_ref ( ) . unwrap_or ( & self . data . cargo_features ) ,
2022-09-19 14:40:43 +00:00
CargoFeaturesDef ::All
2022-04-26 11:00:45 +00:00
) ,
features : match self
2021-01-06 10:54:28 +00:00
. data
2023-01-09 13:15:13 +00:00
. check_features
2021-01-06 10:54:28 +00:00
. clone ( )
2022-04-26 11:00:45 +00:00
. unwrap_or_else ( | | self . data . cargo_features . clone ( ) )
{
2022-09-19 14:40:43 +00:00
CargoFeaturesDef ::All = > vec! [ ] ,
CargoFeaturesDef ::Selected ( it ) = > it ,
2022-04-26 11:00:45 +00:00
} ,
2023-03-12 10:59:57 +00:00
extra_args : self . check_extra_args ( ) ,
2023-01-11 16:10:04 +00:00
extra_env : self . check_extra_env ( ) ,
2023-01-04 17:04:45 +00:00
ansi_color_output : self . color_diagnostic_output ( ) ,
2021-01-06 10:54:28 +00:00
} ,
2022-12-17 22:26:54 +00:00
}
}
pub fn check_on_save ( & self ) -> bool {
2022-12-20 10:31:07 +00:00
self . data . checkOnSave
2021-01-06 10:54:28 +00:00
}
2022-04-14 10:15:58 +00:00
2021-01-06 10:54:28 +00:00
pub fn runnables ( & self ) -> RunnablesConfig {
RunnablesConfig {
2022-04-26 11:00:45 +00:00
override_cargo : self . data . runnables_command . clone ( ) ,
cargo_extra_args : self . data . runnables_extraArgs . clone ( ) ,
2021-01-06 10:54:28 +00:00
}
}
2022-04-14 10:15:58 +00:00
2021-01-06 10:54:28 +00:00
pub fn inlay_hints ( & self ) -> InlayHintsConfig {
InlayHintsConfig {
2022-03-11 20:06:26 +00:00
render_colons : self . data . inlayHints_renderColons ,
2022-04-26 11:00:45 +00:00
type_hints : self . data . inlayHints_typeHints_enable ,
parameter_hints : self . data . inlayHints_parameterHints_enable ,
chaining_hints : self . data . inlayHints_chainingHints_enable ,
2022-12-23 10:28:46 +00:00
discriminant_hints : match self . data . inlayHints_discriminantHints_enable {
DiscriminantHintsDef ::Always = > ide ::DiscriminantHints ::Always ,
DiscriminantHintsDef ::Never = > ide ::DiscriminantHints ::Never ,
DiscriminantHintsDef ::Fieldless = > ide ::DiscriminantHints ::Fieldless ,
} ,
2022-05-28 12:13:25 +00:00
closure_return_type_hints : match self . data . inlayHints_closureReturnTypeHints_enable {
ClosureReturnTypeHintsDef ::Always = > ide ::ClosureReturnTypeHints ::Always ,
ClosureReturnTypeHintsDef ::Never = > ide ::ClosureReturnTypeHints ::Never ,
ClosureReturnTypeHintsDef ::WithBlock = > ide ::ClosureReturnTypeHints ::WithBlock ,
} ,
2022-03-22 15:27:59 +00:00
lifetime_elision_hints : match self . data . inlayHints_lifetimeElisionHints_enable {
2022-05-12 11:39:32 +00:00
LifetimeElisionDef ::Always = > ide ::LifetimeElisionHints ::Always ,
LifetimeElisionDef ::Never = > ide ::LifetimeElisionHints ::Never ,
LifetimeElisionDef ::SkipTrivial = > ide ::LifetimeElisionHints ::SkipTrivial ,
2022-03-19 18:01:19 +00:00
} ,
2022-04-26 11:00:45 +00:00
hide_named_constructor_hints : self . data . inlayHints_typeHints_hideNamedConstructor ,
2022-05-15 11:17:52 +00:00
hide_closure_initialization_hints : self
. data
. inlayHints_typeHints_hideClosureInitialization ,
2023-04-06 12:44:38 +00:00
closure_style : match self . data . inlayHints_closureStyle {
ClosureStyle ::ImplFn = > hir ::ClosureStyle ::ImplFn ,
ClosureStyle ::RustAnalyzer = > hir ::ClosureStyle ::RANotation ,
ClosureStyle ::WithId = > hir ::ClosureStyle ::ClosureWithId ,
ClosureStyle ::Hide = > hir ::ClosureStyle ::Hide ,
} ,
2022-11-04 21:59:07 +00:00
adjustment_hints : match self . data . inlayHints_expressionAdjustmentHints_enable {
AdjustmentHintsDef ::Always = > ide ::AdjustmentHints ::Always ,
AdjustmentHintsDef ::Never = > match self . data . inlayHints_reborrowHints_enable {
ReborrowHintsDef ::Always | ReborrowHintsDef ::Mutable = > {
ide ::AdjustmentHints ::ReborrowOnly
}
ReborrowHintsDef ::Never = > ide ::AdjustmentHints ::Never ,
} ,
AdjustmentHintsDef ::Reborrow = > ide ::AdjustmentHints ::ReborrowOnly ,
2022-05-12 11:39:32 +00:00
} ,
2022-12-21 15:00:05 +00:00
adjustment_hints_mode : match self . data . inlayHints_expressionAdjustmentHints_mode {
AdjustmentHintsModeDef ::Prefix = > ide ::AdjustmentHintsMode ::Prefix ,
AdjustmentHintsModeDef ::Postfix = > ide ::AdjustmentHintsMode ::Postfix ,
AdjustmentHintsModeDef ::PreferPrefix = > ide ::AdjustmentHintsMode ::PreferPrefix ,
AdjustmentHintsModeDef ::PreferPostfix = > ide ::AdjustmentHintsMode ::PreferPostfix ,
} ,
2022-12-21 18:18:12 +00:00
adjustment_hints_hide_outside_unsafe : self
. data
. inlayHints_expressionAdjustmentHints_hideOutsideUnsafe ,
2022-05-14 12:26:08 +00:00
binding_mode_hints : self . data . inlayHints_bindingModeHints_enable ,
2022-03-19 17:11:56 +00:00
param_names_for_lifetime_elision_hints : self
. data
2022-03-19 19:12:14 +00:00
. inlayHints_lifetimeElisionHints_useParameterNames ,
2021-01-06 10:54:28 +00:00
max_length : self . data . inlayHints_maxLength ,
2022-05-13 17:42:59 +00:00
closing_brace_hints_min_lines : if self . data . inlayHints_closingBraceHints_enable {
Some ( self . data . inlayHints_closingBraceHints_minLines )
} else {
None
} ,
2021-01-06 10:54:28 +00:00
}
}
2022-04-14 10:15:58 +00:00
2021-01-16 17:33:36 +00:00
fn insert_use_config ( & self ) -> InsertUseConfig {
InsertUseConfig {
2022-04-29 08:56:32 +00:00
granularity : match self . data . imports_granularity_group {
2021-05-18 17:49:15 +00:00
ImportGranularityDef ::Preserve = > ImportGranularity ::Preserve ,
ImportGranularityDef ::Item = > ImportGranularity ::Item ,
ImportGranularityDef ::Crate = > ImportGranularity ::Crate ,
ImportGranularityDef ::Module = > ImportGranularity ::Module ,
2021-01-16 17:33:36 +00:00
} ,
2022-04-29 08:56:32 +00:00
enforce_granularity : self . data . imports_granularity_enforce ,
2022-04-26 11:00:45 +00:00
prefix_kind : match self . data . imports_prefix {
2021-01-16 17:33:36 +00:00
ImportPrefixDef ::Plain = > PrefixKind ::Plain ,
ImportPrefixDef ::ByCrate = > PrefixKind ::ByCrate ,
ImportPrefixDef ::BySelf = > PrefixKind ::BySelf ,
} ,
2022-04-29 08:56:32 +00:00
group : self . data . imports_group_enable ,
skip_glob_imports : ! self . data . imports_merge_glob ,
2021-01-06 10:54:28 +00:00
}
}
2022-04-14 10:15:58 +00:00
2021-01-06 10:54:28 +00:00
pub fn completion ( & self ) -> CompletionConfig {
2021-01-06 17:43:46 +00:00
CompletionConfig {
enable_postfix_completions : self . data . completion_postfix_enable ,
2021-01-05 08:34:03 +00:00
enable_imports_on_the_fly : self . data . completion_autoimport_enable
2021-01-06 17:43:46 +00:00
& & completion_item_edit_resolve ( & self . caps ) ,
2021-05-30 14:41:33 +00:00
enable_self_on_the_fly : self . data . completion_autoself_enable ,
2022-02-23 15:02:54 +00:00
enable_private_editable : self . data . completion_privateEditable_enable ,
2022-05-14 11:53:41 +00:00
callable : match self . data . completion_callable_snippets {
CallableCompletionDef ::FillArguments = > Some ( CallableSnippets ::FillArguments ) ,
CallableCompletionDef ::AddParentheses = > Some ( CallableSnippets ::AddParentheses ) ,
CallableCompletionDef ::None = > None ,
} ,
2021-01-16 17:33:36 +00:00
insert_use : self . insert_use_config ( ) ,
2022-09-13 13:09:40 +00:00
prefer_no_std : self . data . imports_prefer_no_std ,
2022-04-14 10:15:58 +00:00
snippet_cap : SnippetCap ::new ( try_or_def! (
2021-01-06 17:43:46 +00:00
self . caps
. text_document
. as_ref ( ) ?
. completion
. as_ref ( ) ?
. completion_item
. as_ref ( ) ?
2022-04-14 10:15:58 +00:00
. snippet_support ?
2021-01-06 17:43:46 +00:00
) ) ,
2021-10-04 17:22:41 +00:00
snippets : self . snippets . clone ( ) ,
2023-01-20 02:21:43 +00:00
limit : self . data . completion_limit ,
2021-01-06 17:43:46 +00:00
}
2021-01-06 10:54:28 +00:00
}
2022-04-14 10:15:58 +00:00
2022-09-07 22:53:20 +00:00
pub fn find_all_refs_exclude_imports ( & self ) -> bool {
2022-09-09 17:58:06 +00:00
self . data . references_excludeImports
2022-09-07 22:53:20 +00:00
}
2022-03-27 08:49:00 +00:00
pub fn snippet_cap ( & self ) -> bool {
self . experimental ( " snippetTextEdit " )
}
2021-01-06 10:54:28 +00:00
pub fn assist ( & self ) -> AssistConfig {
2021-01-06 17:43:46 +00:00
AssistConfig {
snippet_cap : SnippetCap ::new ( self . experimental ( " snippetTextEdit " ) ) ,
allowed : None ,
2021-01-05 08:34:03 +00:00
insert_use : self . insert_use_config ( ) ,
2022-09-13 13:09:40 +00:00
prefer_no_std : self . data . imports_prefer_no_std ,
2022-10-06 18:41:02 +00:00
assist_emit_must_use : self . data . assist_emitMustUse ,
2021-01-06 17:43:46 +00:00
}
2021-01-06 10:54:28 +00:00
}
2022-04-14 10:15:58 +00:00
2021-07-05 20:31:44 +00:00
pub fn join_lines ( & self ) -> JoinLinesConfig {
JoinLinesConfig {
join_else_if : self . data . joinLines_joinElseIf ,
remove_trailing_comma : self . data . joinLines_removeTrailingComma ,
unwrap_trivial_blocks : self . data . joinLines_unwrapTrivialBlock ,
2021-08-22 18:28:39 +00:00
join_assignments : self . data . joinLines_joinAssignments ,
2021-07-05 20:31:44 +00:00
}
}
2022-04-14 10:15:58 +00:00
2022-04-26 11:00:45 +00:00
pub fn call_info ( & self ) -> CallInfoConfig {
CallInfoConfig {
2022-04-27 15:51:44 +00:00
params_only : matches ! ( self . data . signatureInfo_detail , SignatureDetail ::Parameters ) ,
2022-04-26 11:00:45 +00:00
docs : self . data . signatureInfo_documentation_enable ,
}
2021-01-06 10:54:28 +00:00
}
2022-04-14 10:15:58 +00:00
2021-01-06 10:54:28 +00:00
pub fn lens ( & self ) -> LensConfig {
LensConfig {
2022-04-26 11:00:45 +00:00
run : self . data . lens_enable & & self . data . lens_run_enable ,
debug : self . data . lens_enable & & self . data . lens_debug_enable ,
2023-04-28 17:14:30 +00:00
interpret : self . data . lens_enable
& & self . data . lens_run_enable
& & self . data . interpret_tests ,
2022-04-26 11:00:45 +00:00
implementations : self . data . lens_enable & & self . data . lens_implementations_enable ,
method_refs : self . data . lens_enable & & self . data . lens_references_method_enable ,
refs_adt : self . data . lens_enable & & self . data . lens_references_adt_enable ,
refs_trait : self . data . lens_enable & & self . data . lens_references_trait_enable ,
enum_variant_refs : self . data . lens_enable
2022-04-27 15:51:44 +00:00
& & self . data . lens_references_enumVariant_enable ,
2022-09-12 21:34:13 +00:00
location : self . data . lens_location ,
2021-01-06 10:54:28 +00:00
}
}
2022-04-14 10:15:58 +00:00
2021-06-21 19:41:06 +00:00
pub fn hover_actions ( & self ) -> HoverActionsConfig {
2022-04-26 11:00:45 +00:00
let enable = self . experimental ( " hoverActions " ) & & self . data . hover_actions_enable ;
2021-06-21 19:41:06 +00:00
HoverActionsConfig {
2022-04-26 11:00:45 +00:00
implementations : enable & & self . data . hover_actions_implementations_enable ,
references : enable & & self . data . hover_actions_references_enable ,
run : enable & & self . data . hover_actions_run_enable ,
debug : enable & & self . data . hover_actions_debug_enable ,
goto_type_def : enable & & self . data . hover_actions_gotoTypeDef_enable ,
2021-06-21 19:41:06 +00:00
}
}
2022-04-14 10:15:58 +00:00
2022-08-22 11:38:35 +00:00
pub fn highlighting_config ( & self ) -> HighlightConfig {
HighlightConfig {
2022-08-22 11:15:42 +00:00
strings : self . data . semanticHighlighting_strings_enable ,
punctuation : self . data . semanticHighlighting_punctuation_enable ,
2022-08-22 11:21:30 +00:00
specialize_punctuation : self
. data
. semanticHighlighting_punctuation_specialization_enable ,
2022-08-22 12:09:38 +00:00
macro_bang : self . data . semanticHighlighting_punctuation_separate_macro_bang ,
2022-08-22 11:21:30 +00:00
operator : self . data . semanticHighlighting_operator_enable ,
specialize_operator : self . data . semanticHighlighting_operator_specialization_enable ,
2022-08-22 11:44:07 +00:00
inject_doc_comment : self . data . semanticHighlighting_doc_comment_inject_enable ,
2022-08-22 11:38:35 +00:00
syntactic_name_ref_highlighting : false ,
2022-08-22 11:15:42 +00:00
}
2021-06-21 19:41:06 +00:00
}
2022-04-14 10:15:58 +00:00
2021-06-21 19:41:06 +00:00
pub fn hover ( & self ) -> HoverConfig {
HoverConfig {
2022-04-26 11:00:45 +00:00
links_in_hover : self . data . hover_links_enable ,
2023-01-20 13:29:12 +00:00
documentation : self . data . hover_documentation_enable ,
format : {
2022-04-14 10:15:58 +00:00
let is_markdown = try_or_def! ( self
. caps
. text_document
. as_ref ( ) ?
. hover
. as_ref ( ) ?
. content_format
. as_ref ( ) ?
. as_slice ( ) )
2021-06-21 19:57:01 +00:00
. contains ( & MarkupKind ::Markdown ) ;
if is_markdown {
HoverDocFormat ::Markdown
} else {
HoverDocFormat ::PlainText
}
2023-01-20 13:29:12 +00:00
} ,
2022-08-16 16:12:15 +00:00
keywords : self . data . hover_documentation_keywords_enable ,
2021-01-06 10:54:28 +00:00
}
}
2021-02-23 12:03:31 +00:00
pub fn workspace_symbol ( & self ) -> WorkspaceSymbolConfig {
WorkspaceSymbolConfig {
search_scope : match self . data . workspace_symbol_search_scope {
2021-10-04 20:13:12 +00:00
WorkspaceSymbolSearchScopeDef ::Workspace = > WorkspaceSymbolSearchScope ::Workspace ,
WorkspaceSymbolSearchScopeDef ::WorkspaceAndDependencies = > {
2021-02-23 12:03:31 +00:00
WorkspaceSymbolSearchScope ::WorkspaceAndDependencies
}
} ,
search_kind : match self . data . workspace_symbol_search_kind {
2021-10-04 20:13:12 +00:00
WorkspaceSymbolSearchKindDef ::OnlyTypes = > WorkspaceSymbolSearchKind ::OnlyTypes ,
WorkspaceSymbolSearchKindDef ::AllSymbols = > WorkspaceSymbolSearchKind ::AllSymbols ,
2021-02-23 12:03:31 +00:00
} ,
2021-11-18 16:30:36 +00:00
search_limit : self . data . workspace_symbol_search_limit ,
2021-02-23 12:03:31 +00:00
}
}
2021-01-06 10:54:28 +00:00
pub fn semantic_tokens_refresh ( & self ) -> bool {
2022-04-14 10:15:58 +00:00
try_or_def! ( self . caps . workspace . as_ref ( ) ? . semantic_tokens . as_ref ( ) ? . refresh_support ? )
2021-01-06 10:54:28 +00:00
}
2022-04-14 10:15:58 +00:00
2021-01-06 10:54:28 +00:00
pub fn code_lens_refresh ( & self ) -> bool {
2022-04-14 10:15:58 +00:00
try_or_def! ( self . caps . workspace . as_ref ( ) ? . code_lens . as_ref ( ) ? . refresh_support ? )
2021-01-06 10:54:28 +00:00
}
2022-04-14 10:15:58 +00:00
2023-01-19 20:44:13 +00:00
pub fn inlay_hints_refresh ( & self ) -> bool {
try_or_def! ( self . caps . workspace . as_ref ( ) ? . inlay_hint . as_ref ( ) ? . refresh_support ? )
}
2021-04-08 12:22:54 +00:00
pub fn insert_replace_support ( & self ) -> bool {
2022-04-14 10:15:58 +00:00
try_or_def! (
2021-04-08 12:22:54 +00:00
self . caps
. text_document
. as_ref ( ) ?
. completion
. as_ref ( ) ?
. completion_item
. as_ref ( ) ?
2022-04-14 10:15:58 +00:00
. insert_replace_support ?
2021-04-08 12:22:54 +00:00
)
}
2022-04-14 10:15:58 +00:00
feat: gate custom clint-side commands behind capabilities
Some features of rust-analyzer requires support for custom commands on
the client side. Specifically, hover & code lens need this.
Stock LSP doesn't have a way for the server to know which client-side
commands are available. For that reason, we historically were just
sending the commands, not worrying whether the client supports then or
not.
That's not really great though, so in this PR we add infrastructure for
the client to explicitly opt-into custom commands, via `extensions`
field of the ClientCapabilities.
To preserve backwards compatability, if the client doesn't set the
field, we assume that it does support all custom commands. In the
future, we'll start treating that case as if the client doesn't support
commands.
So, if you maintain a rust-analyzer client and implement
`rust-analyzer/runSingle` and such, please also advertise this via a
capability.
2021-07-30 16:16:33 +00:00
pub fn client_commands ( & self ) -> ClientCommandsConfig {
let commands =
try_or! ( self . caps . experimental . as_ref ( ) ? . get ( " commands " ) ? , & serde_json ::Value ::Null ) ;
let commands : Option < lsp_ext ::ClientCommandOptions > =
serde_json ::from_value ( commands . clone ( ) ) . ok ( ) ;
let force = commands . is_none ( ) & & self . data . lens_forceCustomCommands ;
let commands = commands . map ( | it | it . commands ) . unwrap_or_default ( ) ;
let get = | name : & str | commands . iter ( ) . any ( | it | it = = name ) | | force ;
ClientCommandsConfig {
run_single : get ( " rust-analyzer.runSingle " ) ,
debug_single : get ( " rust-analyzer.debugSingle " ) ,
show_reference : get ( " rust-analyzer.showReferences " ) ,
goto_location : get ( " rust-analyzer.gotoLocation " ) ,
trigger_parameter_hints : get ( " editor.action.triggerParameterHints " ) ,
}
}
2021-07-22 01:44:16 +00:00
pub fn highlight_related ( & self ) -> HighlightRelatedConfig {
HighlightRelatedConfig {
2022-04-26 11:00:45 +00:00
references : self . data . highlightRelated_references_enable ,
break_points : self . data . highlightRelated_breakPoints_enable ,
exit_points : self . data . highlightRelated_exitPoints_enable ,
yield_points : self . data . highlightRelated_yieldPoints_enable ,
2021-07-22 01:44:16 +00:00
}
}
2022-01-15 02:47:47 +00:00
pub fn prime_caches_num_threads ( & self ) -> u8 {
2022-05-12 10:29:40 +00:00
match self . data . cachePriming_numThreads {
2022-01-15 02:47:47 +00:00
0 = > num_cpus ::get_physical ( ) . try_into ( ) . unwrap_or ( u8 ::MAX ) ,
n = > n ,
}
}
2022-05-25 10:15:36 +00:00
2022-12-09 13:11:46 +00:00
pub fn main_loop_num_threads ( & self ) -> usize {
self . data . numThreads . unwrap_or ( num_cpus ::get_physical ( ) . try_into ( ) . unwrap_or ( 1 ) )
}
2022-05-25 10:15:36 +00:00
pub fn typing_autoclose_angle ( & self ) -> bool {
self . data . typing_autoClosingAngleBrackets_enable
}
2021-01-05 13:57:05 +00:00
}
2022-04-14 10:15:58 +00:00
// Deserialization definitions
2022-04-26 12:39:01 +00:00
macro_rules ! create_bool_or_string_de {
( $ident :ident < $bool :literal , $string :literal > ) = > {
fn $ident < ' de , D > ( d : D ) -> Result < ( ) , D ::Error >
where
D : serde ::Deserializer < ' de > ,
{
struct V ;
impl < ' de > serde ::de ::Visitor < ' de > for V {
type Value = ( ) ;
2022-07-20 13:05:02 +00:00
fn expecting ( & self , formatter : & mut fmt ::Formatter < '_ > ) -> fmt ::Result {
2022-04-26 12:39:01 +00:00
formatter . write_str ( concat! (
stringify! ( $bool ) ,
" or \" " ,
stringify! ( $string ) ,
" \" "
) )
}
fn visit_bool < E > ( self , v : bool ) -> Result < Self ::Value , E >
where
E : serde ::de ::Error ,
{
match v {
$bool = > Ok ( ( ) ) ,
_ = > Err ( serde ::de ::Error ::invalid_value (
serde ::de ::Unexpected ::Bool ( v ) ,
& self ,
) ) ,
}
}
fn visit_str < E > ( self , v : & str ) -> Result < Self ::Value , E >
where
E : serde ::de ::Error ,
{
match v {
$string = > Ok ( ( ) ) ,
_ = > Err ( serde ::de ::Error ::invalid_value (
serde ::de ::Unexpected ::Str ( v ) ,
& self ,
) ) ,
}
}
fn visit_enum < A > ( self , a : A ) -> Result < Self ::Value , A ::Error >
where
A : serde ::de ::EnumAccess < ' de > ,
{
use serde ::de ::VariantAccess ;
let ( variant , va ) = a . variant ::< & ' de str > ( ) ? ;
va . unit_variant ( ) ? ;
match variant {
$string = > Ok ( ( ) ) ,
_ = > Err ( serde ::de ::Error ::invalid_value (
serde ::de ::Unexpected ::Str ( variant ) ,
& self ,
) ) ,
}
}
}
d . deserialize_any ( V )
}
} ;
}
create_bool_or_string_de! ( true_or_always < true , " always " > ) ;
create_bool_or_string_de! ( false_or_never < false , " never " > ) ;
2022-04-29 12:06:43 +00:00
macro_rules ! named_unit_variant {
( $variant :ident ) = > {
pub ( super ) fn $variant < ' de , D > ( deserializer : D ) -> Result < ( ) , D ::Error >
where
D : serde ::Deserializer < ' de > ,
{
struct V ;
impl < ' de > serde ::de ::Visitor < ' de > for V {
type Value = ( ) ;
2022-07-20 13:05:02 +00:00
fn expecting ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
2022-04-29 12:06:43 +00:00
f . write_str ( concat! ( " \" " , stringify! ( $variant ) , " \" " ) )
}
fn visit_str < E : serde ::de ::Error > ( self , value : & str ) -> Result < Self ::Value , E > {
if value = = stringify! ( $variant ) {
Ok ( ( ) )
} else {
Err ( E ::invalid_value ( serde ::de ::Unexpected ::Str ( value ) , & self ) )
}
}
}
deserializer . deserialize_str ( V )
}
} ;
}
mod de_unit_v {
named_unit_variant! ( all ) ;
named_unit_variant! ( skip_trivial ) ;
2022-05-12 11:39:32 +00:00
named_unit_variant! ( mutable ) ;
2022-11-04 21:59:07 +00:00
named_unit_variant! ( reborrow ) ;
2022-12-23 10:28:46 +00:00
named_unit_variant! ( fieldless ) ;
2022-05-28 12:13:25 +00:00
named_unit_variant! ( with_block ) ;
2022-04-29 12:06:43 +00:00
}
2021-10-04 17:22:41 +00:00
#[ derive(Deserialize, Debug, Clone, Copy) ]
2021-10-04 19:44:33 +00:00
#[ serde(rename_all = " snake_case " ) ]
2021-10-04 17:22:41 +00:00
enum SnippetScopeDef {
Expr ,
Item ,
2021-10-05 15:18:40 +00:00
Type ,
2021-10-04 17:22:41 +00:00
}
2021-10-04 19:44:33 +00:00
impl Default for SnippetScopeDef {
fn default ( ) -> Self {
SnippetScopeDef ::Expr
}
}
2021-10-05 15:18:40 +00:00
#[ derive(Deserialize, Debug, Clone, Default) ]
#[ serde(default) ]
2021-10-04 17:22:41 +00:00
struct SnippetDef {
2021-10-04 15:49:21 +00:00
#[ serde(deserialize_with = " single_or_array " ) ]
2021-10-05 15:18:40 +00:00
prefix : Vec < String > ,
2021-10-04 15:49:21 +00:00
#[ serde(deserialize_with = " single_or_array " ) ]
2021-10-05 15:18:40 +00:00
postfix : Vec < String > ,
description : Option < String > ,
#[ serde(deserialize_with = " single_or_array " ) ]
body : Vec < String > ,
2021-10-04 15:49:21 +00:00
#[ serde(deserialize_with = " single_or_array " ) ]
requires : Vec < String > ,
2021-10-04 19:44:33 +00:00
scope : SnippetScopeDef ,
2021-10-04 15:49:21 +00:00
}
fn single_or_array < ' de , D > ( deserializer : D ) -> Result < Vec < String > , D ::Error >
where
D : serde ::Deserializer < ' de > ,
{
struct SingleOrVec ;
impl < ' de > serde ::de ::Visitor < ' de > for SingleOrVec {
type Value = Vec < String > ;
2022-07-20 13:05:02 +00:00
fn expecting ( & self , formatter : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
2021-10-04 15:49:21 +00:00
formatter . write_str ( " string or array of strings " )
}
fn visit_str < E > ( self , value : & str ) -> Result < Self ::Value , E >
where
E : serde ::de ::Error ,
{
Ok ( vec! [ value . to_owned ( ) ] )
}
fn visit_seq < A > ( self , seq : A ) -> Result < Self ::Value , A ::Error >
where
A : serde ::de ::SeqAccess < ' de > ,
{
Deserialize ::deserialize ( serde ::de ::value ::SeqAccessDeserializer ::new ( seq ) )
}
}
deserializer . deserialize_any ( SingleOrVec )
}
2021-01-06 10:54:28 +00:00
#[ derive(Deserialize, Debug, Clone) ]
2020-06-03 12:48:38 +00:00
#[ serde(untagged) ]
2020-06-24 13:52:07 +00:00
enum ManifestOrProjectJson {
2020-06-03 12:48:38 +00:00
Manifest ( PathBuf ) ,
2020-06-24 13:52:07 +00:00
ProjectJson ( ProjectJsonData ) ,
2020-06-03 12:48:38 +00:00
}
2020-07-09 22:28:12 +00:00
2021-12-31 15:11:17 +00:00
#[ derive(Deserialize, Debug, Clone) ]
#[ serde(rename_all = " snake_case " ) ]
2022-07-18 15:50:56 +00:00
enum ExprFillDefaultDef {
2021-12-31 15:11:17 +00:00
Todo ,
2022-01-07 13:13:34 +00:00
Default ,
2021-12-31 15:11:17 +00:00
}
2021-01-06 10:54:28 +00:00
#[ derive(Deserialize, Debug, Clone) ]
2020-10-05 15:41:49 +00:00
#[ serde(rename_all = " snake_case " ) ]
2021-05-18 17:49:15 +00:00
enum ImportGranularityDef {
2021-05-18 18:21:47 +00:00
Preserve ,
2021-05-18 17:49:15 +00:00
Item ,
2021-05-10 19:03:50 +00:00
Crate ,
Module ,
2020-09-12 09:55:01 +00:00
}
2022-05-13 17:52:44 +00:00
#[ derive(Deserialize, Debug, Copy, Clone) ]
2022-03-19 18:01:19 +00:00
#[ serde(rename_all = " snake_case " ) ]
2022-04-26 11:00:45 +00:00
enum CallableCompletionDef {
FillArguments ,
AddParentheses ,
2022-05-14 11:53:41 +00:00
None ,
2022-04-26 11:00:45 +00:00
}
#[ derive(Deserialize, Debug, Clone) ]
#[ serde(untagged) ]
2022-09-19 14:40:43 +00:00
enum CargoFeaturesDef {
2022-04-29 12:06:43 +00:00
#[ serde(deserialize_with = " de_unit_v::all " ) ]
2022-04-26 11:00:45 +00:00
All ,
2022-09-19 14:40:43 +00:00
Selected ( Vec < String > ) ,
2022-04-26 11:00:45 +00:00
}
2022-08-27 16:28:09 +00:00
#[ derive(Deserialize, Debug, Clone) ]
#[ serde(rename_all = " snake_case " ) ]
enum InvocationStrategy {
2022-10-19 21:34:36 +00:00
Once ,
2022-08-27 16:28:09 +00:00
PerWorkspace ,
}
2022-09-24 23:22:27 +00:00
#[ derive(Deserialize, Debug, Clone) ]
struct CheckOnSaveTargets ( #[ serde(deserialize_with = " single_or_array " ) ] Vec < String > ) ;
2022-10-22 21:02:59 +00:00
#[ derive(Deserialize, Debug, Clone) ]
#[ serde(rename_all = " snake_case " ) ]
enum InvocationLocation {
Root ,
Workspace ,
}
2022-03-19 18:01:19 +00:00
#[ derive(Deserialize, Debug, Clone) ]
2022-04-26 12:39:01 +00:00
#[ serde(untagged) ]
2022-03-19 18:01:19 +00:00
enum LifetimeElisionDef {
2022-04-26 12:39:01 +00:00
#[ serde(deserialize_with = " true_or_always " ) ]
2022-03-19 18:01:19 +00:00
Always ,
2022-04-26 12:39:01 +00:00
#[ serde(deserialize_with = " false_or_never " ) ]
2022-03-19 18:01:19 +00:00
Never ,
2022-04-29 12:06:43 +00:00
#[ serde(deserialize_with = " de_unit_v::skip_trivial " ) ]
2022-03-19 18:01:19 +00:00
SkipTrivial ,
}
2022-05-28 12:13:25 +00:00
#[ derive(Deserialize, Debug, Clone) ]
#[ serde(untagged) ]
enum ClosureReturnTypeHintsDef {
#[ serde(deserialize_with = " true_or_always " ) ]
Always ,
#[ serde(deserialize_with = " false_or_never " ) ]
Never ,
#[ serde(deserialize_with = " de_unit_v::with_block " ) ]
WithBlock ,
}
2023-04-06 12:44:38 +00:00
#[ derive(Deserialize, Debug, Clone) ]
#[ serde(rename_all = " snake_case " ) ]
enum ClosureStyle {
ImplFn ,
RustAnalyzer ,
WithId ,
Hide ,
}
2022-05-12 11:39:32 +00:00
#[ derive(Deserialize, Debug, Clone) ]
#[ serde(untagged) ]
enum ReborrowHintsDef {
#[ serde(deserialize_with = " true_or_always " ) ]
Always ,
#[ serde(deserialize_with = " false_or_never " ) ]
Never ,
#[ serde(deserialize_with = " de_unit_v::mutable " ) ]
Mutable ,
}
2022-11-04 21:59:07 +00:00
#[ derive(Deserialize, Debug, Clone) ]
#[ serde(untagged) ]
enum AdjustmentHintsDef {
#[ serde(deserialize_with = " true_or_always " ) ]
Always ,
#[ serde(deserialize_with = " false_or_never " ) ]
Never ,
#[ serde(deserialize_with = " de_unit_v::reborrow " ) ]
Reborrow ,
}
2022-12-23 10:28:46 +00:00
#[ derive(Deserialize, Debug, Clone) ]
#[ serde(untagged) ]
enum DiscriminantHintsDef {
#[ serde(deserialize_with = " true_or_always " ) ]
Always ,
#[ serde(deserialize_with = " false_or_never " ) ]
Never ,
#[ serde(deserialize_with = " de_unit_v::fieldless " ) ]
Fieldless ,
}
2022-12-21 15:00:05 +00:00
#[ derive(Deserialize, Debug, Clone) ]
#[ serde(rename_all = " snake_case " ) ]
enum AdjustmentHintsModeDef {
Prefix ,
Postfix ,
PreferPrefix ,
PreferPostfix ,
}
2022-07-18 15:50:56 +00:00
#[ derive(Deserialize, Debug, Clone) ]
#[ serde(rename_all = " snake_case " ) ]
enum FilesWatcherDef {
Client ,
Notify ,
Server ,
}
2021-01-06 10:54:28 +00:00
#[ derive(Deserialize, Debug, Clone) ]
2020-10-05 15:41:49 +00:00
#[ serde(rename_all = " snake_case " ) ]
enum ImportPrefixDef {
Plain ,
2021-06-13 20:00:39 +00:00
#[ serde(alias = " self " ) ]
2020-10-05 15:41:49 +00:00
BySelf ,
2021-06-13 20:00:39 +00:00
#[ serde(alias = " crate " ) ]
2020-10-05 15:41:49 +00:00
ByCrate ,
}
2021-02-23 12:03:31 +00:00
#[ derive(Deserialize, Debug, Clone) ]
#[ serde(rename_all = " snake_case " ) ]
2021-10-04 20:13:12 +00:00
enum WorkspaceSymbolSearchScopeDef {
2021-02-23 12:03:31 +00:00
Workspace ,
WorkspaceAndDependencies ,
}
2022-04-27 15:51:44 +00:00
#[ derive(Deserialize, Debug, Clone) ]
#[ serde(rename_all = " snake_case " ) ]
enum SignatureDetail {
Full ,
Parameters ,
}
2021-02-23 12:03:31 +00:00
#[ derive(Deserialize, Debug, Clone) ]
#[ serde(rename_all = " snake_case " ) ]
2021-10-04 20:13:12 +00:00
enum WorkspaceSymbolSearchKindDef {
2021-02-23 12:03:31 +00:00
OnlyTypes ,
AllSymbols ,
}
2020-12-02 14:31:24 +00:00
macro_rules ! _config_data {
( struct $name :ident {
$(
$( #[ doc=$doc:literal ] ) *
2021-05-18 17:49:15 +00:00
$field :ident $( | $alias :ident ) * : $ty :ty = $default :expr ,
2020-12-02 14:31:24 +00:00
) *
} ) = > {
2020-07-09 22:28:12 +00:00
#[ allow(non_snake_case) ]
2021-01-06 10:54:28 +00:00
#[ derive(Debug, Clone) ]
2020-07-09 22:28:12 +00:00
struct $name { $( $field : $ty , ) * }
impl $name {
2022-01-06 13:23:35 +00:00
fn from_json ( mut json : serde_json ::Value , error_sink : & mut Vec < ( String , serde_json ::Error ) > ) -> $name {
2020-07-09 22:28:12 +00:00
$name { $(
2021-01-07 11:37:38 +00:00
$field : get_field (
& mut json ,
2022-01-06 13:23:35 +00:00
error_sink ,
2021-01-07 11:37:38 +00:00
stringify! ( $field ) ,
2021-05-18 17:49:15 +00:00
None $( . or ( Some ( stringify! ( $alias ) ) ) ) * ,
2021-01-07 11:37:38 +00:00
$default ,
) ,
2020-07-09 22:28:12 +00:00
) * }
}
2020-12-02 14:31:24 +00:00
fn json_schema ( ) -> serde_json ::Value {
schema ( & [
$( {
let field = stringify! ( $field ) ;
let ty = stringify! ( $ty ) ;
2021-06-14 13:44:32 +00:00
2020-12-02 14:31:24 +00:00
( field , ty , & [ $( $doc ) , * ] , $default )
} , ) *
] )
}
2020-12-09 12:07:37 +00:00
#[ cfg(test) ]
fn manual ( ) -> String {
manual ( & [
$( {
let field = stringify! ( $field ) ;
let ty = stringify! ( $ty ) ;
2021-06-14 13:44:32 +00:00
2020-12-09 12:07:37 +00:00
( field , ty , & [ $( $doc ) , * ] , $default )
} , ) *
] )
}
2020-12-02 14:31:24 +00:00
}
2022-04-26 11:00:45 +00:00
2022-04-26 11:18:51 +00:00
#[ test ]
fn fields_are_sorted ( ) {
[ $( stringify! ( $field ) ) , * ] . windows ( 2 ) . for_each ( | w | assert! ( w [ 0 ] < = w [ 1 ] , " {} <= {} does not hold " , w [ 0 ] , w [ 1 ] ) ) ;
}
2020-07-09 22:28:12 +00:00
} ;
}
2020-12-02 14:31:24 +00:00
use _config_data as config_data ;
fn get_field < T : DeserializeOwned > (
json : & mut serde_json ::Value ,
2022-01-06 13:23:35 +00:00
error_sink : & mut Vec < ( String , serde_json ::Error ) > ,
2020-12-02 14:31:24 +00:00
field : & 'static str ,
2021-01-07 11:37:38 +00:00
alias : Option < & 'static str > ,
2020-12-02 14:31:24 +00:00
default : & str ,
) -> T {
2023-04-13 22:35:00 +00:00
// XXX: check alias first, to work around the VS Code where it pre-fills the
2021-01-07 11:37:38 +00:00
// defaults instead of sending an empty object.
alias
. into_iter ( )
. chain ( iter ::once ( field ) )
2022-12-20 10:31:07 +00:00
. filter_map ( move | field | {
2021-01-07 11:37:38 +00:00
let mut pointer = field . replace ( '_' , " / " ) ;
pointer . insert ( 0 , '/' ) ;
2022-12-20 10:31:07 +00:00
json . pointer_mut ( & pointer )
. map ( | it | serde_json ::from_value ( it . take ( ) ) . map_err ( | e | ( e , pointer ) ) )
} )
. find ( Result ::is_ok )
. and_then ( | res | match res {
Ok ( it ) = > Some ( it ) ,
Err ( ( e , pointer ) ) = > {
tracing ::warn! ( " Failed to deserialize config field at {}: {:?} " , pointer , e ) ;
error_sink . push ( ( pointer , e ) ) ;
None
}
2021-01-07 11:37:38 +00:00
} )
2022-12-20 10:31:07 +00:00
. unwrap_or_else ( | | serde_json ::from_str ( default ) . unwrap ( ) )
2020-12-02 14:31:24 +00:00
}
2020-07-09 22:28:12 +00:00
2020-12-02 14:31:24 +00:00
fn schema ( fields : & [ ( & 'static str , & 'static str , & [ & str ] , & str ) ] ) -> serde_json ::Value {
let map = fields
. iter ( )
. map ( | ( field , ty , doc , default ) | {
2022-03-12 12:22:12 +00:00
let name = field . replace ( '_' , " . " ) ;
2022-12-23 18:42:58 +00:00
let name = format! ( " rust-analyzer. {name} " ) ;
2020-12-02 14:31:24 +00:00
let props = field_props ( field , ty , doc , default ) ;
( name , props )
} )
. collect ::< serde_json ::Map < _ , _ > > ( ) ;
map . into ( )
}
fn field_props ( field : & str , ty : & str , doc : & [ & str ] , default : & str ) -> serde_json ::Value {
2021-03-09 11:43:05 +00:00
let doc = doc_comment_to_string ( doc ) ;
let doc = doc . trim_end_matches ( '\n' ) ;
2020-12-02 14:31:24 +00:00
assert! (
doc . ends_with ( '.' ) & & doc . starts_with ( char ::is_uppercase ) ,
2022-12-30 07:59:11 +00:00
" bad docs for {field}: {doc:?} "
2020-12-02 14:31:24 +00:00
) ;
let default = default . parse ::< serde_json ::Value > ( ) . unwrap ( ) ;
let mut map = serde_json ::Map ::default ( ) ;
macro_rules ! set {
( $( $key :literal : $value :tt ) , * $(, ) ? ) = > { { $(
map . insert ( $key . into ( ) , serde_json ::json! ( $value ) ) ;
) * } } ;
2020-07-09 22:28:12 +00:00
}
2020-12-02 14:31:24 +00:00
set! ( " markdownDescription " : doc ) ;
set! ( " default " : default ) ;
match ty {
" bool " = > set! ( " type " : " boolean " ) ,
2021-11-18 16:30:36 +00:00
" usize " = > set! ( " type " : " integer " , " minimum " : 0 ) ,
2020-12-02 14:31:24 +00:00
" String " = > set! ( " type " : " string " ) ,
" Vec<String> " = > set! {
" type " : " array " ,
2021-01-26 13:18:01 +00:00
" items " : { " type " : " string " } ,
} ,
" Vec<PathBuf> " = > set! {
" type " : " array " ,
2020-12-02 14:31:24 +00:00
" items " : { " type " : " string " } ,
} ,
" FxHashSet<String> " = > set! {
" type " : " array " ,
" items " : { " type " : " string " } ,
" uniqueItems " : true ,
} ,
2022-01-05 18:35:48 +00:00
" FxHashMap<Box<str>, Box<[Box<str>]>> " = > set! {
" type " : " object " ,
} ,
2021-10-04 20:13:12 +00:00
" FxHashMap<String, SnippetDef> " = > set! {
" type " : " object " ,
} ,
2021-04-21 03:03:35 +00:00
" FxHashMap<String, String> " = > set! {
" type " : " object " ,
} ,
2023-03-25 22:03:22 +00:00
" FxHashMap<Box<str>, usize> " = > set! {
" type " : " object " ,
} ,
2020-12-02 14:31:24 +00:00
" Option<usize> " = > set! {
" type " : [ " null " , " integer " ] ,
" minimum " : 0 ,
} ,
" Option<String> " = > set! {
" type " : [ " null " , " string " ] ,
} ,
2021-01-06 10:54:28 +00:00
" Option<PathBuf> " = > set! {
" type " : [ " null " , " string " ] ,
} ,
2020-12-02 14:31:24 +00:00
" Option<bool> " = > set! {
" type " : [ " null " , " boolean " ] ,
} ,
" Option<Vec<String>> " = > set! {
" type " : [ " null " , " array " ] ,
" items " : { " type " : " string " } ,
} ,
2021-12-31 15:11:17 +00:00
" ExprFillDefaultDef " = > set! {
" type " : " string " ,
2022-01-07 13:13:34 +00:00
" enum " : [ " todo " , " default " ] ,
2021-12-31 15:11:17 +00:00
" enumDescriptions " : [
2022-01-07 14:01:37 +00:00
" Fill missing expressions with the `todo` macro " ,
2022-01-07 13:07:02 +00:00
" Fill missing expressions with reasonable defaults, `new` or `default` constructors. "
2021-12-31 15:11:17 +00:00
] ,
} ,
2021-05-18 18:21:47 +00:00
" ImportGranularityDef " = > set! {
" type " : " string " ,
" enum " : [ " preserve " , " crate " , " module " , " item " ] ,
" enumDescriptions " : [
" Do not change the granularity of any imports and preserve the original structure written by the developer. " ,
" Merge imports from the same crate into a single use statement. Conversely, imports from different crates are split into separate statements. " ,
" Merge imports from the same module into a single use statement. Conversely, imports from different modules are split into separate statements. " ,
" Flatten imports so that each has its own use statement. "
2020-12-02 14:31:24 +00:00
] ,
} ,
" ImportPrefixDef " = > set! {
" type " : " string " ,
" enum " : [
" plain " ,
2021-06-13 20:00:39 +00:00
" self " ,
" crate "
2020-12-02 14:31:24 +00:00
] ,
" enumDescriptions " : [
" Insert import paths relative to the current module, using up to one `super` prefix if the parent module contains the requested item. " ,
2021-06-13 20:00:39 +00:00
" Insert import paths relative to the current module, using up to one `super` prefix if the parent module contains the requested item. Prefixes `self` in front of the path if it starts with a module. " ,
" Force import paths to be absolute by always starting them with `crate` or the extern crate name they come from. "
2020-12-02 14:31:24 +00:00
] ,
} ,
" Vec<ManifestOrProjectJson> " = > set! {
" type " : " array " ,
" items " : { " type " : [ " string " , " object " ] } ,
} ,
2021-10-04 20:13:12 +00:00
" WorkspaceSymbolSearchScopeDef " = > set! {
2021-02-23 12:03:31 +00:00
" type " : " string " ,
" enum " : [ " workspace " , " workspace_and_dependencies " ] ,
" enumDescriptions " : [
2022-05-14 11:53:41 +00:00
" Search in current workspace only. " ,
" Search in current workspace and dependencies. "
2021-02-23 12:03:31 +00:00
] ,
} ,
2021-10-04 20:13:12 +00:00
" WorkspaceSymbolSearchKindDef " = > set! {
2021-02-23 12:03:31 +00:00
" type " : " string " ,
" enum " : [ " only_types " , " all_symbols " ] ,
" enumDescriptions " : [
2022-05-14 11:53:41 +00:00
" Search for types only. " ,
" Search for all symbols kinds. "
2021-02-23 12:03:31 +00:00
] ,
} ,
2022-05-12 10:29:40 +00:00
" ParallelCachePrimingNumThreads " = > set! {
2022-01-15 02:47:47 +00:00
" type " : " number " ,
" minimum " : 0 ,
" maximum " : 255
} ,
2022-03-19 18:01:19 +00:00
" LifetimeElisionDef " = > set! {
2022-05-14 11:53:41 +00:00
" type " : " string " ,
" enum " : [
" always " ,
" never " ,
" skip_trivial "
2022-03-19 18:01:19 +00:00
] ,
2022-05-14 11:53:41 +00:00
" enumDescriptions " : [
" Always show lifetime elision hints. " ,
" Never show lifetime elision hints. " ,
" Only show lifetime elision hints if a return type is involved. "
]
2022-03-19 18:01:19 +00:00
} ,
2022-05-28 12:13:25 +00:00
" ClosureReturnTypeHintsDef " = > set! {
" type " : " string " ,
" enum " : [
" always " ,
" never " ,
" with_block "
] ,
" enumDescriptions " : [
" Always show type hints for return types of closures. " ,
" Never show type hints for return types of closures. " ,
" Only show type hints for return types of closures with blocks. "
]
} ,
2022-05-12 11:39:32 +00:00
" ReborrowHintsDef " = > set! {
2022-05-14 11:53:41 +00:00
" type " : " string " ,
" enum " : [
" always " ,
" never " ,
" mutable "
] ,
" enumDescriptions " : [
" Always show reborrow hints. " ,
" Never show reborrow hints. " ,
" Only show mutable reborrow hints. "
]
} ,
2022-11-04 21:59:07 +00:00
" AdjustmentHintsDef " = > set! {
" type " : " string " ,
" enum " : [
" always " ,
" never " ,
" reborrow "
] ,
" enumDescriptions " : [
" Always show all adjustment hints. " ,
" Never show adjustment hints. " ,
" Only show auto borrow and dereference adjustment hints. "
]
} ,
2022-12-23 10:28:46 +00:00
" DiscriminantHintsDef " = > set! {
" type " : " string " ,
" enum " : [
" always " ,
" never " ,
" fieldless "
] ,
" enumDescriptions " : [
" Always show all discriminant hints. " ,
" Never show discriminant hints. " ,
" Only show discriminant hints on fieldless enum variants. "
]
} ,
2022-12-21 15:00:05 +00:00
" AdjustmentHintsModeDef " = > set! {
" type " : " string " ,
" enum " : [
" prefix " ,
" postfix " ,
" prefer_prefix " ,
" prefer_postfix " ,
] ,
" enumDescriptions " : [
" Always show adjustment hints as prefix (`*expr`). " ,
" Always show adjustment hints as postfix (`expr.*`). " ,
2023-04-13 22:35:00 +00:00
" Show prefix or postfix depending on which uses less parenthesis, preferring prefix. " ,
" Show prefix or postfix depending on which uses less parenthesis, preferring postfix. " ,
2022-12-21 15:00:05 +00:00
]
} ,
2022-09-19 14:40:43 +00:00
" CargoFeaturesDef " = > set! {
2022-05-12 16:15:48 +00:00
" anyOf " : [
{
" type " : " string " ,
" enum " : [
2022-05-14 11:53:41 +00:00
" all "
2022-05-12 16:15:48 +00:00
] ,
" enumDescriptions " : [
2022-05-14 11:53:41 +00:00
" Pass `--all-features` to cargo " ,
2022-05-12 16:15:48 +00:00
]
} ,
2022-05-14 11:53:41 +00:00
{
" type " : " array " ,
" items " : { " type " : " string " }
}
2022-04-26 12:39:01 +00:00
] ,
} ,
2022-09-19 14:40:43 +00:00
" Option<CargoFeaturesDef> " = > set! {
2022-05-12 16:15:48 +00:00
" anyOf " : [
{
" type " : " string " ,
" enum " : [
" all "
] ,
" enumDescriptions " : [
" Pass `--all-features` to cargo " ,
]
} ,
{
" type " : " array " ,
" items " : { " type " : " string " }
} ,
{ " type " : " null " }
2022-04-26 12:39:01 +00:00
] ,
} ,
2022-05-14 11:53:41 +00:00
" CallableCompletionDef " = > set! {
" type " : " string " ,
" enum " : [
" fill_arguments " ,
" add_parentheses " ,
" none " ,
2022-04-26 12:39:01 +00:00
] ,
2022-05-14 11:53:41 +00:00
" enumDescriptions " : [
" Add call parentheses and pre-fill arguments. " ,
" Add call parentheses. " ,
" Do no snippet completions for callables. "
]
2022-04-26 12:39:01 +00:00
} ,
2022-04-27 15:51:44 +00:00
" SignatureDetail " = > set! {
" type " : " string " ,
" enum " : [ " full " , " parameters " ] ,
" enumDescriptions " : [
" Show the entire signature. " ,
" Show only the parameters. "
] ,
} ,
2022-07-18 15:50:56 +00:00
" FilesWatcherDef " = > set! {
" type " : " string " ,
" enum " : [ " client " , " server " ] ,
" enumDescriptions " : [
" Use the client (editor) to watch files for changes " ,
" Use server-side file watching " ,
] ,
} ,
2022-09-12 03:40:33 +00:00
" AnnotationLocation " = > set! {
" type " : " string " ,
" enum " : [ " above_name " , " above_whole_item " ] ,
" enumDescriptions " : [
" Render annotations above the name of the item. " ,
" Render annotations above the whole item, including documentation comments and attributes. "
] ,
} ,
2022-08-27 16:28:09 +00:00
" InvocationStrategy " = > set! {
" type " : " string " ,
2022-10-19 21:34:36 +00:00
" enum " : [ " per_workspace " , " once " ] ,
2022-08-27 16:28:09 +00:00
" enumDescriptions " : [
2022-10-22 21:02:59 +00:00
" The command will be executed for each workspace. " ,
" The command will be executed once. "
] ,
} ,
" InvocationLocation " = > set! {
" type " : " string " ,
" enum " : [ " workspace " , " root " ] ,
" enumDescriptions " : [
" The command will be executed in the corresponding workspace root. " ,
" The command will be executed in the project root. "
2022-08-27 16:28:09 +00:00
] ,
} ,
2022-11-21 21:40:32 +00:00
" Option<CheckOnSaveTargets> " = > set! {
2022-09-24 23:22:27 +00:00
" anyOf " : [
2022-11-21 21:40:32 +00:00
{
" type " : " null "
} ,
2022-09-24 23:22:27 +00:00
{
" type " : " string " ,
} ,
{
" type " : " array " ,
" items " : { " type " : " string " }
} ,
] ,
} ,
2023-04-06 12:44:38 +00:00
" ClosureStyle " = > set! {
" type " : " string " ,
" enum " : [ " impl_fn " , " rust_analyzer " , " with_id " , " hide " ] ,
" enumDescriptions " : [
" `impl_fn`: `impl FnMut(i32, u64) -> i8` " ,
" `rust_analyzer`: `|i32, u64| -> i8` " ,
" `with_id`: `{closure#14352}`, where that id is the unique number of the closure in r-a internals " ,
" `hide`: Shows `...` for every closure type " ,
] ,
} ,
2022-12-23 18:42:58 +00:00
_ = > panic! ( " missing entry for {ty} : {default} " ) ,
2020-12-02 14:31:24 +00:00
}
map . into ( )
}
2020-12-09 12:07:37 +00:00
#[ cfg(test) ]
fn manual ( fields : & [ ( & 'static str , & 'static str , & [ & str ] , & str ) ] ) -> String {
fields
. iter ( )
. map ( | ( field , _ty , doc , default ) | {
2022-03-12 12:22:12 +00:00
let name = format! ( " rust-analyzer. {} " , field . replace ( '_' , " . " ) ) ;
2022-12-23 07:51:52 +00:00
let doc = doc_comment_to_string ( doc ) ;
2022-01-11 04:47:54 +00:00
if default . contains ( '\n' ) {
format! (
2022-12-30 07:59:11 +00:00
r #" [[{name}]]{name}::
2022-01-11 04:47:54 +00:00
+
- -
Default :
- - - -
2022-12-30 07:59:11 +00:00
{ default }
2022-01-11 04:47:54 +00:00
- - - -
2022-12-30 07:59:11 +00:00
{ doc }
2022-01-11 04:47:54 +00:00
- -
2022-12-30 07:59:11 +00:00
" #
2022-01-11 04:47:54 +00:00
)
} else {
2022-12-23 18:42:58 +00:00
format! ( " [[ {name} ]] {name} (default: ` {default} `):: \n + \n -- \n {doc} -- \n " )
2022-01-11 04:47:54 +00:00
}
2020-12-09 12:07:37 +00:00
} )
. collect ::< String > ( )
}
2021-03-09 11:43:05 +00:00
fn doc_comment_to_string ( doc : & [ & str ] ) -> String {
2022-12-23 18:42:58 +00:00
doc . iter ( ) . map ( | it | it . strip_prefix ( ' ' ) . unwrap_or ( it ) ) . map ( | it | format! ( " {it} \n " ) ) . collect ( )
2021-03-09 11:43:05 +00:00
}
2020-12-09 12:07:37 +00:00
#[ cfg(test) ]
mod tests {
use std ::fs ;
2021-03-08 17:22:33 +00:00
use test_utils ::{ ensure_file_contents , project_root } ;
2020-12-09 12:07:37 +00:00
use super ::* ;
2020-12-02 14:31:24 +00:00
2020-12-09 12:07:37 +00:00
#[ test ]
2021-03-08 18:13:15 +00:00
fn generate_package_json_config ( ) {
2020-12-09 12:07:37 +00:00
let s = Config ::json_schema ( ) ;
2022-12-23 18:42:58 +00:00
let schema = format! ( " {s:#} " ) ;
2021-01-26 13:03:24 +00:00
let mut schema = schema
. trim_start_matches ( '{' )
. trim_end_matches ( '}' )
. replace ( " " , " " )
2022-03-12 12:22:12 +00:00
. replace ( '\n' , " \n " )
2021-01-26 13:03:24 +00:00
. trim_start_matches ( '\n' )
. trim_end ( )
. to_string ( ) ;
schema . push_str ( " , \n " ) ;
2021-10-29 02:00:10 +00:00
// Transform the asciidoc form link to markdown style.
2021-10-29 10:25:32 +00:00
//
// https://link[text] => [text](https://link)
2021-10-28 02:13:43 +00:00
let url_matches = schema . match_indices ( " https:// " ) ;
2021-10-29 02:00:10 +00:00
let mut url_offsets = url_matches . map ( | ( idx , _ ) | idx ) . collect ::< Vec < usize > > ( ) ;
url_offsets . reverse ( ) ;
for idx in url_offsets {
2021-10-28 02:13:43 +00:00
let link = & schema [ idx .. ] ;
// matching on whitespace to ignore normal links
if let Some ( link_end ) = link . find ( | c | c = = ' ' | | c = = '[' ) {
if link . chars ( ) . nth ( link_end ) = = Some ( '[' ) {
2021-10-29 10:25:32 +00:00
if let Some ( link_text_end ) = link . find ( ']' ) {
let link_text = link [ link_end .. ( link_text_end + 1 ) ] . to_string ( ) ;
schema . replace_range ( ( idx + link_end ) .. ( idx + link_text_end + 1 ) , " " ) ;
schema . insert ( idx , '(' ) ;
schema . insert ( idx + link_end + 1 , ')' ) ;
schema . insert_str ( idx , & link_text ) ;
}
2021-10-28 02:13:43 +00:00
}
}
}
2021-03-08 17:22:33 +00:00
let package_json_path = project_root ( ) . join ( " editors/code/package.json " ) ;
2021-01-26 13:03:24 +00:00
let mut package_json = fs ::read_to_string ( & package_json_path ) . unwrap ( ) ;
2021-06-15 06:32:53 +00:00
let start_marker = " \" $generated-start \" : {}, \n " ;
let end_marker = " \" $generated-end \" : {} \n " ;
2021-01-26 13:03:24 +00:00
let start = package_json . find ( start_marker ) . unwrap ( ) + start_marker . len ( ) ;
let end = package_json . find ( end_marker ) . unwrap ( ) ;
2021-03-08 14:20:36 +00:00
2021-01-26 13:03:24 +00:00
let p = remove_ws ( & package_json [ start .. end ] ) ;
2021-10-29 02:00:10 +00:00
let s = remove_ws ( & schema ) ;
2021-01-26 13:03:24 +00:00
if ! p . contains ( & s ) {
2021-10-29 02:00:10 +00:00
package_json . replace_range ( start .. end , & schema ) ;
2021-03-08 14:20:36 +00:00
ensure_file_contents ( & package_json_path , & package_json )
2021-01-26 13:03:24 +00:00
}
2020-12-09 12:07:37 +00:00
}
2020-12-02 14:31:24 +00:00
2020-12-09 12:07:37 +00:00
#[ test ]
2021-03-08 18:13:15 +00:00
fn generate_config_documentation ( ) {
2021-03-08 17:22:33 +00:00
let docs_path = project_root ( ) . join ( " docs/user/generated_config.adoc " ) ;
2020-12-09 12:07:37 +00:00
let expected = ConfigData ::manual ( ) ;
2021-03-09 11:43:05 +00:00
ensure_file_contents ( & docs_path , & expected ) ;
2020-12-09 12:07:37 +00:00
}
fn remove_ws ( text : & str ) -> String {
text . replace ( char ::is_whitespace , " " )
}
2023-04-28 06:30:41 +00:00
#[ test ]
fn proc_macro_srv_null ( ) {
let mut config =
Config ::new ( AbsPathBuf ::try_from ( project_root ( ) ) . unwrap ( ) , Default ::default ( ) , vec! [ ] ) ;
config
. update ( serde_json ::json! ( {
" procMacro_server " : null ,
} ) )
. unwrap ( ) ;
assert_eq! ( config . proc_macro_srv ( ) , None ) ;
}
#[ test ]
fn proc_macro_srv_abs ( ) {
let mut config =
Config ::new ( AbsPathBuf ::try_from ( project_root ( ) ) . unwrap ( ) , Default ::default ( ) , vec! [ ] ) ;
config
. update ( serde_json ::json! ( {
" procMacro " : { " server " : project_root ( ) . display ( ) . to_string ( ) }
} ) )
. unwrap ( ) ;
assert_eq! ( config . proc_macro_srv ( ) , Some ( AbsPathBuf ::try_from ( project_root ( ) ) . unwrap ( ) ) ) ;
}
#[ test ]
fn proc_macro_srv_rel ( ) {
let mut config =
Config ::new ( AbsPathBuf ::try_from ( project_root ( ) ) . unwrap ( ) , Default ::default ( ) , vec! [ ] ) ;
config
. update ( serde_json ::json! ( {
" procMacro " : { " server " : " ./server " }
} ) )
. unwrap ( ) ;
assert_eq! (
config . proc_macro_srv ( ) ,
Some ( AbsPathBuf ::try_from ( project_root ( ) . join ( " ./server " ) ) . unwrap ( ) )
) ;
}
2020-07-09 22:28:12 +00:00
}