本文整理汇总了Java中org.eclipse.jdt.internal.ui.text.JavaIndenter.computeIndentation方法的典型用法代码示例。如果您正苦于以下问题:Java JavaIndenter.computeIndentation方法的具体用法?Java JavaIndenter.computeIndentation怎么用?Java JavaIndenter.computeIndentation使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.internal.ui.text.JavaIndenter
的用法示例。
在下文中一共展示了JavaIndenter.computeIndentation方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: shiftLines
import org.eclipse.jdt.internal.ui.text.JavaIndenter; //导入方法依赖的package包/类
/**
* Shifts the line range specified by <code>lines</code> in <code>document</code>. The amount that
* the lines get shifted are determined by the first line in the range, all subsequent lines are
* adjusted accordingly. The passed Java project may be <code>null</code>, it is used solely to
* obtain formatter preferences.
*
* @param document the document to be changed
* @param lines the line range to be shifted
* @param project the Java project to get the formatter preferences from, or <code>null</code> if
* global preferences should be used
* @param result the result from a previous call to <code>shiftLines</code>, in order to maintain
* comment line properties, or <code>null</code>. Note that the passed result may be changed
* by the call.
* @return an indent result that may be queried for changes and can be reused in subsequent
* indentation operations
* @throws BadLocationException if <code>lines</code> is not a valid line range on <code>document
* </code>
*/
public static IndentResult shiftLines(
IDocument document, ILineRange lines, IJavaProject project, IndentResult result)
throws BadLocationException {
int numberOfLines = lines.getNumberOfLines();
if (numberOfLines < 1) return new IndentResult(null);
result = reuseOrCreateToken(result, numberOfLines);
result.hasChanged = false;
JavaHeuristicScanner scanner = new JavaHeuristicScanner(document);
JavaIndenter indenter = new JavaIndenter(document, scanner, project);
String current = getCurrentIndent(document, lines.getStartLine());
StringBuffer correct =
indenter.computeIndentation(document.getLineOffset(lines.getStartLine()));
if (correct == null) return result; // bail out
int tabSize = CodeFormatterUtil.getTabWidth(project);
StringBuffer addition = new StringBuffer();
int difference = subtractIndent(correct, current, addition, tabSize);
if (difference == 0) return result;
if (result.leftmostLine == -1) result.leftmostLine = getLeftMostLine(document, lines, tabSize);
int maxReduction =
computeVisualLength(
getCurrentIndent(document, result.leftmostLine + lines.getStartLine()), tabSize);
if (difference > 0) {
for (int line = lines.getStartLine(), last = line + numberOfLines, i = 0; line < last; line++)
addIndent(document, line, addition, result.commentLinesAtColumnZero, i++);
} else {
int reduction = Math.min(-difference, maxReduction);
for (int line = lines.getStartLine(), last = line + numberOfLines, i = 0; line < last; line++)
cutIndent(document, line, reduction, tabSize, result.commentLinesAtColumnZero, i++);
}
result.hasChanged = true;
return result;
}
示例2: shiftLines
import org.eclipse.jdt.internal.ui.text.JavaIndenter; //导入方法依赖的package包/类
/**
* Shifts the line range specified by <code>lines</code> in
* <code>document</code>. The amount that the lines get shifted
* are determined by the first line in the range, all subsequent
* lines are adjusted accordingly. The passed Java project may be
* <code>null</code>, it is used solely to obtain formatter
* preferences.
*
* @param document the document to be changed
* @param lines the line range to be shifted
* @param project the Java project to get the formatter preferences
* from, or <code>null</code> if global preferences should
* be used
* @param result the result from a previous call to
* <code>shiftLines</code>, in order to maintain comment
* line properties, or <code>null</code>. Note that the
* passed result may be changed by the call.
* @return an indent result that may be queried for changes and can
* be reused in subsequent indentation operations
* @throws BadLocationException if <code>lines</code> is not a
* valid line range on <code>document</code>
*/
public static IndentResult shiftLines(IDocument document, ILineRange lines, IJavaProject project, IndentResult result) throws BadLocationException {
int numberOfLines= lines.getNumberOfLines();
if (numberOfLines < 1)
return new IndentResult(null);
result= reuseOrCreateToken(result, numberOfLines);
result.hasChanged= false;
JavaHeuristicScanner scanner= new JavaHeuristicScanner(document);
JavaIndenter indenter= new JavaIndenter(document, scanner, project);
String current= getCurrentIndent(document, lines.getStartLine());
StringBuffer correct= indenter.computeIndentation(document.getLineOffset(lines.getStartLine()));
if (correct == null)
return result; // bail out
int tabSize= CodeFormatterUtil.getTabWidth(project);
StringBuffer addition= new StringBuffer();
int difference= subtractIndent(correct, current, addition, tabSize);
if (difference == 0)
return result;
if (result.leftmostLine == -1)
result.leftmostLine= getLeftMostLine(document, lines, tabSize);
int maxReduction= computeVisualLength(getCurrentIndent(document, result.leftmostLine + lines.getStartLine()), tabSize);
if (difference > 0) {
for (int line= lines.getStartLine(), last= line + numberOfLines, i= 0; line < last; line++)
addIndent(document, line, addition, result.commentLinesAtColumnZero, i++);
} else {
int reduction= Math.min(-difference, maxReduction);
for (int line= lines.getStartLine(), last= line + numberOfLines, i= 0; line < last; line++)
cutIndent(document, line, reduction, tabSize, result.commentLinesAtColumnZero, i++);
}
result.hasChanged= true;
return result;
}
示例3: smartIndentAfterOpeningBracket
import org.eclipse.jdt.internal.ui.text.JavaIndenter; //导入方法依赖的package包/类
private void smartIndentAfterOpeningBracket(IDocument d, DocumentCommand c) {
if (c.offset < 1 || d.getLength() == 0)
return;
JavaHeuristicScanner scanner= new JavaHeuristicScanner(d);
int p= (c.offset == d.getLength() ? c.offset - 1 : c.offset);
try {
// current line
int line= d.getLineOfOffset(p);
int lineOffset= d.getLineOffset(line);
// make sure we don't have any leading comments etc.
if (d.get(lineOffset, p - lineOffset).trim().length() != 0)
return;
// line of last Java code
int pos= scanner.findNonWhitespaceBackward(p, JavaHeuristicScanner.UNBOUND);
if (pos == -1)
return;
int lastLine= d.getLineOfOffset(pos);
// only shift if the last java line is further up and is a braceless block candidate
if (lastLine < line) {
JavaIndenter indenter= new JavaIndenter(d, scanner, fProject);
StringBuffer indent= indenter.computeIndentation(p, true);
String toDelete= d.get(lineOffset, c.offset - lineOffset);
if (indent != null && !indent.toString().equals(toDelete)) {
c.text= indent.append(c.text).toString();
c.length += c.offset - lineOffset;
c.offset= lineOffset;
}
}
} catch (BadLocationException e) {
JavaPlugin.log(e);
}
}