From 905bb8148ec9f4a6cb7a1c1469b42027d2a0ee91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Uhrbach?= Date: Sun, 19 Nov 2023 19:32:16 +0100 Subject: [PATCH] Set target and default language after canceling language prompt --- src/commands.ts | 10 +++++++--- src/helper.ts | 11 +++++++++++ src/state.ts | 5 +++-- 3 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 src/helper.ts diff --git a/src/commands.ts b/src/commands.ts index 291bc00..7cbf51e 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -5,6 +5,7 @@ import { state } from './state'; import { showMessageWithTimeout } from './vscode'; import { showApiKeyInput, showSourceLanguageInput, showTargetLanguageInput, showUseProInput } from "./inputs"; import { TranslateCommandParam, TranslateParam } from './types'; +import { getDefaultSourceLanguage, getDefaultTargetLanguage } from './helper'; function translateSelections(selections: vscode.Selection[], translateParam: TranslateParam): Thenable { const { targetLang, sourceLang, below } = translateParam; @@ -73,15 +74,18 @@ function createTranslateCommand(param: TranslateCommandParam) { ? state.sourceLanguage : null; if (askForSourceLang && sourceLang) { - state.sourceLanguage = sourceLang; + state.sourceLanguage = sourceLang ?? getDefaultSourceLanguage(); } if (askForTargetLang || !state.targetLanguage) { - state.targetLanguage = await showTargetLanguageInput(); + const targetLanguage = await showTargetLanguageInput(); - if (!state.targetLanguage) { + if (!targetLanguage) { + state.targetLanguage = getDefaultTargetLanguage(); return; } + + state.targetLanguage = targetLanguage; } const selections = vscode.window.activeTextEditor?.selections?.filter(selection => !selection.isEmpty); diff --git a/src/helper.ts b/src/helper.ts new file mode 100644 index 0000000..0d69141 --- /dev/null +++ b/src/helper.ts @@ -0,0 +1,11 @@ +import * as vscode from 'vscode'; + +export function getDefaultTargetLanguage(config?: vscode.WorkspaceConfiguration): string | null { + config = config ?? vscode.workspace.getConfiguration('deepl'); + return config.get('defaultTargetLanguage', null); +} + +export function getDefaultSourceLanguage(config?: vscode.WorkspaceConfiguration): string | null { + config = config ?? vscode.workspace.getConfiguration('deepl'); + return config.get('defaultSourceLanguage', null); +} \ No newline at end of file diff --git a/src/state.ts b/src/state.ts index 37fb332..8fae245 100644 --- a/src/state.ts +++ b/src/state.ts @@ -2,6 +2,7 @@ import * as vscode from 'vscode'; import * as debug from './debug'; import { ExtensionState } from './types'; import { reactive, watch, ref } from 'vue'; +import { getDefaultSourceLanguage, getDefaultTargetLanguage } from './helper'; const initialized = ref(false); @@ -43,8 +44,8 @@ export function setup(context: vscode.ExtensionContext) { state.nonSplittingTags = config.get('nonSplittingTags') ?? ""; state.preserveFormatting = config.get('preserveFormatting') ?? false; state.glossaryId = config.get('glossaryId') ?? ""; - state.targetLanguage = context.workspaceState.get('deepl:targetLanguage') ?? config.get('defaultTargetLanguage', null); - state.sourceLanguage = context.workspaceState.get('deepl:sourceLanguage') ?? config.get('defaultSourceLanguage', null); + state.targetLanguage = context.workspaceState.get('deepl:targetLanguage') ?? getDefaultTargetLanguage(config); + state.sourceLanguage = context.workspaceState.get('deepl:sourceLanguage') ?? getDefaultSourceLanguage(config); debug.write(`Initialized extension using state:`); debug.write(JSON.stringify(state, null, 2));