|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Vegard IT GmbH and others. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Sebastian Thomschke (Vegard IT GmbH) - initial implementation. |
| 11 | + *******************************************************************************/ |
| 12 | +package org.eclipse.lsp4e.test.format; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertTrue; |
| 15 | + |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.HashSet; |
| 18 | +import java.util.List; |
| 19 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 20 | + |
| 21 | +import org.eclipse.core.resources.IFile; |
| 22 | +import org.eclipse.core.resources.ResourceAttributes; |
| 23 | +import org.eclipse.jface.text.IDocument; |
| 24 | +import org.eclipse.jface.text.TextSelection; |
| 25 | +import org.eclipse.lsp4e.test.utils.AbstractTestWithProject; |
| 26 | +import org.eclipse.lsp4e.test.utils.TestUtils; |
| 27 | +import org.eclipse.lsp4e.tests.mock.MockLanguageServer; |
| 28 | +import org.eclipse.lsp4e.ui.Messages; |
| 29 | +import org.eclipse.lsp4j.Position; |
| 30 | +import org.eclipse.lsp4j.Range; |
| 31 | +import org.eclipse.lsp4j.TextEdit; |
| 32 | +import org.eclipse.swt.SWT; |
| 33 | +import org.eclipse.swt.widgets.Button; |
| 34 | +import org.eclipse.swt.widgets.Composite; |
| 35 | +import org.eclipse.swt.widgets.Control; |
| 36 | +import org.eclipse.swt.widgets.Display; |
| 37 | +import org.eclipse.swt.widgets.Event; |
| 38 | +import org.eclipse.swt.widgets.Shell; |
| 39 | +import org.eclipse.ui.PlatformUI; |
| 40 | +import org.eclipse.ui.handlers.IHandlerService; |
| 41 | +import org.eclipse.ui.texteditor.ITextEditor; |
| 42 | +import org.junit.Test; |
| 43 | + |
| 44 | +public class FormatHandlerReadOnlyTest extends AbstractTestWithProject { |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testFormatOnReadOnlyFileAndMakeWritable() throws Exception { |
| 48 | + // Mock formatting to prepend "//" at the start of each line |
| 49 | + var edits = List.of( // |
| 50 | + new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), "//"), |
| 51 | + new TextEdit(new Range(new Position(1, 0), new Position(1, 0)), "//")); |
| 52 | + MockLanguageServer.INSTANCE.setFormattingTextEdits(edits); |
| 53 | + |
| 54 | + String content = "line1\nline2\n"; |
| 55 | + IFile file = TestUtils.createUniqueTestFile(project, content); |
| 56 | + |
| 57 | + // Make file read-only |
| 58 | + ResourceAttributes attrs = file.getResourceAttributes(); |
| 59 | + attrs.setReadOnly(true); |
| 60 | + file.setResourceAttributes(attrs); |
| 61 | + assertTrue(file.getResourceAttributes().isReadOnly()); |
| 62 | + |
| 63 | + // Open editor and select the whole document |
| 64 | + var editor = TestUtils.openEditor(file); |
| 65 | + var textEditor = (ITextEditor) editor; |
| 66 | + IDocument doc = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); |
| 67 | + textEditor.getSelectionProvider().setSelection(new TextSelection(0, doc.getLength())); |
| 68 | + |
| 69 | + // Before executing the format command, set up a poller that clicks "Yes" |
| 70 | + // and records that the dialog was shown |
| 71 | + var display = Display.getDefault(); |
| 72 | + var beforeShells = new HashSet<>(Arrays.asList(display.getShells())); |
| 73 | + final var dialogShown = new AtomicBoolean(false); |
| 74 | + display.asyncExec(new Runnable() { |
| 75 | + Button findYesButton(Composite parent) { |
| 76 | + for (Control child : parent.getChildren()) { |
| 77 | + if (child instanceof Button button && button.getText().toLowerCase().contains("yes")) { |
| 78 | + return button; |
| 79 | + } |
| 80 | + if (child instanceof Composite composite) { |
| 81 | + Button result = findYesButton(composite); |
| 82 | + if (result != null) { |
| 83 | + return result; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void run() { |
| 92 | + Shell newShell = TestUtils.findNewShell(beforeShells, display); |
| 93 | + if (newShell == null || !Messages.LSPFormatHandler_ReadOnlyEditor_title.equals(newShell.getText())) { |
| 94 | + display.timerExec(50, this); |
| 95 | + return; |
| 96 | + } |
| 97 | + dialogShown.set(true); |
| 98 | + Button yes = findYesButton(newShell); |
| 99 | + if (yes != null) { |
| 100 | + yes.notifyListeners(SWT.Selection, new Event()); |
| 101 | + } |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + // Run format command which should prompt and then make the file writable |
| 106 | + IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class); |
| 107 | + handlerService.executeCommand("org.eclipse.lsp4e.format", null); |
| 108 | + |
| 109 | + // File was made writable and edits were applied |
| 110 | + TestUtils.waitForAndAssertCondition(5_000, dialogShown::get); |
| 111 | + TestUtils.waitForAndAssertCondition(5_000, () -> !file.getResourceAttributes().isReadOnly()); |
| 112 | + TestUtils.waitForAndAssertCondition(5_000, () -> "//line1\n//line2\n".equals(doc.get())); |
| 113 | + } |
| 114 | +} |
0 commit comments