@@ -14,6 +14,12 @@ import { apiManager } from './apiManager';
1414
1515let serverReady : boolean = false ;
1616
17+ const BRACE_POSITION_KEY = "org.eclipse.jdt.core.formatter.brace_position_for_type_declaration" ;
18+ const END_OF_LINE = "end_of_line" ;
19+ const NEXT_LINE = "next_line" ;
20+ const NEXT_LINE_SHIFTED = "next_line_shifted" ;
21+
22+
1723export function setServerStatus ( ready : boolean ) {
1824 serverReady = ready ;
1925}
@@ -76,6 +82,14 @@ async function handleNewJavaFiles(e: FileCreateEvent) {
7682 // the event source, a workaround is to wait 100ms and let WorkspaceEdit
7783 // to take effect first, then check if the workingcopy is filled with content.
7884 const timeout = setTimeout ( async ( ) => {
85+ const editorConfig = workspace . getConfiguration ( 'editor' ) ;
86+ const insertSpaces = editorConfig . get < boolean > ( 'insertSpaces' , true ) ;
87+ const tabSize = editorConfig . get < number > ( 'tabSize' , 4 ) ;
88+ const indent = insertSpaces ? ' ' . repeat ( tabSize ) : '\t' ;
89+
90+ const projectSetting : { } = await commands . executeCommand < { } > ( Commands . EXECUTE_WORKSPACE_COMMAND , Commands . GET_PROJECT_SETTINGS , emptyFiles [ 0 ] . toString ( ) , [ BRACE_POSITION_KEY ] ) ;
91+ projectSetting [ BRACE_POSITION_KEY ] = projectSetting [ BRACE_POSITION_KEY ] || END_OF_LINE ;
92+
7993 const formatNumber = ( num => num > 9 ? String ( num ) : `0${ num } ` ) ;
8094 for ( let i = 0 ; i < emptyFiles . length ; i ++ ) {
8195 if ( textDocuments [ i ] . getText ( ) ) {
@@ -126,16 +140,37 @@ async function handleNewJavaFiles(e: FileCreateEvent) {
126140 snippets . push ( stringInterpolate ( template , context ) ) ;
127141 }
128142 }
129-
143+ let declaration : string ;
130144 if ( isModuleInfo ) {
131- snippets . push ( `module \${1:name} {` ) ;
145+ declaration = `module \${1:name}` ;
132146 } else if ( ! serverReady || await isVersionLessThan ( emptyFiles [ i ] . toString ( ) , 14 ) ) {
133- snippets . push ( `public \${1|class,interface,enum,abstract class,@interface|} ${ typeName } {` ) ;
147+ declaration = `public \${1|class,interface,enum,abstract class,@interface|} ${ typeName } ` ;
134148 } else {
135- snippets . push ( `public \${1|class ${ typeName } ,interface ${ typeName } ,enum ${ typeName } ,record ${ typeName } (),abstract class ${ typeName } ,@interface ${ typeName } |} {` ) ;
149+ declaration = `public \${1|class ${ typeName } ,interface ${ typeName } ,enum ${ typeName } ,record ${ typeName } (),abstract class ${ typeName } ,@interface ${ typeName } |}` ;
150+ }
151+ let bracePosition = projectSetting [ BRACE_POSITION_KEY ] ;
152+ if ( bracePosition !== END_OF_LINE && bracePosition !== NEXT_LINE && bracePosition !== NEXT_LINE_SHIFTED ) {
153+ bracePosition = END_OF_LINE ;
136154 }
137- snippets . push ( "\t${0}" ) ;
138- snippets . push ( "}" ) ;
155+
156+ let body = `${ indent } \${0}` ;
157+ if ( bracePosition === END_OF_LINE ) {
158+ snippets . push ( `${ declaration } {` ) ;
159+ } else if ( bracePosition === NEXT_LINE ) {
160+ snippets . push ( `${ declaration } ` ) ;
161+ snippets . push ( "{" ) ;
162+ } else if ( bracePosition === NEXT_LINE_SHIFTED ) {
163+ snippets . push ( declaration ) ;
164+ snippets . push ( `${ indent } {` ) ;
165+ body = `${ indent } ${ body } ` ;
166+ }
167+ snippets . push ( body ) ;
168+ if ( bracePosition === NEXT_LINE_SHIFTED ) {
169+ snippets . push ( `${ indent } }` ) ;
170+ } else {
171+ snippets . push ( "}" ) ;
172+ }
173+
139174 snippets . push ( "" ) ;
140175 }
141176 const textEditor = await window . showTextDocument ( textDocuments [ i ] ) ;
0 commit comments