From d85a39dd955dc8fc3943d8eb014fd544c01af93b Mon Sep 17 00:00:00 2001 From: JensLincke Date: Wed, 8 Nov 2023 20:01:48 +0100 Subject: [PATCH] Domain Code green again this was hard SQUASHED: AUTO-COMMIT-src-client-domain-code.js,AUTO-COMMIT-src-components-tools-domain-code-explorer.html,AUTO-COMMIT-src-components-tools-domain-code-explorer.js,AUTO-COMMIT-test-domain-code-test.js, --- src/client/domain-code.js | 111 ++++++++++++------ .../tools/domain-code-explorer.html | 1 + src/components/tools/domain-code-explorer.js | 6 +- test/domain-code-test.js | 37 ++++++ 4 files changed, 120 insertions(+), 35 deletions(-) diff --git a/src/client/domain-code.js b/src/client/domain-code.js index 5897c4c56..255b934fe 100644 --- a/src/client/domain-code.js +++ b/src/client/domain-code.js @@ -86,48 +86,84 @@ export class DomainObject { } updateReplacements() { - this.log("updateReplacements") - for (let replacement of this.replacements) { - var currentMatches = [] - this.visit(ea => { - if (ea.type === replacement.query) { - currentMatches.push(ea) + debugger + this.log(updateReplacements) + let addReplacements = [] + let deleteReplacements = [] + let deleteReplacementIds = new Set() + let keepReplacements = [] + let keepReplacementIds = new Set() + this.visit(domainObject => { + if (domainObject.isReplacement) { + if (domainObject.query == domainObject.type) { + keepReplacements.push(domainObject) + keepReplacementIds.add(domainObject.id) + return // no new matches...? #TODO change this logic for nesting replacements + } else { + deleteReplacements.push(domainObject) + deleteReplacementIds.add(domainObject.id) + domainObject = domainObject.target // take yourself out of the process.... } - }) - - let lastMatches = new Map() - replacement.instances.forEach(ea => lastMatches.set(ea.id, ea)) - + } + for (let replacement of this.replacements) { + if (domainObject.type === replacement.query) { + addReplacements.push([replacement, domainObject]) + } + } - let currentMatchesIds = new Set() - for (let ea of currentMatches) { - let instance = lastMatches.get(ea.id) - if (instance) { - // (A) do nothing + }) + + for (let replacement of this.replacements) { + for(let instance of replacement.instances) { + if (keepReplacementIds.has(instance.id)) { + // do nothing } else { - // (B) add replacement - instance = new replacement.class(ea) - replacement.instances.push(instance) + if (!deleteReplacementIds.has(instance.id)) { + deleteReplacements.push(instance) + deleteReplacementIds.add(instance.id) + } } + } + } + + + + this.log("keep Replacements: " + keepReplacements.map(ea => ea.constructor.name + " " + ea.type + " " + ea.id)) + this.log("delete Replacements: " + deleteReplacements.map(ea => ea.constructor.name + " " + ea.type + " " + ea.id)) + this.log("add Replacements: " + addReplacements.map(ea => ea[0].query + " " + ea[1].type + " " + ea[1].id)) + // this.log("all: " + addMatches.map(ea => ea[0].query + " " + ea[1].type + " " + ea[1].id + " -> " + (ea[1].target ? ea[1].target.constructor.name : ""))) + + // first delete + for (let ea of deleteReplacements) { + // (C) remove replacement + this.log("delete " + ea.constructor.name + " " + ea.id) + ea.replacement.instances = ea.replacement.instances.filter(ea => ea !== ea) + ea.remove() + } + + + for(let replacementAndMatch of addReplacements) { + let replacement = replacementAndMatch[0] + let ea = replacementAndMatch[1] + let instance = new replacement.class(ea) + replacement.instances.push(instance) + this.log("add " + instance.constructor.name + " " + instance.id) + } + + debugger + + // and install and render them all again... + // #TODO check here what actually needs to be done + for (let replacement of this.replacements) { + for (let instance of replacement.instances) { + this.log("install " + instance.constructor.name + " " + instance.id +" -> " + instance.target.constructor.name + " " + instance.target.type + " " + instance.id + (instance.target.query ? " query: " + instance.target.query : "")) instance.install() + instance.query = replacement.query + instance.replacement = replacement if (this.livelyCodeMirror) { instance.renderOn(this.livelyCodeMirror) } - - currentMatchesIds.add(ea.id) - } - - - - - let toDelete = replacement.instances.filter(ea => !currentMatchesIds.has(ea.id)) - for (let ea of toDelete) { - // (C) remove replacement - ea.remove() } - replacement.instances = replacement.instances.filter(ea => currentMatchesIds.has(ea.id)) - - } } @@ -297,18 +333,21 @@ export class DomainObject { } } if (action.type === "update") { + let domainObject = domainObjectByOldId.get(action.node.id) if (!domainObject) { domainObject = domainObjectById.get(action.node.id) } if (!domainObject) { - debugger throw new Error("could not find treeSitter node " + action.node.type + " " + action.node.id) } // we ignore the value change of the update but take the actual other treesitter node that is responsible let otherTreeSitter = newTreeSitterNodeById.get(action.other.id) + rootDomainObject.log("update: " + domainObject.constructor.name + " " + otherTreeSitter.type + " " + otherTreeSitter.id) + + if (!otherTreeSitter) { throw new Error("Update failed: could not find other treeSitter node again") } @@ -629,6 +668,8 @@ export class SmilyReplacementDomainObject extends ReplacementDomainObject { } export class LetSmilyReplacementDomainObject extends SmilyReplacementDomainObject { + + smileContent() { return "😀" } @@ -642,6 +683,8 @@ export class LetSmilyReplacementDomainObject extends SmilyReplacementDomainObjec } export class ConstSmilyReplacementDomainObject extends SmilyReplacementDomainObject { + + smileContent() { return "😇" diff --git a/src/components/tools/domain-code-explorer.html b/src/components/tools/domain-code-explorer.html index d72a66227..a82326cab 100644 --- a/src/components/tools/domain-code-explorer.html +++ b/src/components/tools/domain-code-explorer.html @@ -98,6 +98,7 @@ +
Log:
\ No newline at end of file diff --git a/src/components/tools/domain-code-explorer.js b/src/components/tools/domain-code-explorer.js index 1ca7c1415..4b8e5473a 100644 --- a/src/components/tools/domain-code-explorer.js +++ b/src/components/tools/domain-code-explorer.js @@ -154,6 +154,10 @@ export default class DomainCodeExplorer extends Morph { this.dispatchEvent(new CustomEvent("initialize")); } + onClearLog() { + this.get("#log").innerHTML = "" + } + onEditorCursorActivity(cm) { var from = cm.getCursor(true) var to = cm.getCursor(false) @@ -213,7 +217,7 @@ export default class DomainCodeExplorer extends Morph { onDomainGraphButton() { lively.openMarkdown(lively4url + "/src/components/tools/domain-code-graph.md", - "Domain Graph Graph", {domainObject: this.domainObject}) + "Domain Code Graph", {domainObject: this.domainObject}) } /*MD ## Execution MD*/ diff --git a/test/domain-code-test.js b/test/domain-code-test.js index d382ebd43..017408927 100644 --- a/test/domain-code-test.js +++ b/test/domain-code-test.js @@ -596,5 +596,42 @@ const b = a` var newLetDomainObject = domainObject.children[2].children[0] expect(newLetDomainObject.type, "newLet").to.equal("let") }); + + it('click on const replacement then on new let replacement ', () => { + let livelyCodeMirror = livelyReplacementCodeMirror + + var sourceCode = + `// hello +let a = 3 + 4 +const b = a` + livelyCodeMirror.value = sourceCode + + let domainObject = TreeSitterDomainObject.fromSource(sourceCode) + livelyReplacementCodeMirror.domainObject = domainObject + domainObject.livelyCodeMirror = livelyCodeMirror + domainObject.replaceType('let', LetSmilyReplacementDomainObject) + domainObject.replaceType('const', ConstSmilyReplacementDomainObject) + + + var constReplacement = domainObject.children[2].children[0] + constReplacement.livelyCodeMirror = livelyCodeMirror + expect(constReplacement.isReplacement, "const isReplacement").to.be.true + + constReplacement.onClick() + + + var newLetDomainObject = domainObject.children[2].children[0] + expect(newLetDomainObject.type, "newLet").to.equal("let") + + newLetDomainObject.livelyCodeMirror = livelyCodeMirror + expect(newLetDomainObject.isReplacement, "let isReplacement").to.be.true + + + newLetDomainObject.onClick() + + var newConstDomainObject = domainObject.children[2].children[0] + expect(newConstDomainObject.type, "newConst").to.equal("const") + }); + }) });