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;
}
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;
}
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;
}
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);
}
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;
}
}
Aggregations