1
1
package se .kth .spork .cli ;
2
2
3
+ import spoon .reflect .cu .CompilationUnit ;
3
4
import spoon .reflect .cu .SourcePosition ;
4
5
import spoon .reflect .declaration .CtElement ;
5
6
6
7
import java .io .IOException ;
7
8
import java .nio .charset .Charset ;
8
- import java .util .Arrays ;
9
9
import java .util .List ;
10
10
11
11
/**
@@ -42,6 +42,24 @@ static String getOriginalSource(CtElement elem) {
42
42
return getOriginalSource (pos , pos );
43
43
}
44
44
45
+ /**
46
+ * Get the original source code fragment starting at start and ending at end, including indentation if start is
47
+ * the first element on its source code line.
48
+ *
49
+ * @param start The source position of the first element.
50
+ * @param end The source position of the last element.
51
+ * @return The source code fragment starting at start and ending at end, including leading indentation.
52
+ * @throws IOException
53
+ */
54
+ private static String getOriginalSource (SourcePosition start , SourcePosition end ) {
55
+ CompilationUnit cu = start .getCompilationUnit ();
56
+ String source = cu .getOriginalSourceCode ();
57
+ int startChar = precededByIndentation (source , start ) ?
58
+ getLineStartIdx (start ) : start .getSourceStart ();
59
+ int endChar = end .getSourceEnd ();
60
+ return source .substring (startChar , endChar + 1 );
61
+ }
62
+
45
63
/**
46
64
* Return the indentation count for this element. This is a bit hit-and-miss, but it usually works. It finds
47
65
* the line that the element starts on, and counts the amount of indentation characters until the first character
@@ -53,7 +71,7 @@ static String getOriginalSource(CtElement elem) {
53
71
*/
54
72
static int getIndentation (CtElement elem ) {
55
73
SourcePosition pos = getSourcePos (elem );
56
- byte [] fileBytes = pos .getCompilationUnit ().getOriginalSourceCode (). getBytes ( Charset . defaultCharset () );
74
+ String source = pos .getCompilationUnit ().getOriginalSourceCode ();
57
75
int count = 0 ;
58
76
59
77
int [] lineSepPositions = pos .getCompilationUnit ().getLineSeparatorPositions ();
@@ -63,8 +81,8 @@ static int getIndentation(CtElement elem) {
63
81
64
82
65
83
while (current + count < pos .getSourceStart ()) {
66
- byte b = fileBytes [ current + count ] ;
67
- if (!isIndentation (b )) {
84
+ char c = source . charAt ( current + count ) ;
85
+ if (!isIndentation (c )) {
68
86
break ;
69
87
}
70
88
++count ;
@@ -86,47 +104,18 @@ private static SourcePosition getSourcePos(CtElement elem) {
86
104
return pos ;
87
105
}
88
106
89
- private static String getOriginalSource (SourcePosition start , SourcePosition end ) {
90
- byte [] source = start .getCompilationUnit ().getOriginalSourceCode ().getBytes (Charset .defaultCharset ());
91
- return getOriginalSource (start , end , source );
92
- }
93
-
94
- /**
95
- * Get the original source code fragment starting at start and ending at end, including indentation if start is
96
- * the first element on its source code line.
97
- *
98
- * @param start The source position of the first element.
99
- * @param end The source position of the last element.
100
- * @return The source code fragment starting at start and ending at end, including leading indentation.
101
- * @throws IOException
102
- */
103
- private static String getOriginalSource (SourcePosition start , SourcePosition end , byte [] source ) {
104
- int startByte = precededByIndentation (source , start ) ?
105
- getLineStartByte (start ) : start .getSourceStart ();
106
- int endByte = end .getSourceEnd ();
107
-
108
- if (isIndentation (source [startByte ])) {
109
- startByte ++;
110
- endByte ++;
111
- }
112
-
113
- byte [] content = Arrays .copyOfRange (source , startByte , endByte + 1 );
114
-
115
- return new String (content , Charset .defaultCharset ());
116
- }
117
-
118
- private static boolean precededByIndentation (byte [] source , SourcePosition pos ) {
119
- int lineStartByte = getLineStartByte (pos );
107
+ private static boolean precededByIndentation (String source , SourcePosition pos ) {
108
+ int lineStartIdx = getLineStartIdx (pos );
120
109
121
- for (int i = lineStartByte ; i < pos .getSourceStart (); i ++) {
122
- if (!isIndentation (source [ i ] )) {
110
+ for (int i = lineStartIdx ; i < pos .getSourceStart (); i ++) {
111
+ if (!isIndentation (source . charAt ( i ) )) {
123
112
return false ;
124
113
}
125
114
}
126
115
return true ;
127
116
}
128
117
129
- private static int getLineStartByte (SourcePosition pos ) {
118
+ private static int getLineStartIdx (SourcePosition pos ) {
130
119
if (pos .getLine () == 1 )
131
120
return 0 ;
132
121
@@ -140,7 +129,7 @@ private static int getLineStartByte(SourcePosition pos) {
140
129
return lineSepPositions [current ];
141
130
}
142
131
143
- private static boolean isIndentation (byte b ) {
144
- return b == ' ' || b == '\t' ;
132
+ private static boolean isIndentation (char c ) {
133
+ return c == ' ' || c == '\t' ;
145
134
}
146
135
}
0 commit comments