Skip to content

Commit

Permalink
treesitter works like example from paper
Browse files Browse the repository at this point in the history
SQUASHED: AUTO-COMMIT-src-client-media-diceCoefficient.png,AUTO-COMMIT-src-client-tree-sitter.js,AUTO-COMMIT-src-components-tools-lively-container.js,AUTO-COMMIT-test-tree-sitter-test.js,
  • Loading branch information
JensLincke committed Sep 27, 2023
1 parent d372750 commit 67c1bfc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
Binary file added src/client/media/diceCoefficient.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 16 additions & 11 deletions src/client/tree-sitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -338,24 +341,24 @@ 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 },
function insertCost() { return 1 },
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);
}
}
}
}
}
Expand All @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/components/tools/lively-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -2940,10 +2941,7 @@ export default class Container extends Morph {
// newEditor.textChanged = true
// newEditor.lastText = obj.lastText
newEditor.livelyMigrate(oldEditor)

}


})
} else {
this.isMigrating = false;
Expand Down
6 changes: 4 additions & 2 deletions test/tree-sitter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('tree-sitter', () => {

expect(matches.length).gt(3)

debugger
var result = dice(callExpr1,callExpr2, matches)

expect(result).to.equal(1)
Expand Down Expand Up @@ -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)
}
}
})
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 67c1bfc

Please sign in to comment.