Skip to content

Commit

Permalink
Fix more editor bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Schnick committed Apr 2, 2021
1 parent 2ac8397 commit 5231b1c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ public EditorCollectorNode(EditorNode directParent, String keyName, int parentIn
this.nodes = nodes;
}

@Override
public void updateNodeAtIndex(Node replacementValue, String toInsertKeyName, int index) {
getRealParent().updateNodeAtIndex(replacementValue, keyName, firstNodeIndex + index);
}

@Override
public void replacePart(ArrayNode toInsert, int beginIndex, int length) {
getRealParent().replacePart(
ArrayNode.sameKeyArray(keyName, toInsert.getNodeArray()),
firstNodeIndex + beginIndex,
length);
}

@Override
public void delete() {
getRealParent().replacePart(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.crschnick.pdx_unlimiter.app.editor;

import com.crschnick.pdx_unlimiter.core.node.ArrayNode;
import com.crschnick.pdx_unlimiter.core.node.Node;
import com.crschnick.pdx_unlimiter.core.node.NodeMatcher;

import java.util.ArrayList;
Expand Down Expand Up @@ -72,6 +73,10 @@ public static List<EditorNode> create(EditorNode parent, ArrayNode ar) {
return result;
}

public abstract void updateNodeAtIndex(Node replacementValue, String toInsertKeyName, int index);

public abstract void replacePart(ArrayNode toInsert, int beginIndex, int length);

public abstract void delete();

public abstract boolean filterKey(Predicate<String> filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public void updateColor(Color c) {
cn.set(newColorNode);
}

private void updateParentNodeAtIndex(Node replacementValue, String toInsertKeyName, int index) {
public void updateNodeAtIndex(Node replacementValue, String toInsertKeyName, int index) {
ArrayNode ar = (ArrayNode) backingNode;

var replacement = toInsertKeyName != null ?
ArrayNode.singleKeyNode(toInsertKeyName, replacementValue) : ArrayNode.array(List.of(replacementValue));
this.backingNode = ar.replacePart(replacement, index, 1);
if (getDirectParent() != null) {
getRealParent().updateParentNodeAtIndex(this.backingNode, keyName, getKeyIndex());
getDirectParent().updateNodeAtIndex(this.backingNode, keyName, getKeyIndex());
}
}

Expand All @@ -47,14 +47,14 @@ public void replacePart(ArrayNode toInsert, int beginIndex, int length) {

// Update parent node to reflect change
if (getDirectParent() != null) {
getRealParent().updateParentNodeAtIndex(this.backingNode, keyName, getKeyIndex());
getDirectParent().updateNodeAtIndex(this.backingNode, keyName, getKeyIndex());
}
}

@Override
public void delete() {
if (getDirectParent() != null) {
getRealParent().replacePart(ArrayNode.emptyArray(), getKeyIndex(), 1);
getDirectParent().replacePart(ArrayNode.emptyArray(), getKeyIndex(), 1);
}
}

Expand Down Expand Up @@ -108,9 +108,7 @@ public void update(ArrayNode newNode) {

// Update parent node to reflect change
if (getDirectParent() != null) {
var replacement = getKeyName().map(k -> ArrayNode.singleKeyNode(k, this.backingNode))
.orElse(ArrayNode.array(this.backingNode.getNodeArray()));
getRealParent().replacePart(replacement, getKeyIndex(), 1);
getDirectParent().updateNodeAtIndex(this.backingNode, keyName, getKeyIndex());
}
} else {
var nodeToUse = newNode.getNodeArray().get(0);
Expand Down

0 comments on commit 5231b1c

Please sign in to comment.