Skip to content

Commit

Permalink
Domain Code green again this was hard
Browse files Browse the repository at this point in the history
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,
  • Loading branch information
JensLincke committed Nov 8, 2023
1 parent 1a379f5 commit d85a39d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 35 deletions.
111 changes: 77 additions & 34 deletions src/client/domain-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(<b>updateReplacements</b>)
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))


}
}

Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -629,6 +668,8 @@ export class SmilyReplacementDomainObject extends ReplacementDomainObject {
}

export class LetSmilyReplacementDomainObject extends SmilyReplacementDomainObject {


smileContent() {
return "😀"
}
Expand All @@ -642,6 +683,8 @@ export class LetSmilyReplacementDomainObject extends SmilyReplacementDomainObjec
}

export class ConstSmilyReplacementDomainObject extends SmilyReplacementDomainObject {



smileContent() {
return "😇"
Expand Down
1 change: 1 addition & 0 deletions src/components/tools/domain-code-explorer.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<lively-editor class="pane" id="editor"></lively-editor>
</div>
</div>
<div>Log: <button id="clearLog">clear</button></div>
<div id="log"></div>
</div>
</template>
6 changes: 5 additions & 1 deletion src/components/tools/domain-code-explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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*/

Expand Down
37 changes: 37 additions & 0 deletions test/domain-code-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
});

})
});

0 comments on commit d85a39d

Please sign in to comment.