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

The following examples show how to use org.eclipse.jdt.internal.corext.util.CodeFormatterUtil#getIndentWidth() . 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: 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 3
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 4
Source File: JavaAutoIndentStrategy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Indents line <code>line</code> in <code>document</code> with <code>indent</code>.
 * Leaves leading comment signs alone.
 *
 * @param document the document
 * @param line the line
 * @param indent the indentation to insert
 * @param tabLength the length of a tab
 * @throws BadLocationException on concurrent document modification
 */
private void addIndent(Document document, int line, CharSequence indent, int tabLength) throws BadLocationException {
	IRegion region= document.getLineInformation(line);
	int insert= region.getOffset();
	int endOffset= region.getOffset() + region.getLength();

	// Compute insert after all leading line comment markers
	int newInsert= insert;
	while (newInsert < endOffset - 2 && document.get(newInsert, 2).equals(LINE_COMMENT))
		newInsert += 2;

	// Heuristic to check whether it is commented code or just a comment
	if (newInsert > insert) {
		int whitespaceCount= 0;
		int i= newInsert;
		while (i < endOffset - 1) {
			 char ch= document.get(i, 1).charAt(0);
			 if (!Character.isWhitespace(ch))
				 break;
			 whitespaceCount= whitespaceCount + computeVisualLength(ch, tabLength);
			 i++;
		}

		if (whitespaceCount != 0 && whitespaceCount >= CodeFormatterUtil.getIndentWidth(fProject))
			insert= newInsert;
	}

	// Insert indent
	document.replace(insert, 0, indent.toString());
}
 
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: JavaIndenter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private int prefIndentationSize() {
	return CodeFormatterUtil.getIndentWidth(fProject);
}
 
Example 7
Source File: SmartTypingConfigurationBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private int getIndentSize() {
	return CodeFormatterUtil.getIndentWidth(null);
}