Skip to content

Commit

Permalink
fix(lsp): respect "typescript.suggestionActions.enabled" setting (#27373
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nayeemrmn authored Dec 16, 2024
1 parent f9f5a12 commit 358bf56
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 1 deletion.
17 changes: 17 additions & 0 deletions cli/lsp/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
#[serde(rename_all = "camelCase")]
pub struct UpdateImportsOnFileMoveOptions {
Expand Down Expand Up @@ -490,6 +503,8 @@ pub struct LanguageWorkspaceSettings {
#[serde(default)]
pub suggest: CompletionSettings,
#[serde(default)]
pub suggestion_actions: SuggestionActionsSettings,
#[serde(default)]
pub update_imports_on_file_move: UpdateImportsOnFileMoveOptions,
}

Expand Down Expand Up @@ -2292,6 +2307,7 @@ mod tests {
enabled: true,
},
},
suggestion_actions: SuggestionActionsSettings { enabled: true },
update_imports_on_file_move: UpdateImportsOnFileMoveOptions {
enabled: UpdateImportsOnFileMoveEnabled::Prompt
}
Expand Down Expand Up @@ -2338,6 +2354,7 @@ mod tests {
enabled: true,
},
},
suggestion_actions: SuggestionActionsSettings { enabled: true },
update_imports_on_file_move: UpdateImportsOnFileMoveOptions {
enabled: UpdateImportsOnFileMoveEnabled::Prompt
}
Expand Down
17 changes: 16 additions & 1 deletion cli/lsp/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::resolver::SloppyImportsCachedFs;
use crate::tools::lint::CliLinter;
use crate::tools::lint::CliLinterOptions;
use crate::tools::lint::LintRuleProvider;
use crate::tsc::DiagnosticCategory;
use crate::util::path::to_percent_decoded_str;

use deno_ast::MediaType;
Expand Down Expand Up @@ -906,8 +907,22 @@ async fn generate_ts_diagnostics(
} else {
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 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
.documents
.get(&specifier)
Expand Down
2 changes: 2 additions & 0 deletions cli/lsp/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5746,6 +5746,7 @@ mod tests {
"sourceLine": " import { A } from \".\";",
"category": 2,
"code": 6133,
"reportsUnnecessary": true,
}]
})
);
Expand Down Expand Up @@ -5828,6 +5829,7 @@ mod tests {
"sourceLine": " import {",
"category": 2,
"code": 6192,
"reportsUnnecessary": true,
}, {
"start": {
"line": 8,
Expand Down
6 changes: 6 additions & 0 deletions cli/tsc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ pub struct Diagnostic {
pub file_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
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 {
Expand Down
3 changes: 3 additions & 0 deletions cli/tsc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,9 @@ mod tests {
source_line: None,
file_name: None,
related_information: None,
reports_deprecated: None,
reports_unnecessary: None,
other: Default::default(),
}]),
stats: Stats(vec![("a".to_string(), 12)])
})
Expand Down
82 changes: 82 additions & 0 deletions tests/integration/lsp_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,88 @@ fn lsp_inlay_hints_not_enabled() {
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]
fn lsp_workspace_disable_enable_paths() {
fn run_test(use_trailing_slash: bool) {
Expand Down

0 comments on commit 358bf56

Please sign in to comment.