0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

Reland "feat(lsp): enable via config file detection (#20334)" (#20349)

This commit is contained in:
Nayeem Rahman 2023-09-01 21:13:13 +01:00 committed by GitHub
parent 2b191c6e9d
commit e1fb48524d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 53 additions and 24 deletions

6
Cargo.lock generated
View file

@ -1041,13 +1041,13 @@ dependencies = [
[[package]]
name = "deno_config"
version = "0.2.1"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a42ff2a01961c72ef883375399677ccf046dba607dc14169b313e4a95bd26da2"
checksum = "cc0c89c37b55d33bdd3374fddb9e6bf74dd7d071d1217d1ef3ab7d09bcefb6d3"
dependencies = [
"anyhow",
"deno_semver",
"indexmap 1.9.3",
"indexmap 2.0.0",
"jsonc-parser",
"log",
"percent-encoding",

View file

@ -47,7 +47,7 @@ winres.workspace = true
[dependencies]
deno_ast = { workspace = true, features = ["bundler", "cjs", "codegen", "dep_graph", "module_specifier", "proposal", "react", "sourcemap", "transforms", "typescript", "view", "visit"] }
deno_cache_dir = "=0.5.2"
deno_config = "=0.2.1"
deno_config = "=0.2.4"
deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] }
deno_doc = "=0.65.0"
deno_emit = "=0.26.0"

View file

@ -15,7 +15,7 @@ use deno_npm::resolution::ValidSerializedNpmResolutionSnapshot;
use deno_npm::NpmSystemInfo;
use deno_runtime::deno_tls::RootCertStoreProvider;
use deno_semver::npm::NpmPackageReqReference;
use indexmap1::IndexMap;
use indexmap::IndexMap;
pub use deno_config::BenchConfig;
pub use deno_config::CompilerOptions;

View file

@ -359,7 +359,7 @@ impl ClientTrait for ReplClient {
.into_iter()
.map(|_| {
Ok(SpecifierSettings {
enable: true,
enable: Some(true),
..Default::default()
})
})

View file

@ -236,7 +236,7 @@ impl Default for ImportCompletionSettings {
#[serde(rename_all = "camelCase")]
pub struct SpecifierSettings {
/// A flag that indicates if Deno is enabled for this specifier or not.
pub enable: bool,
pub enable: Option<bool>,
/// A list of paths, using the workspace folder as a base that should be Deno
/// enabled.
#[serde(default)]
@ -288,8 +288,7 @@ fn empty_string_none<'de, D: serde::Deserializer<'de>>(
#[serde(rename_all = "camelCase")]
pub struct WorkspaceSettings {
/// A flag that indicates if Deno is enabled for the workspace.
#[serde(default)]
pub enable: bool,
pub enable: Option<bool>,
/// A list of paths, using the root_uri as a base that should be Deno enabled.
#[serde(default)]
@ -359,7 +358,7 @@ pub struct WorkspaceSettings {
impl Default for WorkspaceSettings {
fn default() -> Self {
WorkspaceSettings {
enable: false,
enable: None,
enable_paths: vec![],
cache: None,
certificate_stores: None,
@ -405,6 +404,7 @@ pub struct ConfigSnapshot {
pub client_capabilities: ClientCapabilities,
pub enabled_paths: HashMap<Url, Vec<Url>>,
pub excluded_paths: Option<Vec<Url>>,
pub has_config_file: bool,
pub settings: Settings,
}
@ -415,6 +415,7 @@ impl ConfigSnapshot {
&self.enabled_paths,
self.excluded_paths.as_ref(),
&self.settings,
self.has_config_file,
specifier,
)
}
@ -523,6 +524,10 @@ impl Config {
self.maybe_config_file_info = None;
}
pub fn has_config_file(&self) -> bool {
self.maybe_config_file_info.is_some()
}
pub fn set_config_file(&mut self, config_file: ConfigFile) {
self.maybe_config_file_info = Some(LspConfigFileInfo {
maybe_lockfile: resolve_lockfile_from_config(&config_file).map(
@ -582,6 +587,7 @@ impl Config {
.maybe_config_file_info
.as_ref()
.map(|i| i.excluded_paths.clone()),
has_config_file: self.has_config_file(),
settings: self.settings.clone(),
})
}
@ -590,6 +596,14 @@ impl Config {
self.settings.specifiers.contains_key(specifier)
}
pub fn enabled(&self) -> bool {
self
.settings
.workspace
.enable
.unwrap_or_else(|| self.has_config_file())
}
pub fn specifier_enabled(&self, specifier: &ModuleSpecifier) -> bool {
specifier_enabled(
&self.enabled_paths,
@ -598,6 +612,7 @@ impl Config {
.as_ref()
.map(|i| &i.excluded_paths),
&self.settings,
self.has_config_file(),
specifier,
)
}
@ -610,7 +625,7 @@ impl Config {
pub fn enabled_urls(&self) -> Vec<Url> {
let mut urls: Vec<Url> = Vec::new();
if !self.settings.workspace.enable && self.enabled_paths.is_empty() {
if !self.enabled() && self.enabled_paths.is_empty() {
// do not return any urls when disabled
return urls;
}
@ -780,6 +795,7 @@ fn specifier_enabled(
enabled_paths: &HashMap<Url, Vec<Url>>,
excluded_paths: Option<&Vec<Url>>,
settings: &Settings,
workspace_has_config_file: bool,
specifier: &Url,
) -> bool {
let specifier_str = specifier.as_str();
@ -800,8 +816,9 @@ fn specifier_enabled(
settings
.specifiers
.get(specifier)
.map(|settings| settings.enable)
.unwrap_or_else(|| settings.workspace.enable)
.and_then(|settings| settings.enable)
.or(settings.workspace.enable)
.unwrap_or(workspace_has_config_file)
}
fn resolve_lockfile_from_config(config_file: &ConfigFile) -> Option<Lockfile> {
@ -916,7 +933,7 @@ mod tests {
assert_eq!(
config.workspace_settings().clone(),
WorkspaceSettings {
enable: false,
enable: None,
enable_paths: Vec::new(),
cache: None,
certificate_stores: None,
@ -1025,14 +1042,14 @@ mod tests {
let mut config = Config::new();
let root_dir = Url::parse("file:///example/").unwrap();
config.root_uri = Some(root_dir.clone());
config.settings.workspace.enable = false;
config.settings.workspace.enable = Some(false);
config.settings.workspace.enable_paths = Vec::new();
assert_eq!(config.enabled_urls(), vec![]);
config.settings.workspace.enable = true;
config.settings.workspace.enable = Some(true);
assert_eq!(config.enabled_urls(), vec![root_dir]);
config.settings.workspace.enable = false;
config.settings.workspace.enable = Some(false);
let root_dir1 = Url::parse("file:///root1/").unwrap();
let root_dir2 = Url::parse("file:///root2/").unwrap();
let root_dir3 = Url::parse("file:///root3/").unwrap();
@ -1060,4 +1077,18 @@ mod tests {
]
);
}
#[test]
fn config_enable_via_config_file_detection() {
let mut config = Config::new();
let root_uri = Url::parse("file:///root/").unwrap();
config.root_uri = Some(root_uri.clone());
config.settings.workspace.enable = None;
assert_eq!(config.enabled_urls(), vec![]);
config.set_config_file(
ConfigFile::new("{}", root_uri.join("deno.json").unwrap()).unwrap(),
);
assert_eq!(config.enabled_urls(), vec![root_uri]);
}
}

View file

@ -1390,7 +1390,7 @@ mod tests {
ConfigSnapshot {
settings: Settings {
workspace: WorkspaceSettings {
enable: true,
enable: Some(true),
lint: true,
..Default::default()
},
@ -1466,7 +1466,7 @@ let c: number = "a";
disabled_config.settings.specifiers.insert(
specifier.clone(),
SpecifierSettings {
enable: false,
enable: Some(false),
enable_paths: Vec::new(),
code_lens: Default::default(),
},

View file

@ -3053,7 +3053,7 @@ impl tower_lsp::LanguageServer for LanguageServer {
let options = DidChangeWatchedFilesRegistrationOptions {
watchers: vec![FileSystemWatcher {
glob_pattern: "**/*.{json,jsonc,lock}".to_string(),
kind: Some(WatchKind::Change),
kind: None,
}],
};
registrations.push(Registration {

View file

@ -284,7 +284,7 @@ fn get_cwd_uri() -> Result<ModuleSpecifier, AnyError> {
pub fn get_repl_workspace_settings() -> WorkspaceSettings {
WorkspaceSettings {
enable: true,
enable: Some(true),
enable_paths: Vec::new(),
config: None,
certificate_stores: None,

View file

@ -17,9 +17,7 @@ use deno_semver::package::PackageNv;
use deno_task_shell::ExecuteResult;
use deno_task_shell::ShellCommand;
use deno_task_shell::ShellCommandContext;
// TODO(mmastrac): Once upstream indexmap is updated, this can go away
use indexmap::IndexMap;
use indexmap1::IndexMap as IndexMap1;
use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;
@ -180,7 +178,7 @@ fn collect_env_vars() -> HashMap<String, String> {
fn print_available_tasks(
// order can be important, so these use an index map
tasks_config: &IndexMap1<String, String>,
tasks_config: &IndexMap<String, String>,
package_json_scripts: &IndexMap<String, String>,
) {
eprintln!("{}", colors::green("Available tasks:"));