diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2023-09-18 20:48:32 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-18 20:48:32 +0100 |
commit | 86c04f43e0eda139a262c4f2e106e9b11e8d7da8 (patch) | |
tree | 71e8661fdaf7ac0e677707d76d6c82d2e0d1dade /cli/lsp | |
parent | f7ba7013045fc0629092ea4e07ce5c768a7ee95f (diff) |
fix(lsp): pass quote preference to tsc (#20547)
Diffstat (limited to 'cli/lsp')
-rw-r--r-- | cli/lsp/language_server.rs | 29 | ||||
-rw-r--r-- | cli/lsp/tsc.rs | 61 |
2 files changed, 82 insertions, 8 deletions
diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index df5552764..102408df4 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -1927,6 +1927,10 @@ impl Inner { ..line_index.offset_tsc(diagnostic.range.end)?, codes, (&self.fmt_options.options).into(), + tsc::UserPreferences { + quote_preference: Some((&self.fmt_options.options).into()), + ..self.config.workspace_settings().into() + }, ) .await; for action in actions { @@ -1984,6 +1988,10 @@ impl Inner { specifier.clone(), line_index.offset_tsc(params.range.start)? ..line_index.offset_tsc(params.range.end)?, + Some(tsc::UserPreferences { + quote_preference: Some((&self.fmt_options.options).into()), + ..self.config.workspace_settings().into() + }), only, ) .await?; @@ -2039,6 +2047,10 @@ impl Inner { self.snapshot(), &code_action_data, (&self.fmt_options.options).into(), + tsc::UserPreferences { + quote_preference: Some((&self.fmt_options.options).into()), + ..self.config.workspace_settings().into() + }, ) .await?; if combined_code_actions.commands.is_some() { @@ -2084,6 +2096,10 @@ impl Inner { ..line_index.offset_tsc(action_data.range.end)?, action_data.refactor_name, action_data.action_name, + Some(tsc::UserPreferences { + quote_preference: Some((&self.fmt_options.options).into()), + ..self.config.workspace_settings().into() + }), ) .await?; code_action.edit = refactor_edit_info.to_workspace_edit(self).await?; @@ -2399,6 +2415,7 @@ impl Inner { position, tsc::GetCompletionsAtPositionOptions { user_preferences: tsc::UserPreferences { + quote_preference: Some((&self.fmt_options.options).into()), allow_incomplete_completions: Some(true), allow_text_changes_in_new_files: Some( specifier.scheme() == "file", @@ -2466,10 +2483,14 @@ impl Inner { })?; if let Some(data) = &data.tsc { let specifier = &data.specifier; - let args = GetCompletionDetailsArgs { + let mut args = GetCompletionDetailsArgs { format_code_settings: Some((&self.fmt_options.options).into()), ..data.into() }; + args + .preferences + .get_or_insert(Default::default()) + .quote_preference = Some((&self.fmt_options.options).into()); let result = self .ts_server .get_completion_details(self.snapshot(), args) @@ -2971,6 +2992,7 @@ impl Inner { (&self.fmt_options.options).into(), tsc::UserPreferences { allow_text_changes_in_new_files: Some(true), + quote_preference: Some((&self.fmt_options.options).into()), ..Default::default() }, ) @@ -3600,7 +3622,10 @@ impl Inner { self.snapshot(), specifier, text_span, - workspace_settings.into(), + tsc::UserPreferences { + quote_preference: Some((&self.fmt_options.options).into()), + ..workspace_settings.into() + }, ) .await?; let maybe_inlay_hints = maybe_inlay_hints.map(|hints| { diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index 2c63131b6..81950f2e3 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -234,6 +234,7 @@ impl TsServer { range: Range<u32>, codes: Vec<String>, format_code_settings: FormatCodeSettings, + preferences: UserPreferences, ) -> Vec<CodeFixAction> { let req = RequestMethod::GetCodeFixes(( specifier, @@ -241,6 +242,7 @@ impl TsServer { range.end, codes, format_code_settings, + preferences, )); match self.request(snapshot, req).await { Ok(items) => items, @@ -260,6 +262,7 @@ impl TsServer { snapshot: Arc<StateSnapshot>, specifier: ModuleSpecifier, range: Range<u32>, + preferences: Option<UserPreferences>, only: String, ) -> Result<Vec<ApplicableRefactorInfo>, LspError> { let req = RequestMethod::GetApplicableRefactors(( @@ -268,6 +271,7 @@ impl TsServer { start: range.start, length: range.end - range.start, }, + preferences, only, )); self.request(snapshot, req).await.map_err(|err| { @@ -281,11 +285,13 @@ impl TsServer { snapshot: Arc<StateSnapshot>, code_action_data: &CodeActionData, format_code_settings: FormatCodeSettings, + preferences: UserPreferences, ) -> Result<CombinedCodeActions, LspError> { let req = RequestMethod::GetCombinedCodeFix(( code_action_data.specifier.clone(), json!(code_action_data.fix_id.clone()), format_code_settings, + preferences, )); self.request(snapshot, req).await.map_err(|err| { log::error!("Unable to get combined fix from TypeScript: {}", err); @@ -293,6 +299,7 @@ impl TsServer { }) } + #[allow(clippy::too_many_arguments)] pub async fn get_edits_for_refactor( &self, snapshot: Arc<StateSnapshot>, @@ -301,6 +308,7 @@ impl TsServer { range: Range<u32>, refactor_name: String, action_name: String, + preferences: Option<UserPreferences>, ) -> Result<RefactorEditInfo, LspError> { let req = RequestMethod::GetEditsForRefactor(( specifier, @@ -311,6 +319,7 @@ impl TsServer { }, refactor_name, action_name, + preferences, )); self.request(snapshot, req).await.map_err(|err| { log::error!("Failed to request to tsserver {}", err); @@ -3507,6 +3516,15 @@ pub enum QuotePreference { Single, } +impl From<&FmtOptionsConfig> for QuotePreference { + fn from(config: &FmtOptionsConfig) -> Self { + match config.single_quote { + Some(true) => QuotePreference::Single, + _ => QuotePreference::Double, + } + } +} + #[derive(Debug, Serialize)] #[serde(rename_all = "kebab-case")] #[allow(dead_code)] @@ -3765,7 +3783,9 @@ enum RequestMethod { }, GetAssets, /// Retrieve the possible refactor info for a range of a file. - GetApplicableRefactors((ModuleSpecifier, TextSpan, String)), + GetApplicableRefactors( + (ModuleSpecifier, TextSpan, Option<UserPreferences>, String), + ), /// Retrieve the refactor edit info for a range. GetEditsForRefactor( ( @@ -3774,6 +3794,7 @@ enum RequestMethod { TextSpan, String, String, + Option<UserPreferences>, ), ), /// Retrieve the refactor edit info for a range. @@ -3786,7 +3807,16 @@ enum RequestMethod { ), ), /// Retrieve code fixes for a range of a file with the provided error codes. - GetCodeFixes((ModuleSpecifier, u32, u32, Vec<String>, FormatCodeSettings)), + GetCodeFixes( + ( + ModuleSpecifier, + u32, + u32, + Vec<String>, + FormatCodeSettings, + UserPreferences, + ), + ), /// Get completion information at a given position (IntelliSense). GetCompletions( ( @@ -3799,7 +3829,9 @@ enum RequestMethod { /// Get details about a specific completion entry. GetCompletionDetails(GetCompletionDetailsArgs), /// Retrieve the combined code fixes for a fix id for a module. - GetCombinedCodeFix((ModuleSpecifier, Value, FormatCodeSettings)), + GetCombinedCodeFix( + (ModuleSpecifier, Value, FormatCodeSettings, UserPreferences), + ), /// Get declaration information for a specific position. GetDefinition((ModuleSpecifier, u32)), /// Return diagnostics for given file. @@ -3876,11 +3908,17 @@ impl RequestMethod { "id": id, "method": "getAssets", }), - RequestMethod::GetApplicableRefactors((specifier, span, kind)) => json!({ + RequestMethod::GetApplicableRefactors(( + specifier, + span, + preferences, + kind, + )) => json!({ "id": id, "method": "getApplicableRefactors", "specifier": state.denormalize_specifier(specifier), "range": { "pos": span.start, "end": span.start + span.length }, + "preferences": preferences, "kind": kind, }), RequestMethod::GetEditsForRefactor(( @@ -3889,6 +3927,7 @@ impl RequestMethod { span, refactor_name, action_name, + preferences, )) => json!({ "id": id, "method": "getEditsForRefactor", @@ -3897,6 +3936,7 @@ impl RequestMethod { "range": { "pos": span.start, "end": span.start + span.length}, "refactorName": refactor_name, "actionName": action_name, + "preferences": preferences, }), RequestMethod::GetEditsForFileRename(( old_specifier, @@ -3917,6 +3957,7 @@ impl RequestMethod { end_pos, error_codes, format_code_settings, + preferences, )) => json!({ "id": id, "method": "getCodeFixes", @@ -3925,17 +3966,20 @@ impl RequestMethod { "endPosition": end_pos, "errorCodes": error_codes, "formatCodeSettings": format_code_settings, + "preferences": preferences, }), RequestMethod::GetCombinedCodeFix(( specifier, fix_id, format_code_settings, + preferences, )) => json!({ "id": id, "method": "getCombinedCodeFix", "specifier": state.denormalize_specifier(specifier), "fixId": fix_id, "formatCodeSettings": format_code_settings, + "preferences": preferences, }), RequestMethod::GetCompletionDetails(args) => json!({ "id": id, @@ -4976,6 +5020,7 @@ mod tests { assert!(result.is_ok()); let fmt_options_config = FmtOptionsConfig { semi_colons: Some(false), + single_quote: Some(true), ..Default::default() }; let result = request( @@ -4986,6 +5031,7 @@ mod tests { position, GetCompletionsAtPositionOptions { user_preferences: UserPreferences { + quote_preference: Some((&fmt_options_config).into()), include_completions_for_module_exports: Some(true), include_completions_with_insert_text: Some(true), ..Default::default() @@ -5011,7 +5057,10 @@ mod tests { position, name: entry.name.clone(), source: entry.source.clone(), - preferences: None, + preferences: Some(UserPreferences { + quote_preference: Some((&fmt_options_config).into()), + ..Default::default() + }), format_code_settings: Some((&fmt_options_config).into()), data: entry.data.clone(), }), @@ -5029,7 +5078,7 @@ mod tests { let change = changes.text_changes.first().unwrap(); assert_eq!( change.new_text, - "import { someLongVariable } from \"./b.ts\"\n" + "import { someLongVariable } from './b.ts'\n" ); } |