1 11 package org.eclipse.jdt.internal.corext.refactoring.code; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IPath; 15 import org.eclipse.core.runtime.NullProgressMonitor; 16 17 import org.eclipse.core.filebuffers.FileBuffers; 18 import org.eclipse.core.filebuffers.ITextFileBuffer; 19 import org.eclipse.core.filebuffers.LocationKind; 20 21 import org.eclipse.jface.text.BadLocationException; 22 import org.eclipse.jface.text.IRegion; 23 24 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 25 26 import org.eclipse.jdt.core.ICompilationUnit; 27 import org.eclipse.jdt.core.dom.ASTNode; 28 import org.eclipse.jdt.core.dom.Block; 29 import org.eclipse.jdt.core.dom.CompilationUnit; 30 import org.eclipse.jdt.core.dom.MethodDeclaration; 31 32 import org.eclipse.jdt.internal.corext.dom.ASTNodes; 33 import org.eclipse.jdt.internal.corext.dom.Selection; 34 import org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer; 35 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 36 import org.eclipse.jdt.internal.corext.util.Messages; 37 import org.eclipse.jdt.internal.corext.util.Strings; 38 39 import org.eclipse.jdt.internal.ui.JavaPlugin; 40 41 public class CodeRefactoringUtil { 42 43 public static RefactoringStatus checkMethodSyntaxErrors(int selectionStart, int selectionLength, CompilationUnit cuNode, String invalidSelectionMessage){ 44 SelectionAnalyzer analyzer= new SelectionAnalyzer(Selection.createFromStartLength(selectionStart, selectionLength), true); 45 cuNode.accept(analyzer); 46 ASTNode coveringNode= analyzer.getLastCoveringNode(); 47 if (! (coveringNode instanceof Block) || ! (coveringNode.getParent() instanceof MethodDeclaration)) 48 return RefactoringStatus.createFatalErrorStatus(invalidSelectionMessage); 49 if (ASTNodes.getMessages(coveringNode, ASTNodes.NODE_ONLY).length == 0) 50 return RefactoringStatus.createFatalErrorStatus(invalidSelectionMessage); 51 52 MethodDeclaration methodDecl= (MethodDeclaration)coveringNode.getParent(); 53 String [] keys= {methodDecl.getName().getIdentifier()}; 54 String message= Messages.format(RefactoringCoreMessages.CodeRefactoringUtil_error_message, keys); 55 return RefactoringStatus.createFatalErrorStatus(message); 56 } 57 58 public static int getIndentationLevel(ASTNode node, ICompilationUnit unit) throws CoreException { 59 IPath fullPath= unit.getCorrespondingResource().getFullPath(); 60 try{ 61 FileBuffers.getTextFileBufferManager().connect(fullPath, LocationKind.IFILE, new NullProgressMonitor()); 62 ITextFileBuffer buffer= FileBuffers.getTextFileBufferManager().getTextFileBuffer(fullPath, LocationKind.IFILE); 63 try { 64 IRegion region= buffer.getDocument().getLineInformationOfOffset(node.getStartPosition()); 65 return Strings.computeIndentUnits(buffer.getDocument().get(region.getOffset(), region.getLength()), unit.getJavaProject()); 66 } catch (BadLocationException exception) { 67 JavaPlugin.log(exception); 68 } 69 return 0; 70 } finally { 71 FileBuffers.getTextFileBufferManager().disconnect(fullPath, LocationKind.IFILE, new NullProgressMonitor()); 72 } 73 } 74 75 private CodeRefactoringUtil() {} 76 } 77 | Popular Tags |