use of org.eclipse.xtext.ui.editor.quickfix.Fix in project dsl-devkit by dsldevkit.
the class FormatQuickfixProvider method removeOverride.
/**
* Semantic quickfix removing the override flag for a rule.
*
* @param issue
* the issue
* @param acceptor
* the acceptor
*/
@Fix(FormatJavaValidator.OVERRIDE_ILLEGAL_CODE)
public void removeOverride(final Issue issue, final IssueResolutionAcceptor acceptor) {
acceptor.accept(issue, "Remove override", "Remove override.", null, new IModification() {
@Override
public void apply(final IModificationContext context) throws BadLocationException {
context.getXtextDocument().modify(new IUnitOfWork<Void, XtextResource>() {
@Override
public java.lang.Void exec(final XtextResource state) {
Rule rule = (Rule) state.getEObject(issue.getUriToProblem().fragment());
rule.setOverride(false);
return null;
}
});
}
});
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project dsl-devkit by dsldevkit.
the class CheckCfgQuickfixProvider method fixSeverityToMaxSeverity.
/**
* Fix severity by setting it to a legal value as is defined by severity range of referenced check. Legal
* severities are passed as issue data (org.eclipse.xtext.validation.Issue#getData()).
*
* @param issue
* the issue
* @param acceptor
* the acceptor
*/
@Fix(IssueCodes.SEVERITY_NOT_ALLOWED)
public void fixSeverityToMaxSeverity(final Issue issue, final IssueResolutionAcceptor acceptor) {
if (issue.getData() != null) {
for (final String severityProposal : issue.getData()) {
final String label = NLS.bind(Messages.CheckCfgQuickfixProvider_CORRECT_SEVERITY_LABEL, severityProposal);
final String descn = NLS.bind(Messages.CheckCfgQuickfixProvider_CORRECT_SEVERITY_DESCN, severityProposal);
acceptor.accept(issue, label, descn, NO_IMAGE, new IModification() {
public void apply(final IModificationContext context) throws BadLocationException {
IXtextDocument xtextDocument = context.getXtextDocument();
xtextDocument.replace(issue.getOffset(), issue.getLength(), severityProposal);
}
});
}
}
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method fixJunitNotOnClasspath.
@Fix(Diagnostic.LINKING_DIAGNOSTIC)
public void fixJunitNotOnClasspath(final Issue issue, final IssueResolutionAcceptor acceptor) {
if (issue.getData().length != 0 && issue.getData()[0] != null) {
String type = issue.getData()[0];
if (type.startsWith("org.junit.jupiter.")) {
acceptor.accept(issue, "Add JUnit 5 lib to classpath", "Add JUnit 5 lib to classpath", "julaunch.gif", new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
ResourceSet resourceSet = element.eResource().getResourceSet();
IJavaProject javaProject = projectProvider.getJavaProject(resourceSet);
junit5LibAdder.addLibsToClasspath(javaProject, new NullProgressMonitor());
}
});
} else if (type.startsWith("org.junit.")) {
acceptor.accept(issue, "Add JUnit 4 lib to classpath", "Add JUnit 4 lib to classpath", "julaunch.gif", new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
ResourceSet resourceSet = element.eResource().getResourceSet();
IJavaProject javaProject = projectProvider.getJavaProject(resourceSet);
junit4LibAdder.addLibsToClasspath(javaProject, new NullProgressMonitor());
}
});
}
}
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method specifyTypeExplicitly.
@Fix(IssueCodes.API_TYPE_INFERENCE)
public void specifyTypeExplicitly(Issue issue, IssueResolutionAcceptor acceptor) {
acceptor.accept(issue, "Infer type", "Infer type", null, new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
EStructuralFeature featureAfterType = null;
JvmIdentifiableElement jvmElement = null;
if (element instanceof XtendFunction) {
XtendFunction function = (XtendFunction) element;
if (function.getCreateExtensionInfo() == null) {
featureAfterType = XtendPackage.Literals.XTEND_FUNCTION__NAME;
} else {
featureAfterType = XtendPackage.Literals.XTEND_FUNCTION__CREATE_EXTENSION_INFO;
}
jvmElement = associations.getDirectlyInferredOperation((XtendFunction) element);
} else if (element instanceof XtendField) {
featureAfterType = XtendPackage.Literals.XTEND_FIELD__NAME;
jvmElement = associations.getJvmField((XtendField) element);
}
if (jvmElement != null) {
LightweightTypeReference type = batchTypeResolver.resolveTypes(element).getActualType(jvmElement);
INode node = Iterables.getFirst(NodeModelUtils.findNodesForFeature(element, featureAfterType), null);
if (node == null) {
throw new IllegalStateException("Could not determine node for " + element);
}
if (type == null) {
throw new IllegalStateException("Could not determine type for " + element);
}
ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) element.eResource(), node.getOffset(), 0);
appendable.append(type);
appendable.append(" ");
appendable.commitChanges();
}
}
});
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method fixWrongFileRenameClass.
@Fix(IssueCodes.WRONG_FILE)
public void fixWrongFileRenameClass(final Issue issue, final IssueResolutionAcceptor acceptor) {
URI uri = issue.getUriToProblem();
String className = uri.trimFileExtension().lastSegment();
String label = String.format("Rename class to '%s'", className);
acceptor.accept(issue, label, label, null, (element, context) -> {
context.getXtextDocument().modify(resource -> {
IRenameElementContext renameContext = renameContextFactory.createRenameElementContext(element, null, new TextSelection(context.getXtextDocument(), issue.getOffset(), issue.getLength()), resource);
final ProcessorBasedRefactoring refactoring = renameRefactoringProvider.getRenameRefactoring(renameContext);
((RenameElementProcessor) refactoring.getProcessor()).setNewName(className);
PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, monitor -> {
try {
if (!refactoring.checkFinalConditions(monitor).isOK())
return;
Change change = refactoring.createChange(monitor);
change.initializeValidationData(monitor);
PerformChangeOperation performChangeOperation = new PerformChangeOperation(change);
performChangeOperation.setUndoManager(RefactoringCore.getUndoManager(), refactoring.getName());
performChangeOperation.setSchedulingRule(ResourcesPlugin.getWorkspace().getRoot());
performChangeOperation.run(monitor);
} catch (CoreException e) {
logger.error(e);
}
});
return null;
});
});
}
Aggregations