2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2021-05-20 05:56:48 -04:00
|
|
|
use crate::tokio_util::create_basic_runtime;
|
|
|
|
|
|
|
|
use deno_core::error::anyhow;
|
|
|
|
use deno_core::error::AnyError;
|
2020-12-07 05:46:39 -05:00
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::Value;
|
2020-12-09 14:50:47 -05:00
|
|
|
use deno_core::url::Url;
|
2021-05-09 21:16:04 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2021-05-20 05:56:48 -04:00
|
|
|
use log::error;
|
2021-06-21 17:18:32 -04:00
|
|
|
use lsp::WorkspaceFolder;
|
2021-01-29 14:34:33 -05:00
|
|
|
use lspower::lsp;
|
2021-05-20 05:56:48 -04:00
|
|
|
use std::collections::BTreeMap;
|
2021-04-08 21:27:27 -04:00
|
|
|
use std::collections::HashMap;
|
2021-05-20 05:56:48 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::RwLock;
|
|
|
|
use std::thread;
|
|
|
|
use tokio::sync::mpsc;
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2021-05-09 21:16:04 -04:00
|
|
|
pub const SETTINGS_SECTION: &str = "deno";
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct ClientCapabilities {
|
|
|
|
pub status_notification: bool,
|
2021-01-04 16:52:20 -05:00
|
|
|
pub workspace_configuration: bool,
|
|
|
|
pub workspace_did_change_watched_files: bool,
|
2021-04-02 02:21:07 -04:00
|
|
|
pub line_folding_only: bool,
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-06-07 07:38:07 -04:00
|
|
|
fn is_true() -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:53:08 -04:00
|
|
|
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
2021-01-31 22:30:41 -05:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CodeLensSettings {
|
2021-02-08 05:45:10 -05:00
|
|
|
/// Flag for providing implementation code lenses.
|
|
|
|
#[serde(default)]
|
|
|
|
pub implementations: bool,
|
|
|
|
/// Flag for providing reference code lenses.
|
2021-01-31 22:30:41 -05:00
|
|
|
#[serde(default)]
|
|
|
|
pub references: bool,
|
|
|
|
/// Flag for providing reference code lens on all functions. For this to have
|
|
|
|
/// an impact, the `references` flag needs to be `true`.
|
|
|
|
#[serde(default)]
|
|
|
|
pub references_all_functions: bool,
|
2021-06-07 07:38:07 -04:00
|
|
|
/// Flag for providing test code lens on `Deno.test` statements. There is
|
|
|
|
/// also the `test_args` setting, but this is not used by the server.
|
|
|
|
#[serde(default = "is_true")]
|
|
|
|
pub test: bool,
|
2021-01-31 22:30:41 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
impl Default for CodeLensSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
implementations: false,
|
|
|
|
references: false,
|
|
|
|
references_all_functions: false,
|
2021-06-07 07:38:07 -04:00
|
|
|
test: true,
|
2021-03-15 18:01:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 07:38:07 -04:00
|
|
|
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CodeLensSpecifierSettings {
|
|
|
|
/// Flag for providing test code lens on `Deno.test` statements. There is
|
|
|
|
/// also the `test_args` setting, but this is not used by the server.
|
|
|
|
#[serde(default = "is_true")]
|
|
|
|
pub test: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CodeLensSpecifierSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self { test: true }
|
|
|
|
}
|
2021-06-01 07:53:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
2021-03-15 18:01:41 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CompletionSettings {
|
|
|
|
#[serde(default)]
|
|
|
|
pub complete_function_calls: bool,
|
2021-06-01 07:53:08 -04:00
|
|
|
#[serde(default = "is_true")]
|
2021-03-15 18:01:41 -04:00
|
|
|
pub names: bool,
|
2021-06-01 07:53:08 -04:00
|
|
|
#[serde(default = "is_true")]
|
2021-03-15 18:01:41 -04:00
|
|
|
pub paths: bool,
|
2021-06-01 07:53:08 -04:00
|
|
|
#[serde(default = "is_true")]
|
2021-03-15 18:01:41 -04:00
|
|
|
pub auto_imports: bool,
|
2021-04-08 21:27:27 -04:00
|
|
|
#[serde(default)]
|
|
|
|
pub imports: ImportCompletionSettings,
|
2021-03-15 18:01:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CompletionSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
complete_function_calls: false,
|
|
|
|
names: true,
|
|
|
|
paths: true,
|
|
|
|
auto_imports: true,
|
2021-04-08 21:27:27 -04:00
|
|
|
imports: ImportCompletionSettings::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:53:08 -04:00
|
|
|
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
2021-04-08 21:27:27 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ImportCompletionSettings {
|
2021-06-01 07:53:08 -04:00
|
|
|
/// A flag that indicates if non-explicitly set origins should be checked for
|
|
|
|
/// supporting import suggestions.
|
|
|
|
#[serde(default = "is_true")]
|
|
|
|
pub auto_discover: bool,
|
|
|
|
/// A map of origins which have had explicitly set if import suggestions are
|
|
|
|
/// enabled.
|
2021-04-08 21:27:27 -04:00
|
|
|
#[serde(default)]
|
|
|
|
pub hosts: HashMap<String, bool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ImportCompletionSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2021-06-01 07:53:08 -04:00
|
|
|
auto_discover: true,
|
2021-04-08 21:27:27 -04:00
|
|
|
hosts: HashMap::default(),
|
2021-03-15 18:01:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 21:16:04 -04:00
|
|
|
/// Deno language server specific settings that can be applied uniquely to a
|
|
|
|
/// specifier.
|
|
|
|
#[derive(Debug, Default, Clone, Deserialize)]
|
2021-06-07 07:38:07 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-05-09 21:16:04 -04:00
|
|
|
pub struct SpecifierSettings {
|
|
|
|
/// A flag that indicates if Deno is enabled for this specifier or not.
|
|
|
|
pub enable: bool,
|
2021-06-07 07:38:07 -04:00
|
|
|
/// Code lens specific settings for the resource.
|
|
|
|
#[serde(default)]
|
|
|
|
pub code_lens: CodeLensSpecifierSettings,
|
2021-05-09 21:16:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Deno language server specific settings that are applied to a workspace.
|
2021-06-01 07:53:08 -04:00
|
|
|
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
|
2020-12-07 05:46:39 -05:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct WorkspaceSettings {
|
2021-05-09 21:16:04 -04:00
|
|
|
/// A flag that indicates if Deno is enabled for the workspace.
|
2021-06-01 07:53:08 -04:00
|
|
|
#[serde(default)]
|
2020-12-07 05:46:39 -05:00
|
|
|
pub enable: bool,
|
2021-05-09 21:16:04 -04:00
|
|
|
|
2021-05-10 12:16:39 -04:00
|
|
|
/// An option that points to a path string of the config file to apply to
|
2021-05-09 21:16:04 -04:00
|
|
|
/// code within the workspace.
|
2020-12-07 05:46:39 -05:00
|
|
|
pub config: Option<String>,
|
2021-05-09 21:16:04 -04:00
|
|
|
|
|
|
|
/// An option that points to a path string of the import map to apply to the
|
|
|
|
/// code within the workspace.
|
2020-12-07 05:46:39 -05:00
|
|
|
pub import_map: Option<String>,
|
2021-05-09 21:16:04 -04:00
|
|
|
|
|
|
|
/// Code lens specific settings for the workspace.
|
2021-03-15 18:01:41 -04:00
|
|
|
#[serde(default)]
|
|
|
|
pub code_lens: CodeLensSettings,
|
2021-05-09 21:16:04 -04:00
|
|
|
|
2021-05-11 00:54:10 -04:00
|
|
|
/// A flag that indicates if internal debug logging should be made available.
|
|
|
|
#[serde(default)]
|
|
|
|
pub internal_debug: bool,
|
2021-01-04 16:52:20 -05:00
|
|
|
|
2021-05-09 21:16:04 -04:00
|
|
|
/// A flag that indicates if linting is enabled for the workspace.
|
2021-01-04 16:52:20 -05:00
|
|
|
#[serde(default)]
|
2020-12-07 05:46:39 -05:00
|
|
|
pub lint: bool,
|
2021-05-09 21:16:04 -04:00
|
|
|
|
|
|
|
/// A flag that indicates if Dene should validate code against the unstable
|
|
|
|
/// APIs for the workspace.
|
2021-05-11 00:54:10 -04:00
|
|
|
#[serde(default)]
|
|
|
|
pub suggest: CompletionSettings,
|
|
|
|
|
2021-01-04 16:52:20 -05:00
|
|
|
#[serde(default)]
|
2020-12-07 05:46:39 -05:00
|
|
|
pub unstable: bool,
|
|
|
|
}
|
|
|
|
|
2021-01-31 22:30:41 -05:00
|
|
|
impl WorkspaceSettings {
|
|
|
|
/// Determine if any code lenses are enabled at all. This allows short
|
|
|
|
/// circuiting when there are no code lenses enabled.
|
|
|
|
pub fn enabled_code_lens(&self) -> bool {
|
2021-03-15 18:01:41 -04:00
|
|
|
self.code_lens.implementations || self.code_lens.references
|
2021-01-31 22:30:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 05:56:48 -04:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct ConfigSnapshot {
|
|
|
|
pub client_capabilities: ClientCapabilities,
|
|
|
|
pub root_uri: Option<Url>,
|
|
|
|
pub settings: Settings,
|
2021-06-21 17:18:32 -04:00
|
|
|
pub workspace_folders: Option<Vec<lsp::WorkspaceFolder>>,
|
2021-05-20 05:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfigSnapshot {
|
|
|
|
pub fn specifier_enabled(&self, specifier: &ModuleSpecifier) -> bool {
|
|
|
|
if let Some(settings) = self.settings.specifiers.get(specifier) {
|
|
|
|
settings.1.enable
|
|
|
|
} else {
|
|
|
|
self.settings.workspace.enable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ConfigRequest {
|
2021-06-01 05:24:36 -04:00
|
|
|
All,
|
2021-05-20 05:56:48 -04:00
|
|
|
Specifier(ModuleSpecifier, ModuleSpecifier),
|
|
|
|
}
|
|
|
|
|
2021-03-09 21:41:35 -05:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2021-05-20 05:56:48 -04:00
|
|
|
pub struct Settings {
|
|
|
|
pub specifiers:
|
|
|
|
BTreeMap<ModuleSpecifier, (ModuleSpecifier, SpecifierSettings)>,
|
|
|
|
pub workspace: WorkspaceSettings,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2020-12-07 05:46:39 -05:00
|
|
|
pub struct Config {
|
|
|
|
pub client_capabilities: ClientCapabilities,
|
2020-12-09 14:50:47 -05:00
|
|
|
pub root_uri: Option<Url>,
|
2021-05-20 05:56:48 -04:00
|
|
|
settings: Arc<RwLock<Settings>>,
|
|
|
|
tx: mpsc::Sender<ConfigRequest>,
|
2021-06-21 17:18:32 -04:00
|
|
|
pub workspace_folders: Option<Vec<WorkspaceFolder>>,
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2021-05-20 05:56:48 -04:00
|
|
|
pub fn new(client: lspower::Client) -> Self {
|
|
|
|
let (tx, mut rx) = mpsc::channel::<ConfigRequest>(100);
|
|
|
|
let settings = Arc::new(RwLock::new(Settings::default()));
|
|
|
|
let settings_ref = settings.clone();
|
|
|
|
|
|
|
|
let _join_handle = thread::spawn(move || {
|
|
|
|
let runtime = create_basic_runtime();
|
|
|
|
|
|
|
|
runtime.block_on(async {
|
|
|
|
loop {
|
|
|
|
match rx.recv().await {
|
|
|
|
None => break,
|
2021-06-01 05:24:36 -04:00
|
|
|
Some(ConfigRequest::All) => {
|
|
|
|
let (specifier_uri_map, items): (
|
2021-05-20 05:56:48 -04:00
|
|
|
Vec<(ModuleSpecifier, ModuleSpecifier)>,
|
|
|
|
Vec<lsp::ConfigurationItem>,
|
|
|
|
) = {
|
|
|
|
let settings = settings_ref.read().unwrap();
|
|
|
|
(
|
|
|
|
settings
|
|
|
|
.specifiers
|
|
|
|
.iter()
|
|
|
|
.map(|(s, (u, _))| (s.clone(), u.clone()))
|
|
|
|
.collect(),
|
|
|
|
settings
|
|
|
|
.specifiers
|
|
|
|
.iter()
|
|
|
|
.map(|(_, (uri, _))| lsp::ConfigurationItem {
|
|
|
|
scope_uri: Some(uri.clone()),
|
|
|
|
section: Some(SETTINGS_SECTION.to_string()),
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if let Ok(configs) = client.configuration(items).await {
|
|
|
|
let mut settings = settings_ref.write().unwrap();
|
|
|
|
for (i, value) in configs.into_iter().enumerate() {
|
2021-06-01 05:24:36 -04:00
|
|
|
match serde_json::from_value::<SpecifierSettings>(value) {
|
|
|
|
Ok(specifier_settings) => {
|
|
|
|
let (specifier, uri) = specifier_uri_map[i].clone();
|
|
|
|
settings
|
|
|
|
.specifiers
|
|
|
|
.insert(specifier, (uri, specifier_settings));
|
2021-05-20 05:56:48 -04:00
|
|
|
}
|
2021-06-01 05:24:36 -04:00
|
|
|
Err(err) => {
|
|
|
|
error!("Error converting specifier settings: {}", err);
|
2021-05-20 05:56:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ConfigRequest::Specifier(specifier, uri)) => {
|
|
|
|
if settings_ref
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.specifiers
|
|
|
|
.contains_key(&specifier)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let Ok(value) = client
|
|
|
|
.configuration(vec![lsp::ConfigurationItem {
|
|
|
|
scope_uri: Some(uri.clone()),
|
|
|
|
section: Some(SETTINGS_SECTION.to_string()),
|
|
|
|
}])
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
match serde_json::from_value::<SpecifierSettings>(
|
|
|
|
value[0].clone(),
|
|
|
|
) {
|
|
|
|
Ok(specifier_settings) => {
|
|
|
|
settings_ref
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.specifiers
|
|
|
|
.insert(specifier, (uri, specifier_settings));
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
error!("Error converting specifier settings: {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error!(
|
|
|
|
"Error retrieving settings for specifier: {}",
|
|
|
|
specifier
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
Self {
|
|
|
|
client_capabilities: ClientCapabilities::default(),
|
|
|
|
root_uri: None,
|
|
|
|
settings,
|
|
|
|
tx,
|
2021-06-21 17:18:32 -04:00
|
|
|
workspace_folders: None,
|
2021-05-20 05:56:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_workspace_settings(&self) -> WorkspaceSettings {
|
|
|
|
self.settings.read().unwrap().workspace.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the workspace settings directly, which occurs during initialization
|
|
|
|
/// and when the client does not support workspace configuration requests
|
|
|
|
pub fn set_workspace_settings(&self, value: Value) -> Result<(), AnyError> {
|
|
|
|
let workspace_settings = serde_json::from_value(value)?;
|
|
|
|
self.settings.write().unwrap().workspace = workspace_settings;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn snapshot(&self) -> Result<ConfigSnapshot, AnyError> {
|
|
|
|
Ok(ConfigSnapshot {
|
|
|
|
client_capabilities: self.client_capabilities.clone(),
|
|
|
|
root_uri: self.root_uri.clone(),
|
|
|
|
settings: self
|
|
|
|
.settings
|
|
|
|
.try_read()
|
|
|
|
.map_err(|_| anyhow!("Error reading settings."))?
|
|
|
|
.clone(),
|
2021-06-21 17:18:32 -04:00
|
|
|
workspace_folders: self.workspace_folders.clone(),
|
2021-05-20 05:56:48 -04:00
|
|
|
})
|
2021-05-09 21:16:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn specifier_enabled(&self, specifier: &ModuleSpecifier) -> bool {
|
2021-05-20 05:56:48 -04:00
|
|
|
let settings = self.settings.read().unwrap();
|
2021-06-07 07:38:07 -04:00
|
|
|
settings
|
|
|
|
.specifiers
|
|
|
|
.get(specifier)
|
|
|
|
.map(|(_, s)| s.enable)
|
|
|
|
.unwrap_or_else(|| settings.workspace.enable)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn specifier_code_lens_test(&self, specifier: &ModuleSpecifier) -> bool {
|
|
|
|
let settings = self.settings.read().unwrap();
|
|
|
|
let value = settings
|
|
|
|
.specifiers
|
|
|
|
.get(specifier)
|
|
|
|
.map(|(_, s)| s.code_lens.test)
|
|
|
|
.unwrap_or_else(|| settings.workspace.code_lens.test);
|
|
|
|
value
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_capabilities(
|
|
|
|
&mut self,
|
2021-01-29 14:34:33 -05:00
|
|
|
capabilities: &lsp::ClientCapabilities,
|
2020-12-07 05:46:39 -05:00
|
|
|
) {
|
|
|
|
if let Some(experimental) = &capabilities.experimental {
|
2021-06-15 13:22:28 -04:00
|
|
|
self.client_capabilities.status_notification = experimental
|
|
|
|
.get("statusNotification")
|
|
|
|
.and_then(|it| it.as_bool())
|
|
|
|
== Some(true)
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
2021-01-04 16:52:20 -05:00
|
|
|
|
|
|
|
if let Some(workspace) = &capabilities.workspace {
|
|
|
|
self.client_capabilities.workspace_configuration =
|
|
|
|
workspace.configuration.unwrap_or(false);
|
|
|
|
self.client_capabilities.workspace_did_change_watched_files = workspace
|
|
|
|
.did_change_watched_files
|
|
|
|
.and_then(|it| it.dynamic_registration)
|
|
|
|
.unwrap_or(false);
|
|
|
|
}
|
2021-04-02 02:21:07 -04:00
|
|
|
|
|
|
|
if let Some(text_document) = &capabilities.text_document {
|
|
|
|
self.client_capabilities.line_folding_only = text_document
|
|
|
|
.folding_range
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|it| it.line_folding_only)
|
|
|
|
.unwrap_or(false);
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
2021-05-09 21:16:04 -04:00
|
|
|
|
2021-06-01 05:24:36 -04:00
|
|
|
/// Update all currently cached specifier settings
|
|
|
|
pub async fn update_all_settings(&self) -> Result<(), AnyError> {
|
2021-05-20 05:56:48 -04:00
|
|
|
self
|
|
|
|
.tx
|
2021-06-01 05:24:36 -04:00
|
|
|
.send(ConfigRequest::All)
|
2021-05-20 05:56:48 -04:00
|
|
|
.await
|
|
|
|
.map_err(|_| anyhow!("Error sending config update task."))
|
2021-05-09 21:16:04 -04:00
|
|
|
}
|
|
|
|
|
2021-06-01 05:24:36 -04:00
|
|
|
/// Update a specific specifiers settings from the client.
|
|
|
|
pub async fn update_specifier_settings(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
uri: &ModuleSpecifier,
|
|
|
|
) -> Result<(), AnyError> {
|
2021-05-20 05:56:48 -04:00
|
|
|
self
|
|
|
|
.tx
|
2021-06-01 05:24:36 -04:00
|
|
|
.send(ConfigRequest::Specifier(specifier.clone(), uri.clone()))
|
2021-05-20 05:56:48 -04:00
|
|
|
.await
|
|
|
|
.map_err(|_| anyhow!("Error sending config update task."))
|
2021-05-09 21:16:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use deno_core::resolve_url;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
|
2021-05-20 05:56:48 -04:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
struct MockLanguageServer;
|
|
|
|
|
|
|
|
#[lspower::async_trait]
|
|
|
|
impl lspower::LanguageServer for MockLanguageServer {
|
|
|
|
async fn initialize(
|
|
|
|
&self,
|
|
|
|
_params: lspower::lsp::InitializeParams,
|
|
|
|
) -> lspower::jsonrpc::Result<lsp::InitializeResult> {
|
|
|
|
Ok(lspower::lsp::InitializeResult {
|
|
|
|
capabilities: lspower::lsp::ServerCapabilities::default(),
|
|
|
|
server_info: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn shutdown(&self) -> lspower::jsonrpc::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup() -> Config {
|
|
|
|
let mut maybe_client: Option<lspower::Client> = None;
|
|
|
|
let (_service, _) = lspower::LspService::new(|client| {
|
|
|
|
maybe_client = Some(client);
|
|
|
|
MockLanguageServer::default()
|
|
|
|
});
|
|
|
|
Config::new(maybe_client.unwrap())
|
2021-05-09 21:16:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_config_specifier_enabled() {
|
2021-05-20 05:56:48 -04:00
|
|
|
let config = setup();
|
2021-05-09 21:16:04 -04:00
|
|
|
let specifier = resolve_url("file:///a.ts").unwrap();
|
|
|
|
assert!(!config.specifier_enabled(&specifier));
|
|
|
|
config
|
2021-05-20 05:56:48 -04:00
|
|
|
.set_workspace_settings(json!({
|
2021-05-09 21:16:04 -04:00
|
|
|
"enable": true
|
|
|
|
}))
|
|
|
|
.expect("could not update");
|
|
|
|
assert!(config.specifier_enabled(&specifier));
|
|
|
|
}
|
2021-06-01 07:53:08 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_set_workspace_settings_defaults() {
|
|
|
|
let config = setup();
|
|
|
|
config
|
|
|
|
.set_workspace_settings(json!({}))
|
|
|
|
.expect("could not update");
|
|
|
|
assert_eq!(
|
|
|
|
config.get_workspace_settings(),
|
|
|
|
WorkspaceSettings {
|
|
|
|
enable: false,
|
|
|
|
config: None,
|
|
|
|
import_map: None,
|
|
|
|
code_lens: CodeLensSettings {
|
|
|
|
implementations: false,
|
|
|
|
references: false,
|
|
|
|
references_all_functions: false,
|
2021-06-07 07:38:07 -04:00
|
|
|
test: true,
|
2021-06-01 07:53:08 -04:00
|
|
|
},
|
|
|
|
internal_debug: false,
|
|
|
|
lint: false,
|
|
|
|
suggest: CompletionSettings {
|
|
|
|
complete_function_calls: false,
|
|
|
|
names: true,
|
|
|
|
paths: true,
|
|
|
|
auto_imports: true,
|
|
|
|
imports: ImportCompletionSettings {
|
|
|
|
auto_discover: true,
|
|
|
|
hosts: HashMap::new(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unstable: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|