-
Notifications
You must be signed in to change notification settings - Fork 67
feat: prompt user when attempting to format a read-only file #1381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/format/FormatHandlerReadOnlyTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2025 Vegard IT GmbH and others. | ||
| * This program and the accompanying materials are made | ||
| * available under the terms of the Eclipse Public License 2.0 | ||
| * which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * Sebastian Thomschke (Vegard IT GmbH) - initial implementation. | ||
| *******************************************************************************/ | ||
| package org.eclipse.lsp4e.test.format; | ||
|
|
||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| import org.eclipse.core.resources.IFile; | ||
| import org.eclipse.core.resources.ResourceAttributes; | ||
| import org.eclipse.jface.text.IDocument; | ||
| import org.eclipse.jface.text.TextSelection; | ||
| import org.eclipse.lsp4e.test.utils.AbstractTestWithProject; | ||
| import org.eclipse.lsp4e.test.utils.TestUtils; | ||
| import org.eclipse.lsp4e.tests.mock.MockLanguageServer; | ||
| import org.eclipse.lsp4e.ui.Messages; | ||
| import org.eclipse.lsp4j.Position; | ||
| import org.eclipse.lsp4j.Range; | ||
| import org.eclipse.lsp4j.TextEdit; | ||
| import org.eclipse.swt.SWT; | ||
| import org.eclipse.swt.widgets.Button; | ||
| import org.eclipse.swt.widgets.Composite; | ||
| import org.eclipse.swt.widgets.Control; | ||
| import org.eclipse.swt.widgets.Display; | ||
| import org.eclipse.swt.widgets.Event; | ||
| import org.eclipse.swt.widgets.Shell; | ||
| import org.eclipse.ui.PlatformUI; | ||
| import org.eclipse.ui.handlers.IHandlerService; | ||
| import org.eclipse.ui.texteditor.ITextEditor; | ||
| import org.junit.Test; | ||
|
|
||
| public class FormatHandlerReadOnlyTest extends AbstractTestWithProject { | ||
|
|
||
| @Test | ||
| public void testFormatOnReadOnlyFileAndMakeWritable() throws Exception { | ||
| // Mock formatting to prepend "//" at the start of each line | ||
| var edits = List.of( // | ||
| new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), "//"), | ||
| new TextEdit(new Range(new Position(1, 0), new Position(1, 0)), "//")); | ||
| MockLanguageServer.INSTANCE.setFormattingTextEdits(edits); | ||
|
|
||
| String content = "line1\nline2\n"; | ||
| IFile file = TestUtils.createUniqueTestFile(project, content); | ||
|
|
||
| // Make file read-only | ||
| ResourceAttributes attrs = file.getResourceAttributes(); | ||
| attrs.setReadOnly(true); | ||
| file.setResourceAttributes(attrs); | ||
| assertTrue(file.getResourceAttributes().isReadOnly()); | ||
|
|
||
| // Open editor and select the whole document | ||
| var editor = TestUtils.openEditor(file); | ||
| var textEditor = (ITextEditor) editor; | ||
| IDocument doc = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); | ||
| textEditor.getSelectionProvider().setSelection(new TextSelection(0, doc.getLength())); | ||
|
|
||
| // Before executing the format command, set up a poller that clicks "Yes" | ||
| // and records that the dialog was shown | ||
| var display = Display.getDefault(); | ||
| var beforeShells = new HashSet<>(Arrays.asList(display.getShells())); | ||
| final var dialogShown = new AtomicBoolean(false); | ||
| display.asyncExec(new Runnable() { | ||
| Button findYesButton(Composite parent) { | ||
| for (Control child : parent.getChildren()) { | ||
| if (child instanceof Button button && button.getText().toLowerCase().contains("yes")) { | ||
| return button; | ||
| } | ||
| if (child instanceof Composite composite) { | ||
| Button result = findYesButton(composite); | ||
| if (result != null) { | ||
| return result; | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| Shell newShell = TestUtils.findNewShell(beforeShells, display); | ||
| if (newShell == null || !Messages.LSPFormatHandler_ReadOnlyEditor_title.equals(newShell.getText())) { | ||
| display.timerExec(50, this); | ||
| return; | ||
| } | ||
| dialogShown.set(true); | ||
| Button yes = findYesButton(newShell); | ||
| if (yes != null) { | ||
| yes.notifyListeners(SWT.Selection, new Event()); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| // Run format command which should prompt and then make the file writable | ||
| IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class); | ||
| handlerService.executeCommand("org.eclipse.lsp4e.format", null); | ||
|
|
||
| // File was made writable and edits were applied | ||
| TestUtils.waitForAndAssertCondition(5_000, dialogShown::get); | ||
| TestUtils.waitForAndAssertCondition(5_000, () -> !file.getResourceAttributes().isReadOnly()); | ||
| TestUtils.waitForAndAssertCondition(5_000, () -> "//line1\n//line2\n".equals(doc.get())); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/ResourceUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2025 Vegard IT GmbH and others. | ||
| * This program and the accompanying materials are made | ||
| * available under the terms of the Eclipse Public License 2.0 | ||
| * which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * Sebastian Thomschke (Vegard IT GmbH) - initial implementation. | ||
| *******************************************************************************/ | ||
| package org.eclipse.lsp4e.internal; | ||
|
|
||
| import org.eclipse.core.resources.IFile; | ||
| import org.eclipse.core.resources.ResourceAttributes; | ||
| import org.eclipse.core.runtime.CoreException; | ||
|
|
||
| public class ResourceUtil { | ||
|
|
||
| public static void setWritable(final IFile file) throws CoreException { | ||
| ResourceAttributes attrs = file.getResourceAttributes(); | ||
| if (attrs != null) { | ||
| attrs.setReadOnly(false); | ||
| file.setResourceAttributes(attrs); | ||
| } | ||
| } | ||
|
|
||
| private ResourceUtil() { | ||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.