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