Skip to content

Commit 0a132e9

Browse files
committed
Add support to compare selection or editor with clipboard
Allows users to compare either the selected text or the entire editor content against the current clipboard contents.
1 parent 27a1efe commit 0a132e9

File tree

3 files changed

+183
-1
lines changed

3 files changed

+183
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 IBM Corporation and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* IBM Corporation - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.compare.internal;
15+
16+
import java.io.ByteArrayInputStream;
17+
import java.io.IOException;
18+
import java.io.InputStream;
19+
import java.lang.reflect.InvocationTargetException;
20+
import java.nio.charset.StandardCharsets;
21+
22+
import org.eclipse.compare.CompareConfiguration;
23+
import org.eclipse.compare.CompareEditorInput;
24+
import org.eclipse.compare.CompareUI;
25+
import org.eclipse.compare.IStreamContentAccessor;
26+
import org.eclipse.compare.ITypedElement;
27+
import org.eclipse.compare.structuremergeviewer.DiffNode;
28+
import org.eclipse.core.resources.IFile;
29+
import org.eclipse.core.runtime.CoreException;
30+
import org.eclipse.core.runtime.IProgressMonitor;
31+
import org.eclipse.jface.dialogs.MessageDialog;
32+
import org.eclipse.jface.text.ITextSelection;
33+
import org.eclipse.jface.viewers.ISelection;
34+
import org.eclipse.swt.dnd.Clipboard;
35+
import org.eclipse.swt.dnd.TextTransfer;
36+
import org.eclipse.swt.graphics.Image;
37+
import org.eclipse.swt.widgets.Display;
38+
import org.eclipse.swt.widgets.Shell;
39+
import org.eclipse.ui.IEditorInput;
40+
import org.eclipse.ui.IEditorPart;
41+
import org.eclipse.ui.IFileEditorInput;
42+
import org.eclipse.ui.IWorkbenchPage;
43+
import org.eclipse.ui.PlatformUI;
44+
import org.eclipse.ui.texteditor.ITextEditor;
45+
46+
public class ClipboardCompare extends BaseCompareAction{
47+
48+
private String clipboard = "Clipboard"; //$NON-NLS-1$
49+
50+
@Override
51+
protected void run(ISelection selection) {
52+
IFile[] files = Utilities.getFiles(selection);
53+
for (IFile file : files) {
54+
try {
55+
processComparison(file);
56+
} catch (Exception e) {
57+
Shell parentShell = CompareUIPlugin.getShell();
58+
MessageDialog.openError(parentShell, "Comparision Failed", e.getMessage()); //$NON-NLS-1$
59+
}
60+
}
61+
}
62+
@Override
63+
protected boolean isEnabled(ISelection selection) {
64+
return Utilities.getFiles(selection).length == 1 && getClipboard() != null;
65+
}
66+
67+
/**
68+
* Process comparison with selection or entire editor contents with contents in
69+
* clipboard
70+
*
71+
* @param file Editor file
72+
* @throws IOException, CoreException
73+
*/
74+
private void processComparison(IFile file) throws IOException, CoreException {
75+
String cb = getClipboard().toString();
76+
String fileName = file.getName();
77+
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
78+
IEditorPart editor = page.getActiveEditor();
79+
IEditorInput input = editor.getEditorInput();
80+
if (input instanceof IFileEditorInput ed) {
81+
IFile file2 = ed.getFile();
82+
String fileName2 = file2.getName();
83+
if (!file.getName().equals(fileName2)) {
84+
String fileContents = new String(file.getContents().readAllBytes(), file.getCharset());
85+
showComparison(fileContents, fileName, cb);
86+
return;
87+
}
88+
}
89+
final String selectionContents;
90+
if (editor instanceof ITextEditor txtEditor) {
91+
ISelection selection = txtEditor.getSelectionProvider().getSelection();
92+
if (selection instanceof ITextSelection textSelection) {
93+
selectionContents = textSelection.getText();
94+
if (selectionContents.isEmpty()) {
95+
String fileContents = new String(file.getContents().readAllBytes(), file.getCharset());
96+
showComparison(fileContents, fileName, cb);
97+
} else {
98+
showComparison(selectionContents, fileName, cb);
99+
}
100+
return;
101+
}
102+
}
103+
}
104+
105+
/**
106+
* Shows comparison result
107+
*
108+
* @param source Either selection from current editor or entire
109+
* editor if no selection
110+
* @param fileName Editor file name
111+
* @param clipboardContents Contents in clipboard
112+
*/
113+
private void showComparison(String source, String fileName, String clipboardContents) {
114+
class ClipboardTypedElement implements ITypedElement, IStreamContentAccessor {
115+
private final String name;
116+
private final String content;
117+
118+
public ClipboardTypedElement(String name, String content) {
119+
this.name = name;
120+
this.content = content;
121+
}
122+
123+
@Override
124+
public String getName() {
125+
return name;
126+
}
127+
128+
@Override
129+
public Image getImage() {
130+
return null;
131+
}
132+
133+
@Override
134+
public String getType() {
135+
return null;
136+
}
137+
138+
@Override
139+
public InputStream getContents() throws CoreException {
140+
return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
141+
}
142+
143+
}
144+
CompareConfiguration config = new CompareConfiguration();
145+
config.setLeftLabel(fileName);
146+
config.setRightLabel(clipboard);
147+
config.setLeftEditable(true);
148+
config.setRightEditable(true);
149+
CompareEditorInput compareInput = new CompareEditorInput(config) {
150+
@Override
151+
protected Object prepareInput(IProgressMonitor monitor)
152+
throws InvocationTargetException, InterruptedException {
153+
return new DiffNode(new ClipboardTypedElement(fileName, source),
154+
new ClipboardTypedElement(clipboard, clipboardContents));
155+
156+
}
157+
};
158+
CompareUI.openCompareEditor(compareInput);
159+
}
160+
161+
/**
162+
* Returns Clipboard Object or null if there is nothing in clipboard
163+
*
164+
* @returns Clipboard Object or null
165+
*/
166+
private Object getClipboard() {
167+
Clipboard clip = new Clipboard(Display.getDefault());
168+
return clip.getContents(TextTransfer.getInstance());
169+
}
170+
171+
}

