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
azerr committed Apr 3, 2023
1 parent e49962d commit 24210fe
Show file tree
Hide file tree
Showing 4 changed files with 41 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 @@ -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<VersionedEdits> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Optional<VersionedEdits>> requestFormatting(@NonNull IDocument document, @NonNull ITextSelection textSelection) throws BadLocationException {

public CompletableFuture<Optional<VersionedEdits>> 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,
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit 24210fe

Please sign in to comment.