Search in sources :

Example 21 with IModification

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

the class XtendQuickfixProvider method fixFileName.

@Fix(IssueCodes.WRONG_FILE)
public void fixFileName(final Issue issue, IssueResolutionAcceptor acceptor) {
    if (issue.getData() != null && issue.getData().length == 1) {
        final String expectedFileName = issue.getData()[0];
        final IFile iFile = projectUtil.findFileStorage(issue.getUriToProblem(), true);
        final IPath pathToMoveTo = iFile.getParent().getFullPath().append(expectedFileName).addFileExtension(iFile.getFileExtension());
        if (!iFile.getWorkspace().getRoot().exists(pathToMoveTo)) {
            final String label = "Rename file to '" + expectedFileName + ".xtend'";
            acceptor.accept(issue, label, label, "xtend_file.png", new IModification() {

                @Override
                public void apply(IModificationContext context) throws Exception {
                    runAsyncInDisplayThread(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                iFile.move(pathToMoveTo, IResource.KEEP_HISTORY, null);
                            } catch (CoreException e) {
                                logger.error(e);
                            }
                        }
                    });
                }
            });
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 22 with IModification

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

the class XtendQuickfixProvider method removeUnusedPrivateMember.

@Fix(IssueCodes.UNUSED_PRIVATE_MEMBER)
public void removeUnusedPrivateMember(final Issue issue, IssueResolutionAcceptor acceptor) {
    // use the same label, description and image
    // to be able to use the quickfixes (issue resolution) in batch mode
    String label = "Remove member.";
    String description = "Remove the unused private member.";
    String image = "delete_edit.png";
    acceptor.accept(issue, label, description, image, new IModification() {

        @Override
        public void apply(IModificationContext context) throws Exception {
            final IXtextDocument document = context.getXtextDocument();
            document.modify(new IUnitOfWork.Void<XtextResource>() {

                @Override
                public void process(XtextResource state) throws Exception {
                    final EObject element = state.getEObject(issue.getUriToProblem().fragment());
                    final ICompositeNode elementNode = NodeModelUtils.findActualNodeFor(element);
                    final Position elementPosition = new Position(elementNode.getOffset(), elementNode.getLength());
                    document.addPosition(elementPosition);
                    if (element instanceof XtendField) {
                        final JvmField field = associations.getJvmField((XtendField) element);
                        final List<Position> assignments = EcoreUtil2.eAllContentsAsList(element.eResource()).stream().filter(XAssignment.class::isInstance).map(XAssignment.class::cast).filter(assignment -> field == assignment.getFeature()).map(assignment -> {
                            final ICompositeNode assignmentNode = NodeModelUtils.findActualNodeFor(assignment);
                            final int offset = assignmentNode.getOffset();
                            final XExpression assignmentValue = assignment.getValue();
                            int length = assignmentNode.getLength();
                            if (expressionHelper.hasSideEffects(assignmentValue)) {
                                length -= NodeModelUtils.findActualNodeFor(assignmentValue).getLength();
                            }
                            return new Position(offset, length);
                        }).collect(Collectors.toList());
                        for (final Position assignment : assignments) {
                            document.addPosition(assignment);
                        }
                        for (final Position assignment : assignments) {
                            document.replace(assignment.getOffset(), assignment.getLength(), "");
                        }
                    }
                    document.replace(elementPosition.getOffset(), elementPosition.getLength(), "");
                }
            });
            organizeImportsHandler.doOrganizeImports(document);
        }
    });
}
Also used : XtendField(org.eclipse.xtend.core.xtend.XtendField) ITextualMultiModification(org.eclipse.xtext.ui.editor.model.edit.ITextualMultiModification) CoreException(org.eclipse.core.runtime.CoreException) IssueResolutionAcceptor(org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) OrganizeImportsHandler(org.eclipse.xtext.xbase.ui.imports.OrganizeImportsHandler) IRenameRefactoringProvider(org.eclipse.xtext.ui.refactoring.IRenameRefactoringProvider) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) PlatformUI(org.eclipse.ui.PlatformUI) Strings(org.eclipse.xtext.util.Strings) Set(java.util.Set) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IssueResolution(org.eclipse.xtext.ui.editor.quickfix.IssueResolution) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) RenameElementProcessor(org.eclipse.xtext.ui.refactoring.impl.RenameElementProcessor) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Singleton(com.google.inject.Singleton) Iterables(com.google.common.collect.Iterables) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) IJavaProjectProvider(org.eclipse.xtext.common.types.access.jdt.IJavaProjectProvider) URI(org.eclipse.emf.common.util.URI) InsertionOffsets(org.eclipse.xtend.ide.codebuilder.InsertionOffsets) StopWatch(org.eclipse.xtext.util.StopWatch) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) Diagnostic(org.eclipse.xtext.diagnostics.Diagnostic) NodeModelUtils(org.eclipse.xtext.nodemodel.util.NodeModelUtils) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ArrayList(java.util.ArrayList) TypesPackage(org.eclipse.xtext.common.types.TypesPackage) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) Lists(com.google.common.collect.Lists) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) OverrideHelper(org.eclipse.xtext.xbase.typesystem.override.OverrideHelper) XbaseQuickfixProvider(org.eclipse.xtext.xbase.ui.quickfix.XbaseQuickfixProvider) IFile(org.eclipse.core.resources.IFile) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtendJvmAssociations(org.eclipse.xtend.core.jvmmodel.IXtendJvmAssociations) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) EReference(org.eclipse.emf.ecore.EReference) XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XtendPackage(org.eclipse.xtend.core.xtend.XtendPackage) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XAssignment(org.eclipse.xtext.xbase.XAssignment) Junit5LibClasspathAdder(org.eclipse.xtend.ide.buildpath.Junit5LibClasspathAdder) Issue(org.eclipse.xtext.validation.Issue) TextSelection(org.eclipse.jface.text.TextSelection) PerformChangeOperation(org.eclipse.ltk.core.refactoring.PerformChangeOperation) ResolvedConstructor(org.eclipse.xtext.xbase.typesystem.override.ResolvedConstructor) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) ResolvedFeatures(org.eclipse.xtext.xbase.typesystem.override.ResolvedFeatures) RefactoringCore(org.eclipse.ltk.core.refactoring.RefactoringCore) Inject(com.google.inject.Inject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) IssueCodes(org.eclipse.xtend.core.validation.IssueCodes) Logger(org.apache.log4j.Logger) Junit4LibClasspathAdder(org.eclipse.xtend.ide.buildpath.Junit4LibClasspathAdder) IPath(org.eclipse.core.runtime.IPath) IRenameContextFactory(org.eclipse.xtext.ui.refactoring.ui.IRenameContextFactory) Change(org.eclipse.ltk.core.refactoring.Change) IRenameElementContext(org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext) INode(org.eclipse.xtext.nodemodel.INode) Objects(com.google.common.base.Objects) Position(org.eclipse.jface.text.Position) IJavaProject(org.eclipse.jdt.core.IJavaProject) EObject(org.eclipse.emf.ecore.EObject) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) XtendGrammarAccess(org.eclipse.xtend.core.services.XtendGrammarAccess) Keyword(org.eclipse.xtext.Keyword) List(java.util.List) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) IBatchTypeResolver(org.eclipse.xtext.xbase.typesystem.IBatchTypeResolver) DisplayRunHelper(org.eclipse.xtext.ui.util.DisplayRunHelper) XBlockExpression(org.eclipse.xtext.xbase.XBlockExpression) TypeReferences(org.eclipse.xtext.common.types.util.TypeReferences) ProjectUtil(org.eclipse.xtext.ui.refactoring.impl.ProjectUtil) XtendLibClasspathAdder(org.eclipse.xtend.ide.buildpath.XtendLibClasspathAdder) XIfExpression(org.eclipse.xtext.xbase.XIfExpression) MemberFromSuperImplementor(org.eclipse.xtend.ide.codebuilder.MemberFromSuperImplementor) JvmType(org.eclipse.xtext.common.types.JvmType) IssueModificationContext(org.eclipse.xtext.ui.editor.model.edit.IssueModificationContext) EcoreUtil2(org.eclipse.xtext.EcoreUtil2) XtextResource(org.eclipse.xtext.resource.XtextResource) ICompositeModification(org.eclipse.xtext.ui.editor.model.edit.ICompositeModification) XExpression(org.eclipse.xtext.xbase.XExpression) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) XtendExecutable(org.eclipse.xtend.core.xtend.XtendExecutable) JvmField(org.eclipse.xtext.common.types.JvmField) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) EList(org.eclipse.emf.common.util.EList) OptionalParameters(org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ITextRegion(org.eclipse.xtext.util.ITextRegion) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IResource(org.eclipse.core.resources.IResource) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix) XtendExpressionHelper(org.eclipse.xtend.core.typing.XtendExpressionHelper) Position(org.eclipse.jface.text.Position) XtextResource(org.eclipse.xtext.resource.XtextResource) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) XtendField(org.eclipse.xtend.core.xtend.XtendField) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XExpression(org.eclipse.xtext.xbase.XExpression) JvmField(org.eclipse.xtext.common.types.JvmField) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) XAssignment(org.eclipse.xtext.xbase.XAssignment) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 23 with IModification

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

