diff --git a/src/extension.ts b/src/extension.ts index aa16bc0f6..3c6eb86b1 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -160,7 +160,7 @@ export function activate(context: ExtensionContext): Promise { { scheme: 'untitled', language: 'java' } ], synchronize: { - configurationSection: 'java', + configurationSection: ['java', 'editor.insertSpaces', 'editor.tabSize'], }, initializationOptions: { bundles: collectJavaExtensions(extensions.all), @@ -403,6 +403,9 @@ export function getJavaConfig(javaHome: string) { // Since output path is a project specific setting. To avoid pollute other project, // we avoid reading the value from the global scope. javaConfig.project.outputPath = origConfig.inspect("project.outputPath").workspaceValue; + const editorConfig = workspace.getConfiguration('editor'); + javaConfig.format.insertSpaces = editorConfig.get('insertSpaces'); + javaConfig.format.tabSize = editorConfig.get('tabSize'); return javaConfig; } @@ -673,48 +676,13 @@ async function addFormatter(extensionPath, formatterUrl, defaultFormatter, relat }); } -export async function applyWorkspaceEdit(obj, languageClient) { +export function applyWorkspaceEdit(obj, languageClient) { const edit = languageClient.protocol2CodeConverter.asWorkspaceEdit(obj); if (edit) { - await workspace.applyEdit(edit); - // By executing the range formatting command to correct the indention according to the VS Code editor settings. - // More details, see: https://github.com/redhat-developer/vscode-java/issues/557 - try { - const currentEditor = window.activeTextEditor; - // If the Uri path of the edit change is not equal to that of the active editor, we will skip the range formatting - if (currentEditor.document.uri.fsPath !== edit.entries()[0][0].fsPath) { - return; - } - const cursorPostion = currentEditor.selection.active; - // Get the array of all the changes - const changes = edit.entries()[0][1]; - // Get the position information of the first change - let startPosition = new Position(changes[0].range.start.line, changes[0].range.start.character); - let lineOffsets = changes[0].newText.split(/\r?\n/).length - 1; - for (let i = 1; i < changes.length; i++) { - // When it comes to a discontinuous range, execute the range formatting and record the new start position - if (changes[i].range.start.line !== startPosition.line) { - await executeRangeFormat(currentEditor, startPosition, lineOffsets); - startPosition = new Position(changes[i].range.start.line, changes[i].range.start.character); - lineOffsets = 0; - } - lineOffsets += changes[i].newText.split(/\r?\n/).length - 1; - } - await executeRangeFormat(currentEditor, startPosition, lineOffsets); - // Recover the cursor's original position - currentEditor.selection = new Selection(cursorPostion, cursorPostion); - } catch (error) { - languageClient.error(error); - } + workspace.applyEdit(edit); } } -async function executeRangeFormat(editor, startPosition, lineOffset) { - const endPosition = editor.document.positionAt(editor.document.offsetAt(new Position(startPosition.line + lineOffset + 1, 0)) - 1); - editor.selection = new Selection(startPosition, endPosition); - await commands.executeCommand('editor.action.formatSelection'); -} - async function getTriggerFiles(): Promise { const openedJavaFiles = []; const activeJavaFile = getJavaFilePathOfTextDocument(window.activeTextEditor && window.activeTextEditor.document);