|
1 | 1 | import * as vscode from 'vscode';
|
2 | 2 | import * as resx from 'resx';
|
| 3 | +import * as path from 'path'; |
3 | 4 | import { getNonce } from './utilities/getNonce';
|
4 | 5 | import { printChannelOutput } from './extension';
|
5 | 6 | import { newResourceInput } from './addNewResource';
|
6 | 7 | import { AppConstants } from './utilities/constants';
|
| 8 | +import { generateDesignerCode } from './utilities/designerGenerator'; |
7 | 9 |
|
8 | 10 | export class ResxProvider implements vscode.CustomTextEditorProvider {
|
9 | 11 |
|
@@ -120,14 +122,56 @@ export class ResxProvider implements vscode.CustomTextEditorProvider {
|
120 | 122 | }
|
121 | 123 |
|
122 | 124 | private async updateTextDocument(document: vscode.TextDocument, json: any) {
|
| 125 | + try { |
| 126 | + const parsedJson = JSON.parse(json); |
| 127 | + const edit = new vscode.WorkspaceEdit(); |
| 128 | + |
| 129 | + // Update the RESX file |
| 130 | + const resxContent = await resx.js2resx(parsedJson); |
| 131 | + edit.replace( |
| 132 | + document.uri, |
| 133 | + new vscode.Range(0, 0, document.lineCount, 0), |
| 134 | + resxContent |
| 135 | + ); |
| 136 | + |
| 137 | + // Check if code generation is enabled |
| 138 | + const config = vscode.workspace.getConfiguration('resx-editor'); |
| 139 | + const generateCode = config.get<boolean>('generateCode', true); |
| 140 | + |
| 141 | + if (generateCode) { |
| 142 | + // Generate and update the Designer.cs file |
| 143 | + const designerPath = document.uri.fsPath.replace('.resx', '.Designer.cs'); |
| 144 | + const designerUri = vscode.Uri.file(designerPath); |
| 145 | + const designerCode = generateDesignerCode(document.uri.fsPath, parsedJson); |
| 146 | + |
| 147 | + try { |
| 148 | + await vscode.workspace.fs.stat(designerUri); |
| 149 | + // File exists, write contents directly |
| 150 | + printChannelOutput(`Updating existing Designer file at ${designerPath}`, true); |
| 151 | + await vscode.workspace.fs.writeFile(designerUri, Buffer.from(designerCode, 'utf8')); |
| 152 | + } catch { |
| 153 | + // File doesn't exist, create it |
| 154 | + printChannelOutput(`Creating new Designer file at ${designerPath}`, true); |
| 155 | + await vscode.workspace.fs.writeFile(designerUri, Buffer.from(designerCode, 'utf8')); |
| 156 | + } |
| 157 | + } else { |
| 158 | + printChannelOutput('Code generation is disabled, skipping Designer.cs file update', true); |
| 159 | + } |
123 | 160 |
|
124 |
| - const edit = new vscode.WorkspaceEdit(); |
125 |
| - |
126 |
| - edit.replace( |
127 |
| - document.uri, |
128 |
| - new vscode.Range(0, 0, document.lineCount, 0), |
129 |
| - await resx.js2resx(JSON.parse(json))); |
130 |
| - return vscode.workspace.applyEdit(edit); |
| 161 | + const success = await vscode.workspace.applyEdit(edit); |
| 162 | + if (success) { |
| 163 | + printChannelOutput(`Successfully updated RESX${generateCode ? ' and Designer' : ''} files`, true); |
| 164 | + } else { |
| 165 | + printChannelOutput(`Failed to apply workspace edits`, true); |
| 166 | + vscode.window.showErrorMessage('Failed to update resource files'); |
| 167 | + } |
| 168 | + return success; |
| 169 | + } catch (error) { |
| 170 | + const errorMessage = `Error updating resource files: ${error instanceof Error ? error.message : String(error)}`; |
| 171 | + printChannelOutput(errorMessage, true); |
| 172 | + vscode.window.showErrorMessage(errorMessage); |
| 173 | + return false; |
| 174 | + } |
131 | 175 | }
|
132 | 176 |
|
133 | 177 | private _getWebviewContent(webview: vscode.Webview) {
|
|
0 commit comments