team/bundles/org.eclipse.compare/plugin.properties

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
###############################################################################
2-
# Copyright (c) 2000, 2016 IBM Corporation and others.
2+
# Copyright (c) 2000, 2025 IBM Corporation and others.
33
#
44
# This program and the accompanying materials
55
# are made available under the terms of the Eclipse Public License 2.0
@@ -98,6 +98,9 @@ CompareWithOtherResource.tooltip= Open the 'Compare With' Dialog
9898
CompareWithHistoryAction.label= &Local History...
9999
CompareWithHistoryAction.tooltip= Compare the Selected Resource with Local History
100100

101+
CompareWithClipboardAction.label= Clipboard
102+
CompareWithClipboardAction.tooltip= Compare the selection or entire contents in editor with contents in clipboard
103+
101104
ReplaceWithMenu.label= Rep&lace With
102105

103106
ReplaceFromHistoryAction.label= &Local History...

team/bundles/org.eclipse.compare/plugin.xml

+8
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,14 @@
309309
name="compareWithGroup">
310310
</separator>
311311
</menu>
312+
<action
313+
label="%CompareWithClipboardAction.label"
314+
tooltip="%CompareWithClipboardAction.tooltip"
315+
class="org.eclipse.compare.internal.ClipboardCompare"
316+
menubarPath="compareWithMenu/compareWithGroup"
317+
enablesFor="1"
318+
id="compareWithClipboard ">
319+
</action>
312320
<action
313321
label="%CompareWithHistoryAction.label"
314322
tooltip="%CompareWithHistoryAction.tooltip"

0 commit comments

Comments
 (0)