Lines 109-116
Link Here
|
109 |
for (int i= 0; i < max; i++) { |
109 |
for (int i= 0; i < max; i++) { |
110 |
char ch= line.charAt(i); |
110 |
char ch= line.charAt(i); |
111 |
if (ch == '\t') { |
111 |
if (ch == '\t') { |
112 |
int reminder= length % tabWidth; |
112 |
length = calculateSpaceEquivalents(tabWidth, length); |
113 |
length += tabWidth - reminder; |
|
|
114 |
} else if (isIndentChar(ch)) { |
113 |
} else if (isIndentChar(ch)) { |
115 |
length++; |
114 |
length++; |
116 |
} else { |
115 |
} else { |
Lines 131-156
Link Here
|
131 |
* @return the indent part of <code>line</code>, but no odd spaces |
130 |
* @return the indent part of <code>line</code>, but no odd spaces |
132 |
* @exception IllegalArgumentException if: |
131 |
* @exception IllegalArgumentException if: |
133 |
* <ul> |
132 |
* <ul> |
134 |
* <li>the given <code>indentWidth</code> is lower or equals to zero</li> |
133 |
* <li>the given <code>indentWidth</code> is lower than zero</li> |
135 |
* <li>the given <code>tabWidth</code> is lower than zero</li> |
134 |
* <li>the given <code>tabWidth</code> is lower than zero</li> |
136 |
* <li>the given <code>line</code> is null</li> |
135 |
* <li>the given <code>line</code> is null</li> |
137 |
* </ul> |
136 |
* </ul> |
138 |
*/ |
137 |
*/ |
139 |
public static String extractIndentString(String line, int tabWidth, int indentWidth) { |
138 |
public static String extractIndentString(String line, int tabWidth, int indentWidth) { |
140 |
if (tabWidth < 0 || indentWidth <= 0 || line == null) { |
139 |
if (tabWidth < 0 || indentWidth < 0 || line == null) { |
141 |
throw new IllegalArgumentException(); |
140 |
throw new IllegalArgumentException(); |
142 |
} |
141 |
} |
143 |
|
142 |
|
144 |
int size= line.length(); |
143 |
int size = line.length(); |
145 |
int end= 0; |
144 |
int end = 0; |
146 |
|
145 |
|
147 |
int spaceEquivs= 0; |
146 |
int spaceEquivs = 0; |
148 |
int characters= 0; |
147 |
int characters = 0; |
149 |
for (int i= 0; i < size; i++) { |
148 |
for (int i = 0; i < size; i++) { |
150 |
char c= line.charAt(i); |
149 |
char c = line.charAt(i); |
151 |
if (c == '\t') { |
150 |
if (c == '\t') { |
152 |
int remainder= spaceEquivs % tabWidth; |
151 |
spaceEquivs = calculateSpaceEquivalents(tabWidth, spaceEquivs); |
153 |
spaceEquivs += tabWidth - remainder; |
|
|
154 |
characters++; |
152 |
characters++; |
155 |
} else if (isIndentChar(c)) { |
153 |
} else if (isIndentChar(c)) { |
156 |
spaceEquivs++; |
154 |
spaceEquivs++; |
Lines 160-167
Link Here
|
160 |
} |
158 |
} |
161 |
if (spaceEquivs >= indentWidth) { |
159 |
if (spaceEquivs >= indentWidth) { |
162 |
end += characters; |
160 |
end += characters; |
163 |
characters= 0; |
161 |
characters = 0; |
164 |
spaceEquivs= spaceEquivs % indentWidth; |
162 |
if(indentWidth == 0) { |
|
|
163 |
spaceEquivs = 0; |
164 |
} |
165 |
else { |
166 |
spaceEquivs = spaceEquivs % indentWidth; |
167 |
} |
165 |
} |
168 |
} |
166 |
} |
169 |
} |
167 |
if (end == 0) { |
170 |
if (end == 0) { |
Lines 176-183
Link Here
|
176 |
|
179 |
|
177 |
/** |
180 |
/** |
178 |
* Removes the given number of indentation units from a given line. If the line |
181 |
* Removes the given number of indentation units from a given line. If the line |
179 |
* has less than the given indent, all the available indentation is removed. |
182 |
* has less indent than the given indentUnitsToRemove, all the available indentation is removed. |
180 |
* If <code>indentsToRemove <= 0</code> the line is returned. |
183 |
* If <code>indentsToRemove <= 0 or indent = 0</code> the line is returned. |
181 |
* |
184 |
* |
182 |
* @param line the line to trim |
185 |
* @param line the line to trim |
183 |
* @param tabWidth the width of one tab in space equivalents |
186 |
* @param tabWidth the width of one tab in space equivalents |
Lines 185-203
Link Here
|
185 |
* @return the trimmed string |
188 |
* @return the trimmed string |
186 |
* @exception IllegalArgumentException if: |
189 |
* @exception IllegalArgumentException if: |
187 |
* <ul> |
190 |
* <ul> |
188 |
* <li>the given <code>indentWidth</code> is lower or equals to zero</li> |
191 |
* <li>the given <code>indentWidth</code> is lower than zero</li> |
189 |
* <li>the given <code>tabWidth</code> is lower than zero</li> |
192 |
* <li>the given <code>tabWidth</code> is lower than zero</li> |
190 |
* <li>the given <code>line</code> is null</li> |
193 |
* <li>the given <code>line</code> is null</li> |
191 |
* </ul> |
194 |
* </ul> |
192 |
*/ |
195 |
*/ |
193 |
public static String trimIndent(String line, int indentUnitsToRemove, int tabWidth, int indentWidth) { |
196 |
public static String trimIndent(String line, int indentUnitsToRemove, int tabWidth, int indentWidth) { |
194 |
if (tabWidth < 0 || indentWidth <= 0 || line == null) { |
197 |
if (tabWidth < 0 || indentWidth < 0 || line == null) { |
195 |
throw new IllegalArgumentException(); |
198 |
throw new IllegalArgumentException(); |
196 |
} |
199 |
} |
197 |
|
200 |
|
198 |
if (indentUnitsToRemove <= 0) |
201 |
if (indentUnitsToRemove <= 0||indentWidth == 0) { |
199 |
return line; |
202 |
return line; |
200 |
|
203 |
} |
201 |
final int spaceEquivalentsToRemove= indentUnitsToRemove * indentWidth; |
204 |
final int spaceEquivalentsToRemove= indentUnitsToRemove * indentWidth; |
202 |
|
205 |
|
203 |
int start= 0; |
206 |
int start= 0; |
Lines 207-214
Link Here
|
207 |
for (int i= 0; i < size; i++) { |
210 |
for (int i= 0; i < size; i++) { |
208 |
char c= line.charAt(i); |
211 |
char c= line.charAt(i); |
209 |
if (c == '\t') { |
212 |
if (c == '\t') { |
210 |
int remainder= spaceEquivalents % tabWidth; |
213 |
spaceEquivalents = calculateSpaceEquivalents(tabWidth, spaceEquivalents); |
211 |
spaceEquivalents += tabWidth - remainder; |
|
|
212 |
} else if (isIndentChar(c)) { |
214 |
} else if (isIndentChar(c)) { |
213 |
spaceEquivalents++; |
215 |
spaceEquivalents++; |
214 |
} else { |
216 |
} else { |
Lines 257-272
Link Here
|
257 |
* @return the newly indent code, containing only the given line delimiters. |
259 |
* @return the newly indent code, containing only the given line delimiters. |
258 |
* @exception IllegalArgumentException if: |
260 |
* @exception IllegalArgumentException if: |
259 |
* <ul> |
261 |
* <ul> |
260 |
* <li>the given <code>indentWidth</code> is lower or equals to zero</li> |
262 |
* <li>the given <code>indentWidth</code> is lower than zero</li> |
261 |
* <li>the given <code>tabWidth</code> is lower than zero</li> |
263 |
* <li>the given <code>tabWidth</code> is lower than zero</li> |
262 |
* <li>the given <code>code</code> is null</li> |
264 |
* <li>the given <code>code</code> is null</li> |
263 |
* <li>the given <code>indentUnitsToRemove</code> is lower than zero</li> |
265 |
* <li>the given <code>indentUnitsToRemove</code> is lower than zero</li> |
|
|
266 |
* <li>the given <code>indentUnitsToRemove</code> is greater than zero when indentWidth equals zero</li> |
264 |
* <li>the given <code>newIndentString</code> is null</li> |
267 |
* <li>the given <code>newIndentString</code> is null</li> |
265 |
* <li>the given <code>lineDelim</code> is null</li> |
268 |
* <li>the given <code>lineDelim</code> is null</li> |
266 |
* </ul> |
269 |
* </ul> |
267 |
*/ |
270 |
*/ |
268 |
public static String changeIndent(String code, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString, String lineDelim) { |
271 |
public static String changeIndent(String code, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString, String lineDelim) { |
269 |
if (tabWidth < 0 || indentWidth <= 0 || code == null || indentUnitsToRemove < 0 || newIndentString == null || lineDelim == null) { |
272 |
if (tabWidth < 0 || indentWidth < 0 || code == null || indentUnitsToRemove < 0 || (indentWidth == 0 && indentUnitsToRemove > 0) || newIndentString == null || lineDelim == null) { |
270 |
throw new IllegalArgumentException(); |
273 |
throw new IllegalArgumentException(); |
271 |
} |
274 |
} |
272 |
|
275 |
|
Lines 291-297
Link Here
|
291 |
} else { // no new line after last line |
294 |
} else { // no new line after last line |
292 |
buf.append(lineDelim); |
295 |
buf.append(lineDelim); |
293 |
buf.append(newIndentString); |
296 |
buf.append(newIndentString); |
294 |
buf.append(trimIndent(line, indentUnitsToRemove, tabWidth, indentWidth)); |
297 |
if(indentWidth != 0) { |
|
|
298 |
buf.append(trimIndent(line, indentUnitsToRemove, tabWidth, indentWidth)); |
299 |
} |
300 |
else { |
301 |
buf.append(line); |
302 |
} |
295 |
} |
303 |
} |
296 |
} |
304 |
} |
297 |
return buf.toString(); |
305 |
return buf.toString(); |
Lines 316-330
Link Here
|
316 |
* @return returns the resulting text edits |
324 |
* @return returns the resulting text edits |
317 |
* @exception IllegalArgumentException if: |
325 |
* @exception IllegalArgumentException if: |
318 |
* <ul> |
326 |
* <ul> |
319 |
* <li>the given <code>indentWidth</code> is lower or equals to zero</li> |
327 |
* <li>the given <code>indentWidth</code> is lower than zero</li> |
320 |
* <li>the given <code>tabWidth</code> is lower than zero</li> |
328 |
* <li>the given <code>tabWidth</code> is lower than zero</li> |
321 |
* <li>the given <code>source</code> is null</li> |
329 |
* <li>the given <code>source</code> is null</li> |
322 |
* <li>the given <code>indentUnitsToRemove</code> is lower than zero</li> |
330 |
* <li>the given <code>indentUnitsToRemove</code> is lower than zero</li> |
|
|
331 |
* <li>the given <code>indentUnitsToRemove</code> is greater than zero when indentWidth equals zero</li> |
323 |
* <li>the given <code>newIndentString</code> is null</li> |
332 |
* <li>the given <code>newIndentString</code> is null</li> |
324 |
* </ul> |
333 |
* </ul> |
325 |
*/ |
334 |
*/ |
326 |
public static ReplaceEdit[] getChangeIndentEdits(String source, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString) { |
335 |
public static ReplaceEdit[] getChangeIndentEdits(String source, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString) { |
327 |
if (tabWidth < 0 || indentWidth <= 0 || source == null || indentUnitsToRemove < 0 || newIndentString == null) { |
336 |
if (tabWidth < 0 || indentWidth < 0 || source == null || indentUnitsToRemove < 0 || (indentWidth == 0 && indentUnitsToRemove > 0) || newIndentString == null) { |
328 |
throw new IllegalArgumentException(); |
337 |
throw new IllegalArgumentException(); |
329 |
} |
338 |
} |
330 |
|
339 |
|
Lines 368-375
Link Here
|
368 |
for (int i= 0; i < size && blanks < spaceEquivalents; i++) { |
377 |
for (int i= 0; i < size && blanks < spaceEquivalents; i++) { |
369 |
char c= line.charAt(i); |
378 |
char c= line.charAt(i); |
370 |
if (c == '\t') { |
379 |
if (c == '\t') { |
371 |
int remainder= blanks % tabWidth; |
380 |
blanks = calculateSpaceEquivalents(tabWidth, blanks); |
372 |
blanks += tabWidth - remainder; |
|
|
373 |
} else if (isIndentChar(c)) { |
381 |
} else if (isIndentChar(c)) { |
374 |
blanks++; |
382 |
blanks++; |
375 |
} else { |
383 |
} else { |
Lines 382-387
Link Here
|
382 |
return result + 1; |
390 |
return result + 1; |
383 |
} |
391 |
} |
384 |
|
392 |
|
|
|
393 |
/* |
394 |
* Calculates space equivalents up to the next tab stop |
395 |
*/ |
396 |
private static int calculateSpaceEquivalents(int tabWidth, int spaceEquivalents) { |
397 |
if (tabWidth == 0){ |
398 |
return spaceEquivalents; |
399 |
} |
400 |
int remainder = spaceEquivalents % tabWidth; |
401 |
spaceEquivalents += tabWidth - remainder; |
402 |
return spaceEquivalents; |
403 |
} |
404 |
|
385 |
/** |
405 |
/** |
386 |
* Returns the tab width as configured in the given map. |
406 |
* Returns the tab width as configured in the given map. |
387 |
* <p>Use {@link org.eclipse.jdt.core.IJavaProject#getOptions(boolean)} to get the most current project options.</p> |
407 |
* <p>Use {@link org.eclipse.jdt.core.IJavaProject#getOptions(boolean)} to get the most current project options.</p> |