Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#418 - preserve folded sections when editing (DO NOT MERGE) #434

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public abstract class JsonEditor extends YEdit implements IShowInSource, IShowIn

private final Validator validator = createValidator();
private ProjectionSupport projectionSupport;
private Annotation[] oldAnnotations;
private Map<AbstractNode, ProjectionAnnotation> oldProjectionAnnotations = new HashMap<>();;
private ProjectionAnnotationModel annotationModel;
private Composite topPanel;
protected JsonSourceViewerConfiguration sourceViewerConfiguration;
Expand Down Expand Up @@ -312,14 +312,23 @@ public void widgetSelected(SelectionEvent e) {
return viewer;
}

public void updateFoldingStructure(List<Position> positions) {
public void updateFoldingStructure(Iterable<AbstractNode> newNodes, JsonDocument document) {
final Map<Annotation, Position> newAnnotations = new HashMap<Annotation, Position>();
for (Position position : positions) {
newAnnotations.put(new ProjectionAnnotation(), position);
final Map<AbstractNode, ProjectionAnnotation> node2Annotations = new HashMap<>();
for (AbstractNode node : newNodes) {
if (node.isObject()) {
Position position = node.getPosition(document, true);
boolean isCollapsed = false;
if (oldProjectionAnnotations.get(node) != null) {
isCollapsed = oldProjectionAnnotations.get(node).isCollapsed();
}
ProjectionAnnotation newAnnotation = new ProjectionAnnotation(isCollapsed);
newAnnotations.put(newAnnotation, position);
node2Annotations.put(node, newAnnotation);
}
}

annotationModel.modifyAnnotations(oldAnnotations, newAnnotations, null);
oldAnnotations = newAnnotations.keySet().toArray(new Annotation[0]);
annotationModel.modifyAnnotations(oldProjectionAnnotations.values().toArray(new Annotation[0]), newAnnotations, null);
oldProjectionAnnotations = node2Annotations;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@
*******************************************************************************/
package com.reprezen.swagedit.core.editor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.reconciler.DirtyRegion;
import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension;
import org.eclipse.swt.widgets.Display;
import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.NodeTuple;

import com.reprezen.swagedit.core.model.Model;

public class JsonReconcilingStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension {

Expand Down Expand Up @@ -62,45 +56,15 @@ protected void calculatePositions() {
if (!(document instanceof JsonDocument))
return;

final Node yaml = ((JsonDocument) document).getYaml();
if (!(yaml instanceof MappingNode)) {
return;
}

final Model model = ((JsonDocument) document).getModel();

Display.getDefault().asyncExec(new Runnable() {
public void run() {
editor.updateFoldingStructure(calculatePositions((MappingNode) yaml));
editor.updateFoldingStructure(model.allNodes(), (JsonDocument) document);
}
});
}

protected List<Position> calculatePositions(MappingNode mapping) {
List<Position> positions = new ArrayList<>();
int start;
int end = -1;

for (NodeTuple tuple : mapping.getValue()) {
start = tuple.getKeyNode().getStartMark().getLine();
end = tuple.getValueNode().getEndMark().getLine();

if ((end - start) > 0) {
try {
int startOffset = document.getLineOffset(start);
int endOffset = document.getLineOffset(end);

positions.add(new Position(startOffset, (endOffset - startOffset)));
} catch (BadLocationException e) {
}
}

if (tuple.getValueNode() instanceof MappingNode) {
positions.addAll(calculatePositions((MappingNode) tuple.getValueNode()));
}
}

return positions;
}

public void setEditor(JsonEditor editor) {
this.editor = editor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,18 @@ public int size() {
* @return position inside the document
*/
public Position getPosition(IDocument document) {
boolean selectEntireElement = false;
return getPosition(document, false);
}

/**
* Returns the position of the node inside the given document. <br/>
* The position matches the area that contains all the node's content.
*
* @param document
* @param selectEntireElement - the entire element will be selected if true, only the first line otherwise
* @return position inside the document
*/
public Position getPosition(IDocument document, boolean selectEntireElement) {
int startLine = getStart().getLine();
int offset = 0;
int length = 0;
Expand Down