use of org.eclipse.jdt.ui.text.java.IInvocationContext 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;
}
}
use of org.eclipse.jdt.ui.text.java.IInvocationContext in project bndtools by bndtools.
the class ImportPackageQuickFixProcessorBndBuildPathHandlerTest method setUp.
@Before
public void setUp() {
fakeFile = fakeFile();
bundles = new ArrayList<>();
bundles.add(new VersionedClause("my.test.bundle", new Attrs()));
Attrs attrs = new Attrs();
attrs.put(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, "1.2.3");
bundles.add(new VersionedClause("my.second.bundle", attrs));
setBuildPath(bundles);
IProject eclipse = mock(IProject.class, DO_NOT_CALL);
doReturn(fakeFile).when(eclipse).getFile(Project.BNDFILE);
doReturn(BSN).when(eclipse).getName();
IJavaProject jProject = mock(IJavaProject.class, DO_NOT_CALL);
doReturn(eclipse).when(jProject).getProject();
doReturn(BSN).when(jProject).getElementName();
ICompilationUnit unit = mock(ICompilationUnit.class, DO_NOT_CALL);
doReturn(jProject).when(unit).getJavaProject();
IInvocationContext context = mock(IInvocationContext.class, DO_NOT_CALL);
doReturn(unit).when(context).getCompilationUnit();
sut = new ImportPackageQuickFixProcessor.BndBuildPathHandler(context) {
@Override
Workspace getWorkspace() throws Exception {
return null;
}
};
}
use of org.eclipse.jdt.ui.text.java.IInvocationContext in project che by eclipse.
the class NullAnnotationsCorrectionProcessor method addNullAnnotationInSignatureProposal.
public static void addNullAnnotationInSignatureProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, ChangeKind changeKind, boolean isArgumentProblem) {
NullAnnotationsFix fix = NullAnnotationsFix.createNullAnnotationInSignatureFix(context.getASTRoot(), problem, changeKind, isArgumentProblem);
if (fix != null) {
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
Map<String, String> options = new Hashtable<String, String>();
if (fix.getCu() != context.getASTRoot()) {
// workaround: adjust the unit to operate on, depending on the findings of RewriteOperations.createAddAnnotationOperation(..)
final CompilationUnit cu = fix.getCu();
final IInvocationContext originalContext = context;
context = new IInvocationContext() {
public int getSelectionOffset() {
return originalContext.getSelectionOffset();
}
public int getSelectionLength() {
return originalContext.getSelectionLength();
}
public ASTNode getCoveringNode() {
return originalContext.getCoveringNode();
}
public ASTNode getCoveredNode() {
return originalContext.getCoveredNode();
}
public ICompilationUnit getCompilationUnit() {
return (ICompilationUnit) cu.getJavaElement();
}
public CompilationUnit getASTRoot() {
return cu;
}
};
}
//raise local change above change in overridden method
int relevance = (changeKind == ChangeKind.OVERRIDDEN) ? IProposalRelevance.CHANGE_NULLNESS_ANNOTATION_IN_OVERRIDDEN_METHOD : IProposalRelevance.CHANGE_NULLNESS_ANNOTATION;
FixCorrectionProposal proposal = new FixCorrectionProposal(fix, new NullAnnotationsCleanUp(options, problem.getProblemId()), relevance, image, context);
proposals.add(proposal);
}
}
Aggregations