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


Java JavaIndenter类代码示例

本文整理汇总了Java中org.eclipse.jdt.internal.ui.text.JavaIndenter的典型用法代码示例。如果您正苦于以下问题:Java JavaIndenter类的具体用法?Java JavaIndenter怎么用?Java JavaIndenter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


JavaIndenter类属于org.eclipse.jdt.internal.ui.text包,在下文中一共展示了JavaIndenter类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: indentLines

import org.eclipse.jdt.internal.ui.text.JavaIndenter; //导入依赖的package包/类
/**
 * Indents the line range specified by <code>lines</code> in <code>document</code>. 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 indented
 * @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>indentLines</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 indentLines(
    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);

  JavaHeuristicScanner scanner = new JavaHeuristicScanner(document);
  JavaIndenter indenter = new JavaIndenter(document, scanner, project);
  boolean changed = false;
  int tabSize = CodeFormatterUtil.getTabWidth(project);
  for (int line = lines.getStartLine(), last = line + numberOfLines, i = 0; line < last; line++) {
    changed |=
        indentLine(
            document, line, indenter, scanner, result.commentLinesAtColumnZero, i++, tabSize);
  }
  result.hasChanged = changed;

  return result;
}
 
开发者ID:eclipse,项目名称:che,代码行数:39,代码来源:IndentUtil.java

示例2: indentLines

import org.eclipse.jdt.internal.ui.text.JavaIndenter; //导入依赖的package包/类
/**
 * Indents the line range specified by <code>lines</code> in
 * <code>document</code>. 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 indented
 * @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>indentLines</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 indentLines(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);

	JavaHeuristicScanner scanner= new JavaHeuristicScanner(document);
	JavaIndenter indenter= new JavaIndenter(document, scanner, project);
	boolean changed= false;
	int tabSize= CodeFormatterUtil.getTabWidth(project);
	for (int line= lines.getStartLine(), last= line + numberOfLines, i= 0; line < last; line++) {
		changed |= indentLine(document, line, indenter, scanner, result.commentLinesAtColumnZero, i++, tabSize);
	}
	result.hasChanged= changed;

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

示例3: indentLine

import org.eclipse.jdt.internal.ui.text.JavaIndenter; //导入依赖的package包/类
/**
 * Indents a single line using the java heuristic scanner. Javadoc and multiline comments are
 * indented as specified by the <code>JavaDocAutoIndentStrategy</code>.
 *
 * @param document the document
 * @param line the line to be indented
 * @param caret the caret position
 * @param indenter the java indenter
 * @param scanner the heuristic scanner
 * @param multiLine <code>true</code> if more than one line is being indented
 * @return <code>true</code> if <code>document</code> was modified, <code>false</code> otherwise
 * @throws BadLocationException if the document got changed concurrently
 */
