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
|
|
|
|
|
|
|
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;
|
2020-12-21 08:44:26 -05:00
|
|
|
use lspower::jsonrpc::Error as LSPError;
|
|
|
|
use lspower::jsonrpc::Result as LSPResult;
|
2021-01-29 14:34:33 -05:00
|
|
|
use lspower::lsp;
|
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,
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-01-31 22:30:41 -05:00
|
|
|
#[derive(Debug, Default, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CodeLensSettings {
|
|
|
|
/// Flag for providing reference code lens.
|
|
|
|
#[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,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Deserialize)]
|
2020-12-07 05:46:39 -05:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct WorkspaceSettings {
|
|
|
|
pub enable: bool,
|
|
|
|
pub config: Option<String>,
|
|
|
|
pub import_map: Option<String>,
|
2021-01-31 22:30:41 -05:00
|
|
|
pub code_lens: Option<CodeLensSettings>,
|
2021-01-04 16:52:20 -05:00
|
|
|
|
|
|
|
#[serde(default)]
|
2020-12-07 05:46:39 -05:00
|
|
|
pub lint: bool,
|
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 {
|
|
|
|
if let Some(code_lens) = &self.code_lens {
|
|
|
|
// This should contain all the "top level" code lens references
|
|
|
|
code_lens.references
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enabled_code_lens_references(&self) -> bool {
|
|
|
|
if let Some(code_lens) = &self.code_lens {
|
|
|
|
code_lens.references
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enabled_code_lens_references_all_functions(&self) -> bool {
|
|
|
|
if let Some(code_lens) = &self.code_lens {
|
|
|
|
code_lens.references_all_functions
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
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>,
|
2020-12-07 05:46:39 -05:00
|
|
|
pub settings: WorkspaceSettings,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2020-12-21 08:44:26 -05:00
|
|
|
pub fn update(&mut self, value: Value) -> LSPResult<()> {
|
|
|
|
let settings: WorkspaceSettings = serde_json::from_value(value)
|
|
|
|
.map_err(|err| LSPError::invalid_params(err.to_string()))?;
|
2020-12-07 05:46:39 -05:00
|
|
|
self.settings = settings;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::redundant_closure_call)]
|
|
|
|
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 {
|
|
|
|
let get_bool =
|
|
|
|
|k: &str| experimental.get(k).and_then(|it| it.as_bool()) == Some(true);
|
|
|
|
|
|
|
|
self.client_capabilities.status_notification =
|
|
|
|
get_bool("statusNotification");
|
|
|
|
}
|
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);
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|