Skip to content

Commit e9cff2c

Browse files
authored
NEW (Extension) @W-17865130@ Fix indentation for paranthesis and square brackets (#181)
1 parent 1aaf2cb commit e9cff2c

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

src/extension.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import * as DeltaRunFunctions from './deltarun/delta-run-service';
2828
import * as os from 'os';
2929
import * as fs from 'fs';
3030
import { ApexPmdViolationsFixer } from './modelBasedFixers/apex-pmd-violations-fixer'
31-
import { VSCodeUnifiedDiff, DiffHunk } from './shared/UnifiedDiff';
31+
import { VSCodeUnifiedDiff, DiffHunk, CODEGENIE_UNIFIED_DIFF_ACCEPT, CODEGENIE_UNIFIED_DIFF_REJECT, CODEGENIE_UNIFIED_DIFF_ACCEPT_ALL, CODEGENIE_UNIFIED_DIFF_REJECT_ALL } from './shared/UnifiedDiff';
3232

3333
export type RunInfo = {
3434
diagnosticCollection?: vscode.DiagnosticCollection;
@@ -238,18 +238,18 @@ function setupUnifiedDiff(context: vscode.ExtensionContext, diagnosticManager: D
238238
})
239239
);
240240
context.subscriptions.push(
241-
vscode.commands.registerCommand(Constants.UNIFIED_DIFF_ACCEPT, async (hunk: DiffHunk, range: vscode.Range) => {
241+
vscode.commands.registerCommand(CODEGENIE_UNIFIED_DIFF_ACCEPT, async (hunk: DiffHunk, range: vscode.Range) => {
242242
await VSCodeUnifiedDiff.singleton.unifiedDiffAccept(hunk);
243243
apexPmdFixer.removeDiagnosticsWithInRange(vscode.window.activeTextEditor.document.uri, range, diagnosticCollection);
244244
})
245245
);
246246
context.subscriptions.push(
247-
vscode.commands.registerCommand(Constants.UNIFIED_DIFF_REJECT, async (hunk: DiffHunk) => {
247+
vscode.commands.registerCommand(CODEGENIE_UNIFIED_DIFF_REJECT, async (hunk: DiffHunk) => {
248248
await VSCodeUnifiedDiff.singleton.unifiedDiffReject(hunk);
249249
})
250250
);
251251
context.subscriptions.push(
252-
vscode.commands.registerCommand(Constants.UNIFIED_DIFF_ACCEPT_ALL, async () => {
252+
vscode.commands.registerCommand(CODEGENIE_UNIFIED_DIFF_ACCEPT_ALL, async () => {
253253
await VSCodeUnifiedDiff.singleton.unifiedDiffAcceptAll();
254254
// For accept all, it is tricky to get all the code that gets accepted and to remove them from diagnostic.
255255
// Hence, we save the file and rerun the scan instead.
@@ -262,7 +262,7 @@ function setupUnifiedDiff(context: vscode.ExtensionContext, diagnosticManager: D
262262
})
263263
);
264264
context.subscriptions.push(
265-
vscode.commands.registerCommand(Constants.UNIFIED_DIFF_REJECT_ALL, async () => {
265+
vscode.commands.registerCommand(CODEGENIE_UNIFIED_DIFF_REJECT_ALL, async () => {
266266
await VSCodeUnifiedDiff.singleton.unifiedDiffRejectAll();
267267
})
268268
);

src/lib/constants.ts

-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,3 @@ export const APEX_GURU_RETRY_INTERVAL_MILLIS = 1000;
4646
export const ENABLE_A4D_INTEGRATION = false;
4747
export const A4D_FIX_AVAILABLE_RULES = ['ApexCRUDViolation', 'ApexSharingViolations', 'EmptyCatchBlock', 'EmptyTryOrFinallyBlock', 'EmptyWhileStmt', 'EmptyIfStmt'];
4848
export const UNIFIED_DIFF = 'unifiedDiff';
49-
export const UNIFIED_DIFF_ACCEPT = 'unifiedDiff.accept';
50-
export const UNIFIED_DIFF_REJECT = 'unifiedDiff.reject';
51-
export const UNIFIED_DIFF_ACCEPT_ALL = 'unifiedDiff.acceptAll';
52-
export const UNIFIED_DIFF_REJECT_ALL = 'unifiedDiff.rejectAll';

src/modelBasedFixers/apex-pmd-violations-fixer.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -211,22 +211,42 @@ export class ApexPmdViolationsFixer implements vscode.CodeActionProvider {
211211
const normalizedLines = lines.map(line => line.trimStart());
212212

213213
let indentLevel = 0;
214+
let braceLevel = 0;
215+
let parenLevel = 0;
216+
let bracketLevel = 0;
214217

215218
// Process each line to match the document's indentation style
216219
const formattedLines = normalizedLines.map((line) => {
217220
if (line.trim() === '') {
218221
// Preserve empty lines without indentation
219222
return '';
220223
}
224+
indentLevel = braceLevel + parenLevel + bracketLevel;
225+
221226
if (line.startsWith('}')) {
227+
braceLevel = Math.max(0, braceLevel - 1);
228+
indentLevel = Math.max(0, indentLevel - 1);
229+
}
230+
231+
if (line.startsWith(')')) {
232+
parenLevel = Math.max(0, parenLevel - 1);
233+
indentLevel = Math.max(0, indentLevel - 1);
234+
}
235+
236+
if (line.startsWith(']')) {
237+
bracketLevel = Math.max(0, bracketLevel - 1);
222238
indentLevel = Math.max(0, indentLevel - 1);
223239
}
224240

225241
const indentation = baseIndentation + ' '.repeat(indentLevel);
226242

227243
// Adjust for next lines, this line needs no changes
228244
if (line.endsWith('{')) {
229-
indentLevel++;
245+
braceLevel++;
246+
} else if (line.endsWith('(')) {
247+
parenLevel++;
248+
} else if (line.endsWith('[')) {
249+
braceLevel++;
230250
}
231251

232252
return indentation + line;

src/test/suite/modelBasedFixers/apex-pmd-violations-fixer.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,26 @@ suite('apex-pmd-violations-fixer.ts', () => {
144144
expect(result).to.equal('class Test {\n void method() {\n int x = 1;\n x++;\n x=10;\n }\n}');
145145
});
146146

147+
test('should replace code while preserving indentation for multiple lines for brackets', () => {
148+
const fileContent = 'class Test {\n String soql = [\n // soql\n ]\n}';
149+
const replaceCode = 'SELECT ID\nFROM\nACCOUNT';
150+
(fakeDocument.lineAt as Sinon.SinonStub).callsFake((line: number) => ({
151+
text: fileContent.split("\n")[line]
152+
}));
153+
const result = fixer.replaceCodeInFile(fileContent, replaceCode, 3, 3, fakeDocument);
154+
expect(result).to.equal('class Test {\n String soql = [\n SELECT ID\n FROM\n ACCOUNT\n ]\n}');
155+
});
156+
157+
test('should replace code while preserving indentation for multiple lines for paranthesis', () => {
158+
const fileContent = 'class Test {\n String soql = (\n // soql\n )\n}';
159+
const replaceCode = 'SELECT ID\nFROM\nACCOUNT';
160+
(fakeDocument.lineAt as Sinon.SinonStub).callsFake((line: number) => ({
161+
text: fileContent.split("\n")[line]
162+
}));
163+
const result = fixer.replaceCodeInFile(fileContent, replaceCode, 3, 3, fakeDocument);
164+
expect(result).to.equal('class Test {\n String soql = (\n SELECT ID\n FROM\n ACCOUNT\n )\n}');
165+
});
166+
147167
test('should replace code for windows', () => {
148168
const fileContent = 'class Test {\r\n void method() {\r\n // code\r\n }\r\n}';
149169
const replaceCode = 'int x = 1;';

0 commit comments

Comments
 (0)