22
22
import org .eclipse .compare .CompareConfiguration ;
23
23
import org .eclipse .compare .CompareEditorInput ;
24
24
import org .eclipse .compare .CompareUI ;
25
+ import org .eclipse .compare .IEditableContent ;
25
26
import org .eclipse .compare .IStreamContentAccessor ;
26
27
import org .eclipse .compare .ITypedElement ;
28
+ import org .eclipse .compare .ResourceNode ;
27
29
import org .eclipse .compare .structuremergeviewer .DiffNode ;
28
30
import org .eclipse .core .resources .IFile ;
31
+ import org .eclipse .core .resources .IResource ;
29
32
import org .eclipse .core .runtime .CoreException ;
30
33
import org .eclipse .core .runtime .IProgressMonitor ;
31
34
import org .eclipse .jface .action .IAction ;
@@ -50,15 +53,26 @@ public class ClipboardCompare extends BaseCompareAction implements IObjectAction
50
53
private final String clipboard = "Clipboard" ; //$NON-NLS-1$
51
54
private final String compareFailed = "Comparision Failed" ; //$NON-NLS-1$
52
55
56
+ private IFile currentResouce ;
57
+
53
58
private IWorkbenchPart activePart ;
54
59
60
+ private int offSet ;
61
+ private int len ;
62
+
63
+ private boolean partialSelection ;
64
+
55
65
@ Override
56
66
protected void run (ISelection selection ) {
67
+ offSet = -1 ;
68
+ len = -1 ;
69
+ partialSelection = false ;
57
70
IFile [] files = Utilities .getFiles (selection );
58
71
Shell parentShell = CompareUIPlugin .getShell ();
59
72
for (IFile file : files ) {
73
+ currentResouce = file ;
60
74
try {
61
- processComparison (file , parentShell );
75
+ processComparison (parentShell );
62
76
} catch (Exception e ) {
63
77
MessageDialog .openError (parentShell , compareFailed , e .getMessage ());
64
78
}
@@ -73,17 +87,16 @@ protected boolean isEnabled(ISelection selection) {
73
87
* Process comparison with selection or entire editor contents with contents in
74
88
* clipboard
75
89
*
76
- * @param file Editor file
77
90
* @param parentShell The shell containing this window's controls
78
91
* @throws IOException, CoreException
79
92
*/
80
- private void processComparison (IFile file , Shell parentShell ) throws IOException , CoreException {
93
+ private void processComparison (Shell parentShell ) throws IOException , CoreException {
81
94
String cb = getClipboard ().toString ();
82
- String fileName = file .getName ();
95
+ String fileName = currentResouce .getName ();
83
96
IWorkbenchPage page = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getActivePage ();
84
97
IEditorPart editor = page .getActiveEditor ();
85
98
if (activePart instanceof IViewPart ) {
86
- String fileContents = new String (file .getContents ().readAllBytes (), file .getCharset ());
99
+ String fileContents = new String (currentResouce .getContents ().readAllBytes (), currentResouce .getCharset ());
87
100
showComparison (fileContents , fileName , cb , parentShell );
88
101
return ;
89
102
}
@@ -93,9 +106,13 @@ private void processComparison(IFile file, Shell parentShell) throws IOException
93
106
if (selection instanceof ITextSelection textSelection ) {
94
107
selectionContents = textSelection .getText ();
95
108
if (selectionContents .isEmpty ()) {
96
- String fileContents = new String (file .getContents ().readAllBytes (), file .getCharset ());
109
+ String fileContents = new String (currentResouce .getContents ().readAllBytes (),
110
+ currentResouce .getCharset ());
97
111
showComparison (fileContents , fileName , cb , parentShell );
98
112
} else {
113
+ offSet = textSelection .getOffset ();
114
+ len = textSelection .getLength ();
115
+ partialSelection = true ;
99
116
showComparison (selectionContents , fileName , cb , parentShell );
100
117
}
101
118
return ;
@@ -105,7 +122,8 @@ private void processComparison(IFile file, Shell parentShell) throws IOException
105
122
ISelection selection = existingCompare .getSite ().getSelectionProvider ().getSelection ();
106
123
if (selection instanceof ITextSelection textSelection ) {
107
124
String selectedText = textSelection .getText ();
108
- String fileContents = new String (file .getContents ().readAllBytes (), file .getCharset ());
125
+ String fileContents = new String (currentResouce .getContents ().readAllBytes (),
126
+ currentResouce .getCharset ());
109
127
showComparison (fileContents , fileName , selectedText , parentShell );
110
128
}
111
129
}
@@ -151,6 +169,65 @@ public InputStream getContents() throws CoreException {
151
169
}
152
170
153
171
}
172
+ class EditableFileNode extends ResourceNode implements IEditableContent {
173
+
174
+ private final int selectionOffset ;
175
+ private final int selectionLength ;
176
+
177
+ public EditableFileNode (IFile file , int selectionOffset , int selectionLength ) {
178
+ super (file );
179
+ this .selectionOffset = selectionOffset ;
180
+ this .selectionLength = selectionLength ;
181
+ }
182
+
183
+ @ Override
184
+ public InputStream getContents () throws CoreException {
185
+ IFile file = (IFile ) getResource ();
186
+ if (!partialSelection ) {
187
+ return new ByteArrayInputStream (file .readAllBytes ());
188
+ }
189
+ try {
190
+ String content = new String (file .getContents ().readAllBytes (), file .getCharset ());
191
+ int start = Math .max (0 , Math .min (selectionOffset , content .length ()));
192
+ int end = Math .max (start , Math .min (selectionOffset + selectionLength , content .length ()));
193
+ String selectedPart = content .substring (start , end );
194
+ return new ByteArrayInputStream (selectedPart .getBytes (file .getCharset ()));
195
+ } catch (IOException e ) {
196
+ MessageDialog .openError (CompareUIPlugin .getShell (), compareFailed , e .getMessage ());
197
+ }
198
+ return new ByteArrayInputStream (file .readAllBytes ());
199
+ }
200
+
201
+ @ Override
202
+ public void setContent (byte [] newContent ) {
203
+ try {
204
+ if (selectionLength <= 1 ) {
205
+ ((IFile ) getResource ()).setContents (new ByteArrayInputStream (newContent ),
206
+ IResource .FORCE | IResource .KEEP_HISTORY , null );
207
+ } else {
208
+ IFile file = (IFile ) getResource ();
209
+ String charset = file .getCharset ();
210
+ String original = new String (file .getContents ().readAllBytes (), charset );
211
+ String updatedSelection = new String (newContent , charset );
212
+ int offset = Math .max (0 , Math .min (selectionOffset , original .length ()));
213
+ int end = Math .max (offset , Math .min (offset + selectionLength , original .length ()));
214
+ String newFileContent = original .substring (0 , offset ) + updatedSelection
215
+ + original .substring (end );
216
+ ByteArrayInputStream updatedStream = new ByteArrayInputStream (newFileContent .getBytes (charset ));
217
+ file .setContents (updatedStream , IResource .FORCE | IResource .KEEP_HISTORY , null );
218
+ }
219
+
220
+ } catch (Exception e ) {
221
+ MessageDialog .openError (CompareUIPlugin .getShell (), compareFailed , e .getMessage ());
222
+ }
223
+ }
224
+
225
+ @ Override
226
+ public boolean isEditable () {
227
+ return true ;
228
+ }
229
+ }
230
+
154
231
if (source == null ) {
155
232
MessageDialog .openInformation (parentShell , compareFailed , "Failed to process selected file" ); //$NON-NLS-1$
156
233
return ;
@@ -164,11 +241,18 @@ public InputStream getContents() throws CoreException {
164
241
@ Override
165
242
protected Object prepareInput (IProgressMonitor monitor )
166
243
throws InvocationTargetException , InterruptedException {
167
- return new DiffNode (new ClipboardTypedElement (fileName , source ),
168
- new ClipboardTypedElement (clipboard , clipboardContents ));
244
+ ITypedElement left ;
245
+ if (offSet >= 0 && len >= 0 ) {
246
+ left = new EditableFileNode (currentResouce , offSet , len );
247
+ } else {
248
+ left = new EditableFileNode (currentResouce , 0 , Integer .MAX_VALUE );
249
+ }
250
+ ITypedElement right = new ClipboardTypedElement (clipboard , clipboardContents );
251
+ return new DiffNode (left , right );
169
252
170
253
}
171
254
};
255
+ compareInput .setTitle (currentResouce .getName ());
172
256
CompareUI .openCompareEditor (compareInput );
173
257
}
174
258
@@ -187,4 +271,4 @@ public void setActivePart(IAction action, IWorkbenchPart targetPart) {
187
271
this .activePart = targetPart ;
188
272
}
189
273
190
- }
274
+ }
0 commit comments