Search in sources :

Example 31 with IErlElement

use of org.erlide.engine.model.IErlElement in project erlide_eclipse by erlang.

the class ErlStructureCreator method createStructureComparator.

@Override
protected IStructureComparator createStructureComparator(final Object element, final IDocument document0, final ISharedDocumentAdapter sharedDocumentAdapter, final IProgressMonitor monitor) throws CoreException {
    IErlModule module = null;
    final IErlModel model = ErlangEngine.getInstance().getModel();
    String s = "";
    IDocument document = document0;
    if (element instanceof ResourceNode) {
        final ResourceNode rn = (ResourceNode) element;
        final IResource r = rn.getResource();
        if (r instanceof IFile) {
            final IFile f = (IFile) r;
            final IErlElement e = model.findElement(r);
            if (e instanceof IErlModule) {
                module = (IErlModule) e;
            }
            if (document == null) {
                try {
                    s = ErlStructureCreator.readString(f.getContents());
                    document = new Document(s);
                } catch (final CoreException e1) {
                }
            }
        }
    } else if (document == null && element instanceof IStreamContentAccessor) {
        try {
            try (final InputStream contents = ((IStreamContentAccessor) element).getContents()) {
                s = ErlStructureCreator.readString(contents);
            } catch (final IOException e) {
            }
            document = new Document(s);
        } catch (final CoreException ex) {
        }
    } else if (document != null) {
        s = document.get();
    }
    if (module == null) {
        String name = "comptemp";
        if (element instanceof ITypedElement) {
            final ITypedElement typedElement = (ITypedElement) element;
            name = typedElement.getName();
        }
        module = model.getModuleFromText(model, name, s, null);
    }
    ErlNode root = null;
    if (element != null && document != null) {
        try {
            module.open(null);
            root = new RootErlNode(document, element);
            recursiveMakeErlNodes(module, root, document);
        } catch (final ErlModelException e) {
            ErlLogger.warn(e);
        }
    }
    return root;
}
Also used : IFile(org.eclipse.core.resources.IFile) IErlModel(org.erlide.engine.model.root.IErlModel) InputStream(java.io.InputStream) ITypedElement(org.eclipse.compare.ITypedElement) IOException(java.io.IOException) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) ResourceNode(org.eclipse.compare.ResourceNode) IErlElement(org.erlide.engine.model.IErlElement) CoreException(org.eclipse.core.runtime.CoreException) ErlModelException(org.erlide.engine.model.ErlModelException) IErlModule(org.erlide.engine.model.root.IErlModule) IStreamContentAccessor(org.eclipse.compare.IStreamContentAccessor) IDocument(org.eclipse.jface.text.IDocument) IResource(org.eclipse.core.resources.IResource)

Example 32 with IErlElement

use of org.erlide.engine.model.IErlElement in project erlide_eclipse by erlang.

the class ErlStructureCreator method recursiveMakeErlNodes.

private ErlNode recursiveMakeErlNodes(final IErlElement element, final ErlNode parent, final IDocument doc) throws ErlModelException {
    final ErlNode n = ErlNode.createErlNode(parent, element, doc);
    if (element instanceof IOpenable) {
        final IOpenable o = (IOpenable) element;
        o.open(null);
    }
    if (element instanceof IParent) {
        final IParent p = (IParent) element;
        final Collection<IErlElement> children = p.getChildren();
        for (final IErlElement child : children) {
            recursiveMakeErlNodes(child, n, doc);
        }
    }
    return n;
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) IOpenable(org.erlide.engine.model.root.IOpenable) IParent(org.erlide.engine.model.IParent)

Example 33 with IErlElement

use of org.erlide.engine.model.IErlElement in project erlide_eclipse by erlang.

the class ErlStructureCreator method getPath.

