Java Code Examples for org.eclipse.jdt.internal.corext.util.Strings#computeIndentUnits()

The following examples show how to use org.eclipse.jdt.internal.corext.util.Strings#computeIndentUnits() . 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: CodeRefactoringUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static int getIndentationLevel(ASTNode node, ICompilationUnit unit) throws CoreException {
	IPath fullPath= unit.getCorrespondingResource().getFullPath();
	try{
		FileBuffers.getTextFileBufferManager().connect(fullPath, LocationKind.IFILE, new NullProgressMonitor());
		ITextFileBuffer buffer= FileBuffers.getTextFileBufferManager().getTextFileBuffer(fullPath, LocationKind.IFILE);
		try {
			IRegion region= buffer.getDocument().getLineInformationOfOffset(node.getStartPosition());
			return Strings.computeIndentUnits(buffer.getDocument().get(region.getOffset(), region.getLength()), unit.getJavaProject());
		} catch (BadLocationException exception) {
			JavaPlugin.log(exception);
		}
		return 0;
	} finally {
		FileBuffers.getTextFileBufferManager().disconnect(fullPath, LocationKind.IFILE, new NullProgressMonitor());
	}
}
 
Example 2
Source File: PullUpMethodPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void setSourceViewerContents(String contents) {
	if (contents != null) {
		final IJavaProject project= fProcessor.getDestinationType().getJavaProject();
		final String[] lines= Strings.convertIntoLines(contents);
		if (lines.length > 0) {
			final int indent= Strings.computeIndentUnits(lines[lines.length - 1], project);
			contents= Strings.changeIndent(contents, indent, project, "", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}
	final IDocument document= (contents == null) ? new Document() : new Document(contents);
	JavaPlugin.getDefault().getJavaTextTools().setupJavaDocumentPartitioner(document);
	fSourceViewer.setDocument(document);
}
 
Example 3
Source File: JavaContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the indentation level at the position of code completion.
 *
 * @return the indentation level at the position of the code completion
 */
private int getIndentation() {
	int start= getStart();
	IDocument document= getDocument();
	try {
		IRegion region= document.getLineInformationOfOffset(start);
		String lineContent= document.get(region.getOffset(), region.getLength());
		IJavaProject project= getJavaProject();
		return Strings.computeIndentUnits(lineContent, project);
	} catch (BadLocationException e) {
		return 0;
	}
}
 
Example 4
Source File: JavaDocContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the indentation level at the position of code completion.
 *
 * @return the indentation level at the position of the code completion
 */
private int getIndentation() {
	int start= getStart();
	IDocument document= getDocument();
	try {
		IRegion region= document.getLineInformationOfOffset(start);
		String lineContent= document.get(region.getOffset(), region.getLength());
		IJavaProject project= getJavaProject();
		return Strings.computeIndentUnits(lineContent, project);
	} catch (BadLocationException e) {
		return 0;
	}
}
 
Example 5
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static int getIndentUsed(IBuffer buffer, int offset, IJavaProject project) {
	int i= offset;
	// find beginning of line
	while (i > 0 && !IndentManipulation.isLineDelimiterChar(buffer.getChar(i - 1))) {
		i--;
	}
	return Strings.computeIndentUnits(buffer.getText(i, offset - i), project);
}
 
Example 6
Source File: GetterSetterCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {

	CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fField.getJavaProject());
	boolean addComments= settings.createComments;
	int flags= Flags.AccPublic | (fField.getFlags() & Flags.AccStatic);

	String stub;
	if (fIsGetter) {
		String getterName= GetterSetterUtil.getGetterName(fField, null);
		stub= GetterSetterUtil.getGetterStub(fField, getterName, addComments, flags);
	} else {
		String setterName= GetterSetterUtil.getSetterName(fField, null);
		stub= GetterSetterUtil.getSetterStub(fField, setterName, addComments, flags);
	}

	// use the code formatter
	String lineDelim= TextUtilities.getDefaultLineDelimiter(document);

	IRegion region= document.getLineInformationOfOffset(getReplacementOffset());
	int lineStart= region.getOffset();
	int indent= Strings.computeIndentUnits(document.get(lineStart, getReplacementOffset() - lineStart), settings.tabWidth, settings.indentWidth);

	String replacement= CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, indent, lineDelim, fField.getJavaProject());

	if (replacement.endsWith(lineDelim)) {
		replacement= replacement.substring(0, replacement.length() - lineDelim.length());
	}

	setReplacementString(Strings.trimLeadingTabsAndSpaces(replacement));
	return true;
}
 
