From 54afd57582c3d0d5f0fa62e7ab883206211c4a04 Mon Sep 17 00:00:00 2001 From: JensLincke Date: Wed, 18 Oct 2023 17:44:04 +0200 Subject: [PATCH] domain code_ more green SQUASHED: more-green, --- src/client/domain-code.js | 11 ++-- .../domain-code/chawathe-script-generator.js | 4 ++ src/components/tools/domain-code-explorer.js | 6 ++- test/domain-code-test.js | 54 ++++++++++++++++--- 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/client/domain-code.js b/src/client/domain-code.js index fad35dfbd..26fb32cb0 100644 --- a/src/client/domain-code.js +++ b/src/client/domain-code.js @@ -262,10 +262,13 @@ export class DomainObject { if (!domainObject) { domainObject = domainObjectById.get(action.node.id) } + if (!domainObject) { + throw new Error("could not find treeSitter node") + } // 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) - debugger + if (!otherTreeSitter) { throw new Error("could not find other treeSitter node again") } @@ -351,8 +354,6 @@ export class TreeSitterDomainObject extends DomainObject { newEndPosition: loc(newTo).asTreeSitter(), } - this.treeSitter.tree.edit(edit); - DomainObject.edit(this.rootNode(), livelyCodeMirror.value, edit) livelyCodeMirror.dispatchEvent(new CustomEvent("domain-code-changed", {detail: {node: this, edit: edit}})) @@ -453,7 +454,9 @@ export class ReplacementDomainObject extends DomainObject { return this.target && this.target.endPosition } - + get id() { + return this.target.id + } get inspectorClassName() { if (this.type) { diff --git a/src/client/domain-code/chawathe-script-generator.js b/src/client/domain-code/chawathe-script-generator.js index 780decd27..8636c1551 100644 --- a/src/client/domain-code/chawathe-script-generator.js +++ b/src/client/domain-code/chawathe-script-generator.js @@ -35,6 +35,10 @@ import {addMapping, getSrcForDst, getDstForSrc, isSrcMapped, isDstMapped, label, function positionInParent(node) { // return node.parent.children.indexOf(node) // object identity might be a problem? + if (!node.parent) { + return -1 + } + // search for myself based on explicit id and not implicit identitity for(let i=0; i < node.parent.children.length; i++) { if (node.parent.children[i].id == node.id) return i diff --git a/src/components/tools/domain-code-explorer.js b/src/components/tools/domain-code-explorer.js index 076affa04..a6de7b597 100644 --- a/src/components/tools/domain-code-explorer.js +++ b/src/components/tools/domain-code-explorer.js @@ -163,8 +163,10 @@ export default class DomainCodeExplorer extends Morph { this._autoUpdate = false this.sourceEditor.setText(this.editor.getText()) - this.treeSitterRootNode = evt.detail.node.debugNewAST.rootNode - this.astInspector.inspect(this.treeSitterRootNode) + + // TODO + // this.treeSitterRootNode = evt.detail.node.debugNewAST.rootNode + // this.astInspector.inspect(this.treeSitterRootNode) this._autoUpdate = true } diff --git a/test/domain-code-test.js b/test/domain-code-test.js index 539167572..10b6940e3 100644 --- a/test/domain-code-test.js +++ b/test/domain-code-test.js @@ -368,8 +368,43 @@ a = 3` describe('SmilyReplacementDomainObject', () => { - it('click on let replacement works', () => { + it('click on let replacement works MANAL', () => { + + var sourceCode = +`// hello +let a = 3 + 4 +const b = a` + livelyCodeMirror.value = sourceCode + + let domainObject = TreeSitterDomainObject.fromSource(sourceCode) + domainObject.replaceType('let', LetSmilyReplacementDomainObject) + domainObject.replaceType('const', ConstSmilyReplacementDomainObject) + + expect(domainObject.children.length, "childrens").to.equal(3) + + var letReplacement = domainObject.children[1].children[0] + expect(letReplacement.isReplacement).to.be.true + expect(letReplacement.type).to.equal("let") + // letReplacement.target.setText(livelyCodeMirror, "const") + + // MANUAL + livelyCodeMirror.value = `// hello +const a = 3 + 4 +const b = a` + DomainObject.edit(domainObject, livelyCodeMirror.value) + + + expect(livelyCodeMirror.value).to.match(/const a/) + expect(domainObject.treeSitter.childCount, "childCount after replacement").to.equal(3) + expect(domainObject.children.length, "children after replacement").to.equal(3) + + var newConsDomainObject = domainObject.children[1].children[0] + expect(newConsDomainObject.type, "newConst").to.equal("const") + }); + + it('click on let replacement works via setText', () => { + var sourceCode = `// hello let a = 3 + 4 @@ -387,6 +422,13 @@ const b = a` expect(letReplacement.type).to.equal("let") letReplacement.target.setText(livelyCodeMirror, "const") + + expect(livelyCodeMirror.value).to.equal(`// hello +const a = 3 + 4 +const b = a`) + + + expect(livelyCodeMirror.value).to.match(/const a/) expect(domainObject.treeSitter.childCount, "childCount after replacement").to.equal(3) expect(domainObject.children.length, "children after replacement").to.equal(3) @@ -395,6 +437,7 @@ const b = a` expect(newConsDomainObject.type, "newConst").to.equal("const") }); + // #WIP continue here #KnownToFail xit('click on const and then on let replacement', () => { var sourceCode = @@ -408,10 +451,9 @@ const b = a` domainObject.replaceType('const', ConstSmilyReplacementDomainObject) - var consReplacement = domainObject.children[2].children[0] + var constReplacement = domainObject.children[2].children[0] - debugger - consReplacement.target.setText(livelyCodeMirror, "let") + constReplacement.target.setText(livelyCodeMirror, "let") expect(livelyCodeMirror.value).to.match(/let b/) expect(domainObject.treeSitter.childCount, "childCount after replacement").to.equal(3) @@ -445,8 +487,8 @@ const b = a` var letReplacement = domainObject.children[1].children[0] letReplacement.target.setText(livelyCodeMirror, "const") - var consReplacement = domainObject.children[2].children[0] - consReplacement.target.setText(livelyCodeMirror, "let") + var constReplacement = domainObject.children[2].children[0] + constReplacement.target.setText(livelyCodeMirror, "let") expect(livelyCodeMirror.value).to.match(/let b/) expect(domainObject.treeSitter.childCount, "childCount after replacement").to.equal(3) expect(domainObject.children.length, "children after replacement").to.equal(3)