From 5db3d86cac2229f133998e8fdd9e86173f11e024 Mon Sep 17 00:00:00 2001 From: azerr Date: Fri, 28 Oct 2022 09:10:22 +0200 Subject: [PATCH] Get tabWidth / insertSpaces from editor preferences store Fixes #245 Signed-off-by: azerr --- .../eclipse/lsp4e/test/format/FormatTest.java | 27 ++++++++---------- .../operations/format/LSPFormatHandler.java | 2 +- .../lsp4e/operations/format/LSPFormatter.java | 28 +++++++++++++++---- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/format/FormatTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/format/FormatTest.java index ef5925b85..fde89b16a 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/format/FormatTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/format/FormatTest.java @@ -11,7 +11,7 @@ *******************************************************************************/ package org.eclipse.lsp4e.test.format; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import java.util.ArrayList; import java.util.Collections; @@ -35,7 +35,6 @@ import org.eclipse.lsp4j.Position; import org.eclipse.lsp4j.Range; import org.eclipse.lsp4j.TextEdit; -import org.eclipse.ui.IEditorPart; import org.eclipse.ui.texteditor.ITextEditor; import org.junit.Before; import org.junit.Rule; @@ -56,7 +55,7 @@ public void testFormattingInvalidDocument() throws InterruptedException, Executi LSPFormatter formatter = new LSPFormatter(); ITextSelection selection = TextSelection.emptySelection(); - List edits = formatter.requestFormatting(new Document(), selection).get(); + List edits = formatter.requestFormatting(new Document(), selection, null).get(); assertEquals(0, edits.size()); } @@ -66,21 +65,20 @@ public void testFormattingNoChanges() MockLanguageServer.INSTANCE.setFormattingTextEdits(Collections.emptyList()); IFile file = TestUtils.createUniqueTestFile(project, "Formatting Other Text"); - IEditorPart editor = TestUtils.openEditor(file); - ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor); + ITextEditor textEditor = (ITextEditor) TestUtils.openEditor(file); + ITextViewer viewer = LSPEclipseUtils.getTextViewer(textEditor); LSPFormatter formatter = new LSPFormatter(); ISelection selection = viewer.getSelectionProvider().getSelection(); - List edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection) + List edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection, textEditor) .get(); - editor.getSite().getShell().getDisplay().syncExec(() -> formatter.applyEdits(viewer.getDocument(), edits)); + textEditor.getSite().getShell().getDisplay().syncExec(() -> formatter.applyEdits(viewer.getDocument(), edits)); - ITextEditor textEditor = (ITextEditor) editor; textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); assertEquals("Formatting Other Text", viewer.getDocument().get()); - TestUtils.closeEditor(editor, false); + TestUtils.closeEditor(textEditor, false); } @Test @@ -93,21 +91,20 @@ public void testFormatting() MockLanguageServer.INSTANCE.setFormattingTextEdits(formattingTextEdits); IFile file = TestUtils.createUniqueTestFile(project, "Formatting Other Text"); - IEditorPart editor = TestUtils.openEditor(file); - ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor); + ITextEditor textEditor = (ITextEditor) TestUtils.openEditor(file); + ITextViewer viewer = LSPEclipseUtils.getTextViewer(textEditor); LSPFormatter formatter = new LSPFormatter(); ISelection selection = viewer.getSelectionProvider().getSelection(); - List edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection) + List edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection, textEditor) .get(); - editor.getSite().getShell().getDisplay().syncExec(() -> formatter.applyEdits(viewer.getDocument(), edits)); + textEditor.getSite().getShell().getDisplay().syncExec(() -> formatter.applyEdits(viewer.getDocument(), edits)); - ITextEditor textEditor = (ITextEditor) editor; textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); assertEquals("MyFormattingOther Text Second", viewer.getDocument().get()); - TestUtils.closeEditor(editor, false); + TestUtils.closeEditor(textEditor, false); } @Test diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatHandler.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatHandler.java index c369609f0..c8ee04ea6 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatHandler.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatHandler.java @@ -50,7 +50,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException { final Shell shell = textEditor.getSite().getShell(); ISelection selection = HandlerUtil.getCurrentSelection(event); if (document != null && selection instanceof ITextSelection textSelection) { - formatter.requestFormatting(document, textSelection).thenAcceptAsync( + formatter.requestFormatting(document, textSelection, textEditor).thenAcceptAsync( edits -> shell.getDisplay().asyncExec(() -> formatter.applyEdits(document, edits))); } } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatter.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatter.java index aadbf7c5a..fa860063a 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatter.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatter.java @@ -19,6 +19,7 @@ import java.util.concurrent.CompletableFuture; import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; @@ -37,6 +38,7 @@ import org.eclipse.lsp4j.TextEdit; import org.eclipse.ui.editors.text.EditorsUI; import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants; +import org.eclipse.ui.texteditor.ITextEditor; public class LSPFormatter { @@ -49,7 +51,7 @@ public void applyEdits(IDocument document, List edits) { } public CompletableFuture> requestFormatting(@NonNull IDocument document, - @NonNull ITextSelection textSelection) { + @NonNull ITextSelection textSelection, @NonNull ITextEditor textEditor) { Collection<@NonNull LSPDocumentInfo> infos = LanguageServiceAccessor.getLSPDocumentInfosFor(document, LSPFormatter::supportFormatting); if (infos.isEmpty()) { @@ -58,7 +60,7 @@ public CompletableFuture> requestFormatting(@NonNull ID // TODO consider a better strategy for that, maybe iterate on all LS until one gives a result LSPDocumentInfo info = infos.iterator().next(); try { - return requestFormatting(info, textSelection); + return requestFormatting(info, textSelection, textEditor); } catch (BadLocationException e) { LanguageServerPlugin.logError(e); } @@ -66,12 +68,12 @@ public CompletableFuture> requestFormatting(@NonNull ID } private CompletableFuture> requestFormatting(LSPDocumentInfo info, - ITextSelection textSelection) throws BadLocationException { + ITextSelection textSelection, ITextEditor textEditor) throws BadLocationException { TextDocumentIdentifier docId = new TextDocumentIdentifier(info.getFileUri().toString()); ServerCapabilities capabilities = info.getCapabilites(); - IPreferenceStore store = EditorsUI.getPreferenceStore(); - int tabWidth = store.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH); - boolean insertSpaces = store.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS); + @Nullable IPreferenceStore editorPreferenceStore = textEditor.getAdapter(IPreferenceStore.class); + int tabWidth = getInt(editorPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH); + boolean insertSpaces = getBoolean(editorPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS); // use range formatting if standard formatting is not supported or text is selected if (capabilities != null && isDocumentRangeFormattingSupported(capabilities) @@ -97,6 +99,20 @@ && isDocumentRangeFormattingSupported(capabilities) .thenComposeAsync(server -> server.getTextDocumentService().formatting(params)); } + private static int getInt(IPreferenceStore editorPreferenceStore, String name) { + if (editorPreferenceStore != null && editorPreferenceStore.contains(name)) { + return editorPreferenceStore.getInt(name); + } + return EditorsUI.getPreferenceStore().getInt(name); + } + + private static boolean getBoolean(IPreferenceStore editorPreferenceStore, String name) { + if (editorPreferenceStore != null && editorPreferenceStore.contains(name)) { + return editorPreferenceStore.getBoolean(name); + } + return EditorsUI.getPreferenceStore().getBoolean(name); + } + private static boolean isDocumentRangeFormattingSupported(ServerCapabilities capabilities) { return LSPEclipseUtils.hasCapability(capabilities.getDocumentRangeFormattingProvider()); }