Skip to content

Commit 67f214a

Browse files
Fix method spacing and add Red Hat Java formatter integration (#9)
* Initial plan * Fix blank lines between methods and add Red Hat Java formatting Co-authored-by: unknowIfGuestInDream <57802425+unknowIfGuestInDream@users.noreply.github.com> * Address code review feedback: fix comment and remove console.log Co-authored-by: unknowIfGuestInDream <57802425+unknowIfGuestInDream@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: unknowIfGuestInDream <57802425+unknowIfGuestInDream@users.noreply.github.com>
1 parent a370505 commit 67f214a

3 files changed

Lines changed: 83 additions & 3 deletions

File tree

src/extension.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import * as vscode from 'vscode';
22
import { JavaMethodSorter } from './sorter/javaMethodSorter';
33
import { SortingOptions } from './sorter/types';
44

5+
// Extension ID for Red Hat Java Language Support
6+
const REDHAT_JAVA_EXTENSION_ID = 'redhat.java';
7+
58
/**
69
* Get sorting options from VS Code configuration
710
*/
@@ -19,6 +22,30 @@ function getSortingOptions(): SortingOptions {
1922
};
2023
}
2124

25+
/**
26+
* Check if the Red Hat Java extension is installed
27+
*/
28+
function isRedHatJavaExtensionAvailable(): boolean {
29+
const extension = vscode.extensions.getExtension(REDHAT_JAVA_EXTENSION_ID);
30+
return extension !== undefined;
31+
}
32+
33+
/**
34+
* Format the document using Red Hat Java extension if available
35+
*/
36+
async function formatDocumentWithRedHatJava(): Promise<void> {
37+
if (!isRedHatJavaExtensionAvailable()) {
38+
return;
39+
}
40+
41+
try {
42+
// Execute the format document command
43+
await vscode.commands.executeCommand('editor.action.formatDocument');
44+
} catch {
45+
// Silently ignore formatting errors - the sorting is complete
46+
}
47+
}
48+
2249
/**
2350
* Sort methods in the active Java editor
2451
*/
@@ -54,6 +81,10 @@ async function sortMethods(): Promise<void> {
5481
);
5582
edit.replace(document.uri, fullRange, sortedText);
5683
await vscode.workspace.applyEdit(edit);
84+
85+
// Format document with Red Hat Java extension if available
86+
await formatDocumentWithRedHatJava();
87+
5788
vscode.window.showInformationMessage('Methods sorted successfully');
5889
} catch (error) {
5990
const message = error instanceof Error ? error.message : 'Failed to sort methods';
@@ -95,6 +126,10 @@ async function shuffleMethodsRandomly(): Promise<void> {
95126
);
96127
edit.replace(document.uri, fullRange, shuffledText);
97128
await vscode.workspace.applyEdit(edit);
129+
130+
// Format document with Red Hat Java extension if available
131+
await formatDocumentWithRedHatJava();
132+
98133
vscode.window.showInformationMessage('Methods shuffled randomly');
99134
} catch (error) {
100135
const message = error instanceof Error ? error.message : 'Failed to shuffle methods';

src/sorter/javaMethodSorter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,9 @@ export class JavaMethodSorter {
299299
return normalizedFullText;
300300
});
301301

302-
// Remove trailing whitespace from preContent to avoid duplicate indentation
303-
// since leadingContent already includes the proper indentation
304-
const normalizedPreContent = preContent.replace(/[ \t]+$/, '');
302+
// Normalize preContent: remove trailing whitespace and excess newlines
303+
// Add consistent single blank line before first method
304+
const normalizedPreContent = preContent.replace(/\s+$/, '') + '\n\n';
305305

306306
return normalizedPreContent + methodTexts.join('\n\n') + postContent;
307307
}

src/test/sorter.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,51 @@ public class MyClass {
425425
failed++;
426426
}
427427

428+
// Test 13: Multiple blank lines in source are normalized to single blank line
429+
try {
430+
const source = `public class MyClass {
431+
432+
433+
// Comment with 2 blank lines before it
434+
private void methodA() {
435+
System.out.println("A");
436+
}
437+
438+
439+
440+
// Comment with 3 blank lines before it
441+
public void methodB() {
442+
System.out.println("B");
443+
}
444+
}`;
445+
const options: SortingOptions = {
446+
sortingStrategy: 'depth-first',
447+
applyWorkingListHeuristics: false,
448+
respectBeforeAfterRelation: false,
449+
clusterOverloadedMethods: false,
450+
clusterGetterSetter: false,
451+
separateByAccessLevel: true,
452+
separateConstructors: false,
453+
applyLexicalOrdering: false
454+
};
455+
const sorter = new JavaMethodSorter(options);
456+
const sorted = sorter.sort(source);
457+
458+
// Should not have more than 2 consecutive newlines (1 blank line)
459+
const hasMultipleBlankLines = /\n{3,}/.test(sorted);
460+
if (!hasMultipleBlankLines) {
461+
console.log('✓ Test 13 passed: Multiple blank lines in source are normalized');
462+
passed++;
463+
} else {
464+
console.log('✗ Test 13 failed: Found multiple blank lines in output');
465+
console.log('Sorted output:', sorted);
466+
failed++;
467+
}
468+
} catch (e) {
469+
console.log('✗ Test 13 failed with error:', e);
470+
failed++;
471+
}
472+
428473
console.log(`\nResults: ${passed} passed, ${failed} failed`);
429474
}
430475

0 commit comments

Comments
 (0)