Java Code Examples for org.eclipse.jdt.internal.corext.util.CodeFormatterUtil#getTabWidth()

The following examples show how to use org.eclipse.jdt.internal.corext.util.CodeFormatterUtil#getTabWidth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaSourceViewerConfiguration.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
		IJavaProject project= getProject();
	final int tabWidth= CodeFormatterUtil.getTabWidth(project);
	final int indentWidth= CodeFormatterUtil.getIndentWidth(project);
	boolean allowTabs= tabWidth <= indentWidth;

	String indentMode;
	if (project == null)
		indentMode= JavaCore.getOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR);
	else
		indentMode= project.getOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, true);

	boolean useSpaces= JavaCore.SPACE.equals(indentMode) || DefaultCodeFormatterConstants.MIXED.equals(indentMode);

	Assert.isLegal(allowTabs || useSpaces);

	if (!allowTabs) {
		char[] spaces= new char[indentWidth];
		Arrays.fill(spaces, ' ');
		return new String[] { new String(spaces), "" }; //$NON-NLS-1$
	} else if  (!useSpaces)
		return getIndentPrefixesForTab(tabWidth);
	else
		return getIndentPrefixesForSpaces(tabWidth);
}
 
Example 2
Source File: IndentUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: DelegateCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Performs the actual rewriting and adds an edit to the ASTRewrite set with
 * {@link #setSourceRewrite(CompilationUnitRewrite)}.
 *
 * @throws JavaModelException
 */
public void createEdit() throws JavaModelException {
	try {
		IDocument document= new Document(fDelegateRewrite.getCu().getBuffer().getContents());
		TextEdit edit= fDelegateRewrite.getASTRewrite().rewriteAST(document, fDelegateRewrite.getCu().getJavaProject().getOptions(true));
		edit.apply(document, TextEdit.UPDATE_REGIONS);

		int tabWidth = CodeFormatterUtil.getTabWidth(fOriginalRewrite.getCu().getJavaProject());
		int identWidth = CodeFormatterUtil.getIndentWidth(fOriginalRewrite.getCu().getJavaProject());

		String newSource= Strings.trimIndentation(document.get(fTrackedPosition.getStartPosition(), fTrackedPosition.getLength()),
				tabWidth, identWidth, false);

		ASTNode placeholder= fOriginalRewrite.getASTRewrite().createStringPlaceholder(newSource, fDeclaration.getNodeType());

		CategorizedTextEditGroup groupDescription= fOriginalRewrite.createCategorizedGroupDescription(getTextEditGroupLabel(), CATEGORY_DELEGATE);
		ListRewrite bodyDeclarationsListRewrite= fOriginalRewrite.getASTRewrite().getListRewrite(fDeclaration.getParent(), getTypeBodyDeclarationsProperty());
		if (fCopy) {
			if (fInsertBefore) {
				bodyDeclarationsListRewrite.insertBefore(placeholder, fDeclaration, groupDescription);
			} else {
				bodyDeclarationsListRewrite.insertAfter(placeholder, fDeclaration, groupDescription);
			}
		} else {
			bodyDeclarationsListRewrite.replace(fDeclaration, placeholder, groupDescription);
		}

	} catch (BadLocationException e) {
		//JavaPlugin.log(e);
	}
}
 
Example 4
Source File: PreferenceManager.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static CodeGenerationSettings getCodeGenerationSettings(IResource resource) {
	IJavaProject project = JavaCore.create(resource.getProject());

	CodeGenerationSettings res = new CodeGenerationSettings();
	res.overrideAnnotation = true;
	res.createComments = false;
	// TODO indentation settings should be retrieved from client/external
	// settings?
	res.tabWidth = CodeFormatterUtil.getTabWidth(project);
	res.indentWidth = CodeFormatterUtil.getIndentWidth(project);
	return res;
}
 
Example 5
Source File: JavaPreferencesSettings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static CodeGenerationSettings getCodeGenerationSettings(IJavaProject project) {
	CodeGenerationSettings res= new CodeGenerationSettings();
	res.createComments= Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_ADD_COMMENTS, project)).booleanValue();
	res.useKeywordThis= Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_KEYWORD_THIS, project)).booleanValue();
	res.overrideAnnotation= Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_USE_OVERRIDE_ANNOTATION, project)).booleanValue();
	res.importIgnoreLowercase= Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.ORGIMPORTS_IGNORELOWERCASE, project)).booleanValue();
	res.tabWidth= CodeFormatterUtil.getTabWidth(project);
	res.indentWidth= CodeFormatterUtil.getIndentWidth(project);
	return res;
}
 
Example 6
Source File: JavaSourceViewerConfiguration.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public int getTabWidth(ISourceViewer sourceViewer) {
	return CodeFormatterUtil.getTabWidth(getProject());
}
 
Example 7
Source File: IndentUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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;

}
 
Example 8
Source File: JavaIndenter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private int prefTabSize() {
	return CodeFormatterUtil.getTabWidth(fProject);
}
 
Example 9
Source File: SmartTypingConfigurationBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private int getTabDisplaySize() {
	return CodeFormatterUtil.getTabWidth(null);
}
 
Example 10
Source File: JavaAutoIndentStrategy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * The preference setting for the visual tabulator display.
 *
 * @return the number of spaces displayed for a tabulator in the editor
 */
private int getVisualTabLengthPreference() {
	return CodeFormatterUtil.getTabWidth(fProject);
}