use of org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor in project flux by eclipse.
the class QuickAssistService method applyProposals.
private JSONObject applyProposals(int offset, int length, boolean applyFix, ICompilationUnit liveEditUnit, IProblemLocation problem) throws CoreException, JavaModelException, JSONException {
IInvocationContext context = new AssistContext(liveEditUnit, offset, length);
QuickFixProcessor processor = new QuickFixProcessor();
IJavaCompletionProposal[] proposals = processor.getCorrections(context, new IProblemLocation[] { problem });
if (proposals == null || proposals.length == 0) {
return null;
}
if (applyFix) {
IBuffer buffer = liveEditUnit.getBuffer();
if (buffer != null) {
IDocument document = buffer instanceof IDocument ? (IDocument) buffer : new DocumentAdapter(buffer);
if (proposals[0] instanceof CUCorrectionProposal) {
CUCorrectionProposal proposal = (CUCorrectionProposal) proposals[0];
String preview = proposal.getPreviewContent();
System.out.println(document.getLength());
System.out.println(preview.length());
try {
document.addDocumentListener(this.documentListener);
document.replace(0, preview.length(), preview);
//proposal.apply(document);
liveEditUnit.getBuffer().setContents(proposal.getPreviewContent());
liveEditUnit.reconcile(ICompilationUnit.NO_AST, true, null, null);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
return null;
} else {
List<JSONObject> jsonProposals = new ArrayList<JSONObject>(proposals.length);
for (IJavaCompletionProposal proposal : proposals) {
JSONObject jsonDescription = getDescription(proposal);
JSONObject jsonProposal = new JSONObject();
jsonProposal.put("description", jsonDescription);
jsonProposal.put("relevance", proposal.getRelevance());
jsonProposals.add(jsonProposal);
}
JSONObject result = new JSONObject();
result.put("quickfix", jsonProposals);
return result;
}
}
Aggregations