Author: dazarov
Date: 2012-01-19 15:56:33 -0500 (Thu, 19 Jan 2012)
New Revision: 37973
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/AddAnnotationMarkerResolution.java
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/marker/AddSuppressWarningsMarkerResolution.java
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/refactoring/MarkerResolutionUtils.java
Log:
Add the code which is supposed to be inserted by the Quick Fix into its description
https://issues.jboss.org/browse/JBIDE-10636
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/AddAnnotationMarkerResolution.java
===================================================================
---
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/AddAnnotationMarkerResolution.java 2012-01-19
20:41:24 UTC (rev 37972)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/AddAnnotationMarkerResolution.java 2012-01-19
20:56:33 UTC (rev 37973)
@@ -21,6 +21,7 @@
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
+import org.eclipse.ltk.core.refactoring.TextChange;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.graphics.Image;
import org.eclipse.text.edits.MultiTextEdit;
@@ -29,12 +30,15 @@
import org.jboss.tools.cdi.internal.core.refactoring.CDIMarkerResolutionUtils;
import org.jboss.tools.cdi.ui.CDIUIMessages;
import org.jboss.tools.cdi.ui.CDIUIPlugin;
+import org.jboss.tools.common.refactoring.MarkerResolutionUtils;
+import org.jboss.tools.common.ui.CommonUIPlugin;
public class AddAnnotationMarkerResolution implements
IMarkerResolution2 {
private IJavaElement element;
private String qualifiedName;
private String label;
+ private String description;
public AddAnnotationMarkerResolution(IJavaElement element, String qualifiedName){
this.element = element;
@@ -63,6 +67,7 @@
}
label = NLS.bind(CDIUIMessages.ADD_ANNOTATION_MARKER_RESOLUTION_TITLE, new
String[]{shortName, element.getElementName(), type});
+ description = getPreview();
}
@Override
@@ -76,15 +81,9 @@
ICompilationUnit original =
CDIMarkerResolutionUtils.getJavaMember(element).getCompilationUnit();
ICompilationUnit compilationUnit = original.getWorkingCopy(new
NullProgressMonitor());
- CompilationUnitChange change = new CompilationUnitChange("",
compilationUnit);
+ CompilationUnitChange change = getChange(compilationUnit);
- MultiTextEdit edit = new MultiTextEdit();
-
- change.setEdit(edit);
-
- CDIMarkerResolutionUtils.addAnnotation(qualifiedName, compilationUnit, element,
"", edit);
-
- if(edit.hasChildren()){
+ if(change.getEdit().hasChildren()){
change.perform(new NullProgressMonitor());
original.reconcile(ICompilationUnit.NO_AST, false, null, new NullProgressMonitor());
}
@@ -94,9 +93,43 @@
}
}
+ private CompilationUnitChange getChange(ICompilationUnit compilationUnit) throws
JavaModelException{
+ CompilationUnitChange change = new CompilationUnitChange("",
compilationUnit);
+
+ MultiTextEdit edit = new MultiTextEdit();
+
+ change.setEdit(edit);
+
+ CDIMarkerResolutionUtils.addAnnotation(qualifiedName, compilationUnit, element,
"", edit);
+
+ return change;
+ }
+
+ private CompilationUnitChange getPreviewChange(){
+ try{
+ ICompilationUnit original =
CDIMarkerResolutionUtils.getJavaMember(element).getCompilationUnit();
+
+ return getChange(original);
+ }catch(CoreException ex){
+ CDIUIPlugin.getDefault().logError(ex);
+ }
+ return null;
+ }
+
+ private String getPreview(){
+ TextChange previewChange = getPreviewChange();
+
+ try {
+ return MarkerResolutionUtils.getPreview(previewChange);
+ } catch (CoreException e) {
+ CommonUIPlugin.getDefault().logError(e);
+ }
+ return label;
+ }
+
@Override
public String getDescription() {
- return label;
+ return description;
}
@Override
Modified:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/refactoring/MarkerResolutionUtils.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/refactoring/MarkerResolutionUtils.java 2012-01-19
20:41:24 UTC (rev 37972)
+++
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/refactoring/MarkerResolutionUtils.java 2012-01-19
20:56:33 UTC (rev 37973)
@@ -10,6 +10,7 @@
******************************************************************************/
package org.jboss.tools.common.refactoring;
+import java.util.ArrayList;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -579,59 +580,171 @@
}
public static String getPreview(TextChange previewChange) throws CoreException{
+ if(previewChange == null)
+ return null;
+
String preview = previewChange.getPreviewContent(new NullProgressMonitor());
+
TextEdit edit = previewChange.getEdit();
- String text = null;
- if(edit instanceof InsertEdit){
- text = ((InsertEdit) edit).getText();
- }else if(edit instanceof ReplaceEdit){
- text = ((ReplaceEdit) edit).getText();
+
+ EditSet editSet = new EditSet(edit);
+
+ // select
+ preview = editSet.select(preview);
+
+ // cut
+ preview = editSet.cut(preview);
+
+ // format
+ preview = preview.replaceAll(NEW_LINE, LINE_BREAK);
+
+ return preview;
+ }
+
+ static class EditSet{
+ private ArrayList<TextEdit> edits = new ArrayList<TextEdit>();
+ private ArrayList<Region> regions = new ArrayList<Region>();
+
+ private int lastOffset = 0;
+
+ public EditSet(TextEdit edit){
+ addEdits(edit);
+ sort();
}
- if(edit != null && text != null){
- int offset = edit.getOffset();
- int length = text.length();
+
+ private void addEdits(TextEdit edit){
+ edits.add(edit);
+ for(TextEdit child : edit.getChildren()){
+ addEdits(child);
+ }
+ }
+
+ private void sort(){
+ }
+
+ private int getFirstOffset(){
+ if(edits.size() > 0){
+ return edits.get(0).getOffset();
+ }else{
+ return 0;
+ }
+ }
+
+ private int getLastOffset(){
+ return lastOffset;
+ }
+
+ public String select(String preview){
+ int delta = 0;
+ for(TextEdit edit : edits){
+ String text = null;
+ int addings = 0;
+ if(edit instanceof InsertEdit){
+ text = ((InsertEdit) edit).getText();
+ addings = text.length();
+ }else if(edit instanceof ReplaceEdit){
+ text = ((ReplaceEdit) edit).getText();
+ addings = text.length()-edit.getLength();
+ }
+ if(text != null){
+ int offset = edit.getOffset()+delta;
+ int length = text.length();
+ regions.add(new Region(offset, length));
+ lastOffset = offset+length;
+
+ // select
+ String before = preview.substring(0, offset);
+ String after = preview.substring(offset+length);
+ preview = before+OPEN_BOLD+text+CLOSE_BOLD+after;
+
+ delta += OPEN_BOLD.length()+CLOSE_BOLD.length()+addings;
+ }
+ }
+ return preview;
+ }
+
+ public String cut(String preview){
+ // process regions
+ Region prevRegion = null;
+ for(Region region : regions){
+ int position = region.offset;
+ int count = NUMBER_OF_STRINGS;
+ int lowLimit = 0;
+ if(prevRegion != null){
+ lowLimit = prevRegion.offset+prevRegion.length;
+ }
+ while(position >= lowLimit){
+ char c = preview.charAt(position);
+ if(c == C_NEW_LINE){
+ count--;
+ if(count == 0){
+ position++;
+ break;
+ }
+ }
+ position--;
+ }
+ if(prevRegion != null && position == prevRegion.offset+prevRegion.length){
+ prevRegion.active = false;
+ int shift = region.offset - prevRegion.offset;
+ region.offset = prevRegion.offset;
+ region.length += shift;
+ }else{
+ int shift = region.offset-position;
+ region.offset = position;
+ region.length += shift;
+
+ }
+
+ position = region.offset+region.length;
+ count = NUMBER_OF_STRINGS;
+ while(position < preview.length()-1){
+ char c = preview.charAt(position);
+ if(c == C_NEW_LINE){
+ count--;
+ if(count == 0){
+ break;
+ }
+ }
+ position++;
+ }
+ region.length += position - (region.offset + region.length);
+ prevRegion = region;
+ }
- // select
- String before = preview.substring(0, offset);
- String after = preview.substring(offset+length);
- preview = before+OPEN_BOLD+text+CLOSE_BOLD+after;
// cut
- int position = offset;
- int count = NUMBER_OF_STRINGS;
- String startText = NEW_LINE;
- while(position >= 0){
- char c = preview.charAt(position);
- if(c == C_NEW_LINE){
- count--;
- if(count == 0){
- position++;
- startText = DOTS+startText;
- break;
- }
+ StringBuffer buffer = new StringBuffer();
+ int index = 0;
+ for(Region region : regions){
+ if(!region.active){
+ continue;
}
- position--;
- }
- int start = position;
- String endText = NEW_LINE;
- position = offset+length;
- count = NUMBER_OF_STRINGS;
- while(position < preview.length()-1){
- char c = preview.charAt(position);
- if(c == C_NEW_LINE){
- count--;
- if(count == 0){
- endText += DOTS;
- break;
+ if(index == 0 && region.offset != 0){
+ buffer.append(DOTS+NEW_LINE);
+ }
+ buffer.append(preview.substring(region.offset, region.offset + region.length));
+ if((region.offset + region.length) < (preview.length()-1)){
+ if(index == regions.size()-1){
+ buffer.append(NEW_LINE+DOTS);
+ }else{
+ buffer.append(NEW_LINE+DOTS+NEW_LINE);
}
}
- position++;
+ index++;
}
- preview = startText+preview.substring(start, position)+endText;
-
- // format
- preview = preview.replaceAll(NEW_LINE, LINE_BREAK);
+ return buffer.toString();
}
- return preview;
}
+
+ static class Region{
+ public int offset;
+ public int length;
+ public boolean active = true;
+
+ public Region(int offset, int length){
+ this.offset = offset;
+ this.length = length;
+ }
+ }
}
Modified:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/marker/AddSuppressWarningsMarkerResolution.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/marker/AddSuppressWarningsMarkerResolution.java 2012-01-19
20:41:24 UTC (rev 37972)
+++
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/marker/AddSuppressWarningsMarkerResolution.java 2012-01-19
20:56:33 UTC (rev 37973)
@@ -121,7 +121,6 @@
private TextChange getPreviewChange(){
if(element != null && preferenceKey != null){
- disablePreference();
try {
ICompilationUnit original = EclipseUtil.getCompilationUnit(file);
if(original == null) {