/** update the display folding */ private void updateFolding(final DFPropModel dfmodel) { ISourceViewer viewer = getSourceViewer(); if (viewer instanceof ProjectionViewer) { ProjectionAnnotationModel annotationModel = ((ProjectionViewer) viewer).getProjectionAnnotationModel(); Iterator<?> annotations = annotationModel.getAnnotationIterator(); Map<Integer, ProjectionAnnotation> annotationsMap = new HashMap<Integer, ProjectionAnnotation>(); while (annotations.hasNext()) { Object next = annotations.next(); if (next instanceof ProjectionAnnotation) { ProjectionAnnotation annotation = (ProjectionAnnotation) next; Position position = annotationModel.getPosition(annotation); if (position != null) { int offset = position.getOffset(); annotationsMap.put(offset, annotation); } } } Map<Annotation, Position> additions = applyFolding(annotationModel, dfmodel, annotationsMap); annotationModel.modifyAnnotations( annotationsMap.values().toArray(new ProjectionAnnotation[annotationsMap.size()]), additions, new ProjectionAnnotation[0]); } }
/** * @param element * @param elements * @param model * @return */ protected boolean isInsideLast( PyProjectionAnnotation element, List elements, ProjectionAnnotationModel model) { if (elements.size() == 0) { return false; } PyProjectionAnnotation top = (PyProjectionAnnotation) elements.get(elements.size() - 1); Position p1 = model.getPosition(element); Position pTop = model.getPosition(top); int p1Offset = p1.getOffset(); int pTopoffset = pTop.getOffset(); int pTopLen = pTopoffset + pTop.getLength(); if (p1Offset > pTopoffset && p1Offset < pTopLen) { return true; } return false; }
/** Saves the code folding state into the given memento. */ public void saveCodeFolding(org.eclipse.ui.IMemento memento) { java.util.Iterator<?> annotationIt = projectionAnnotationModel.getAnnotationIterator(); while (annotationIt.hasNext()) { org.eclipse.jface.text.source.projection.ProjectionAnnotation annotation = (org.eclipse.jface.text.source.projection.ProjectionAnnotation) annotationIt.next(); org.eclipse.ui.IMemento annotationMemento = memento.createChild(ANNOTATION); org.eclipse.jface.text.Position position = projectionAnnotationModel.getPosition(annotation); annotationMemento.putBoolean(IS_COLLAPSED, annotation.isCollapsed()); annotationMemento.putInteger(OFFSET, position.offset); annotationMemento.putInteger(LENGTH, position.length); } }
/** * Tries to add this position into the model. Only positions with more than 3 lines can be taken * in. If multiple positions exist on the same line, the longest will be chosen. The shorter ones * will be deleted. * * @param position the position to be added. */ private void addPosition(org.eclipse.jface.text.Position position) { org.eclipse.jface.text.IDocument document = sourceViewer.getDocument(); int lines = 0; try { lines = document.getNumberOfLines(position.offset, position.length); } catch (org.eclipse.jface.text.BadLocationException e) { e.printStackTrace(); return; } if (lines < 3) { return; } // if a position to add existed on the same line, the longest one will be chosen try { for (org.eclipse.jface.text.source.projection.ProjectionAnnotation annotationToAdd : additions.keySet()) { org.eclipse.jface.text.Position positionToAdd = additions.get(annotationToAdd); if (document.getLineOfOffset(position.offset) == document.getLineOfOffset(positionToAdd.offset)) { if (positionToAdd.length < position.length) { additions.remove(annotationToAdd); } else { return; } } } } catch (org.eclipse.jface.text.BadLocationException e) { return; } for (org.eclipse.jface.text.source.projection.ProjectionAnnotation annotationInModel : oldAnnotations) { org.eclipse.jface.text.Position positionInModel = projectionAnnotationModel.getPosition(annotationInModel); if (position.offset == positionInModel.offset && position.length == positionInModel.length) { oldAnnotations.remove(annotationInModel); return; } } additions.put(new org.eclipse.jface.text.source.projection.ProjectionAnnotation(), position); }