use of org.eclipse.text.edits.RangeMarker in project che by eclipse.
the class SourceProvider method getBlocks.
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;
}
use of org.eclipse.text.edits.RangeMarker in project che by eclipse.
the class SourceProvider method getCodeBlocks.
public String[] getCodeBlocks(CallContext context, ImportRewrite importRewrite) throws CoreException {
final ASTRewrite rewriter = ASTRewrite.create(fDeclaration.getAST());
replaceParameterWithExpression(rewriter, context, importRewrite);
updateImplicitReceivers(rewriter, context);
makeNamesUnique(rewriter, context.scope);
updateTypeReferences(rewriter, context);
updateStaticReferences(rewriter, context);
updateTypeVariables(rewriter, context);
updateMethodTypeVariable(rewriter, context);
List<IRegion> ranges = null;
if (hasReturnValue()) {
if (context.callMode == ASTNode.RETURN_STATEMENT) {
ranges = getStatementRanges();
} else {
ranges = getExpressionRanges();
}
} else {
ASTNode last = getLastStatement();
if (last != null && last.getNodeType() == ASTNode.RETURN_STATEMENT) {
ranges = getReturnStatementRanges();
} else {
ranges = getStatementRanges();
}
}
final TextEdit dummy = rewriter.rewriteAST(fDocument, fTypeRoot.getJavaProject().getOptions(true));
int size = ranges.size();
RangeMarker[] markers = new RangeMarker[size];
for (int i = 0; i < markers.length; i++) {
IRegion range = ranges.get(i);
markers[i] = new RangeMarker(range.getOffset(), range.getLength());
}
int split;
if (size <= 1) {
split = Integer.MAX_VALUE;
} else {
IRegion region = ranges.get(0);
split = region.getOffset() + region.getLength();
}
TextEdit[] edits = dummy.removeChildren();
for (int i = 0; i < edits.length; i++) {
TextEdit edit = edits[i];
int pos = edit.getOffset() >= split ? 1 : 0;
markers[pos].addChild(edit);
}
MultiTextEdit root = new MultiTextEdit(0, fDocument.getLength());
root.addChildren(markers);
try {
TextEditProcessor processor = new TextEditProcessor(fDocument, root, TextEdit.CREATE_UNDO | TextEdit.UPDATE_REGIONS);
UndoEdit undo = processor.performEdits();
String[] result = getBlocks(markers);
// It is faster to undo the changes than coping the buffer over and over again.
processor = new TextEditProcessor(fDocument, undo, TextEdit.UPDATE_REGIONS);
processor.performEdits();
return result;
} catch (MalformedTreeException exception) {
JavaPlugin.log(exception);
} catch (BadLocationException exception) {
JavaPlugin.log(exception);
}
return new String[] {};
}
use of org.eclipse.text.edits.RangeMarker in project che by eclipse.
the class InlineTempRefactoring method createParameterizedInvocation.
private String createParameterizedInvocation(Expression invocation, ITypeBinding[] typeArguments, CompilationUnitRewrite cuRewrite) throws JavaModelException {
ASTRewrite rewrite = ASTRewrite.create(invocation.getAST());
ListRewrite typeArgsRewrite = Invocations.getInferredTypeArgumentsRewrite(rewrite, invocation);
for (int i = 0; i < typeArguments.length; i++) {
Type typeArgumentNode = cuRewrite.getImportRewrite().addImport(typeArguments[i], cuRewrite.getAST());
typeArgsRewrite.insertLast(typeArgumentNode, null);
}
if (invocation instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation) invocation;
Expression expression = methodInvocation.getExpression();
if (expression == null) {
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
if (methodBinding != null && Modifier.isStatic(methodBinding.getModifiers())) {
expression = cuRewrite.getAST().newName(cuRewrite.getImportRewrite().addImport(methodBinding.getDeclaringClass().getTypeDeclaration()));
} else {
expression = invocation.getAST().newThisExpression();
}
rewrite.set(invocation, MethodInvocation.EXPRESSION_PROPERTY, expression, null);
}
}
IDocument document = new Document(fCu.getBuffer().getContents());
final RangeMarker marker = new RangeMarker(invocation.getStartPosition(), invocation.getLength());
IJavaProject project = fCu.getJavaProject();
TextEdit[] rewriteEdits = rewrite.rewriteAST(document, project.getOptions(true)).removeChildren();
marker.addChildren(rewriteEdits);
try {
marker.apply(document, TextEdit.UPDATE_REGIONS);
String rewrittenInitializer = document.get(marker.getOffset(), marker.getLength());
IRegion region = document.getLineInformation(document.getLineOfOffset(marker.getOffset()));
int oldIndent = Strings.computeIndentUnits(document.get(region.getOffset(), region.getLength()), project);
//$NON-NLS-1$
return Strings.changeIndent(rewrittenInitializer, oldIndent, project, "", TextUtilities.getDefaultLineDelimiter(document));
} catch (MalformedTreeException e) {
JavaPlugin.log(e);
} catch (BadLocationException e) {
JavaPlugin.log(e);
}
//fallback:
return fCu.getBuffer().getText(invocation.getStartPosition(), invocation.getLength());
}
Aggregations