the class CodeBuilderQuickfix method addQuickfix.

public void addQuickfix(final ICodeBuilder builder, final String label, final Issue issue, final IssueResolutionAcceptor acceptor) {
    boolean _isValid = builder.isValid();
    if (_isValid) {
        IModification _switchResult = null;
        boolean _matched = false;
        if (builder instanceof ICodeBuilder.Xtend) {
            _matched = true;
            _switchResult = this.getXtendModification(((ICodeBuilder.Xtend) builder));
        }
        if (!_matched) {
            if (builder instanceof ICodeBuilder.Java) {
                _matched = true;
                _switchResult = this.getJavaModification(((ICodeBuilder.Java) builder));
            }
        }
        final IModification modification = _switchResult;
        acceptor.accept(issue, label, builder.getPreview(), builder.getImage(), modification);
    }
}
Also used : ICodeBuilder(org.eclipse.xtend.ide.codebuilder.ICodeBuilder) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification)

Example 24 with IModification

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

the class CodeBuilderQuickfix method getJavaModification.

protected IModification getJavaModification(final ICodeBuilder.Java builder) {
    final IModification _function = (IModificationContext it) -> {
        final IType type = builder.getIType();
        char _charAt = ".".charAt(0);
        final ImportManager importManager = new ImportManager(true, _charAt);
        final StringBuilderBasedAppendable content = new StringBuilderBasedAppendable(importManager);
        builder.build(content);
        final Consumer<String> _function_1 = (String it_1) -> {
            try {
                ICompilationUnit _compilationUnit = type.getCompilationUnit();
                NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
                _compilationUnit.createImport(it_1, null, _nullProgressMonitor);
            } catch (Throwable _e) {
                throw Exceptions.sneakyThrow(_e);
            }
        };
        importManager.getImports().forEach(_function_1);
        Object _switchResult = null;
        boolean _matched = false;
        if (builder instanceof JavaFieldBuilder) {
            _matched = true;
            String _string = content.toString();
            NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
            _switchResult = type.createField(_string, null, true, _nullProgressMonitor);
        }
        if (!_matched) {
            if (builder instanceof JavaConstructorBuilder) {
                _matched = true;
                String _string = content.toString();
                NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
                _switchResult = type.createMethod(_string, null, true, _nullProgressMonitor);
            }
        }
        if (!_matched) {
            if (builder instanceof JavaMethodBuilder) {
                _matched = true;
                String _string = content.toString();
                NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
                _switchResult = type.createMethod(_string, null, true, _nullProgressMonitor);
            }
        }
        if (!_matched) {
            _switchResult = null;
        }
        final Object element = ((Object) _switchResult);
        if ((element != null)) {
            JdtHyperlink _jdtHyperlink = new JdtHyperlink();
            final Procedure1<JdtHyperlink> _function_2 = (JdtHyperlink it_1) -> {
                it_1.setJavaElement(((IMember) element));
                it_1.open();
            };
            ObjectExtensions.<JdtHyperlink>operator_doubleArrow(_jdtHyperlink, _function_2);
        }
    };
    return _function;
}
Also used : ImportManager(org.eclipse.xtext.xbase.compiler.ImportManager) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaMethodBuilder(org.eclipse.xtend.ide.codebuilder.JavaMethodBuilder) JavaConstructorBuilder(org.eclipse.xtend.ide.codebuilder.JavaConstructorBuilder) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType) Consumer(java.util.function.Consumer) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) StringBuilderBasedAppendable(org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable) EObject(org.eclipse.emf.ecore.EObject) JavaFieldBuilder(org.eclipse.xtend.ide.codebuilder.JavaFieldBuilder) JdtHyperlink(org.eclipse.xtext.common.types.xtext.ui.JdtHyperlink) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification)

