Skip to content

Commit b03d6c9

Browse files
committed
Fix blank file scenario
1 parent 0c4a26b commit b03d6c9

File tree

3 files changed

+67
-76
lines changed

3 files changed

+67
-76
lines changed

esbuild.js

+27-38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { build } = require("esbuild");
1+
const { build, context } = require("esbuild");
22

33
const baseConfig = {
44
bundle: true,
@@ -24,41 +24,30 @@ const webviewConfig = {
2424
outfile: "./out/webview.js",
2525
};
2626

27-
const watchConfig = {
28-
watch: {
29-
onRebuild(error, result) {
30-
console.log("[watch] build started");
31-
if (error) {
32-
error.errors.forEach(error =>
33-
console.error(`> ${error.location.file}:${error.location.line}:${error.location.column}: error: ${error.text}`)
34-
);
35-
} else {
36-
console.log("[watch] build finished");
37-
}
38-
},
39-
},
40-
};
41-
42-
(async () => {
43-
const args = process.argv.slice(2);
44-
try {
45-
if (args.includes("--watch")) {
46-
// Build and watch source code
47-
console.log("[watch] build started");
48-
await build({
49-
...extensionConfig,
50-
...watchConfig,
51-
...webviewConfig
52-
});
53-
console.log("[watch] build finished");
54-
} else {
55-
// Build source code
56-
await build(extensionConfig);
57-
await build(webviewConfig);
58-
console.log("build complete");
59-
}
60-
} catch (err) {
61-
process.stderr.write(err.stderr);
62-
process.exit(1);
27+
(async () => {
28+
const args = process.argv.slice(2);
29+
try {
30+
if (args.includes("--watch")) {
31+
// Build and watch source code
32+
console.log("[watch] build started");
33+
34+
const extensionCtx = await context(extensionConfig);
35+
const webviewCtx = await context(webviewConfig);
36+
37+
await Promise.all([
38+
extensionCtx.watch(),
39+
webviewCtx.watch()
40+
]);
41+
42+
console.log("[watch] watching...");
43+
} else {
44+
// Build source code
45+
await build(extensionConfig);
46+
await build(webviewConfig);
47+
console.log("build complete");
6348
}
64-
})();
49+
} catch (err) {
50+
process.stderr.write(err?.stderr || String(err));
51+
process.exit(1);
52+
}
53+
})();

src/addNewResource.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,10 @@ export async function newResourceInput(context: ExtensionContext) {
6060
}
6161

6262
function shouldResume() {
63-
// Could show a notification with the option to resume.
64-
return new Promise<boolean>((resolve, reject) => {
65-
// noop
66-
});
63+
return Promise.resolve(false);
6764
}
6865

69-
async function validateNotNull(name: string) {
70-
await new Promise(resolve => setTimeout(resolve, 1000));
66+
async function validateNotNull(name: string): Promise<string | undefined> {
7167
return name === '' ? 'Must not be empty' : undefined;
7268
}
7369

@@ -81,11 +77,10 @@ export async function newResourceInput(context: ExtensionContext) {
8177
// Helper code that wraps the API for the multi-step case.
8278
// -------------------------------------------------------
8379

84-
85-
class InputFlowAction {
86-
static back = new InputFlowAction();
87-
static cancel = new InputFlowAction();
88-
static resume = new InputFlowAction();
80+
enum InputFlowAction {
81+
back,
82+
cancel,
83+
resume
8984
}
9085

9186
type InputStep = (input: MultiStepInput) => Thenable<InputStep | void>;

src/webview/webview.js

+34-27
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,14 @@ let currentRowData = null;
136136
if (message.key) {
137137
const index = grid.rowsData.findIndex(x => x.Key === message.key);
138138
if (index === -1) {
139+
if (!grid.rowsData) {
140+
grid.rowsData = [];
141+
}
139142
// eslint-disable-next-line @typescript-eslint/naming-convention
140143
grid.rowsData.push({ Key: message.key, Value: message.value, Comment: message.comment });
141144
refreshResxData();
145+
// Force grid to update by reassigning the rowsData
146+
grid.rowsData = [...grid.rowsData];
142147
}
143148
else {
144149
// create vscode notification
@@ -176,38 +181,40 @@ let currentRowData = null;
176181
}
177182

178183
function updateContent(/** @type {string} **/ text) {
179-
if (text) {
180-
181-
var resxValues = [];
182-
183-
let json;
184-
try {
185-
json = JSON.parse(text);
186-
}
187-
catch
188-
{
189-
console.log("error parsing json");
190-
return;
191-
}
192-
193-
for (const node in json || []) {
194-
if (node) {
195-
let res = json[node];
196-
// eslint-disable-next-line @typescript-eslint/naming-convention
197-
var item = { Key: node, "Value": res.value || '', "Comment": res.comment || '' };
198-
resxValues.push(item);
199-
}
200-
else {
201-
console.log('node is undefined or null');
202-
}
203-
}
184+
var resxValues = [];
204185

186+
if (!text || text.trim() === '') {
187+
// Handle blank file by initializing with empty data
205188
grid.rowsData = resxValues;
189+
return;
206190
}
207-
else {
208-
console.log("text is null");
191+
192+
let json;
193+
try {
194+
json = JSON.parse(text);
195+
}
196+
catch (e) {
197+
console.log("error parsing json:", e);
198+
vscode.postMessage({
199+
type: 'error',
200+
message: 'Error parsing resource file content'
201+
});
209202
return;
210203
}
204+
205+
for (const node in json || []) {
206+
if (node) {
207+
let res = json[node];
208+
// eslint-disable-next-line @typescript-eslint/naming-convention
209+
var item = { Key: node, "Value": res.value || '', "Comment": res.comment || '' };
210+
resxValues.push(item);
211+
}
212+
else {
213+
console.log('node is undefined or null');
214+
}
215+
}
216+
217+
grid.rowsData = resxValues;
211218
}
212219

213220
const state = vscode.getState();

0 commit comments

Comments
 (0)