1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-18 13:22:55 -05:00

fix(lsp): respect "typescript.suggestionActions.enabled" setting (#27373)

This commit is contained in:
Nayeem Rahman 2024-12-16 17:25:49 +00:00 committed by GitHub
parent f9f5a12a16
commit 358bf566c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 126 additions and 1 deletions

View file

@ -459,6 +459,19 @@ impl Default for LanguagePreferences {
} }
} }
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct SuggestionActionsSettings {
#[serde(default = "is_true")]
pub enabled: bool,
}
impl Default for SuggestionActionsSettings {
fn default() -> Self {
SuggestionActionsSettings { enabled: true }
}
}
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq)] #[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct UpdateImportsOnFileMoveOptions { pub struct UpdateImportsOnFileMoveOptions {
@ -490,6 +503,8 @@ pub struct LanguageWorkspaceSettings {
#[serde(default)] #[serde(default)]
pub suggest: CompletionSettings, pub suggest: CompletionSettings,
#[serde(default)] #[serde(default)]
pub suggestion_actions: SuggestionActionsSettings,
#[serde(default)]
pub update_imports_on_file_move: UpdateImportsOnFileMoveOptions, pub update_imports_on_file_move: UpdateImportsOnFileMoveOptions,
} }
@ -2292,6 +2307,7 @@ mod tests {
enabled: true, enabled: true,
}, },
}, },
suggestion_actions: SuggestionActionsSettings { enabled: true },
update_imports_on_file_move: UpdateImportsOnFileMoveOptions { update_imports_on_file_move: UpdateImportsOnFileMoveOptions {
enabled: UpdateImportsOnFileMoveEnabled::Prompt enabled: UpdateImportsOnFileMoveEnabled::Prompt
} }
@ -2338,6 +2354,7 @@ mod tests {
enabled: true, enabled: true,
}, },
}, },
suggestion_actions: SuggestionActionsSettings { enabled: true },
update_imports_on_file_move: UpdateImportsOnFileMoveOptions { update_imports_on_file_move: UpdateImportsOnFileMoveOptions {
enabled: UpdateImportsOnFileMoveEnabled::Prompt enabled: UpdateImportsOnFileMoveEnabled::Prompt
} }

View file

@ -24,6 +24,7 @@ use crate::resolver::SloppyImportsCachedFs;
use crate::tools::lint::CliLinter; use crate::tools::lint::CliLinter;
use crate::tools::lint::CliLinterOptions; use crate::tools::lint::CliLinterOptions;
use crate::tools::lint::LintRuleProvider; use crate::tools::lint::LintRuleProvider;
use crate::tsc::DiagnosticCategory;
use crate::util::path::to_percent_decoded_str; use crate::util::path::to_percent_decoded_str;
use deno_ast::MediaType; use deno_ast::MediaType;
@ -906,8 +907,22 @@ async fn generate_ts_diagnostics(
} else { } else {
Default::default() Default::default()
}; };
for (specifier_str, ts_json_diagnostics) in ts_diagnostics_map { for (specifier_str, mut ts_json_diagnostics) in ts_diagnostics_map {
let specifier = resolve_url(&specifier_str)?; let specifier = resolve_url(&specifier_str)?;
let suggestion_actions_settings = snapshot
.config
.language_settings_for_specifier(&specifier)
.map(|s| s.suggestion_actions.clone())
.unwrap_or_default();
if !suggestion_actions_settings.enabled {
ts_json_diagnostics.retain(|d| {
d.category != DiagnosticCategory::Suggestion
// Still show deprecated and unused diagnostics.
// https://github.com/microsoft/vscode/blob/ce50bd4876af457f64d83cfd956bc916535285f4/extensions/typescript-language-features/src/languageFeatures/diagnostics.ts#L113-L114
|| d.reports_deprecated == Some(true)
|| d.reports_unnecessary == Some(true)
});
}
let version = snapshot let version = snapshot
.documents .documents
.get(&specifier) .get(&specifier)

View file

@ -5746,6 +5746,7 @@ mod tests {
"sourceLine": " import { A } from \".\";", "sourceLine": " import { A } from \".\";",
"category": 2, "category": 2,
"code": 6133, "code": 6133,
"reportsUnnecessary": true,
}] }]
}) })
); );
@ -5828,6 +5829,7 @@ mod tests {
"sourceLine": " import {", "sourceLine": " import {",
"category": 2, "category": 2,
"code": 6192, "code": 6192,
"reportsUnnecessary": true,
}, { }, {
"start": { "start": {
"line": 8, "line": 8,

View file

@ -133,6 +133,12 @@ pub struct Diagnostic {
pub file_name: Option<String>, pub file_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub related_information: Option<Vec<Diagnostic>>, pub related_information: Option<Vec<Diagnostic>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reports_deprecated: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reports_unnecessary: Option<bool>,
#[serde(flatten)]
pub other: deno_core::serde_json::Map<String, deno_core::serde_json::Value>,
} }
impl Diagnostic { impl Diagnostic {

View file

@ -1444,6 +1444,9 @@ mod tests {
source_line: None, source_line: None,
file_name: None, file_name: None,
related_information: None, related_information: None,
reports_deprecated: None,
reports_unnecessary: None,
other: Default::default(),
}]), }]),
stats: Stats(vec![("a".to_string(), 12)]) stats: Stats(vec![("a".to_string(), 12)])
}) })

View file

@ -2082,6 +2082,88 @@ fn lsp_inlay_hints_not_enabled() {
client.shutdown(); client.shutdown();
} }
#[test]
fn lsp_suggestion_actions_disabled() {
let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir();
let mut client = context.new_lsp_command().build();
client.initialize_default();
client.change_configuration(json!({
"deno": {
"enable": true,
"lint": false,
},
"typescript": {
"suggestionActions": {
"enabled": false,
},
},
}));
client.read_diagnostics();
let diagnostics = client.did_open(json!({
"textDocument": {
"uri": temp_dir.url().join("file.ts").unwrap(),
"languageId": "typescript",
"version": 1,
"text": r#"
// The settings should disable the suggestion for this to be async.
function asyncLikeFunction() {
return new Promise((r) => r(null)).then((v) => v);
}
console.log(asyncLikeFunction);
// Deprecated warnings should remain.
/** @deprecated */
function deprecatedFunction() {}
console.log(deprecatedFunction);
// Unused warnings should remain.
const unsusedVariable = 1;
"#,
},
}));
assert_eq!(
json!(diagnostics.all()),
json!([
{
"range": {
"start": { "line": 10, "character": 20 },
"end": { "line": 10, "character": 38 },
},
"severity": 4,
"code": 6385,
"source": "deno-ts",
"message": "'deprecatedFunction' is deprecated.",
"relatedInformation": [
{
"location": {
"uri": temp_dir.url().join("file.ts").unwrap(),
"range": {
"start": { "line": 8, "character": 12 },
"end": { "line": 8, "character": 24 },
},
},
"message": "The declaration was marked as deprecated here.",
},
],
"tags": [2],
},
{
"range": {
"start": { "line": 13, "character": 14 },
"end": { "line": 13, "character": 29 },
},
"severity": 4,
"code": 6133,
"source": "deno-ts",
"message": "'unsusedVariable' is declared but its value is never read.",
"tags": [1],
},
]),
);
client.shutdown();
}
#[test] #[test]
fn lsp_workspace_disable_enable_paths() { fn lsp_workspace_disable_enable_paths() {
fn run_test(use_trailing_slash: bool) { fn run_test(use_trailing_slash: bool) {