]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | package no.uio.ifi.refaktor.textselection; |
2 | ||
3 | import no.uio.ifi.refaktor.debugging.RefaktorDebug; | |
4 | ||
5 | import org.eclipse.jdt.core.IMethod; | |
6 | import org.eclipse.jdt.core.JavaModelException; | |
7 | ||
8 | ||
9 | public class MethodRelativeCompilationUnitTextSelection extends CompilationUnitTextSelection { | |
10 | ||
11 | private final IMethod method; | |
12 | private final int relativeOffset; | |
13 | ||
14 | public MethodRelativeCompilationUnitTextSelection(IMethod method, CompilationUnitTextSelection textSelection) { | |
15 | super(textSelection); | |
16 | this.method = method; | |
17 | ||
18 | // Using the name of the method as reference point | |
19 | // in case the method's access modifier changes | |
20 | int methodNameOffset = 0; | |
21 | try { | |
22 | methodNameOffset = freshMethodNameOffset(); | |
23 | } catch (JavaModelException e) { | |
24 | RefaktorDebug.log(e); | |
25 | } | |
26 | relativeOffset = textSelection.getOffset() - methodNameOffset; | |
27 | } | |
28 | ||
29 | private int freshMethodNameOffset() throws JavaModelException { | |
30 | return method.getNameRange().getOffset(); | |
31 | } | |
32 | ||
33 | @Override | |
34 | public int getOffset() { | |
35 | try { | |
36 | return freshMethodNameOffset() + relativeOffset; | |
37 | } catch (JavaModelException e) { | |
38 | RefaktorDebug.log(e); | |
39 | return super.getOffset(); | |
40 | } | |
41 | } | |
42 | ||
43 | } |