private void calculatePositions() { if (hasSnippetsModifications()) { final Map<ProjectionAnnotation, Position> annotations = getAllSnippetsAnnotations(); Display.getDefault().asyncExec(new Runnable() { @Override public void run() { if (!annotations.isEmpty() && getProjectionAnnotationModel() == null) { enableProjection(); } if (getProjectionAnnotationModel() != null) { Annotation[] oldAnno = oldAnnotations.keySet().toArray(new Annotation[0]); getProjectionAnnotationModel().modifyAnnotations(oldAnno, annotations, null); oldAnnotations.clear(); oldAnnotations.putAll(annotations); if (annotations.isEmpty()) { disableProjection(); } } } }); } }
private void updateFolding(EditorConfig editorConfig) { if (projectionAnnotationModel == null) { return; } List<Section> sections = editorConfig.getSections(); CommentBlocks commentBlocks = editorConfig.getAdapter(CommentBlocks.class); List<CommentBlock> comments = commentBlocks != null ? commentBlocks.getCommentBlocks() : Collections.emptyList(); Map<Annotation, Position> newAnnotations = new HashMap<>(); // Collection section and comment spans; List<Span> spans = /*Stream.concat(sections.stream(), comments.stream())*/ sections.stream() .map(a -> a.getAdapter(Span.class)) .sorted((s1, s2) -> s1.getStart().getLine() - s2.getStart().getLine()).collect(Collectors.toList()); Annotation[] annotations = new Annotation[spans.size()]; for (int i = 0; i < spans.size(); i++) { Span span = spans.get(i); int startOffset = span.getStart().getOffset(); int endOffset = span.getEnd().getOffset(); ProjectionAnnotation annotation = new ProjectionAnnotation(); newAnnotations.put(annotation, new Position(startOffset, endOffset - startOffset)); annotations[i] = annotation; } projectionAnnotationModel.modifyAnnotations(oldAnnotations, newAnnotations, null); oldAnnotations = annotations; }
/** * <p> * Checks whether the given positions are in the * <code>ProjectionAnnotationModel</code> or in the addition set. If not it tries * to add into <code>additions</code>. Deletes old ProjectionAnnotation with line * count less than 2. * </p> * * @param positions a list of available foldable positions */ public void updateCodefolding(List<Position> positions) { IDocument document = sourceViewer.getDocument(); if (document == null) { return; } oldAnnotations.clear(); Iterator<?> annotationIterator = projectionAnnotationModel.getAnnotationIterator(); while (annotationIterator.hasNext()) { oldAnnotations.add((ProjectionAnnotation) annotationIterator.next()); } // Add new Position with a unique line offset for (Position position : positions) { if (!isInAdditions(position)) { addPosition(position); } } projectionAnnotationModel.modifyAnnotations(oldAnnotations.toArray(new Annotation[0]), additions, null); additions.clear(); }
/** * Saves the code folding state into the given memento. */ public void saveCodeFolding(IMemento memento) { // The annotation model might be null if the editor opened an storage input // instead of a file input. if (projectionAnnotationModel == null) { return; } Iterator<?> annotationIt = projectionAnnotationModel.getAnnotationIterator(); while (annotationIt.hasNext()) { ProjectionAnnotation annotation = (ProjectionAnnotation) annotationIt.next(); IMemento annotationMemento = memento.createChild(ANNOTATION); Position position = projectionAnnotationModel.getPosition(annotation); annotationMemento.putBoolean(IS_COLLAPSED, annotation.isCollapsed()); annotationMemento.putInteger(OFFSET, position.offset); annotationMemento.putInteger(LENGTH, position.length); } }
/** * Update the annotation structure in the editor. * * This is only currently used by comment * folding and should be removed because it * is incorrect. * * @param positions * @deprecated */ public void updateFoldingStructure(List<Position> positions) { if (annotationModel == null) { return; } Annotation[] annotations = new Annotation[positions.size()]; // this will hold the new annotations along // with their corresponding positions Map<ProjectionAnnotation, Position> newAnnotations = new HashMap<ProjectionAnnotation, Position>(); for (int i = 0; i < positions.size(); i++) { ProjectionAnnotation annotation = new ProjectionAnnotation(); newAnnotations.put(annotation, positions.get(i)); annotations[i] = annotation; } // If this method is called too early, then annotationModel // can be null. This should obviously be addressed. this.annotationModel.modifyAnnotations(oldAnnotations, newAnnotations, null); oldAnnotations = annotations; }
/** * Collapses all proofs. * * @param cursorOffset */ private void foldAllProofs() { Vector<Annotation> modifiedAnnotations = new Vector<Annotation>(); for (Iterator<TLAProofPosition> it = foldPositions.iterator(); it.hasNext();) { TLAProofPosition proofPosition = it.next(); if (!proofPosition.getAnnotation().isCollapsed()) { // should fold every proof // so that only theorem statements are shown proofPosition.getAnnotation().markCollapsed(); modifiedAnnotations.add(proofPosition.getAnnotation()); } } editor.modifyProjectionAnnotations((Annotation[]) modifiedAnnotations .toArray(new ProjectionAnnotation[modifiedAnnotations.size()])); }
private void expandAllProofs() { Vector<Annotation> modifiedAnnotations = new Vector<Annotation>(); for (Iterator<TLAProofPosition> it = foldPositions.iterator(); it.hasNext();) { TLAProofPosition proofPosition = it.next(); if (proofPosition.getAnnotation().isCollapsed()) { // should fold every proof // so that only theorem statements are shown proofPosition.getAnnotation().markExpanded(); modifiedAnnotations.add(proofPosition.getAnnotation()); } } editor.modifyProjectionAnnotations((Annotation[]) modifiedAnnotations .toArray(new ProjectionAnnotation[modifiedAnnotations.size()])); }
private Annotation[] computeDifferences(ProjectionAnnotationModel model, Set current) { List deletions = new ArrayList(); for (Iterator iter = model.getAnnotationIterator(); iter.hasNext();) { Object annotation = iter.next(); if (annotation instanceof ProjectionAnnotation) { Position position = model.getPosition((Annotation) annotation); if (current.contains(position)) current.remove(position); else deletions.add(annotation); } } return (Annotation[]) deletions.toArray(new Annotation[deletions.size()]); }
@Override public void run() { ITextEditor editor = getTextEditor(); ISelection selection = editor.getSelectionProvider().getSelection(); if (selection instanceof ITextSelection) { ITextSelection textSelection = (ITextSelection) selection; if (!textSelection.isEmpty()) { IAnnotationModel model = getAnnotationModel(editor); if (model != null) { int start = textSelection.getStartLine(); int end = textSelection.getEndLine(); try { IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput()); int offset = document.getLineOffset(start); int endOffset = document.getLineOffset(end + 1); Position position = new Position(offset, endOffset - offset); model.addAnnotation(new ProjectionAnnotation(), position); } catch (BadLocationException x) { // ignore } } } } }
/** * Calculate where to fold, sticking the info into newAnnotations * @param doc * @param headers * @param newAnnotations * @param endParent */ private void updateSectionFoldingAnnotations2(IDocument doc, List<Header> headers, Map<Annotation, Position> newAnnotations, int endParent) { for (int i=0; i<headers.size(); i++) { Header header = headers.get(i); ProjectionAnnotation annotation = new ProjectionAnnotation(); try { int line = header.getLineNumber(); int start = doc.getLineOffset(line); int end = (i==headers.size()-1)? endParent : doc.getLineOffset(headers.get(i+1).getLineNumber()); Position position = new Position(start, end-start); newAnnotations.put(annotation, position); // Recurse List<Header> subHeaders = header.getSubHeaders(); if (subHeaders.size() > 0) { updateSectionFoldingAnnotations2(doc, subHeaders, newAnnotations, end); } } catch (Exception ex) { System.out.println(ex); } } }
public void updateFoldingStructure(List<Position> positions) { ProjectionAnnotation[] annotations = new ProjectionAnnotation[positions.size()]; //this will hold the new annotations along //with their corresponding positions HashMap<ProjectionAnnotation, Position> newAnnotations = new HashMap<ProjectionAnnotation, Position>(); for (int i = 0; i < positions.size(); i++) { ProjectionAnnotation annotation = new ProjectionAnnotation(); newAnnotations.put(annotation,positions.get(i)); annotations[i] = annotation; if (annotationCollapsedState != null && annotationCollapsedState.length > i && annotationCollapsedState[i]) { annotation.markCollapsed(); } } annotationModel.modifyAnnotations(oldAnnotations,newAnnotations,null); oldAnnotations = annotations; }
/** * @return an annotation that should be added (or null if that entry already has an annotation * added for it). */ private Tuple<ProjectionAnnotation, Position> getAnnotationToAdd(FoldingEntry node, int start, int end, ProjectionAnnotationModel model, List<Annotation> existing) throws BadLocationException { try { IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput()); int offset = document.getLineOffset(start); int endOffset = offset; try { endOffset = document.getLineOffset(end); } catch (Exception e) { //sometimes when we are at the last line, the command above will not work very well IRegion lineInformation = document.getLineInformation(end); endOffset = lineInformation.getOffset() + lineInformation.getLength(); } Position position = new Position(offset, endOffset - offset); return getAnnotationToAdd(position, node, model, existing); } catch (BadLocationException x) { //this could happen } return null; }
private void addFoldingRegions(Set<Position> regions, CommonElement[] elements, Map<Annotation,Position> map) throws BadLocationException { for (int i = 0; i < elements.length; i++) { CommonElement element = elements[i]; try { int startLine = fDocument.getLineOfOffset(element.getOffset()); int endLine = fDocument.getLineOfOffset(element.getOffset() + element.getLength()); if (startLine >= 0 && startLine < endLine) { int start = fDocument.getLineOffset(startLine); int end = fDocument.getLineOffset(endLine) + fDocument.getLineLength(endLine); Position position = new Position(start, end - start); regions.add(position); if (isInitiallyFolded(element)) map.put(new ProjectionAnnotation(true), position); } } catch (BadLocationException x) { } CommonElement[] children = element.getChildren(); if (children != null) addFoldingRegions(regions, children, map); } }
private Map<ProjectionAnnotation, Position> getAllSnippetsAnnotations() { Map<ProjectionAnnotation, Position> annotations = new HashMap<ProjectionAnnotation, Position>(); IDocument document = getDocument(); int curOffset = 0; FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(document); try { IRegion startRegion = frda.find(curOffset, "SNIPPET_START", true, false, false, false); //$NON-NLS-1$ while (startRegion != null && startRegion.getOffset() >= curOffset) { int startLine = document.getLineOfOffset(startRegion.getOffset()); int startOffset = document.getLineOffset(startLine); curOffset = startOffset + document.getLineLength(startLine); IRegion endRegion = frda.find(startRegion.getOffset(), "SNIPPET_END", true, false, false, false); //$NON-NLS-1$ if (endRegion != null) { int endLine = document.getLineOfOffset(endRegion.getOffset()); int endOffset = document.getLineOffset(endLine); endOffset += document.getLineLength(endLine); curOffset = endOffset; String text = document.get(startOffset, endOffset - startOffset); ProjectionAnnotation annotation = new ProjectionAnnotation(true); annotation.setText(text); annotation.setRangeIndication(true); annotations.put(annotation, new Position(startOffset, endOffset - startOffset)); } if (curOffset < document.getLength()) { startRegion = frda.find(curOffset, "SNIPPET_START", true, false, false, false); //$NON-NLS-1$ } } } catch (BadLocationException e) { } return annotations; }
public void run() { ITextEditor editor = getTextEditor(); ISelection selection = editor.getSelectionProvider().getSelection(); if (selection instanceof ITextSelection) { ITextSelection textSelection = (ITextSelection) selection; if (textSelection.getLength() != 0) { IAnnotationModel model = getAnnotationModel(editor); if (model != null) { int start = textSelection.getStartLine(); int end = textSelection.getEndLine(); try { IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput()); int offset = document.getLineOffset(start); int endOffset = document.getLineOffset(end + 1); Position position = new Position(offset, endOffset - offset); model.addAnnotation(new ProjectionAnnotation(), position); } catch (BadLocationException x) { // ignore } } } } }
/** * Folds all proofs not containing the cursor. * * Note that this will fold every proof if the cursor * is not in a proof. * * @param cursorOffset */ private void foldEverythingUnusable(int cursorOffset) { Vector<Annotation> modifiedAnnotations = new Vector<Annotation>(); for (Iterator<TLAProofPosition> it = foldPositions.iterator(); it.hasNext();) { TLAProofPosition proofPosition = it.next(); try { if (proofPosition.containsInProofOrStatement(cursorOffset, document)) { if (proofPosition.getAnnotation().isCollapsed()) { proofPosition.getAnnotation().markExpanded(); modifiedAnnotations.add(proofPosition.getAnnotation()); } } else if (!proofPosition.getAnnotation().isCollapsed()) { proofPosition.getAnnotation().markCollapsed(); modifiedAnnotations.add(proofPosition.getAnnotation()); } } catch (BadLocationException e) { Activator.getDefault().logError("Error changing expansion state of proofs.", e); } } editor.modifyProjectionAnnotations((Annotation[]) modifiedAnnotations .toArray(new ProjectionAnnotation[modifiedAnnotations.size()])); }
public void updateFoldingStructure(ArrayList positions) { Annotation[] annotations = new Annotation[positions.size()]; // this will hold the new annotations along // with their corresponding positions HashMap newAnnotations = new HashMap(); HashMap<Position, Boolean> oldPosisitions = new HashMap<Position, Boolean>(); if (oldAnnotations != null) { for (int i = 0; i < oldAnnotations.length; i++) { ProjectionAnnotation tmp = (ProjectionAnnotation) oldAnnotations[i]; if (tmp.isCollapsed()) { oldPosisitions.put(annotationModel.getPosition(tmp), true); } } } for (int i = 0; i < positions.size(); i++) { ProjectionAnnotation annotation = new ProjectionAnnotation(); if (oldPosisitions.containsKey(positions.get(i)) && oldPosisitions.get(positions.get(i)).booleanValue()) // if // was // collapsed annotation.markCollapsed(); newAnnotations.put(annotation, positions.get(i)); annotations[i] = annotation; } // Melhorar isto aqui Remover apenas as necessarias e nao todas as // antigas annotationModel.modifyAnnotations(oldAnnotations, newAnnotations, null); oldAnnotations = annotations; }
@SuppressWarnings("unchecked") protected Annotation[] mergeFoldingRegions(Collection<FoldedPosition> foldedPositions, ProjectionAnnotationModel projectionAnnotationModel) { List<Annotation> deletions = new ArrayList<Annotation>(); for (Iterator<Annotation> iterator = projectionAnnotationModel.getAnnotationIterator(); iterator.hasNext();) { Annotation annotation = iterator.next(); if (annotation instanceof ProjectionAnnotation) { Position position = projectionAnnotationModel.getPosition(annotation); if (!foldedPositions.remove(position)) { deletions.add(annotation); } } } return deletions.toArray(new Annotation[deletions.size()]); }
protected void updateFoldingRegions(boolean allowCollapse, ProjectionAnnotationModel model, Collection<FoldedPosition> foldedPositions, Annotation[] deletions) { Map<ProjectionAnnotation, Position> additionsMap = Maps.newHashMap(); for (FoldedPosition foldedPosition: foldedPositions) { addProjectionAnnotation(allowCollapse, foldedPosition, additionsMap); } if (deletions.length != 0 || additionsMap.size() != 0) { model.modifyAnnotations(deletions, additionsMap, new Annotation[] {}); } }
private void updateFoldingRegions(ProjectionAnnotationModel model, Set currentRegions) { Annotation[] deletions = computeDifferences(model, currentRegions); Map additionsMap = new HashMap(); for (Iterator iter = currentRegions.iterator(); iter.hasNext();) additionsMap.put(new ProjectionAnnotation(), iter.next()); if ((deletions.length != 0 || additionsMap.size() != 0) && (fProgressMonitor == null || !fProgressMonitor.isCanceled())) model.modifyAnnotations(deletions, additionsMap, new Annotation[] {}); }
public void fold(int foldingStart, int foldingEnd) { Position foldingPosition = new Position(foldingStart, foldingEnd - foldingStart); if (getSourceViewer().getAnnotationModel() != null) { getSourceViewer().getAnnotationModel().addAnnotation(new ProjectionAnnotation(), foldingPosition); } }
public void updateFoldingStructure(List<Position> positions) { final Map<Annotation, Position> newAnnotations = new HashMap<Annotation, Position>(); for (Position position : positions) { newAnnotations.put(new ProjectionAnnotation(), position); } annotationModel.modifyAnnotations(oldAnnotations, newAnnotations, null); oldAnnotations = newAnnotations.keySet().toArray(new Annotation[0]); }
GamaFoldingActionGroup(final ITextEditor editor, final ITextViewer viewer) { super(editor, viewer); if (!(viewer instanceof ProjectionViewer)) { return; } this.viewwer = (ProjectionViewer) viewer; collapseStrings = new FoldingAction() { // $NON-NLS-1$ // private final EClass type = GamlPackage. @Override public void run() { final ProjectionAnnotationModel model = viewwer.getProjectionAnnotationModel(); final Iterator<?> iterator = model.getAnnotationIterator(); final List<Annotation> toCollapse = new ArrayList<Annotation>(); while (iterator.hasNext()) { final Object next = iterator.next(); if (next instanceof ProjectionAnnotation) { final ProjectionAnnotation pa = (ProjectionAnnotation) next; final Position position = model.getPosition(pa); if (position instanceof TypedFoldedPosition) if (((TypedFoldedPosition) position).getType().equals("__comment")) { pa.markCollapsed(); toCollapse.add(pa); } } } model.modifyAnnotations(null, null, toCollapse.toArray(new Annotation[0])); } }; collapseStrings.setActionDefinitionId("org.xtext.example.folding.ui.folding.collapseStrings"); editor.setAction("FoldingCollapseStrings", collapseStrings); //$NON-NLS-1$ }
protected void calculatePositions(boolean initialReconcile, IProgressMonitor monitor, IParseRootNode ast) { if (monitor != null && monitor.isCanceled()) { return; } // Folding... try { Map<ProjectionAnnotation, Position> positions = folder.emitFoldingRegions(initialReconcile, monitor, ast); synchronized (fPositionsLock) { fPositions = positions; } } catch (BadLocationException e) { IdeLog.logError(CommonEditorPlugin.getDefault(), e); } // If we had all positions we shouldn't probably listen to cancel, but we may have exited emitFoldingRegions // early because of cancel... if (monitor != null && monitor.isCanceled() || !shouldUpdatePositions(folder)) { return; } updatePositions(); }
/** * Update the folding positions in the document */ protected void updatePositions() { AbstractThemeableEditor editor = fEditor; if (editor != null) { HashMap<ProjectionAnnotation, Position> positions; synchronized (fPositionsLock) { // Create a copy to pass to updateFoldingStructure, as it may take more time there. positions = new HashMap<ProjectionAnnotation, Position>(fPositions); } editor.updateFoldingStructure(positions); } }
public Map<ProjectionAnnotation, Position> getPositions() { synchronized (fPositionsLock) { return new HashMap<ProjectionAnnotation, Position>(fPositions); } }
public void updateFoldingStructure(Map<ProjectionAnnotation, Position> annotations) { synchronized (lockUpdateFoldingStructure) { List<Annotation> deletions = new ArrayList<Annotation>(); Collection<Position> additions = annotations.values(); ProjectionAnnotationModel currentModel = getAnnotationModel(); if (currentModel == null) { return; } for (@SuppressWarnings("rawtypes") Iterator iter = currentModel.getAnnotationIterator(); iter.hasNext();) { Object annotation = iter.next(); if (annotation instanceof ProjectionAnnotation) { Position position = currentModel.getPosition((Annotation) annotation); if (additions.contains(position)) { additions.remove(position); } else { deletions.add((Annotation) annotation); } } } if (annotations.size() != 0 || deletions.size() != 0) { currentModel.modifyAnnotations(deletions.toArray(new Annotation[deletions.size()]), annotations, null); } } }
/** * Given the ast, create the needed marks and set them in the passed model. */ private synchronized void addMarksToModel(SimpleNode root2, ProjectionAnnotationModel model) { try { if (model != null) { ArrayList<Annotation> existing = new ArrayList<Annotation>(); //get the existing annotations Iterator<Annotation> iter = model.getAnnotationIterator(); while (iter != null && iter.hasNext()) { Annotation element = iter.next(); existing.add(element); } //now, remove the annotations not used and add the new ones needed IDocument doc = editor.getDocument(); if (doc != null) { //this can happen if we change the input of the editor very quickly. boolean foldInitial = initialFolding; initialFolding = false; List<FoldingEntry> marks = getMarks(doc, root2, foldInitial); Map<ProjectionAnnotation, Position> annotationsToAdd; if (marks.size() > OptimizationRelatedConstants.MAXIMUM_NUMBER_OF_CODE_FOLDING_MARKS) { annotationsToAdd = new HashMap<ProjectionAnnotation, Position>(); } else { annotationsToAdd = getAnnotationsToAdd(marks, model, existing); } model.replaceAnnotations(existing.toArray(new Annotation[existing.size()]), annotationsToAdd); } } } catch (Exception e) { Log.log(e); } }