Skip to content

Commit bca7d6d

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 02ffafb commit bca7d6d

File tree

3 files changed

+171
-1
lines changed

3 files changed

+171
-1
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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.IEditorPart;
40+
import org.eclipse.ui.IWorkbenchPage;
41+
import org.eclipse.ui.PlatformUI;
42+
import org.eclipse.ui.texteditor.ITextEditor;
43+
44+
public class ClipboardCompare extends BaseCompareAction{
45+
46+
private String clipboard = "Clipboard"; //$NON-NLS-1$
47+
48+
@Override
49+
protected void run(ISelection selection) {
50+
IFile[] files = Utilities.getFiles(selection);
51+
for (IFile file : files) {
52+
try {
53+
processComparison(file);
54+
} catch (Exception e) {
55+
Shell parentShell = CompareUIPlugin.getShell();
56+
MessageDialog.openError(parentShell, "Comparision Failed", e.getMessage()); //$NON-NLS-1$
57+
}
58+
}
59+
}
60+
@Override
61+
protected boolean isEnabled(ISelection selection) {
62+
return Utilities.getFiles(selection).length == 1 && getClipboard() != null;
63+
}
64+
65+
/**
66+
* Process comparison with selection or entire editor contents with contents in
67+
* clipboard
68+
*
69+
* @param file Editor file
70+
* @throws IOException, CoreException
71+
*/
72+
private void processComparison(IFile file) throws IOException, CoreException {
73+
String cb = getClipboard().toString();
74+
String fileName = file.getName();
75+
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
76+
IEditorPart editor = page.getActiveEditor();
77+
final String selectionContents;
78+
if (editor instanceof ITextEditor txtEditor) {
79+
ISelection selection = txtEditor.getSelectionProvider().getSelection();
80+
if (selection instanceof ITextSelection textSelection) {
81+
selectionContents = textSelection.getText();
82+
if (selectionContents.isEmpty()) {
83+
String fileContents = new String(file.getContents().readAllBytes(), file.getCharset());
84+
showComparison(fileContents, fileName, cb);
85+
} else {
86+
showComparison(selectionContents, fileName, cb);
87+
}
88+
return;
89+
}
90+
}
91+
}
92+
93+
/**
94+
* Shows comparison result
95+
*
96+
* @param source Either selection from current editor or entire
97+
* editor if no selection
98+
* @param fileName Editor file name
99+
* @param clipboardContents Contents in clipboard
100+
*/
101+
private void showComparison(String source, String fileName, String clipboardContents) {
102+
class ClipboardTypedElement implements ITypedElement, IStreamContentAccessor {
103+
private final String name;
104+
private final String content;
105+
106+
public ClipboardTypedElement(String name, String content) {
107+
this.name = name;
108+
this.content = content;
109+
}
110+
111+
@Override
112+
public String getName() {
113+
return name;
114+
}
115+
116+
@Override
117+
public Image getImage() {
118+
return null;
119+
}
120+
121+
@Override
122+
public String getType() {
123+
return null;
124+
}
125+
126+
@Override
127+
public InputStream getContents() throws CoreException {
128+
return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
129+
}
130+
131+
}
132+
CompareConfiguration config = new CompareConfiguration();
133+
config.setLeftLabel(fileName);
134+
config.setRightLabel(clipboard);
135+
config.setLeftEditable(true);
136+
config.setRightEditable(true);
137+
CompareEditorInput compareInput = new CompareEditorInput(config) {
138+
@Override
139+
protected Object prepareInput(IProgressMonitor monitor)
140+
throws InvocationTargetException, InterruptedException {
141+
return new DiffNode(new ClipboardTypedElement(fileName, source),
142+
new ClipboardTypedElement(clipboard, clipboardContents));
143+
144+
}
145+
};
146+
CompareUI.openCompareEditor(compareInput);
147+
}
148+
149+
/**
150+
* Returns Clipboard Object or null if there is nothing in clipboard
151+
*
152+
* @returns Clipboard Object or null
153+
*/
154+
private Object getClipboard() {
155+
Clipboard clip = new Clipboard(Display.getDefault());
156+
return clip.getContents(TextTransfer.getInstance());
157+
}
158+
159+
}

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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 8 additions & 0 deletions
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)