use of org.eclipse.jface.text.IDocument in project che by eclipse.
the class DelegateCreator method createEdit.
/**
* 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);
}
}
use of org.eclipse.jface.text.IDocument in project che by eclipse.
the class ReorgCorrectionsSubProcessor method removeImportStatementProposals.
public static void removeImportStatementProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
IProposableFix fix = UnusedCodeFix.createRemoveUnusedImportFix(context.getASTRoot(), problem);
if (fix != null) {
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_DELETE_IMPORT);
Map<String, String> options = new Hashtable<String, String>();
options.put(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS, CleanUpOptions.TRUE);
FixCorrectionProposal proposal = new FixCorrectionProposal(fix, new UnusedCodeCleanUp(options), IProposalRelevance.REMOVE_UNUSED_IMPORT, image, context);
proposals.add(proposal);
}
final ICompilationUnit cu = context.getCompilationUnit();
String name = CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description;
ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(name, null, IProposalRelevance.ORGANIZE_IMPORTS, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE)) {
@Override
public void apply(IDocument document) {
// }
throw new UnsupportedOperationException("apply doesn't supported for action proposal");
}
@Override
public String getActionId() {
return "javaOrganizeImports";
}
};
proposals.add(proposal);
}
use of org.eclipse.jface.text.IDocument in project che by eclipse.
the class LocalCorrectionsSubProcessor method getInvalidOperatorProposals.
public static void getInvalidOperatorProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
CompilationUnit root = context.getASTRoot();
AST ast = root.getAST();
ASTNode selectedNode = problem.getCoveringNode(root);
while (selectedNode instanceof ParenthesizedExpression) {
selectedNode = ((ParenthesizedExpression) selectedNode).getExpression();
}
if (selectedNode instanceof PrefixExpression) {
// !x instanceof X -> !(x instanceof X)
PrefixExpression expression = (PrefixExpression) selectedNode;
if (expression.getOperator() == PrefixExpression.Operator.NOT) {
ASTNode parent = expression.getParent();
String label = null;
switch(parent.getNodeType()) {
case ASTNode.INSTANCEOF_EXPRESSION:
label = CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_instanceof_description;
break;
case ASTNode.INFIX_EXPRESSION:
InfixExpression infixExpression = (InfixExpression) parent;
label = Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_description, infixExpression.getOperator().toString());
break;
}
if (label != null) {
ASTRewrite rewrite = ASTRewrite.create(ast);
rewrite.replace(selectedNode, rewrite.createMoveTarget(expression.getOperand()), null);
ParenthesizedExpression newParentExpr = ast.newParenthesizedExpression();
newParentExpr.setExpression((Expression) rewrite.createMoveTarget(parent));
PrefixExpression newPrefixExpr = ast.newPrefixExpression();
newPrefixExpr.setOperand(newParentExpr);
newPrefixExpr.setOperator(PrefixExpression.Operator.NOT);
rewrite.replace(parent, newPrefixExpr, null);
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVALID_OPERATOR, image);
proposals.add(proposal);
}
}
} else if (selectedNode instanceof InfixExpression && isBitOperation((((InfixExpression) selectedNode).getOperator()))) {
// a & b == c -> (a & b) == c
final CompareInBitWiseOpFinder opFinder = new CompareInBitWiseOpFinder(selectedNode);
if (opFinder.getCompareExpression() != null) {
// compare operation inside bit operations: set parents
String label = CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_bitop_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
CUCorrectionProposal proposal = new CUCorrectionProposal(label, context.getCompilationUnit(), IProposalRelevance.INVALID_OPERATOR, image) {
@Override
protected void addEdits(IDocument document, TextEdit edit) throws CoreException {
InfixExpression compareExpression = opFinder.getCompareExpression();
InfixExpression expression = opFinder.getParentInfixExpression();
ASTNode left = compareExpression.getLeftOperand();
if (expression.getStartPosition() < left.getStartPosition()) {
edit.addChild(new InsertEdit(expression.getStartPosition(), String.valueOf('(')));
edit.addChild(new InsertEdit(ASTNodes.getExclusiveEnd(left), String.valueOf(')')));
}
ASTNode rigth = compareExpression.getRightOperand();
int selEnd = ASTNodes.getExclusiveEnd(expression);
if (selEnd > ASTNodes.getExclusiveEnd(rigth)) {
edit.addChild(new InsertEdit(rigth.getStartPosition(), String.valueOf('(')));
edit.addChild(new InsertEdit(selEnd, String.valueOf(')')));
}
}
};
proposals.add(proposal);
}
}
}
use of org.eclipse.jface.text.IDocument in project che by eclipse.
the class CodeAssist method createOrganizeImportOperation.
private OrganizeImportResult createOrganizeImportOperation(ICompilationUnit compilationUnit, List<String> chosen) throws CoreException {
CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(compilationUnit.getJavaProject());
OrganizeImportsOperation operation = new OrganizeImportsOperation(compilationUnit, null, settings.importIgnoreLowercase, !compilationUnit.isWorkingCopy(), true, chosen, null);
NullProgressMonitor monitor = new NullProgressMonitor();
TextEdit edit = operation.createTextEdit(monitor);
OrganizeImportResult result = DtoFactory.newDto(OrganizeImportResult.class);
TypeNameMatch[][] choices = operation.getChoices();
//or all conflicts were resolved (!chosen.isEmpty())
if ((chosen != null && !chosen.isEmpty()) || choices == null || choices.length == 0) {
IBuffer buffer = compilationUnit.getBuffer();
IDocument document = new Document(buffer.getContents());
DocumentChangeListener documentChangeListener = new DocumentChangeListener(document);
try {
edit.apply(document);
} catch (BadLocationException e) {
LOG.debug("Applying Organize import text edits goes wrong:", e);
}
result.setChanges(documentChangeListener.getChanges());
return result;
}
result.setConflicts(createListOfDTOMatches(choices));
return result;
}
use of org.eclipse.jface.text.IDocument in project che by eclipse.
the class DocumentReader method handleDocumentAboutToBeChanged.
/**
* Copies the document prior to modification and removes the document listener.
*/
private void handleDocumentAboutToBeChanged() {
IDocument document = fDocument;
if (fCharSequence == null || document == null)
return;
String content = document.get();
synchronized (this) {
if (fCharSequence == null)
return;
fCharSequence = content;
}
releaseDocument();
}
Aggregations