Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Surround with try/catch reformats document #1631

Merged
merged 1 commit into from
Feb 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 6 additions & 38 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
{ scheme: 'untitled', language: 'java' }
],
synchronize: {
configurationSection: 'java',
configurationSection: ['java', 'editor.insertSpaces', 'editor.tabSize'],
},
initializationOptions: {
bundles: collectJavaExtensions(extensions.all),
Expand Down Expand Up @@ -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<string>("project.outputPath").workspaceValue;
const editorConfig = workspace.getConfiguration('editor');
javaConfig.format.insertSpaces = editorConfig.get('insertSpaces');
javaConfig.format.tabSize = editorConfig.get('tabSize');
Comment on lines +406 to +408
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add "editor.insertSpaces" and "editor.tabSize" to the synchronized configuration sections as well.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

return javaConfig;
}

Expand Down Expand Up @@ -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:/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<string[]> {
const openedJavaFiles = [];
const activeJavaFile = getJavaFilePathOfTextDocument(window.activeTextEditor && window.activeTextEditor.document);
Expand Down