@Override
protected String[] getPath(final Object element, final Object input) {
    if (element instanceof IErlElement) {
        IErlElement e = (IErlElement) element;
        // build a path starting at the given element and walk
        // up the parent chain until we reach a module
        final List<String> args = new ArrayList<>();
        while (e != null) {
            // each path component has a name that uses the same
            // conventions as a ErlNode name
            final String name = ErlangCompareUtilities.getErlElementID(e);
            if (name == null) {
                return null;
            }
            args.add(name);
            if (e instanceof IErlModule) {
                break;
            }
            e = (IErlElement) e.getParent();
        }
        Collections.reverse(args);
        return args.toArray(new String[args.size()]);
    }
    return null;
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) ArrayList(java.util.ArrayList) IErlModule(org.erlide.engine.model.root.IErlModule)

Example 34 with IErlElement

use of org.erlide.engine.model.IErlElement in project erlide_eclipse by erlang.

the class DefaultErlangFoldingStructureProvider method match.

/**
 * Matches deleted annotations to changed or added ones. A deleted
 * annotation/position tuple that has a matching addition / change is
 * updated and marked as changed. The matching tuple is not added (for
 * additions) or marked as deletion instead (for changes). The result is
 * that more annotations are changed and fewer get deleted/re-added.
 */
private void match(final List<ErlangProjectionAnnotation> deletions, final Map<ErlangProjectionAnnotation, Position> additions, final List<ErlangProjectionAnnotation> changes) {
    if (deletions.isEmpty() || additions.isEmpty() && changes.isEmpty()) {
        return;
    }
    final List<ErlangProjectionAnnotation> newDeletions = new ArrayList<>();
    final List<ErlangProjectionAnnotation> newChanges = new ArrayList<>();
    final Iterator<ErlangProjectionAnnotation> deletionIterator = deletions.iterator();
    while (deletionIterator.hasNext()) {
        final ErlangProjectionAnnotation deleted = deletionIterator.next();
        final Position deletedPosition = fCachedModel.getPosition(deleted);
        if (deletedPosition == null) {
            continue;
        }
        final Tuple deletedTuple = new Tuple(deleted, deletedPosition);
        Tuple match = findMatch(deletedTuple, changes, null);
        boolean addToDeletions = true;
        if (match == null) {
            match = findMatch(deletedTuple, additions.keySet(), additions);
            addToDeletions = false;
        }
        if (match != null) {
            final IErlElement element = match.annotation.getElement();
            deleted.setElement(element);
            deletedPosition.setLength(match.position.getLength());
            if (deletedPosition instanceof ErlangElementPosition && element instanceof IErlMember) {
                final ErlangElementPosition eep = (ErlangElementPosition) deletedPosition;
                eep.setMember((IErlMember) element);
            }
            deletionIterator.remove();
            newChanges.add(deleted);
            if (addToDeletions) {
                newDeletions.add(match.annotation);
            }
        }
    }
    deletions.addAll(newDeletions);
    changes.addAll(newChanges);
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) IErlMember(org.erlide.engine.model.erlang.IErlMember) Position(org.eclipse.jface.text.Position) IProjectionPosition(org.eclipse.jface.text.source.projection.IProjectionPosition) ArrayList(java.util.ArrayList)

Example 35 with IErlElement

use of org.erlide.engine.model.IErlElement in project erlide_eclipse by erlang.

the class DefaultErlangFoldingStructureProvider method processDelta.

