package no.uio.ifi.refaktor.textselection; import no.uio.ifi.refaktor.debugging.RefaktorDebug; import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.JavaModelException; public class MethodRelativeCompilationUnitTextSelection extends CompilationUnitTextSelection { private final IMethod method; private final int relativeOffset; public MethodRelativeCompilationUnitTextSelection(IMethod method, CompilationUnitTextSelection textSelection) { super(textSelection); this.method = method; // Using the name of the method as reference point // in case the method's access modifier changes int methodNameOffset = 0; try { methodNameOffset = freshMethodNameOffset(); } catch (JavaModelException e) { RefaktorDebug.log(e); } relativeOffset = textSelection.getOffset() - methodNameOffset; } private int freshMethodNameOffset() throws JavaModelException { return method.getNameRange().getOffset(); } @Override public int getOffset() { try { return freshMethodNameOffset() + relativeOffset; } catch (JavaModelException e) { RefaktorDebug.log(e); return super.getOffset(); } } }