1 11 package org.eclipse.jdt.internal.ui.fix; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.text.edits.DeleteEdit; 17 import org.eclipse.text.edits.MultiTextEdit; 18 import org.eclipse.text.edits.TextEdit; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Status; 23 24 import org.eclipse.jface.text.BadLocationException; 25 import org.eclipse.jface.text.Document; 26 import org.eclipse.jface.text.IRegion; 27 import org.eclipse.jface.text.TextUtilities; 28 29 import org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup; 30 import org.eclipse.ltk.core.refactoring.GroupCategory; 31 import org.eclipse.ltk.core.refactoring.GroupCategorySet; 32 import org.eclipse.ltk.core.refactoring.TextChange; 33 34 import org.eclipse.jdt.core.ICompilationUnit; 35 import org.eclipse.jdt.core.formatter.CodeFormatter; 36 37 import org.eclipse.jdt.internal.corext.fix.IFix; 38 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange; 39 import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil; 40 41 import org.eclipse.jdt.internal.ui.JavaPlugin; 42 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 43 44 public class CodeFormatFix implements IFix { 45 46 public static IFix createCleanUp(ICompilationUnit cu, boolean format, boolean removeTrailingWhitespacesAll, boolean removeTrailingWhitespacesIgnorEmpty) throws CoreException { 47 if (!format && !removeTrailingWhitespacesAll && !removeTrailingWhitespacesIgnorEmpty) 48 return null; 49 50 if (format) { 51 Map fomatterSettings= new HashMap (cu.getJavaProject().getOptions(true)); 52 53 String content= cu.getBuffer().getContents(); 54 Document document= new Document(content); 55 56 TextEdit edit= CodeFormatterUtil.reformat(CodeFormatter.K_COMPILATION_UNIT, content, 0, TextUtilities.getDefaultLineDelimiter(document), fomatterSettings); 57 if (edit == null || !edit.hasChildren()) 58 return null; 59 60 String label= MultiFixMessages.CodeFormatFix_description; 61 TextChange change= new CompilationUnitChange(label, cu); 62 change.setEdit(edit); 63 64 CategorizedTextEditGroup group= new CategorizedTextEditGroup(label, new GroupCategorySet(new GroupCategory(label, label, label))); 65 group.addTextEdit(edit); 66 change.addTextEditGroup(group); 67 68 return new CodeFormatFix(change, cu); 69 } else if (removeTrailingWhitespacesAll || removeTrailingWhitespacesIgnorEmpty) { 70 try { 71 MultiTextEdit multiEdit= new MultiTextEdit(); 72 Document document= new Document(cu.getBuffer().getContents()); 73 74 int lineCount= document.getNumberOfLines(); 75 for (int i= 0; i < lineCount; i++) { 76 77 IRegion region= document.getLineInformation(i); 78 if (region.getLength() == 0) 79 continue; 80 81 int lineStart= region.getOffset(); 82 int lineExclusiveEnd= lineStart + region.getLength(); 83 int j= getIndexOfRightMostNoneWhitspaceCharacter(lineStart, lineExclusiveEnd - 1, document); 84 85 if (removeTrailingWhitespacesAll) { 86 j++; 87 if (j < lineExclusiveEnd) 88 multiEdit.addChild(new DeleteEdit(j, lineExclusiveEnd - j)); 89 } else if (removeTrailingWhitespacesIgnorEmpty) { 90 if (j >= lineStart) { 91 if (document.getChar(j) == '*' && getIndexOfRightMostNoneWhitspaceCharacter(lineStart, j - 1, document) < lineStart) { 92 j++; 93 } 94 j++; 95 if (j < lineExclusiveEnd) 96 multiEdit.addChild(new DeleteEdit(j, lineExclusiveEnd - j)); 97 } 98 } 99 } 100 101 if (multiEdit.getChildrenSize() == 0) 102 return null; 103 104 String label= MultiFixMessages.CodeFormatFix_RemoveTrailingWhitespace_changeDescription; 105 CompilationUnitChange change= new CompilationUnitChange(label, cu); 106 change.setEdit(multiEdit); 107 108 CategorizedTextEditGroup group= new CategorizedTextEditGroup(label, new GroupCategorySet(new GroupCategory(label, label, label))); 109 group.addTextEdit(multiEdit); 110 change.addTextEditGroup(group); 111 112 return new CodeFormatFix(change, cu); 113 } catch (BadLocationException x) { 114 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, "", x)); } 116 } 117 118 return null; 119 } 120 121 133 private static int getIndexOfRightMostNoneWhitspaceCharacter(int start, int end, Document document) throws BadLocationException { 134 int position= end; 135 while (position >= start && Character.isWhitespace(document.getChar(position))) 136 position--; 137 138 return position; 139 } 140 141 private final ICompilationUnit fCompilationUnit; 142 private final TextChange fChange; 143 144 public CodeFormatFix(TextChange change, ICompilationUnit compilationUnit) { 145 fChange= change; 146 fCompilationUnit= compilationUnit; 147 } 148 149 152 public TextChange createChange() throws CoreException { 153 return fChange; 154 } 155 156 159 public ICompilationUnit getCompilationUnit() { 160 return fCompilationUnit; 161 } 162 163 166 public String getDescription() { 167 return MultiFixMessages.CodeFormatFix_description; 168 } 169 170 173 public IStatus getStatus() { 174 return StatusInfo.OK_STATUS; 175 } 176 177 } 178 | Popular Tags |