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

The following examples show how to use org.eclipse.jdt.internal.corext.util.Strings#trimIndentation() . 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: DelegateCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 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);

		String newSource= Strings.trimIndentation(document.get(fTrackedPosition.getStartPosition(), fTrackedPosition.getLength()),
				fPreferences.tabWidth, fPreferences.indentWidth, 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 2
Source File: SourceProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String[] getBlocks(RangeMarker[] markers) throws BadLocationException {
	String[] result= new String[markers.length];
	for (int i= 0; i < markers.length; i++) {
		RangeMarker marker= markers[i];
		String content= fDocument.get(marker.getOffset(), marker.getLength());
		String lines[]= Strings.convertIntoLines(content);
		Strings.trimIndentation(lines, fTypeRoot.getJavaProject(), false);
		if (fMarkerMode == STATEMENT_MODE && lines.length == 2 && isSingleControlStatementWithoutBlock()) {
			lines[1]= CodeFormatterUtil.createIndentString(1, fTypeRoot.getJavaProject()) + lines[1];
		}
		result[i]= Strings.concatenate(lines, TextUtilities.getDefaultLineDelimiter(fDocument));
	}
	return result;
}
 
Example 3
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static String getUnindentedSource(ISourceReference sourceReference) throws JavaModelException {

			String[] lines= Strings.convertIntoLines(sourceReference.getSource());
			final IJavaProject project= ((IJavaElement) sourceReference).getJavaProject();
			Strings.trimIndentation(lines, project, false);
			return Strings.concatenate(lines, StubUtility.getLineDelimiterUsed((IJavaElement) sourceReference));
		}
 
Example 4
Source File: TypedSource.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static String getSourceOfDeclararationNode(IJavaElement elem, ICompilationUnit cu) throws JavaModelException, CoreException {
	Assert.isTrue(elem.getElementType() != IJavaElement.IMPORT_CONTAINER);
	if (elem instanceof ISourceReference) {
		ISourceReference reference= (ISourceReference) elem;
		String source= reference.getSource();
		if (source != null)
			return Strings.trimIndentation(source, cu.getJavaProject(), false);
	}
	return ""; //$NON-NLS-1$
}
 
Example 5
Source File: JavaSourceHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the trimmed source lines.
 *
 * @param source the source string, could be <code>null</code>
 * @param javaElement the java element
 * @return the trimmed source lines or <code>null</code>
 */
private String[] getTrimmedSource(String source, IJavaElement javaElement) {
	if (source == null)
		return null;
	source= removeLeadingComments(source);
	String[] sourceLines= Strings.convertIntoLines(source);
	Strings.trimIndentation(sourceLines, javaElement.getJavaProject());
	return sourceLines;
}
 
Example 6
Source File: JavaHistoryActionImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
static String trimTextBlock(String content, String delimiter, IJavaProject currentProject) {
	if (content != null) {
		String[] lines= Strings.convertIntoLines(content);
		if (lines != null) {
			Strings.trimIndentation(lines, currentProject);
			return Strings.concatenate(lines, delimiter);
		}
	}
	return null;
}
 
Example 7
Source File: HierarchyProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected static String getUnindentedText(final String text, final ICompilationUnit declaringCu) throws JavaModelException {
	final String[] lines= Strings.convertIntoLines(text);
	Strings.trimIndentation(lines, declaringCu.getJavaProject(), false);
	return Strings.concatenate(lines, StubUtility.getLineDelimiterUsed(declaringCu));
}
 
Example 8
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private String getAlignedSourceBlock(final ICompilationUnit unit, final String block) {
	Assert.isNotNull(block);
	final String[] lines= Strings.convertIntoLines(block);
	Strings.trimIndentation(lines, unit.getJavaProject(), false);
	return Strings.concatenate(lines, StubUtility.getLineDelimiterUsed(fType.getJavaProject()));
}
 
Example 9
Source File: MoveStaticMembersProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private String getUpdatedMember(IDocument document, BodyDeclaration declaration) throws BadLocationException {
	ITrackedNodePosition trackedPosition= (ITrackedNodePosition) declaration.getProperty(TRACKED_POSITION_PROPERTY);
	return Strings.trimIndentation(document.get(trackedPosition.getStartPosition(), trackedPosition.getLength()), fPreferences.tabWidth, fPreferences.indentWidth, false);
}
 
Example 10
Source File: SourceView.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected Object computeInput(Object input) {

	if (fViewer == null || !(input instanceof ISourceReference))
		return null;

	ISourceReference sourceRef= (ISourceReference)input;

	if (fLastOpenedElement != null && input instanceof IJavaElement && ((IJavaElement)input).getHandleIdentifier().equals(fLastOpenedElement.getHandleIdentifier())) {
		fLastOpenedElement= null;
		return null;
	} else {
		fLastOpenedElement= null;
	}

	String source;
	try {
		source= sourceRef.getSource();
	} catch (JavaModelException ex) {
		return ""; //$NON-NLS-1$
	}

	if (source == null)
		return ""; //$NON-NLS-1$

	source= removeLeadingComments(source);
	String delim= StubUtility.getLineDelimiterUsed((IJavaElement) input);

	String[] sourceLines= Strings.convertIntoLines(source);
	if (sourceLines == null || sourceLines.length == 0)
		return ""; //$NON-NLS-1$

	String firstLine= sourceLines[0];
	boolean firstCharNotWhitespace= firstLine != null && firstLine.length() > 0 && !Character.isWhitespace(firstLine.charAt(0));
	if (firstCharNotWhitespace)
		sourceLines[0]= ""; //$NON-NLS-1$
	IJavaProject project;
	if (input instanceof IJavaElement)
		project= ((IJavaElement) input).getJavaProject();
	else
		project= null;
	Strings.trimIndentation(sourceLines, project);

	if (firstCharNotWhitespace)
		sourceLines[0]= firstLine;

	return Strings.concatenate(sourceLines, delim);
}
 
Example 11
Source File: ExtractInterfaceProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Normalizes the indentation of the specified text.
 *
 * @param code
 *            the text to normalize
 * @return the normalized text
 * @throws JavaModelException
 *             if an error occurs
 */
protected final String normalizeText(final String code) throws JavaModelException {
	Assert.isNotNull(code);
	final String[] lines= Strings.convertIntoLines(code);
	final IJavaProject project= fSubType.getJavaProject();
	Strings.trimIndentation(lines, project, false);
	return Strings.concatenate(lines, StubUtility.getLineDelimiterUsed(project));
}