Example 25 with IModification

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

the class CodeBuilderQuickfix method getXtendModification.

protected IModification getXtendModification(final ICodeBuilder.Xtend builder) {
    final IModification _function = (IModificationContext it) -> {
        final XtendTypeDeclaration xtendClass = builder.getXtendType();
        final IEditorPart editor = this.editorOpener.open(EcoreUtil.getURI(xtendClass), false);
        if ((!(editor instanceof XtextEditor))) {
            return;
        }
        final XtextEditor xtextEditor = ((XtextEditor) editor);
        final IXtextDocument document = xtextEditor.getDocument();
        final Wrapper<Integer> wrapper = Wrapper.<Integer>forType(Integer.class);
        final IUnitOfWork<ReplacingAppendable, XtextResource> _function_1 = (XtextResource resource) -> {
            ReplacingAppendable _xblockexpression = null;
            {
                int offset = builder.getInsertOffset(resource);
                wrapper.set(Integer.valueOf(offset));
                final int typeIndentation = this.getTypeIndentation(resource, document, xtendClass);
                DocumentSourceAppender.Factory.OptionalParameters _optionalParameters = new DocumentSourceAppender.Factory.OptionalParameters();
                final Procedure1<DocumentSourceAppender.Factory.OptionalParameters> _function_2 = (DocumentSourceAppender.Factory.OptionalParameters it_1) -> {
                    int _indentationLevel = builder.getIndentationLevel();
                    int _plus = (_indentationLevel + typeIndentation);
                    it_1.baseIndentationLevel = _plus;
                    it_1.ensureEmptyLinesAround = true;
                };
                DocumentSourceAppender.Factory.OptionalParameters _doubleArrow = ObjectExtensions.<DocumentSourceAppender.Factory.OptionalParameters>operator_doubleArrow(_optionalParameters, _function_2);
                _xblockexpression = this.appendableFactory.create(document, resource, offset, 0, _doubleArrow);
            }
            return _xblockexpression;
        };
        final ReplacingAppendable appendable = document.<ReplacingAppendable>readOnly(_function_1);
        Integer offset = wrapper.get();
        builder.build(appendable);
        appendable.commitChanges();
        xtextEditor.setHighlightRange(((offset).intValue() + 1), appendable.length(), true);
    };
    return _function;
}
Also used : Wrapper(org.eclipse.xtext.util.Wrapper) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XtextResource(org.eclipse.xtext.resource.XtextResource) IEditorPart(org.eclipse.ui.IEditorPart) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) DocumentSourceAppender(org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Aggregations

IModification (org.eclipse.xtext.ui.editor.model.edit.IModification)25 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)23 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)16 BadLocationException (org.eclipse.jface.text.BadLocationException)12 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)11 XtextResource (org.eclipse.xtext.resource.XtextResource)7 CoreException (org.eclipse.core.runtime.CoreException)5 EObject (org.eclipse.emf.ecore.EObject)5 WizardDialog (org.eclipse.jface.wizard.WizardDialog)5 IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)5 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 List (java.util.List)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 WrappedException (org.eclipse.emf.common.util.WrappedException)3 INode (org.eclipse.xtext.nodemodel.INode)3 Rule (com.avaloq.tools.ddk.xtext.format.format.Rule)2 Map (java.util.Map)2 Set (java.util.Set)2 IFile (org.eclipse.core.resources.IFile)2