diff --git a/src/client/media/diceCoefficient.png b/src/client/media/diceCoefficient.png new file mode 100644 index 000000000..5e53906e3 Binary files /dev/null and b/src/client/media/diceCoefficient.png differ diff --git a/src/client/tree-sitter.js b/src/client/tree-sitter.js index ab75924ba..03f8dec08 100644 --- a/src/client/tree-sitter.js +++ b/src/client/tree-sitter.js @@ -136,16 +136,19 @@ export function isomorphic(node1, node2) { return true; } +// similarity coefficient + + +/*MD + +![](media/diceCoefficient.png){width=100px} +MD*/ export function dice(t1, t2, M) { let descendantsT1 = s(t1) let descendantsT2 = s(t2) - - let mappedElements = [] // the ratio of common descendants between two nodes given a set of mappings M - - for (let m of M) { if (descendantsT1.has(m.node1.id) && descendantsT2.has(m.node2.id)) { mappedElements.push(m) @@ -338,7 +341,6 @@ function isLeaf(node) { function lastChanceMatch(mappings, src, dst, maxSize) { - debugger if (s(src).size < maxSize || s(dst).size < maxSize) { let zsMappings = zhangShashaMapping(src, dst, function children(node) { return node.children }, @@ -346,16 +348,17 @@ function lastChanceMatch(mappings, src, dst, maxSize) { function removeCost() { return 1 }, function updateCost(from, to) { if (from.type === to.type) { - debugger return qGramsDifference(label(from), label(from), 2) } else { return 1 } }); for (let candidate of zsMappings) { - let srcCand = candidate.t1; - let dstCand = candidate.t2; - addMapping(mappings, srcCand, dstCand); + if (candidate.t1 && candidate.t2) { + if (!isSrcMapped(candidate.t1, mappings) && !isDstMapped(candidate.t2, mappings)) { + addMapping(mappings, candidate.t1, candidate.t2); + } + } } } } @@ -371,8 +374,10 @@ function bottomUpPhase(T1, dst, mappings, minDice, maxSize) { visitPostorder(T1, t => { if (!t.parent) { - addMapping(mappings, t, dst) - lastChanceMatch(mappings, t, dst, maxSize); + if (!isSrcMapped(t, mappings)) { + addMapping(mappings, t, dst) + lastChanceMatch(mappings, t, dst, maxSize); + } } else if (!isSrcMapped(t, mappings) && !isLeaf(t)) { let candidatesList = candidates(t, mappings); let best = null; diff --git a/src/components/tools/lively-container.js b/src/components/tools/lively-container.js index ac0edfcdb..b08dd0782 100644 --- a/src/components/tools/lively-container.js +++ b/src/components/tools/lively-container.js @@ -2932,6 +2932,7 @@ export default class Container extends Morph { // TEST + if (unsavedText) { // container or editor load text async and we have to interact with them // await lively.sleep(1000) @@ -2940,10 +2941,7 @@ export default class Container extends Morph { // newEditor.textChanged = true // newEditor.lastText = obj.lastText newEditor.livelyMigrate(oldEditor) - } - - }) } else { this.isMigrating = false; diff --git a/test/tree-sitter-test.js b/test/tree-sitter-test.js index f89eb7a20..d0e964648 100644 --- a/test/tree-sitter-test.js +++ b/test/tree-sitter-test.js @@ -49,6 +49,7 @@ describe('tree-sitter', () => { expect(matches.length).gt(3) + debugger var result = dice(callExpr1,callExpr2, matches) expect(result).to.equal(1) @@ -132,7 +133,7 @@ a = 3 + 4`]) expect(matches.length).gt(5) for(let match of matches) { if (match.node1.text !== tree1.text) { - expect(match.node1.text).to.equal(match.node2.text) + expect(match.node1.text, match.node1.type + " -> " + match.node2.type).to.equal(match.node2.text) } } }) @@ -175,12 +176,13 @@ if (true) { // that.tree.language.query("(variable_declarator)@a").captures(this) var classDecl1 = tree1.child(0) - var classDecl1 = tree2.child(0) + var classDecl2 = tree2.child(0) var matches = match(tree1, tree2) expect(matches.length).gt(10) + debugger let found = matches.find(ea => ea.node1.id == classDecl1.id && ea.node2.id == classDecl2.id) expect(found).to.not.be.undefined