Skip to content

Commit cbbb7c1

Browse files
committed
Preserve namespace
1 parent a94b2f0 commit cbbb7c1

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

src/addNewResource.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function newResourceInput(context: ExtensionContext) {
5454
totalSteps: 3,
5555
value: state.comment || '',
5656
prompt: AppConstants.promptCommentName,
57-
validate: validateNotNull,
57+
validate: () => Promise.resolve(undefined), // Allow empty comments
5858
shouldResume: shouldResume
5959
});
6060
}

src/resxProvider.ts

+7
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ export class ResxProvider implements vscode.CustomTextEditorProvider {
126126
const parsedJson = JSON.parse(json);
127127
const edit = new vscode.WorkspaceEdit();
128128

129+
// Filter out empty comments before converting to RESX
130+
for (const key in parsedJson) {
131+
if (parsedJson[key].comment === '') {
132+
delete parsedJson[key].comment;
133+
}
134+
}
135+
129136
// Update the RESX file
130137
const resxContent = await resx.js2resx(parsedJson);
131138
edit.replace(

src/utilities/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export class AppConstants {
44
static viewTypeId = 'resx-editor.editor';
55
static promptKeyName = 'Provide a Key for the resource';
66
static promptValueName = 'Provide a Value for the resource';
7-
static promptCommentName = 'Provide a Comment for the resource';
7+
static promptCommentName = 'Provide a Comment for the resource (optional)';
88
static addNewTitle = 'Add new resource';
99
static openInTextEditorCommand = 'resx-editor.openInTextEditor';
1010
static openPreviewCommand = 'resx-editor.openPreview';

src/utilities/designerGenerator.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as path from 'path';
22

3-
export function generateDesignerCode(resxPath: string, resources: { [key: string]: { value: string; comment?: string } }, accessLevel: 'public' | 'internal' = 'public'): string {
3+
export function generateDesignerCode(resxPath: string, resources: { [key: string]: { value: string; comment?: string } }, accessLevel: 'public' | 'internal' = 'public', existingNamespace?: string): string {
44
const fileName = path.basename(resxPath, '.resx');
5-
const namespaceName = fileName.includes('.') ? fileName.split('.')[0] : 'Resources';
5+
// Use existing namespace if available, otherwise compute from filename
6+
const namespaceName = existingNamespace || (fileName.includes('.') ? fileName.split('.')[0] : 'Resources');
67
const className = fileName.includes('.') ? fileName.split('.')[1] : fileName;
78

89
let code = `//------------------------------------------------------------------------------
@@ -18,9 +19,14 @@ export function generateDesignerCode(resxPath: string, resources: { [key: string
1819
namespace ${namespaceName} {
1920
using System;
2021
22+
2123
/// <summary>
2224
/// A strongly-typed resource class, for looking up localized strings, etc.
2325
/// </summary>
26+
// This class was auto-generated by the StronglyTypedResourceBuilder
27+
// class via a tool like ResGen or Visual Studio.
28+
// To add or remove a member, edit your .ResX file then rerun ResGen
29+
// with the /str option, or rebuild your VS project.
2430
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("VS Code RESX Editor", "1.0.0.0")]
2531
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
2632
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
@@ -66,11 +72,14 @@ namespace ${namespaceName} {
6672

6773
// Generate a property for each resource
6874
for (const [key, resource] of Object.entries(resources)) {
75+
// Always add a summary - either the comment or a default message
76+
code += ` /// <summary>\n`;
6977
if (resource.comment) {
70-
code += ` /// <summary>\n`;
7178
code += ` /// ${resource.comment}\n`;
72-
code += ` /// </summary>\n`;
79+
} else {
80+
code += ` /// Looks up a localized string similar to ${resource.value}\n`;
7381
}
82+
code += ` /// </summary>\n`;
7483
code += ` ${accessLevel} static string ${key} {
7584
get {
7685
return ResourceManager.GetString("${key}", resourceCulture);

src/utilities/generateCode.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import * as path from 'path';
44
import { generateDesignerCode } from './designerGenerator';
55
import { printChannelOutput } from '../extension';
66

7+
async function extractExistingNamespace(designerPath: string): Promise<string | undefined> {
8+
try {
9+
const designerUri = vscode.Uri.file(designerPath);
10+
const content = await vscode.workspace.fs.readFile(designerUri);
11+
const match = content.toString().match(/namespace\s+([^\s{]+)/);
12+
return match?.[1];
13+
} catch {
14+
return undefined;
15+
}
16+
}
17+
718
async function checkResxGeneratorType(resxPath: string): Promise<'public' | 'internal'> {
819
// Look for a .csproj file in the same directory or parent directories
920
const dir = path.dirname(resxPath);
@@ -63,10 +74,13 @@ export async function generateAndUpdateDesignerFile(document: vscode.TextDocumen
6374
// Determine the access level based on .csproj settings
6475
const accessLevel = await checkResxGeneratorType(document.uri.fsPath);
6576

66-
// Generate and update the Designer.cs file
77+
// Get the existing namespace if the Designer file exists
6778
const designerPath = document.uri.fsPath.replace('.resx', '.Designer.cs');
79+
const existingNamespace = await extractExistingNamespace(designerPath);
80+
81+
// Generate and update the Designer.cs file
6882
const designerUri = vscode.Uri.file(designerPath);
69-
const designerCode = generateDesignerCode(document.uri.fsPath, parsedJson, accessLevel);
83+
const designerCode = generateDesignerCode(document.uri.fsPath, parsedJson, accessLevel, existingNamespace);
7084

7185
try {
7286
await vscode.workspace.fs.stat(designerUri);

0 commit comments

Comments
 (0)