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 1cb3c98ec..7ec4b9598 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 @@ -64,7 +64,7 @@ public void testFormattingInvalidDocument() throws Exception { LSPFormatter formatter = new LSPFormatter(); ITextSelection selection = TextSelection.emptySelection(); - Optional edits = formatter.requestFormatting(new Document(), selection).get(); + Optional edits = formatter.requestFormatting(new Document(), selection, null).get(); assertTrue(edits.isEmpty()); } @@ -73,26 +73,26 @@ public void testFormattingNoChanges() throws Exception { 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(); - Optional edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection).get(); - assertTrue(edits.isPresent()); - editor.getSite().getShell().getDisplay().syncExec(() -> { + Optional edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection, textEditor).get(); + assertTrue(textEditor.isPresent()); + textEditor.getSite().getShell().getDisplay().syncExec(() -> { try { edits.get().apply(); } catch (ConcurrentModificationException | BadLocationException e) { fail(e.getMessage()); } }); - 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 @@ -105,15 +105,15 @@ 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(); - Optional edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection).get(); + Optional edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection, textEditor).get(); assertTrue(edits.isPresent()); - editor.getSite().getShell().getDisplay().syncExec(() -> { + textEditor.getSite().getShell().getDisplay().syncExec(() -> { try { edits.get().apply(); } catch (ConcurrentModificationException | BadLocationException e) { @@ -121,11 +121,10 @@ public void testFormatting() } }); - 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 @@ -134,20 +133,20 @@ public void testOutdatedFormatting() 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(); - Optional edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection).get(); + Optional edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection, textEditor).get(); assertTrue(edits.isPresent()); viewer.getDocument().replace(0, 0, "Hello"); waitForAndAssertCondition(1_000, numberOfChangesIs(1)); assertThrows(ConcurrentModificationException.class, () -> edits.get().apply()); - TestUtils.closeEditor(editor, false); + TestUtils.closeEditor(textEditor, false); } } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatFilesHandler.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatFilesHandler.java index a8c5a11c6..81ccf2b94 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatFilesHandler.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatFilesHandler.java @@ -86,7 +86,7 @@ protected void formatFile(final @NonNull IFile file, final IProgressMonitor moni monitor.setTaskName(NLS.bind(Messages.LSPFormatFilesHandler_FormattingFile, file.getFullPath())); final Optional formatting = formatter.requestFormatting(doc, - new TextSelection(0, 0)).get(SINGLE_FILE_TIMEOUT_MS, TimeUnit.MILLISECONDS); + new TextSelection(0, 0), null).get(SINGLE_FILE_TIMEOUT_MS, TimeUnit.MILLISECONDS); formatting.ifPresent(edits -> { docProvider.aboutToChange(doc); 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 a5d75e956..cd77a100c 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(final ExecutionEvent event) throws ExecutionException { return null; try { - formatter.requestFormatting(doc, textSelection) + formatter.requestFormatting(doc, textSelection, textEditor) .thenAcceptAsync(result -> { result.ifPresent(edits -> { try { 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 3d0c3bf5e..7271fcb6b 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 @@ -36,15 +36,17 @@ import org.eclipse.lsp4j.TextDocumentIdentifier; import org.eclipse.ui.editors.text.EditorsUI; import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants; +import org.eclipse.ui.texteditor.ITextEditor; public class LSPFormatter { - public CompletableFuture> requestFormatting(@NonNull IDocument document, @NonNull ITextSelection textSelection) throws BadLocationException { + + public CompletableFuture> requestFormatting(@NonNull IDocument document, @NonNull ITextSelection textSelection, ITextEditor textEditor) throws BadLocationException { URI uri = LSPEclipseUtils.toUri(document); if (uri == null) { return CompletableFuture.completedFuture(Optional.empty()); } LanguageServerDocumentExecutor executor = LanguageServers.forDocument(document).withFilter(LSPFormatter::supportsFormatting); - FormattingOptions formatOptions = getFormatOptions(); + FormattingOptions formatOptions = getFormatOptions(textEditor); TextDocumentIdentifier docId = new TextDocumentIdentifier(uri.toString()); DocumentRangeFormattingParams rangeParams = getRangeFormattingParams(document, textSelection, formatOptions, @@ -90,13 +92,27 @@ private DocumentRangeFormattingParams getRangeFormattingParams(IDocument documen return rangeParams; } - private FormattingOptions getFormatOptions() { - IPreferenceStore store = EditorsUI.getPreferenceStore(); - int tabWidth = store.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH); - boolean insertSpaces = store.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS); + private static FormattingOptions getFormatOptions(@NonNull ITextEditor textEditor) { + IPreferenceStore editorPreferenceStore = textEditor != null ? textEditor.getAdapter(IPreferenceStore.class) : null; + int tabWidth = getInt(editorPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH); + boolean insertSpaces = getBoolean(editorPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS); return new FormattingOptions(tabWidth, insertSpaces); } + 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()); }