当前位置: 首页>>代码示例>>Java>>正文


Java JavaIndenter.computeIndentation方法代码示例

本文整理汇总了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;
}
 
开发者ID:eclipse,项目名称:che,代码行数:62,代码来源:IndentUtil.java

示例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;

}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:66,代码来源:IndentUtil.java

示例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);
	}

}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:42,代码来源:JavaAutoIndentStrategy.java


注:本文中的org.eclipse.jdt.internal.ui.text.JavaIndenter.computeIndentation方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。