Skip to content

Commit

Permalink
Get tabWidth / insertSpaces from editor preferences store
Browse files Browse the repository at this point in the history
Fixes #245

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
azerr committed Nov 1, 2022
1 parent c685985 commit 5db3d86
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -56,7 +55,7 @@ public void testFormattingInvalidDocument() throws InterruptedException, Executi
LSPFormatter formatter = new LSPFormatter();
ITextSelection selection = TextSelection.emptySelection();

List<? extends TextEdit> edits = formatter.requestFormatting(new Document(), selection).get();
List<? extends TextEdit> edits = formatter.requestFormatting(new Document(), selection, null).get();
assertEquals(0, edits.size());
}

Expand All @@ -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<? extends TextEdit> edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection)
List<? extends TextEdit> 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
Expand All @@ -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<? extends TextEdit> edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection)
List<? extends TextEdit> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -49,7 +51,7 @@ public void applyEdits(IDocument document, List<? extends TextEdit> edits) {
}

public CompletableFuture<List<? extends TextEdit>> 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()) {
Expand All @@ -58,20 +60,20 @@ public CompletableFuture<List<? extends TextEdit>> 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);
}
return CompletableFuture.completedFuture(Collections.emptyList());
}

private CompletableFuture<List<? extends TextEdit>> 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)
Expand All @@ -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());
}
Expand Down

0 comments on commit 5db3d86

Please sign in to comment.