-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.ts
More file actions
165 lines (139 loc) · 5.22 KB
/
Copy pathextension.ts
File metadata and controls
165 lines (139 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as vscode from 'vscode';
import { JavaMethodSorter } from './sorter/javaMethodSorter';
import { SortingOptions } from './sorter/types';
// Extension ID for Red Hat Java Language Support
const REDHAT_JAVA_EXTENSION_ID = 'redhat.java';
/**
* Get sorting options from VS Code configuration
*/
function getSortingOptions(): SortingOptions {
const config = vscode.workspace.getConfiguration('tlcsdm.methodsorter');
return {
sortingStrategy: config.get<string>('sortingStrategy', 'depth-first'),
applyWorkingListHeuristics: config.get<boolean>('applyWorkingListHeuristics', true),
respectBeforeAfterRelation: config.get<boolean>('respectBeforeAfterRelation', true),
clusterOverloadedMethods: config.get<boolean>('clusterOverloadedMethods', false),
clusterGetterSetter: config.get<boolean>('clusterGetterSetter', false),
separateByAccessLevel: config.get<boolean>('separateByAccessLevel', true),
separateConstructors: config.get<boolean>('separateConstructors', true),
applyLexicalOrdering: config.get<boolean>('applyLexicalOrdering', true)
};
}
/**
* Check if the Red Hat Java extension is installed
*/
function isRedHatJavaExtensionAvailable(): boolean {
const extension = vscode.extensions.getExtension(REDHAT_JAVA_EXTENSION_ID);
return extension !== undefined;
}
/**
* Format the document using Red Hat Java extension if available
*/
async function formatDocumentWithRedHatJava(): Promise<void> {
if (!isRedHatJavaExtensionAvailable()) {
return;
}
try {
// Execute the format document command
await vscode.commands.executeCommand('editor.action.formatDocument');
} catch {
// Silently ignore formatting errors - the sorting is complete
}
}
/**
* Sort methods in the active Java editor
*/
async function sortMethods(): Promise<void> {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showWarningMessage('No active text editor');
return;
}
if (editor.document.languageId !== 'java') {
vscode.window.showWarningMessage('This command only works with Java files');
return;
}
const document = editor.document;
const text = document.getText();
try {
const options = getSortingOptions();
const sorter = new JavaMethodSorter(options);
const sortedText = sorter.sort(text);
if (sortedText === text) {
vscode.window.showInformationMessage('Methods are already sorted');
return;
}
const edit = new vscode.WorkspaceEdit();
const fullRange = new vscode.Range(
document.positionAt(0),
document.positionAt(text.length)
);
edit.replace(document.uri, fullRange, sortedText);
await vscode.workspace.applyEdit(edit);
// Format document with Red Hat Java extension if available
await formatDocumentWithRedHatJava();
vscode.window.showInformationMessage('Methods sorted successfully');
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to sort methods';
vscode.window.showErrorMessage(message);
}
}
/**
* Shuffle methods randomly in the active Java editor
*/
async function shuffleMethodsRandomly(): Promise<void> {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showWarningMessage('No active text editor');
return;
}
if (editor.document.languageId !== 'java') {
vscode.window.showWarningMessage('This command only works with Java files');
return;
}
const document = editor.document;
const text = document.getText();
try {
const sorter = new JavaMethodSorter(getSortingOptions());
const shuffledText = sorter.shuffleRandomly(text);
if (shuffledText === text) {
vscode.window.showInformationMessage('No methods to shuffle');
return;
}
const edit = new vscode.WorkspaceEdit();
const fullRange = new vscode.Range(
document.positionAt(0),
document.positionAt(text.length)
);
edit.replace(document.uri, fullRange, shuffledText);
await vscode.workspace.applyEdit(edit);
// Format document with Red Hat Java extension if available
await formatDocumentWithRedHatJava();
vscode.window.showInformationMessage('Methods shuffled randomly');
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to shuffle methods';
vscode.window.showErrorMessage(message);
}
}
/**
* Extension activation
*/
export function activate(context: vscode.ExtensionContext): void {
// Register commands
const sortMethodsCmd = vscode.commands.registerCommand(
'tlcsdm.methodsorter.sortMethods',
sortMethods
);
const shuffleMethodsCmd = vscode.commands.registerCommand(
'tlcsdm.methodsorter.shuffleMethodsRandomly',
shuffleMethodsRandomly
);
context.subscriptions.push(sortMethodsCmd, shuffleMethodsCmd);
console.log('Java Method Sorter extension is now active');
}
/**
* Extension deactivation
*/
export function deactivate(): void {
// Clean up resources if needed
}