下面列出了怎么用org.eclipse.jface.text.ILineTracker的API类实例代码及写法,或者点击链接到github查看源代码。
private static String changeLineDelimiter(String code, String lineDelim) {
try {
ILineTracker tracker= new DefaultLineTracker();
tracker.set(code);
int nLines= tracker.getNumberOfLines();
if (nLines == 1) {
return code;
}
StringBuffer buf= new StringBuffer();
for (int i= 0; i < nLines; i++) {
if (i != 0) {
buf.append(lineDelim);
}
IRegion region = tracker.getLineInformation(i);
String line= code.substring(region.getOffset(), region.getOffset() + region.getLength());
buf.append(line);
}
return buf.toString();
} catch (BadLocationException e) {
// can not happen
return code;
}
}
/**
* Converts the given string into an array of lines. The lines
* don't contain any line delimiter characters.
*
* @param input the string
* @return the string converted into an array of strings. Returns <code>
* null</code> if the input string can't be converted in an array of lines.
*/
public static String[] convertIntoLines(String input) {
try {
ILineTracker tracker= new DefaultLineTracker();
tracker.set(input);
int size= tracker.getNumberOfLines();
String result[]= new String[size];
for (int i= 0; i < size; i++) {
IRegion region= tracker.getLineInformation(i);
int offset= region.getOffset();
result[i]= input.substring(offset, offset + region.getLength());
}
return result;
} catch (BadLocationException e) {
return null;
}
}
public static String trimIndentation(String source, int tabWidth, int indentWidth, boolean considerFirstLine) {
try {
ILineTracker tracker= new DefaultLineTracker();
tracker.set(source);
int size= tracker.getNumberOfLines();
if (size == 1)
return source;
String lines[]= new String[size];
for (int i= 0; i < size; i++) {
IRegion region= tracker.getLineInformation(i);
int offset= region.getOffset();
lines[i]= source.substring(offset, offset + region.getLength());
}
Strings.trimIndentation(lines, tabWidth, indentWidth, considerFirstLine);
StringBuffer result= new StringBuffer();
int last= size - 1;
for (int i= 0; i < size; i++) {
result.append(lines[i]);
if (i < last)
result.append(tracker.getLineDelimiter(i));
}
return result.toString();
} catch (BadLocationException e) {
Assert.isTrue(false,"Can not happend"); //$NON-NLS-1$
return null;
}
}
/**
* Change the indent of a, possible multiple line, code string. The given number of indent units is removed,
* and a new indent string is added.
* <p>The first line of the code will not be changed (It is considered to have no indent as it might start in
* the middle of a line).</p>
*
* @param code the code to change the indent of
* @param indentUnitsToRemove the number of indent units to remove from each line (except the first) of the given code
* @param tabWidth the size of one tab in space equivalents
* @param indentWidth the width of one indentation unit in space equivalents
* @param newIndentString the new indent string to be added to all lines (except the first)
* @param lineDelim the new line delimiter to be used. The returned code will contain only this line delimiter.
* @return the newly indent code, containing only the given line delimiters.
* @exception IllegalArgumentException if:
* <ul>
* <li>the given <code>indentWidth</code> is lower than zero</li>
* <li>the given <code>tabWidth</code> is lower than zero</li>
* <li>the given <code>code</code> is null</li>
* <li>the given <code>indentUnitsToRemove</code> is lower than zero</li>
* <li>the given <code>newIndentString</code> is null</li>
* <li>the given <code>lineDelim</code> is null</li>
* </ul>
*/
public static String changeIndent(String code, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString, String lineDelim) {
if (tabWidth < 0 || indentWidth < 0 || code == null || indentUnitsToRemove < 0 || newIndentString == null || lineDelim == null) {
throw new IllegalArgumentException();
}
try {
ILineTracker tracker= new DefaultLineTracker();
tracker.set(code);
int nLines= tracker.getNumberOfLines();
if (nLines == 1) {
return code;
}
StringBuffer buf= new StringBuffer();
for (int i= 0; i < nLines; i++) {
IRegion region= tracker.getLineInformation(i);
int start= region.getOffset();
int end= start + region.getLength();
String line= code.substring(start, end);
if (i == 0) { // no indent for first line (contained in the formatted string)
buf.append(line);
} else { // no new line after last line
buf.append(lineDelim);
buf.append(newIndentString);
if(indentWidth != 0) {
buf.append(trimIndent(line, indentUnitsToRemove, tabWidth, indentWidth));
} else {
buf.append(line);
}
}
}
return buf.toString();
} catch (BadLocationException e) {
// can not happen
return code;
}
}
/**
* Returns the text edits retrieved after changing the indentation of a, possible multi-line, code string.
*
* <p>The given number of indent units is removed, and a new indent string is added.</p>
* <p>The first line of the code will not be changed (It is considered to have no indent as it might start in
* the middle of a line).</p>
*
* @param source The code to change the indent of
* @param indentUnitsToRemove the number of indent units to remove from each line (except the first) of the given code
* @param tabWidth the size of one tab in space equivalents
* @param indentWidth the width of one indentation unit in space equivalents
* @param newIndentString the new indent string to be added to all lines (except the first)
* @return returns the resulting text edits
* @exception IllegalArgumentException if:
* <ul>
* <li>the given <code>indentWidth</code> is lower than zero</li>
* <li>the given <code>tabWidth</code> is lower than zero</li>
* <li>the given <code>source</code> is null</li>
* <li>the given <code>indentUnitsToRemove</code> is lower than zero</li>
* <li>the given <code>newIndentString</code> is null</li>
* </ul>
*/
public static ReplaceEdit[] getChangeIndentEdits(String source, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString) {
if (tabWidth < 0 || indentWidth < 0 || source == null || indentUnitsToRemove < 0 || newIndentString == null) {
throw new IllegalArgumentException();
}
ArrayList result= new ArrayList();
try {
ILineTracker tracker= new DefaultLineTracker();
tracker.set(source);
int nLines= tracker.getNumberOfLines();
if (nLines == 1)
return (ReplaceEdit[])result.toArray(new ReplaceEdit[result.size()]);
for (int i= 1; i < nLines; i++) {
IRegion region= tracker.getLineInformation(i);
int offset= region.getOffset();
String line= source.substring(offset, offset + region.getLength());
int length= indexOfIndent(line, indentUnitsToRemove, tabWidth, indentWidth);
if (length >= 0) {
result.add(new ReplaceEdit(offset, length, newIndentString));
} else {
length= measureIndentUnits(line, tabWidth, indentWidth);
result.add(new ReplaceEdit(offset, length, "")); //$NON-NLS-1$
}
}
} catch (BadLocationException cannotHappen) {
// can not happen
}
return (ReplaceEdit[])result.toArray(new ReplaceEdit[result.size()]);
}
public ILineTracker createLineTracker(Object element) {
return new DefaultLineTracker();
}
/**
* Creates a line tracker for the given element. It is of the same kind as the one that would be
* used for a newly created document for the given element.
*
* @param element the element
* @return a line tracker for the given element
*/
ILineTracker createLineTracker(Object element);