Skip to content

Commit

Permalink
fixed duplicate ids, added better checking for duplicate ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Dec 3, 2023
1 parent e523772 commit 4b99f18
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 38 deletions.
22 changes: 11 additions & 11 deletions model/array.ast-model
Original file line number Diff line number Diff line change
Expand Up @@ -2731,7 +2731,7 @@
]
},
{
"id": "1566ddcf1d4c1d48555470ba",
"id": "2feaaf1ac0a4494341348ed8",
"class": "62e5339b564d29f772934526"
},
{
Expand Down Expand Up @@ -2831,7 +2831,7 @@
"62e5339b564d29f772934527",
[
{
"id": "2a9d94709d28240a4b856585",
"id": "2fda48fdd87f2e1b78085f4c",
"class": "62e5339f564d29f772934545",
"properties": [
[
Expand All @@ -2848,14 +2848,14 @@
"62e5339e564d29f772934542",
[
{
"id": "2a9d94709d28240a4b856586",
"id": "2fda665dd87f2e1b7808655a",
"class": "62e5339e564d29f77293453f",
"children": [
[
"62e5339e564d29f77293453e",
[
{
"id": "2a9d94709d28240a4b856587",
"id": "2fda62fcd87f2e1b78086217",
"class": "62e533a0564d29f772934550",
"references": [
[
Expand All @@ -2870,7 +2870,7 @@
"62e5339e564d29f77293453d",
[
{
"id": "2a9da9279d28240a4b8569d9",
"id": "2fda8dc4d87f2e1b780879ab",
"class": "62e53397564d29f772934502"
}
]
Expand Down Expand Up @@ -3126,12 +3126,12 @@
"654fbb281446e19b3822520b",
[
{
"id": "2aad679417dc0276354f17e9",
"id": "2fdabba5d87f2e1b780887ab",
"class": "62e533a0564d29f772934550",
"references": [
[
"62e533a0564d29f77293454f",
"2a9d94709d28240a4b856585"
"2fda48fdd87f2e1b78085f4c"
]
]
}
Expand Down Expand Up @@ -3207,12 +3207,12 @@
"654fbb281446e19b3822520b",
[
{
"id": "2aaf21b317dc0276354f7b44",
"id": "2fdaedccd87f2e1b78088d15",
"class": "62e533a0564d29f772934550",
"references": [
[
"62e533a0564d29f77293454f",
"2a9d94709d28240a4b856585"
"2fda48fdd87f2e1b78085f4c"
]
]
}
Expand Down Expand Up @@ -3395,12 +3395,12 @@
]
},
{
"id": "2aa9ec7e8c8ee86e32e317a4",
"id": "2fdafce3d87f2e1b7808909f",
"class": "62e533a0564d29f772934550",
"references": [
[
"62e533a0564d29f77293454f",
"2a9d94709d28240a4b856585"
"2fda48fdd87f2e1b78085f4c"
]
]
}
Expand Down
3 changes: 3 additions & 0 deletions scripting/absytree_internal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ proc editor_model_gotoDefinition_void_ModelDocumentEditor_bool_impl*(
proc editor_model_toggleBoolCell_void_ModelDocumentEditor_bool_impl*(
self: ModelDocumentEditor; select: bool = false) =
discard
proc editor_model_invertSelection_void_ModelDocumentEditor_impl*(
self: ModelDocumentEditor) =
discard
proc editor_model_moveCursorLeft_void_ModelDocumentEditor_bool_impl*(
self: ModelDocumentEditor; select: bool = false) =
discard
Expand Down
2 changes: 2 additions & 0 deletions scripting/absytree_internal_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ proc editor_model_gotoDefinition_void_ModelDocumentEditor_bool_impl(
self: ModelDocumentEditor; select: bool = false) {.importc.}
proc editor_model_toggleBoolCell_void_ModelDocumentEditor_bool_impl(
self: ModelDocumentEditor; select: bool = false) {.importc.}
proc editor_model_invertSelection_void_ModelDocumentEditor_impl(
self: ModelDocumentEditor) {.importc.}
proc editor_model_moveCursorLeft_void_ModelDocumentEditor_bool_impl(
self: ModelDocumentEditor; select: bool = false) {.importc.}
proc editor_model_moveCursorRight_void_ModelDocumentEditor_bool_impl(
Expand Down
2 changes: 2 additions & 0 deletions scripting/editor_model_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ proc gotoDefinition*(self: ModelDocumentEditor; select: bool = false) =
editor_model_gotoDefinition_void_ModelDocumentEditor_bool_impl(self, select)
proc toggleBoolCell*(self: ModelDocumentEditor; select: bool = false) =
editor_model_toggleBoolCell_void_ModelDocumentEditor_bool_impl(self, select)
proc invertSelection*(self: ModelDocumentEditor) =
editor_model_invertSelection_void_ModelDocumentEditor_impl(self)
proc moveCursorLeft*(self: ModelDocumentEditor; select: bool = false) =
editor_model_moveCursorLeft_void_ModelDocumentEditor_bool_impl(self, select)
proc moveCursorRight*(self: ModelDocumentEditor; select: bool = false) =
Expand Down
14 changes: 14 additions & 0 deletions scripting/editor_model_api_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ proc toggleBoolCell*(self: ModelDocumentEditor; select: bool = false) =
argsJsonString.cstring)


proc editor_model_invertSelection_void_ModelDocumentEditor_wasm(arg: cstring): cstring {.
importc.}
proc invertSelection*(self: ModelDocumentEditor) =
var argsJson = newJArray()
argsJson.add block:
when ModelDocumentEditor is JsonNode:
self
else:
self.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_model_invertSelection_void_ModelDocumentEditor_wasm(
argsJsonString.cstring)


proc editor_model_moveCursorLeft_void_ModelDocumentEditor_bool_wasm(arg: cstring): cstring {.
importc.}
proc moveCursorLeft*(self: ModelDocumentEditor; select: bool = false) =
Expand Down
66 changes: 43 additions & 23 deletions src/ast/model.nim
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,48 @@ type
loaded*: bool = false
computationContext*: ModelComputationContextBase

proc resolveReference*(self: Model, id: NodeId): Option[AstNode]
proc resolveReference*(self: Project, id: NodeId): Option[AstNode]
proc dump*(node: AstNode, model: Model = nil, recurse: bool = false): string

template forEach2*(node: AstNode, it: untyped, body: untyped): untyped =
node.forEach proc(n: AstNode) =
let it = n
body

iterator childrenRec*(node: AstNode): AstNode =
var stack: seq[tuple[node: AstNode, listIndex: int, childIndex: int]]
stack.add (node, 0, 0)

while stack.len > 0:
let (currentNode, listIndex, childIndex) = stack.pop
if listIndex == 0 and childIndex == 0:
yield currentNode

if listIndex == currentNode.childLists.len:
continue

if childIndex == currentNode.childLists[listIndex].nodes.len:
stack.add (currentNode, listIndex + 1, 0)
else:
stack.add (currentNode, listIndex, childIndex + 1)
stack.add (currentNode.childLists[listIndex].nodes[childIndex], 0, 0)

proc newProject*(): Project =
new result

proc addModel*(self: Project, model: Model) =
# log lvlWarn, fmt"addModel: {model.path}, {model.id}"
var foundExistingNodes = false
for root in model.rootNodes:
for node in root.childrenRec:
if self.resolveReference(node.id).getSome(existing):
log lvlError, &"addModel({model.path} {model.id}): Node with id {node.id} already exists in model {existing.model.path} ({existing.model.id}).\nExisting node: {existing.dump(recurse=true)}\nNew node: {node.dump(recurse=true)}"
foundExistingNodes = true

if foundExistingNodes:
return

assert model.project.isNil
model.project = self
self.models[model.id] = model
Expand Down Expand Up @@ -261,29 +298,6 @@ proc forEach*(node: AstNode, f: proc(node: AstNode) {.closure.}) =
for c in item.nodes:
c.forEach(f)

template forEach2*(node: AstNode, it: untyped, body: untyped): untyped =
node.forEach proc(n: AstNode) =
let it = n
body

iterator childrenRec*(node: AstNode): AstNode =
var stack: seq[tuple[node: AstNode, listIndex: int, childIndex: int]]
stack.add (node, 0, 0)

while stack.len > 0:
let (currentNode, listIndex, childIndex) = stack.pop
if listIndex == 0 and childIndex == 0:
yield currentNode

if listIndex == currentNode.childLists.len:
continue

if childIndex == currentNode.childLists[listIndex].nodes.len:
stack.add (currentNode, listIndex + 1, 0)
else:
stack.add (currentNode, listIndex, childIndex + 1)
stack.add (currentNode.childLists[listIndex].nodes[childIndex], 0, 0)

proc verify*(self: Language): bool =
result = true
for c in self.classes.values:
Expand Down Expand Up @@ -419,6 +433,12 @@ proc resolveReference*(self: Model, id: NodeId): Option[AstNode] =
return model.nodes[id].some
return AstNode.none

proc resolveReference*(self: Project, id: NodeId): Option[AstNode] =
for model in self.models.values:
if model.nodes.contains(id):
return model.nodes[id].some
return AstNode.none

proc newNodeClass*(
id: ClassId,
name: string,
Expand Down
11 changes: 7 additions & 4 deletions src/model_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1794,8 +1794,12 @@ proc gotoDefinition*(self: ModelDocumentEditor, select: bool = false) {.expose("

if cell.node.references.len == 1:
if cell.node.model.resolveReference(cell.node.references[0].node).getSome(target):
self.cursor = self.getFirstEditableCellOfNode(target).get
self.updateScrollOffset()
if target.model == self.document.model:
self.cursor = self.getFirstEditableCellOfNode(target).get
self.updateScrollOffset()
else:
# todo: open node in new/existing editor
discard

self.markDirty()

Expand Down Expand Up @@ -2943,7 +2947,6 @@ proc runSelectedFunction*(self: ModelDocumentEditor) {.expose("editor.model").}

proc copyNode*(self: ModelDocumentEditor) {.expose("editor.model").} =
let (parentCell, _, _) = self.selection.getParentInfo

let json = parentCell.node.toJson
self.app.setRegisterText($json, "")

Expand All @@ -2965,7 +2968,7 @@ proc pasteNode*(self: ModelDocumentEditor) {.expose("editor.model").} =
if self.getNodeFromRegister("").getSome(node):
var foundExisting = false
for child in node.childrenRec:
if self.document.model.resolveReference(node.id).isSome:
if self.document.project.resolveReference(node.id).isSome:
log lvlWarn, fmt"Node {node} already exists in model"
foundExisting = true
break
Expand Down

0 comments on commit 4b99f18

Please sign in to comment.