@@ -4,52 +4,63 @@ import { posix } from 'path';
44import { generateTestClassFileContent } from './file-content-generator' ;
55
66export 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
1214export 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
4554function 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
5062function 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-
0 commit comments