protected void processDelta(final IErlElementDelta delta) {
    if (!isInstalled()) {
        return;
    }
    if ((delta.getFlags() & (IErlElementDelta.F_CONTENT | IErlElementDelta.F_CHILDREN)) == 0) {
        return;
    }
    final IErlElement de = delta.getElement();
    if (de instanceof IErlModule && de != fModule) {
        return;
    }
    final ProjectionAnnotationModel model = fEditor.getAdapter(ProjectionAnnotationModel.class);
    if (model == null) {
        return;
    }
    final IDocumentProvider provider = fEditor.getDocumentProvider();
    try {
        fCachedModel = model;
        fCachedDocument = provider.getDocument(fEditor.getEditorInput());
        if (fCachedDocument.getNumberOfLines() > PerformanceTuning.get().getFoldingLimit()) {
            // disable folding for files larger than this
            model.removeAllAnnotations();
            return;
        }
        final Map<ErlangProjectionAnnotation, Position> additions = new HashMap<>();
        final List<ErlangProjectionAnnotation> deletions = new ArrayList<>();
        final List<ErlangProjectionAnnotation> updates = new ArrayList<>();
        // use a linked map to maintain ordering of comments
        final Map<ErlangProjectionAnnotation, Position> updated = new LinkedHashMap<>();
        computeAdditions(fModule, updated);
        final Map<Object, List<Tuple>> previous = createAnnotationMap(model);
        for (final Entry<ErlangProjectionAnnotation, Position> entry : updated.entrySet()) {
            final ErlangProjectionAnnotation newAnnotation = entry.getKey();
            final IErlElement element = newAnnotation.getElement();
            final Position newPosition = entry.getValue();
            final List<Tuple> annotations = previous.get(element);
            if (annotations == null) {
                additions.put(newAnnotation, newPosition);
            } else {
                final Iterator<Tuple> x = annotations.iterator();
                boolean matched = false;
                while (x.hasNext()) {
                    final Tuple tuple = x.next();
                    final ErlangProjectionAnnotation existingAnnotation = tuple.annotation;
                    final Position existingPosition = tuple.position;
                    if (newAnnotation.isComment() == existingAnnotation.isComment()) {
                        if (existingPosition != null && !newPosition.equals(existingPosition)) {
                            existingPosition.setOffset(newPosition.getOffset());
                            existingPosition.setLength(newPosition.getLength());
                            updates.add(existingAnnotation);
                        }
                        matched = true;
                        x.remove();
                        break;
                    }
                }
                if (!matched) {
                    additions.put(newAnnotation, newPosition);
                }
                if (annotations.isEmpty()) {
                    previous.remove(element);
                }
            }
        }
        for (final List<Tuple> l : previous.values()) {
            for (final Tuple t : l) {
                deletions.add(t.annotation);
            }
        }
        match(deletions, additions, updates);
        final Annotation[] removals = new Annotation[deletions.size()];
        deletions.toArray(removals);
        final Annotation[] changes = new Annotation[updates.size()];
        updates.toArray(changes);
        model.modifyAnnotations(removals, additions, changes);
        fFirstTimeInitialCollapse = false;
    } finally {
        fCachedDocument = null;
        fCachedModel = null;
    // fFirstType= null;
    // fHasHeaderComment = false;
    }
}
Also used : Position(org.eclipse.jface.text.Position) IProjectionPosition(org.eclipse.jface.text.source.projection.IProjectionPosition) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Annotation(org.eclipse.jface.text.source.Annotation) ProjectionAnnotation(org.eclipse.jface.text.source.projection.ProjectionAnnotation) LinkedHashMap(java.util.LinkedHashMap) IErlElement(org.erlide.engine.model.IErlElement) ProjectionAnnotationModel(org.eclipse.jface.text.source.projection.ProjectionAnnotationModel) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IErlModule(org.erlide.engine.model.root.IErlModule) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

IErlElement (org.erlide.engine.model.IErlElement)123 Test (org.junit.Test)36 IErlModule (org.erlide.engine.model.root.IErlModule)35 ErlModelException (org.erlide.engine.model.ErlModelException)28 IParent (org.erlide.engine.model.IParent)17 IFile (org.eclipse.core.resources.IFile)16 ArrayList (java.util.ArrayList)12 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)11 IResource (org.eclipse.core.resources.IResource)11 IErlProject (org.erlide.engine.model.root.IErlProject)10 IOpenable (org.erlide.engine.model.root.IOpenable)10 IProject (org.eclipse.core.resources.IProject)9 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)8 IErlModel (org.erlide.engine.model.root.IErlModel)8 WranglerException (org.erlide.wrangler.refactoring.exception.WranglerException)8 OtpErlangRangeException (com.ericsson.otp.erlang.OtpErlangRangeException)7 IFolder (org.eclipse.core.resources.IFolder)7 CoreException (org.eclipse.core.runtime.CoreException)7 OtpErlangAtom (com.ericsson.otp.erlang.OtpErlangAtom)6 OtpErlangTuple (com.ericsson.otp.erlang.OtpErlangTuple)6