Example 7
Source File: MethodDeclarationCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {

	CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fType.getJavaProject());
	boolean addComments= settings.createComments;

	String[] empty= new String[0];
	String lineDelim= TextUtilities.getDefaultLineDelimiter(document);
	String declTypeName= fType.getTypeQualifiedName('.');
	boolean isInterface= fType.isInterface();

	StringBuffer buf= new StringBuffer();
	if (addComments) {
		String comment= CodeGeneration.getMethodComment(fType.getCompilationUnit(), declTypeName, fMethodName, empty, empty, fReturnTypeSig, empty, null, lineDelim);
		if (comment != null) {
			buf.append(comment);
			buf.append(lineDelim);
		}
	}
	if (fReturnTypeSig != null) {
		if (!isInterface) {
			buf.append("private "); //$NON-NLS-1$
		}
	} else {
		if (fType.isEnum())
			buf.append("private "); //$NON-NLS-1$
		else
			buf.append("public "); //$NON-NLS-1$
	}

	if (fReturnTypeSig != null) {
		buf.append(Signature.toString(fReturnTypeSig));
	}
	buf.append(' ');
	buf.append(fMethodName);
	if (isInterface) {
		buf.append("();"); //$NON-NLS-1$
		buf.append(lineDelim);
	} else {
		buf.append("() {"); //$NON-NLS-1$
		buf.append(lineDelim);

		String body= CodeGeneration.getMethodBodyContent(fType.getCompilationUnit(), declTypeName, fMethodName, fReturnTypeSig == null, "", lineDelim); //$NON-NLS-1$
		if (body != null) {
			buf.append(body);
			buf.append(lineDelim);
		}
		buf.append("}"); //$NON-NLS-1$
		buf.append(lineDelim);
	}
	String stub=  buf.toString();

	// use the code formatter
	IRegion region= document.getLineInformationOfOffset(getReplacementOffset());
	int lineStart= region.getOffset();
	int indent= Strings.computeIndentUnits(document.get(lineStart, getReplacementOffset() - lineStart), settings.tabWidth, settings.indentWidth);

	String replacement= CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, indent, lineDelim, fType.getJavaProject());

	if (replacement.endsWith(lineDelim)) {
		replacement= replacement.substring(0, replacement.length() - lineDelim.length());
	}

	setReplacementString(Strings.trimLeadingTabsAndSpaces(replacement));
	return true;
}
 
Example 8
Source File: AnonymousTypeCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {
	fImportRewrite= impRewrite;
	String newBody= createNewBody(impRewrite);
	if (newBody == null)
		return false;

	CompletionProposal coreProposal= ((MemberProposalInfo)getProposalInfo()).fProposal;
	boolean isAnonymousConstructorInvoc= coreProposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION;

	boolean replacementStringEndsWithParentheses= isAnonymousConstructorInvoc || getReplacementString().endsWith(")"); //$NON-NLS-1$

	// construct replacement text: an expression to be formatted
	StringBuffer buf= new StringBuffer("new A("); //$NON-NLS-1$
	if (!replacementStringEndsWithParentheses || isAnonymousConstructorInvoc)
		buf.append(')');
	buf.append(newBody);

	// use the code formatter
	String lineDelim= TextUtilities.getDefaultLineDelimiter(document);
	final IJavaProject project= fCompilationUnit.getJavaProject();
	IRegion lineInfo= document.getLineInformationOfOffset(getReplacementOffset());
	int indent= Strings.computeIndentUnits(document.get(lineInfo.getOffset(), lineInfo.getLength()), project);

	Map<String, String> options= project != null ? project.getOptions(true) : JavaCore.getOptions();
	options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_EMPTY_LINES, DefaultCodeFormatterConstants.TRUE);
	String replacementString= CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, buf.toString(), 0, lineDelim, options);

	int lineEndOffset= lineInfo.getOffset() + lineInfo.getLength();

	int p= offset;
	char ch= document.getChar(p);
	while (p < lineEndOffset) {
		if (ch == '(' || ch == ')' || ch == ';' || ch == ',')
			break;
		ch= document.getChar(++p);
	}

	if (ch != ';' && ch != ',' && ch != ')')
		replacementString= replacementString + ';';

	replacementString= Strings.changeIndent(replacementString, 0, project, CodeFormatterUtil.createIndentString(indent, project), lineDelim);
	
	int beginIndex= replacementString.indexOf('(');
	if (!isAnonymousConstructorInvoc)
		beginIndex++;
	replacementString= replacementString.substring(beginIndex);

	int pos= offset;
	if (isAnonymousConstructorInvoc && (insertCompletion() ^ isInsertModeToggled())) {
		// Keep existing code
		int endPos= pos;
		ch= document.getChar(endPos);
		while (endPos < lineEndOffset && ch != '(' && ch != ')' && ch != ';' && ch != ',' && !Character.isWhitespace(ch))
			ch= document.getChar(++endPos);

		int keepLength= endPos - pos;
		if (keepLength > 0) {
			String keepStr= document.get(pos, keepLength);
			replacementString= replacementString + keepStr;
			setCursorPosition(replacementString.length() - keepLength);
		}
	} else
		setCursorPosition(replacementString.length());
	
	setReplacementString(replacementString);

	if (pos < document.getLength() && document.getChar(pos) == ')') {
		int currentLength= getReplacementLength();
		if (replacementStringEndsWithParentheses)
			setReplacementLength(currentLength + pos - offset);
		else
			setReplacementLength(currentLength + pos - offset + 1);
	}
	return false;
}