-
Notifications
You must be signed in to change notification settings - Fork 174
Add Compare with Clipboard action to Console #2923
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
Open
SougandhS
wants to merge
1
commit into
eclipse-platform:master
Choose a base branch
from
SougandhS:ConsoleCompare
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
145 changes: 145 additions & 0 deletions
145
...g.eclipse.ui.console/src/org/eclipse/ui/internal/console/CompareConsoleWithClipboard.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,145 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2026 IBM Corporation. | ||
| * | ||
| * This program and the accompanying materials | ||
| * are made available under the terms of the Eclipse Public License 2.0 | ||
| * which accompanies this distribution, and is available at | ||
| * https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * IBM Corporation - initial API and implementation | ||
| *******************************************************************************/ | ||
| package org.eclipse.ui.internal.console; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.InputStream; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| import org.eclipse.compare.CompareConfiguration; | ||
| import org.eclipse.compare.CompareEditorInput; | ||
| import org.eclipse.compare.CompareUI; | ||
| import org.eclipse.compare.IEncodedStreamContentAccessor; | ||
| import org.eclipse.compare.ITypedElement; | ||
| import org.eclipse.compare.structuremergeviewer.DiffNode; | ||
| import org.eclipse.core.runtime.IProgressMonitor; | ||
| import org.eclipse.core.runtime.Platform; | ||
| import org.eclipse.jface.action.Action; | ||
| import org.eclipse.jface.dialogs.MessageDialog; | ||
| import org.eclipse.jface.text.ITextSelection; | ||
| import org.eclipse.jface.viewers.ISelection; | ||
| import org.eclipse.swt.dnd.Clipboard; | ||
| import org.eclipse.swt.dnd.TextTransfer; | ||
| import org.eclipse.swt.graphics.Image; | ||
| import org.eclipse.swt.widgets.Display; | ||
| import org.eclipse.ui.PlatformUI; | ||
| import org.eclipse.ui.console.TextConsoleViewer; | ||
|
|
||
| public class CompareConsoleWithClipboard extends Action { | ||
|
|
||
| private final TextConsoleViewer console; | ||
|
SougandhS marked this conversation as resolved.
|
||
|
|
||
| public CompareConsoleWithClipboard(TextConsoleViewer consoleView) { | ||
| super(ConsoleMessages.CompareConsoleWithClipboard_0); | ||
| this.console = consoleView; | ||
| setToolTipText(ConsoleMessages.CompareConsoleWithClipboard_6); | ||
| PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IConsoleHelpContextIds.CONSOLE_COMPARE_ACTION); | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| ISelection selection = console.getSelection(); | ||
| if (selection instanceof ITextSelection textSelection) { | ||
| String selectedText = textSelection.getText(); | ||
| if (selectedText.isEmpty()) { | ||
| selectedText = console.getDocument().get(); | ||
| } | ||
| compareWithClipboard(ConsoleMessages.CompareConsoleWithClipboard_1, selectedText); | ||
| } | ||
| } | ||
|
|
||
| private static void compareWithClipboard(String leftLabel, String content) { | ||
| Object clipboardContents = getClipboard(); | ||
| if (!(clipboardContents instanceof String clipboardText)) { | ||
| MessageDialog.openInformation(Display.getDefault().getActiveShell(), ConsoleMessages.CompareConsoleWithClipboard_2, | ||
| ConsoleMessages.CompareConsoleWithClipboard_3); | ||
| return; | ||
| } | ||
|
|
||
| class StringTypedElement implements ITypedElement, IEncodedStreamContentAccessor { | ||
| private final String name; | ||
| private final String text; | ||
|
|
||
| StringTypedElement(String name, String text) { | ||
| this.name = name; | ||
| this.text = text; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public Image getImage() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public String getType() { | ||
| return ITypedElement.TEXT_TYPE; | ||
| } | ||
|
|
||
| @Override | ||
| public String getCharset() { | ||
| return StandardCharsets.UTF_8.name(); | ||
| } | ||
|
|
||
| @Override | ||
| public InputStream getContents() { | ||
| return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
| } | ||
|
|
||
| CompareConfiguration configuration = new CompareConfiguration(); | ||
| configuration.setLeftLabel(leftLabel); | ||
| configuration.setRightLabel(ConsoleMessages.CompareConsoleWithClipboard_4); | ||
| configuration.setLeftEditable(false); | ||
| configuration.setRightEditable(false); | ||
|
|
||
| CompareEditorInput input = new CompareEditorInput(configuration) { | ||
| @Override | ||
| protected Object prepareInput(IProgressMonitor monitor) | ||
| throws InvocationTargetException, InterruptedException { | ||
| return new DiffNode(new StringTypedElement(leftLabel, content), | ||
| new StringTypedElement(ConsoleMessages.CompareConsoleWithClipboard_4, clipboardText)); | ||
| } | ||
| }; | ||
|
|
||
| input.setTitle(ConsoleMessages.CompareConsoleWithClipboard_5); | ||
| CompareUI.openCompareEditor(input); | ||
| } | ||
|
|
||
| private static Object getClipboard() { | ||
| Clipboard clipboard = new Clipboard(Display.getDefault()); | ||
| try { | ||
| return clipboard.getContents(TextTransfer.getInstance()); | ||
| } finally { | ||
| clipboard.dispose(); | ||
| } | ||
| } | ||
|
|
||
| public static boolean isAvailable() { | ||
| org.osgi.framework.Bundle bundle = Platform.getBundle("org.eclipse.compare"); //$NON-NLS-1$ | ||
| return bundle != null && (bundle.getState() & (org.osgi.framework.Bundle.RESOLVED | ||
| | org.osgi.framework.Bundle.STARTING | org.osgi.framework.Bundle.ACTIVE)) != 0; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEnabled() { | ||
| return console.getDocument().getLength() > 0; | ||
| } | ||
| } | ||
|
|
||
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
Oops, something went wrong.
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.