Author: dazarov
Date: 2012-02-01 19:53:05 -0500 (Wed, 01 Feb 2012)
New Revision: 38385
Modified:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/refactoring/MarkerResolutionUtils.java
trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/el/refactoring/RenameMethodParticipant.java
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/marker/AbstractSeamMarkerResolution.java
Log:
Seam quick fixes insert incorrectly formated code
https://issues.jboss.org/browse/JBIDE-10766
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-02-02
00:52:32 UTC (rev 38384)
+++
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/refactoring/MarkerResolutionUtils.java 2012-02-02
00:53:05 UTC (rev 38385)
@@ -12,6 +12,7 @@
import java.util.ArrayList;
import java.util.HashSet;
+import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -36,6 +37,7 @@
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.internal.core.JavaElement;
+import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
import org.eclipse.jpt.common.core.internal.utility.jdt.ASTTools;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
@@ -254,6 +256,43 @@
}
return list;
}
+
+ public static void addMethod(List<String> lines, ICompilationUnit compilationUnit,
IType type, MultiTextEdit rootEdit) throws JavaModelException{
+ IType workingCopyType = findWorkingCopy(compilationUnit, type);
+ if(workingCopyType == null){
+ return;
+ }
+ IBuffer buffer = compilationUnit.getBuffer();
+ String lineSeparator = compilationUnit.findRecommendedLineSeparator();
+
+ int position =
workingCopyType.getSourceRange().getOffset()+workingCopyType.getSource().lastIndexOf("}");
+ if(position > 0){
+ String spaces = getLeadingSpacesToInsert(position, buffer);
+
+ int indentWidth = CodeFormatterUtil.getIndentWidth(compilationUnit.getJavaProject());
+ for(int i = 0; i < indentWidth; i++){
+ spaces += SPACE;
+ }
+
+ String text = "";
+ for(String line : lines){
+ text += spaces;
+ text += line;
+ text += lineSeparator;
+ }
+
+ if(rootEdit != null){
+ TextEdit edit = new InsertEdit(position, text);
+ rootEdit.addChild(edit);
+ }else{
+ buffer.replace(position, 0, text);
+
+ synchronized(compilationUnit) {
+ compilationUnit.reconcile(ICompilationUnit.NO_AST, true, null, null);
+ }
+ }
+ }
+ }
public static void addAnnotation(String qualifiedName, ICompilationUnit compilationUnit,
IJavaElement element) throws JavaModelException{
addAnnotation(qualifiedName, compilationUnit, element, "");
@@ -364,18 +403,7 @@
str += compilationUnit.findRecommendedLineSeparator();
- int index = position;
- while(index >= 0){
- char c = buffer.getChar(index);
- if(c == C_CARRIAGE_RETURN || c == C_NEW_LINE)
- break;
- index--;
- }
- index++;
- if(index != position){
- String spaces = buffer.getText(index, position-index);
- str += spaces;
- }
+ str += getLeadingSpacesToInsert(position, buffer);
}else{
str += SPACE;
@@ -520,7 +548,7 @@
if(annotation != null){
IBuffer buffer = compilationUnit.getBuffer();
- int numberOfSpaces = getNumberOfSpaces(annotation.getSourceRange().getOffset() +
annotation.getSourceRange().getLength(), buffer);
+ int numberOfSpaces = getNumberOfSpacesToDelete(annotation.getSourceRange().getOffset()
+ annotation.getSourceRange().getLength(), buffer);
// delete annotation
if(rootEdit != null){
@@ -535,7 +563,7 @@
}
}
- private static int getNumberOfSpaces(int startPosition, IBuffer buffer){
+ private static int getNumberOfSpacesToDelete(int startPosition, IBuffer buffer){
int position = startPosition;
int numberOfSpaces = 0;
if(position < buffer.getLength()-1){
@@ -549,6 +577,21 @@
return numberOfSpaces;
}
+ private static String getLeadingSpacesToInsert(int startPosition, IBuffer buffer){
+ int position = startPosition;
+ while(position >= 0){
+ char c = buffer.getChar(position);
+ if(c == C_CARRIAGE_RETURN || c == C_NEW_LINE)
+ break;
+ position--;
+ }
+ position++;
+ if(position != startPosition){
+ return buffer.getText(position, startPosition-position);
+ }
+ return "";
+ }
+
public static void deleteImportForAnnotation(String qualifiedName, IAnnotation
annotation, ICompilationUnit compilationUnit, IBuffer buffer, MultiTextEdit rootEdit)
throws JavaModelException{
IImportDeclaration importDeclaration = compilationUnit.getImport(qualifiedName);
IImportContainer importContainer = compilationUnit.getImportContainer();
@@ -563,7 +606,7 @@
if(checkImport(textBefore, qualifiedName) && checkImport(textAfter,
qualifiedName)){
int numberOfSpaces = 0;
if(!isLastImport(importContainer, importDeclaration)){
- numberOfSpaces = getNumberOfSpaces(importDeclaration.getSourceRange().getOffset()
+ importDeclaration.getSourceRange().getLength(), buffer);
+ numberOfSpaces =
getNumberOfSpacesToDelete(importDeclaration.getSourceRange().getOffset() +
importDeclaration.getSourceRange().getLength(), buffer);
}
TextEdit edit = new DeleteEdit(importDeclaration.getSourceRange().getOffset(),
importDeclaration.getSourceRange().getLength()+numberOfSpaces);
Modified:
trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/el/refactoring/RenameMethodParticipant.java
===================================================================
---
trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/el/refactoring/RenameMethodParticipant.java 2012-02-02
00:52:32 UTC (rev 38384)
+++
trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/el/refactoring/RenameMethodParticipant.java 2012-02-02
00:53:05 UTC (rev 38385)
@@ -16,7 +16,6 @@
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jdt.core.IJavaElement;
@@ -33,7 +32,6 @@
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.text.edits.TextEdit;
-import org.jboss.tools.common.model.project.ProjectHome;
import org.jboss.tools.common.util.BeanUtil;
import org.jboss.tools.jsf.ui.JsfUIMessages;
import org.jboss.tools.jst.web.kb.refactoring.ELProjectSetExtension;
Modified:
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/marker/AbstractSeamMarkerResolution.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/marker/AbstractSeamMarkerResolution.java 2012-02-02
00:52:32 UTC (rev 38384)
+++
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/marker/AbstractSeamMarkerResolution.java 2012-02-02
00:53:05 UTC (rev 38385)
@@ -10,6 +10,7 @@
******************************************************************************/
package org.jboss.tools.seam.ui.marker;
+import java.util.ArrayList;
import java.util.Map;
import org.eclipse.core.resources.IFile;
@@ -17,9 +18,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.jdt.core.IBuffer;
import org.eclipse.jdt.core.ICompilationUnit;
-import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
@@ -164,33 +163,38 @@
}
ICompilationUnit compilationUnit = original.getWorkingCopy(new
NullProgressMonitor());
- final String lineDelim= compilationUnit.findRecommendedLineSeparator();
-
IType type = compilationUnit.findPrimaryType();
if(type != null){
- IImportDeclaration importDeclaration = compilationUnit.getImport(qualifiedName);
- if(importDeclaration == null || !importDeclaration.exists())
- compilationUnit.createImport(qualifiedName, null, new NullProgressMonitor());
String annotation = MarkerResolutionUtils.getShortName(qualifiedName);
String methodName = annotation.toLowerCase();
+ CompilationUnitChange change = new CompilationUnitChange("",
compilationUnit);
+
+ MultiTextEdit edit = new MultiTextEdit();
+
+ change.setEdit(edit);
+
IMethod oldMethod = type.getMethod(methodName, new String[]{});
if(oldMethod == null || !oldMethod.exists()){
- StringBuffer buf= new StringBuffer();
- buf.append("@"+annotation); //$NON-NLS-1$
- buf.append(lineDelim);
- buf.append("public void "+methodName+"() {"); //$NON-NLS-1$
//$NON-NLS-2$
- buf.append(lineDelim);
- buf.append("}"); //$NON-NLS-1$
- type.createMethod(buf.toString(), null, false, new NullProgressMonitor());
+ MarkerResolutionUtils.addImport(qualifiedName, compilationUnit, edit);
+
+ ArrayList<String> lines = new ArrayList<String>();
+
+ lines.add("@"+annotation); //$NON-NLS-1$
+ lines.add("public void "+methodName+"() {"); //$NON-NLS-1$
//$NON-NLS-2$
+ lines.add("}"); //$NON-NLS-1$
+
+ MarkerResolutionUtils.addMethod(lines, compilationUnit, type, edit);
}else{
- IBuffer buffer = compilationUnit.getBuffer();
- buffer.replace(oldMethod.getSourceRange().getOffset(), 0,
"@"+annotation+lineDelim);
+ MarkerResolutionUtils.addAnnotation(qualifiedName, compilationUnit, oldMethod,
"", edit);
}
-
- compilationUnit.commitWorkingCopy(false, new NullProgressMonitor());
+
+ if(edit.hasChildren()){
+ change.perform(new NullProgressMonitor());
+ original.reconcile(ICompilationUnit.NO_AST, false, null, new
NullProgressMonitor());
+ }
compilationUnit.discardWorkingCopy();
}
}catch(CoreException ex){