Search in sources :

Example 1 with IssueModificationContext

use of org.eclipse.xtext.ui.editor.model.edit.IssueModificationContext in project xtext-xtend by eclipse.

the class XtendQuickfixProvider method removeUnnecessaryModifier.

@Fix(IssueCodes.UNNECESSARY_MODIFIER)
public void removeUnnecessaryModifier(final Issue issue, IssueResolutionAcceptor acceptor) {
    String[] issueData = issue.getData();
    if (issueData == null || issueData.length == 0) {
        return;
    }
    // use the same label, description and image
    // to be able to use the quickfixes (issue resolution) in batch mode
    String label = "Remove the unnecessary modifier.";
    String description = "The modifier is unnecessary and could be removed.";
    String image = "fix_indent.gif";
    acceptor.accept(issue, label, description, image, new ITextualMultiModification() {

        @Override
        public void apply(IModificationContext context) throws Exception {
            if (context instanceof IssueModificationContext) {
                Issue theIssue = ((IssueModificationContext) context).getIssue();
                Integer offset = theIssue.getOffset();
                IXtextDocument document = context.getXtextDocument();
                document.replace(offset, theIssue.getLength(), "");
                while (Character.isWhitespace(document.getChar(offset))) {
                    document.replace(offset, 1, "");
                }
            }
        }
    });
}
Also used : IssueModificationContext(org.eclipse.xtext.ui.editor.model.edit.IssueModificationContext) Issue(org.eclipse.xtext.validation.Issue) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ITextualMultiModification(org.eclipse.xtext.ui.editor.model.edit.ITextualMultiModification) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 2 with IssueModificationContext

use of org.eclipse.xtext.ui.editor.model.edit.IssueModificationContext in project dsl-devkit by dsldevkit.

the class AbstractQuickFixTest method sortResolutionsByOffsetDecreasing.

/**
 * Sort issue resolutions by offset in document decreasing.
 *
 * @param resolutions
 *          resolutions to sort
 * @return a copy of {@code resolutions} sorted by offset in document decreasing
 */
protected List<IssueResolution> sortResolutionsByOffsetDecreasing(final List<IssueResolution> resolutions) {
    final Function<IssueResolution, Integer> getLocationFunction = new Function<IssueResolution, Integer>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public Integer apply(final IssueResolution from) {
            if (from != null) {
                if (from instanceof IssueResolutionWrapper) {
                    ICoreModificationContext context = ((IssueResolutionWrapper) from).getCoreModificationContext();
                    if (context instanceof CoreIssueModificationContext) {
                        return ((CoreIssueModificationContext) context).getIssue().getOffset();
                    }
                } else {
                    IModificationContext context = from.getModificationContext();
                    if (context instanceof IssueModificationContext) {
                        return ((IssueModificationContext) context).getIssue().getOffset();
                    }
                }
            }
            return Integer.MIN_VALUE;
        }
    };
    Ordering<IssueResolution> ordering = Ordering.natural().onResultOf(getLocationFunction).reverse();
    return new ArrayList<IssueResolution>(ordering.sortedCopy(resolutions));
}
Also used : ICoreModificationContext(com.avaloq.tools.ddk.check.runtime.quickfix.ICoreModificationContext) IssueModificationContext(org.eclipse.xtext.ui.editor.model.edit.IssueModificationContext) CoreIssueModificationContext(com.avaloq.tools.ddk.check.runtime.ui.quickfix.CoreIssueModificationContext) Function(com.google.common.base.Function) CoreIssueModificationContext(com.avaloq.tools.ddk.check.runtime.ui.quickfix.CoreIssueModificationContext) IssueResolution(org.eclipse.xtext.ui.editor.quickfix.IssueResolution) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ArrayList(java.util.ArrayList) IssueResolutionWrapper(com.avaloq.tools.ddk.check.runtime.ui.quickfix.IssueResolutionWrapper)

Aggregations

IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)2 IssueModificationContext (org.eclipse.xtext.ui.editor.model.edit.IssueModificationContext)2 ICoreModificationContext (com.avaloq.tools.ddk.check.runtime.quickfix.ICoreModificationContext)1 CoreIssueModificationContext (com.avaloq.tools.ddk.check.runtime.ui.quickfix.CoreIssueModificationContext)1 IssueResolutionWrapper (com.avaloq.tools.ddk.check.runtime.ui.quickfix.IssueResolutionWrapper)1 Function (com.google.common.base.Function)1 ArrayList (java.util.ArrayList)1 CoreException (org.eclipse.core.runtime.CoreException)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)1 ITextualMultiModification (org.eclipse.xtext.ui.editor.model.edit.ITextualMultiModification)1 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)1 IssueResolution (org.eclipse.xtext.ui.editor.quickfix.IssueResolution)1 Issue (org.eclipse.xtext.validation.Issue)1