Skip to content

Commit 5d24101

Browse files
committed
changed command ID; implemented menu and config.
implemented menu context option to create/open the test; config to define where to open the file
1 parent 8258edf commit 5d24101

File tree

3 files changed

+80
-44
lines changed

3 files changed

+80
-44
lines changed

package.json

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
{
22
"name": "vscode-java-tests",
33
"publisher": "wesleyegberto",
4-
"displayName": "vscode-java-tests",
4+
"displayName": "VSCode Java Tests",
55
"description": "Extension to help write Java tests",
66
"version": "0.0.2",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/wesleyegberto/vscode-java-tests.git"
10+
},
711
"engines": {
812
"vscode": "^1.47.0"
913
},
@@ -12,16 +16,37 @@
1216
"Snippets"
1317
],
1418
"activationEvents": [
15-
"onCommand:vscode-java-tests.createTestClass"
19+
"onLanguage:java",
20+
"onCommand:java.tests.createTestClass"
1621
],
1722
"main": "./out/extension.js",
1823
"contributes": {
1924
"commands": [
2025
{
21-
"command": "vscode-java-tests.createTestClass",
22-
"title": "Java Tests: Create Test Class"
26+
"command": "java.tests.createTestClass",
27+
"title": "Java Tests: Create/Open Test Class"
2328
}
24-
]
29+
],
30+
"menus": {
31+
"explorer/context": [
32+
{
33+
"when": "resourceLangId == java",
34+
"command": "java.tests.createTestClass",
35+
"group": "vscodeJavaTests@1"
36+
}
37+
]
38+
},
39+
"configuration": {
40+
"title": "Java Tests",
41+
"properties": {
42+
"javaTests.file.openLocation": {
43+
"type": "string",
44+
"default": "beside",
45+
"enum": ["beside", "currentGroup"],
46+
"description": "Controls where test class should be opened."
47+
}
48+
}
49+
}
2550
},
2651
"scripts": {
2752
"vscode:prepublish": "npm run compile",

src/extension.ts

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,63 @@ import { posix } from 'path';
44
import { generateTestClassFileContent } from './file-content-generator';
55

66
export function activate(context: vscode.ExtensionContext) {
7-
let disposable = vscode.commands.registerCommand('vscode-java-tests.createTestClass', createTestClass);
7+
console.debug('Java Tests - Extension loaded');
88

9-
context.subscriptions.push(disposable);
9+
context.subscriptions.push(
10+
vscode.commands.registerCommand('java.tests.createTestClass', createTestClass)
11+
);
1012
}
1113

1214
export function deactivate() {}
1315

14-
async function createTestClass() {
15-
const activeEditor = vscode.window.activeTextEditor;
16+
async function createTestClass(args: any) {
17+
let javaFileUri: vscode.Uri;
1618

17-
if (!activeEditor) {
18-
vscode.window.showWarningMessage("Please, open a Java file.");
19-
return;
20-
}
19+
if (args && args.scheme === 'file' && args.path) {
20+
javaFileUri = args as vscode.Uri;
2121

22-
if (!activeEditor.document.fileName.endsWith('.java')) {
23-
vscode.window.showWarningMessage('Only allowed to create test from a Java file');
24-
return;
25-
}
22+
} else {
23+
const activeEditor = vscode.window.activeTextEditor;
2624

27-
const javaFileUri = activeEditor.document.uri;
28-
const javaClassName = posix.basename(javaFileUri.path, '.java');
25+
if (!activeEditor) {
26+
vscode.window.showWarningMessage('Please, open a Java file.');
27+
return;
28+
}
2929

30-
const testClassName = `${javaClassName}Test`;
31-
const testFileUri = getTestFileUri(javaFileUri, testClassName);
30+
javaFileUri = activeEditor.document.uri;
31+
}
3232

33-
try {
34-
await vscode.workspace.fs.stat(testFileUri);
35-
showTestFile(testFileUri);
33+
if (!javaFileUri || !javaFileUri.path.endsWith('.java')) {
34+
vscode.window.showWarningMessage('Only allowed to create test from a Java file');
35+
return;
36+
}
3637

37-
} catch {
38-
const fileContent = generateTestClassFileContent(javaFileUri, javaClassName, testFileUri, testClassName);
39-
await vscode.workspace.fs.writeFile(testFileUri, fileContent);
38+
const javaClassName = posix.basename(javaFileUri.path, '.java');
4039

41-
showTestFile(testFileUri);
42-
}
40+
const testClassName = `${javaClassName}Test`;
41+
const testFileUri = getTestFileUri(javaFileUri, testClassName);
42+
43+
try {
44+
await vscode.workspace.fs.stat(testFileUri);
45+
showTestFile(testFileUri);
46+
} catch {
47+
const fileContent = generateTestClassFileContent(javaFileUri, javaClassName, testFileUri, testClassName);
48+
await vscode.workspace.fs.writeFile(testFileUri, fileContent);
49+
50+
showTestFile(testFileUri);
51+
}
4352
}
4453

4554
function showTestFile(testFileUri: vscode.Uri) {
46-
// TODO: extract configuration to define where to open it: beside (new editor group) or current editor group
47-
vscode.window.showTextDocument(testFileUri, { viewColumn: vscode.ViewColumn.Beside });
55+
const config = vscode.workspace.getConfiguration('javaTests');
56+
const configOpenLocationValue = config.get('file.openLocation', 'beside');
57+
vscode.window.showTextDocument(testFileUri, {
58+
viewColumn: configOpenLocationValue === 'beside' ? vscode.ViewColumn.Beside : vscode.ViewColumn.Active
59+
});
4860
}
4961

5062
function getTestFileUri(javaFileUri: vscode.Uri, testClassName: string) {
51-
const testPath = javaFileUri.path.replace('/src/main/java', '/src/test/java');
52-
const testFilePath = posix.join(testPath, '..', `${testClassName}.java`);
53-
return javaFileUri.with({ path: testFilePath });
63+
const testPath = javaFileUri.path.replace('/src/main/java', '/src/test/java');
64+
const testFilePath = posix.join(testPath, '..', `${testClassName}.java`);
65+
return javaFileUri.with({ path: testFilePath });
5466
}
55-

src/file-content-generator.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,25 @@ public class ${testClassName} {
4141
function generateTestClassPackageDeclaration(testFileUri: vscode.Uri, testClassName: string) {
4242
const packageName = extractPackageName(testFileUri, testClassName, true);
4343
if (!packageName.length) {
44-
return '';
44+
return '';
4545
}
4646
return `package ${packageName};`;
4747
}
4848

4949
function generateTargetTestClassPackageImport(javaFileUri: vscode.Uri, javaClassName: string) {
5050
const packageName = extractPackageName(javaFileUri, javaClassName, false);
5151
if (!packageName.length) {
52-
return '';
52+
return '';
5353
}
5454
return `import ${packageName}.${javaClassName};`;
5555
}
5656

5757
function extractPackageName(fileUri: vscode.Uri, fileClassName: string, isTest: boolean): string {
58-
const pathPrefix = isTest ? '/src/test/java' : '/src/main/java';
59-
const startIndex = fileUri.fsPath.indexOf(pathPrefix) + 15; // '/src/test/java/'.length
60-
const endIndex = fileUri.fsPath.indexOf(fileClassName) - 1;
61-
if (startIndex >= endIndex) {
62-
return '';
63-
}
64-
return fileUri.fsPath.substring(startIndex, endIndex).replace(/\//g, '.');
58+
const pathPrefix = isTest ? '/src/test/java' : '/src/main/java';
59+
const startIndex = fileUri.fsPath.indexOf(pathPrefix) + 15; // '/src/test/java/'.length
60+
const endIndex = fileUri.fsPath.indexOf(fileClassName) - 1;
61+
if (startIndex >= endIndex) {
62+
return '';
63+
}
64+
return fileUri.fsPath.substring(startIndex, endIndex).replace(/\//g, '.');
6565
}

0 commit comments

Comments
 (0)