Skip to content

Commit dba5417

Browse files
authored
fix configuration not being reloaded, "generationEnabled" not being used, formatting when running "Sort arb Files", add option to run sort & format when manually editing (#432)
Authored-by: Stefan Schindler <[email protected]>
1 parent 3827f26 commit dba5417

16 files changed

+244
-112
lines changed

.DS_Store

6 KB
Binary file not shown.

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@
1313
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
1414
"typescript.tsc.autoDetect": "off",
1515
"editor.defaultFormatter": "esbenp.prettier-vscode",
16-
"editor.formatOnSave": true
16+
"editor.formatOnSave": false,
17+
"[dart]": {
18+
"editor.formatOnSave": true
19+
}
1720
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"icon": "images/logo.png",
33
"name": "l10nization",
44
"displayName": "L10nization",
5-
"version": "3.2.0",
5+
"version": "3.2.2",
66
"publisher": "lsaudon",
77
"description": "A tool for extracting text to arb files in a Flutter application.",
88
"author": {
@@ -90,6 +90,10 @@
9090
"type": "boolean",
9191
"default": true,
9292
"markdownDescription": "If enabled, sort keys alphabetically except for `@@locale` is first and `@key` are below their `key`."
93+
}, "l10nization.organizeOnSave": {
94+
"type": "boolean",
95+
"default": true,
96+
"markdownDescription": "If enabled and editing arb files manually, when saving runs sort if enabled and formats file"
9397
},
9498
"l10nization.addNewMessagesIn": {
9599
"type": "string",

src/extension/applySaveAndRunFlutterPubGet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
22
import { EditFilesParameters } from '../commands/editFilesParameters';
33
import { getChangesForArbFiles } from './getChangesForArbFiles';
4-
import { runGeneration } from './runFlutterPubGet';
4+
import { runGeneration } from './runGeneration';
55

66
export async function applySaveAndRunGeneration(editFilesParameters: EditFilesParameters): Promise<void> {
77
const { workspace } = vscode;

src/extension/extension.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1+
import * as path from 'path';
12
import * as vscode from 'vscode';
23
import { CommandParameters } from '../commands/commandParameters';
4+
import { Configuration } from '../shared/configuration';
35
import { EditFilesCommand } from '../commands/editFilesCommand';
46
import { EditFilesParameters } from '../commands/editFilesParameters';
57
import { InputBoxCommand } from '../commands/inputBoxCommand';
68
import { LocalizationActionProvider } from '../codeActions/localizationActionProvider';
79
import { applySaveAndRunGeneration } from './applySaveAndRunFlutterPubGet';
810
import { setEditFilesParameters } from './setEditFilesParameters';
11+
import { sortAndFormat } from './sortAndFormat';
912
import { sortAndSave } from './sortAndSave';
1013

1114
export function activate(context: vscode.ExtensionContext): void {
15+
vscode.workspace.onDidChangeConfiguration((event) => {
16+
const affected = event.affectsConfiguration('l10nization');
17+
if (affected) {
18+
// reload configuration on changes to l10nization settings
19+
Configuration.getInstance().reload();
20+
}
21+
});
1222
context.subscriptions.push(
1323
vscode.languages.registerCodeActionsProvider('dart', new LocalizationActionProvider(), {
1424
providedCodeActionKinds: LocalizationActionProvider.providedCodeActionKinds,
@@ -24,6 +34,20 @@ export function activate(context: vscode.ExtensionContext): void {
2434
vscode.commands.registerCommand(EditFilesCommand.commandName, async (...args: EditFilesParameters[]): Promise<void> => applySaveAndRunGeneration(args[0])),
2535
);
2636
context.subscriptions.push(vscode.commands.registerCommand('l10nization.sortArbFiles', async (): Promise<void> => sortAndSave()));
37+
38+
context.subscriptions.push(
39+
vscode.workspace.onWillSaveTextDocument((e) => {
40+
const organize = Configuration.getInstance().getOrganizeOnSave();
41+
42+
if (organize) {
43+
const editor = vscode.window.activeTextEditor;
44+
45+
if (editor && editor.document === e.document && path.extname(e.document.fileName) === '.arb') {
46+
e.waitUntil(sortAndFormat(e.document, editor));
47+
}
48+
}
49+
}),
50+
);
2751
}
2852

2953
export function deactivate(): void {}

src/extension/getArbFiles.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22
import * as yaml from 'yaml';
3-
import { AddMessageInStatus, getAddNewMessagesIn, getYamlFileName } from '../shared/configuration';
3+
import { AddMessageInStatus, Configuration } from '../shared/configuration';
44
import { resolvePath } from '../shared/resolvePath';
55

66
async function findYamlFiles(projectName: string, yamlFileName: string): Promise<vscode.Uri[]> {
@@ -24,7 +24,7 @@ async function findArbFiles(projectName: string, arbDir: string): Promise<vscode
2424
}
2525

2626
export async function getArbFiles(projectName: string): Promise<[vscode.Uri[], vscode.Uri | undefined]> {
27-
const yamlFileName = getYamlFileName;
27+
const yamlFileName = Configuration.getInstance().getYamlFileName();
2828
const yamlFiles = await findYamlFiles(projectName, yamlFileName);
2929

3030
if (yamlFiles.length === 0) {
@@ -41,7 +41,7 @@ export async function getArbFiles(projectName: string): Promise<[vscode.Uri[], v
4141

4242
const templateArbFileName = (parsedConfiguration.get('template-arb-file') as string | undefined) ?? 'app_en.arb';
4343
const templateArbFile = arbFiles.find((arbFile) => arbFile.path.endsWith(templateArbFileName));
44-
const addMessageInStatus = getAddNewMessagesIn;
44+
const addMessageInStatus = Configuration.getInstance().getAddNewMessagesIn();
4545
switch (addMessageInStatus) {
4646
case AddMessageInStatus.All:
4747
return [arbFiles, templateArbFile];

src/extension/getChangesForArbFiles.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode';
2-
import { getAppLocalizationsVariable, getCopyMetadataInAllFiles, getSortArbEnabled } from '../shared/configuration';
2+
import { Configuration } from '../shared/configuration';
33
import { EditFilesParameters } from '../commands/editFilesParameters';
4+
import { L10nObject } from './l10nObject';
45
import { getArbFiles } from './getArbFiles';
56
import { getFunctionCall } from './getFunctionCall';
67
import { getProjectName } from './getProjectName';
@@ -24,18 +25,18 @@ export async function getChangesForArbFiles(parameters: EditFilesParameters): Pr
2425
const workspaceEdit = new vscode.WorkspaceEdit();
2526
const { key, value } = parameters.keyValue;
2627
const { description, placeholders } = parameters;
27-
const sortArbEnabled = getSortArbEnabled;
28+
const sortArbEnabled = Configuration.getInstance().getSortArbEnabled();
2829
(await Promise.all(openTextDocuments)).forEach((content, index) => {
2930
const file = files[index];
30-
const isMetadataEnabled = getCopyMetadataInAllFiles;
31+
const isMetadataEnabled = Configuration.getInstance().getCopyMetadataInAllFiles();
3132
workspaceEdit.replace(
3233
file,
3334
new vscode.Range(new vscode.Position(0, 0), new vscode.Position(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER)),
34-
toJson(content.getText(), isMetadataEnabled || file === templateFile, key, description, value, placeholders, sortArbEnabled),
35+
toJson(content.getText(), new L10nObject(isMetadataEnabled || file === templateFile, key, description, value, placeholders), sortArbEnabled),
3536
);
3637
});
3738

38-
const appLocalizationsVariable = getAppLocalizationsVariable;
39+
const appLocalizationsVariable = Configuration.getInstance().getAppLocalizationsVariable();
3940
workspaceEdit.replace(
4041
parameters.uri,
4142
parameters.range,

src/extension/l10nObject.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Placeholder } from '../placeholders/placeholder';
2+
3+
export class L10nObject {
4+
readonly isMetadataEnabled: boolean;
5+
readonly key: string;
6+
readonly description: string | null;
7+
readonly value: string;
8+
readonly placeholders: Placeholder[];
9+
10+
constructor(isMetadataEnabled: boolean, key: string, description: string | null, value: string, placeholders: Placeholder[]) {
11+
this.isMetadataEnabled = isMetadataEnabled;
12+
this.key = key;
13+
this.description = description;
14+
this.value = value;
15+
this.placeholders = placeholders;
16+
}
17+
}

src/extension/runFlutterPubGet.ts renamed to src/extension/runGeneration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { getGenerationActivted } from '../shared/configuration';
1+
import { Configuration } from '../shared/configuration';
22
import { runIfExist } from './runIfExist';
33

44
export async function runGeneration(): Promise<void> {
5-
if (!getGenerationActivted) {
5+
if (!Configuration.getInstance().getGenerationActivated()) {
66
return;
77
}
88
await runIfExist('flutter.task.genl10n');

0 commit comments

Comments
 (0)