Skip to content

Commit

Permalink
Set target and default language after canceling language prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenuhrbach committed Nov 19, 2023
1 parent efdb8cd commit 905bb81
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const { targetLang, sourceLang, below } = translateParam;
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions src/helper.ts
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 3 additions & 2 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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<string>('deepl:targetLanguage') ?? config.get('defaultTargetLanguage', null);
state.sourceLanguage = context.workspaceState.get<string>('deepl:sourceLanguage') ?? config.get('defaultSourceLanguage', null);
state.targetLanguage = context.workspaceState.get<string>('deepl:targetLanguage') ?? getDefaultTargetLanguage(config);
state.sourceLanguage = context.workspaceState.get<string>('deepl:sourceLanguage') ?? getDefaultSourceLanguage(config);

debug.write(`Initialized extension using state:`);
debug.write(JSON.stringify(state, null, 2));
Expand Down

0 comments on commit 905bb81

Please sign in to comment.