Skip to content

Commit

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

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Apr 3, 2023
1 parent 4ba7f96 commit b2babdc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testFormattingInvalidDocument() throws Exception {
LSPFormatter formatter = new LSPFormatter();
ITextSelection selection = TextSelection.emptySelection();

Optional<VersionedEdits> edits = formatter.requestFormatting(new Document(), selection).get();
Optional<VersionedEdits> edits = formatter.requestFormatting(new Document(), selection, null).get();
assertTrue(edits.isEmpty());
}

Expand All @@ -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<VersionedEdits> edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection).get();
assertTrue(edits.isPresent());
editor.getSite().getShell().getDisplay().syncExec(() -> {
Optional<VersionedEdits> 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
Expand All @@ -105,27 +105,26 @@ 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<VersionedEdits> edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection).get();
Optional<VersionedEdits> 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) {
fail(e.getMessage());
}
});

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 All @@ -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<VersionedEdits> edits = formatter.requestFormatting(viewer.getDocument(), (ITextSelection) selection).get();
Optional<VersionedEdits> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.e4.core.commands.ExpressionContext;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextSelection;
Expand Down Expand Up @@ -84,9 +85,10 @@ protected void formatFile(final @NonNull IFile file, final IProgressMonitor moni
if (doc == null)
return;

IPreferenceStore filePreferenceStore = getFilePreferenceStore(file);
monitor.setTaskName(NLS.bind(Messages.LSPFormatFilesHandler_FormattingFile, file.getFullPath()));
final Optional<VersionedEdits> formatting = formatter.requestFormatting(doc,
new TextSelection(0, 0)).get(SINGLE_FILE_TIMEOUT_MS, TimeUnit.MILLISECONDS);
new TextSelection(0, 0), filePreferenceStore).get(SINGLE_FILE_TIMEOUT_MS, TimeUnit.MILLISECONDS);

formatting.ifPresent(edits -> {
docProvider.aboutToChange(doc);
Expand Down Expand Up @@ -114,6 +116,12 @@ protected void formatFile(final @NonNull IFile file, final IProgressMonitor moni
}
}

private static IPreferenceStore getFilePreferenceStore(@NonNull IFile file) {
// TODO : use preference extension point to get the proper preference store of the file
// See https://github.com/eclipse-platform/eclipse.platform.text/pull/130
return null;
}

protected IDocumentProvider getDocumentProvider(IFile file) {
return DocumentProviderRegistry.getDefault().getDocumentProvider(new FileEditorInput(file));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
Expand Down Expand Up @@ -50,7 +51,8 @@ public Object execute(final ExecutionEvent event) throws ExecutionException {
return null;

try {
formatter.requestFormatting(doc, textSelection)
IPreferenceStore editorPreferenceStore = textEditor.getAdapter(IPreferenceStore.class);
formatter.requestFormatting(doc, textSelection, editorPreferenceStore)
.thenAcceptAsync(result -> {
result.ifPresent(edits -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;

public class LSPFormatter {
public CompletableFuture<Optional<VersionedEdits>> requestFormatting(@NonNull IDocument document, @NonNull ITextSelection textSelection) throws BadLocationException {

public CompletableFuture<Optional<VersionedEdits>> requestFormatting(@NonNull IDocument document, @NonNull ITextSelection textSelection, IPreferenceStore preferenceStore) 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(preferenceStore);
TextDocumentIdentifier docId = new TextDocumentIdentifier(uri.toString());

DocumentRangeFormattingParams rangeParams = getRangeFormattingParams(document, textSelection, formatOptions,
Expand Down Expand Up @@ -90,13 +91,26 @@ 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(IPreferenceStore editorPreferenceStore) {
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());
}
Expand Down

0 comments on commit b2babdc

Please sign in to comment.