private boolean indentLine(IDocument document, int line, int caret, JavaIndenter indenter, JavaHeuristicScanner scanner, boolean multiLine) throws BadLocationException {
	IJavaProject project= getJavaProject();
	ReplaceData data= computeReplaceData(document, line, indenter, scanner, multiLine, fIsTabAction, project);

	String indent= data.indent;
	int end= data.end;
	int offset= data.offset;

	int length= end - offset;
	String currentIndent= document.get(offset, length);

	// if we are right before the text start / line end, and already after the insertion point
	// then just insert a tab.
	if (fIsTabAction && caret == end && whiteSpaceLength(currentIndent, project) >= whiteSpaceLength(indent, project)) {
		String tab= getTabEquivalent(project);
		document.replace(caret, 0, tab);
		fCaretOffset= caret + tab.length();
		return true;
	}

	// set the caret offset so it can be used when setting the selection
	if (caret >= offset && caret <= end)
		fCaretOffset= offset + indent.length();
	else
		fCaretOffset= -1;

	// only change the document if it is a real change
	if (!indent.equals(currentIndent)) {
		document.replace(offset, length, indent);
		return true;
	} else
		return false;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:47,代码来源:IndentAction.java

示例4: smartIndentAfterClosingBracket

import org.eclipse.jdt.internal.ui.text.JavaIndenter; //导入依赖的package包/类
private void smartIndentAfterClosingBracket(IDocument d, DocumentCommand c) {
	if (c.offset == -1 || d.getLength() == 0)
		return;

	try {
		int p= (c.offset == d.getLength() ? c.offset - 1 : c.offset);
		int line= d.getLineOfOffset(p);
		int start= d.getLineOffset(line);
		int whiteend= findEndOfWhiteSpace(d, start, c.offset);

		JavaHeuristicScanner scanner= new JavaHeuristicScanner(d);
		JavaIndenter indenter= new JavaIndenter(d, scanner, fProject);

		// shift only when line does not contain any text up to the closing bracket
		if (whiteend == c.offset) {
			// evaluate the line with the opening bracket that matches out closing bracket
			int reference= indenter.findReferencePosition(c.offset, false, true, false, false);
			int indLine= d.getLineOfOffset(reference);
			if (indLine != -1 && indLine != line) {
				// take the indent of the found line
				StringBuffer replaceText= new StringBuffer(getIndentOfLine(d, indLine));
				// add the rest of the current line including the just added close bracket
				replaceText.append(d.get(whiteend, c.offset - whiteend));
				replaceText.append(c.text);
				// modify document command
				c.length += c.offset - start;
				c.offset= start;
				c.text= replaceText.toString();
			}
		}
	} catch (BadLocationException e) {
		JavaPlugin.log(e);
	}
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:35,代码来源:JavaAutoIndentStrategy.java

示例5: 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

示例6: 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

示例7: indent

import org.eclipse.jdt.internal.ui.text.JavaIndenter; //导入依赖的package包/类
/**
 * Indent the given <code>document</code> based on the <code>project</code> settings and
 * return a text edit describing the changes applied to the document. Returns <b>null</b>
 * if no changes have been applied.
 * <p>
 * WARNING: This method does change the content of the given document.
 * </p>
 * <p>
 * This method is for internal use only, it should not be called.
 * </p>
 *
 * @param document the document to indent must have a java partitioning installed
 * @param project the project to retrieve the indentation settings from, <b>null</b> for workspace settings
 * @return a text edit describing the changes or <b>null</b> if no changes required
 * @throws BadLocationException if the document got modified concurrently
 *
 * @since 3.4
 */
public static TextEdit indent(IDocument document, IJavaProject project) throws BadLocationException {
	int offset= 0;
	int length= document.getLength();

	JavaHeuristicScanner scanner= new JavaHeuristicScanner(document);
	JavaIndenter indenter= new JavaIndenter(document, scanner, project);

	ArrayList<ReplaceEdit> edits= new ArrayList<ReplaceEdit>();

	int firstLine= document.getLineOfOffset(offset);
	// check for marginal (zero-length) lines
	int minusOne= length == 0 ? 0 : 1;
	int numberOfLines= document.getLineOfOffset(offset + length - minusOne) - firstLine + 1;

	int shift= 0;
	for (int i= 0; i < numberOfLines; i++) {
		ReplaceData data= computeReplaceData(document, firstLine + i, indenter, scanner, numberOfLines > 1, false, project);

		int replaceLength= data.end - data.offset;
		String currentIndent= document.get(data.offset, replaceLength);

		// only change the document if it is a real change
		if (!data.indent.equals(currentIndent)) {
			edits.add(new ReplaceEdit(data.offset + shift, replaceLength, data.indent));
			//We need to change the document, the indenter depends on it.
			document.replace(data.offset, replaceLength, data.indent);
			shift-= data.indent.length() - replaceLength;
		}
	}

	if (edits.size() == 0)
		return null;

	if (edits.size() == 1)
		return edits.get(0);

	MultiTextEdit result= new MultiTextEdit();
	for (Iterator<ReplaceEdit> iterator= edits.iterator(); iterator.hasNext();) {
		TextEdit edit= iterator.next();
		result.addChild(edit);
	}

	return result;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:63,代码来源:IndentAction.java

示例8: 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类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。