Skip to content

Commit

Permalink
Merge branch 'gh-pages' of https://github.com/LivelyKernel/lively4-core
Browse files Browse the repository at this point in the history
… into gh-pages
  • Loading branch information
onsetsu committed Oct 29, 2023
2 parents 4d247ef + bc0a922 commit 5baec81
Show file tree
Hide file tree
Showing 11 changed files with 416 additions and 151 deletions.
80 changes: 80 additions & 0 deletions demos/tree-sitter/edit-domain-code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Edit Domain Code

<script>
import {visit, Parser, JavaScript, match, debugPrint} from 'src/client/tree-sitter.js';
import { ChawatheScriptGenerator} from 'src/client/domain-code/chawathe-script-generator.js';
import {treesitterVisit, parseQuery, DomainObject, TreeSitterDomainObject, LetSmilyReplacementDomainObject, ConstSmilyReplacementDomainObject} from 'src/client/domain-code.js';


let editor1 = await (<lively-code-mirror style="display:inline-block; width: 400px; height: 200px; border: 1px solid gray"></lively-code-mirror>)
let editor2 = await (<lively-code-mirror style="display:inline-block; width: 400px; height: 200px; border: 1px solid gray"></lively-code-mirror>)


var parser = new Parser();
parser.setLanguage(JavaScript);
var list = <ul></ul>

// editor1.value = `let a = 3 + 4`
editor1.value = `let a = 3`
// editor2.value = `let a = 3 + 4\na++`
editor2.value = `const a = 3`

editor1.editor.on("change", (() => update()).debounce(500));
editor2.editor.on("change", (() => update()).debounce(500));


async function update() {
let sourceOriginal = editor1.value
let sourceNew = editor2.value

let root = TreeSitterDomainObject.fromSource(sourceOriginal)


// editDiv.innerHTML = JSON.stringify(edit, undefined, 2)
domain1.innerHTML = "DOMAIN1\n" + root.debugPrint()

try {
DomainObject.edit(root, sourceNew, undefined, {
newAST(ast) {
newtree.innerHTML = "TREESITTER2\n" + debugPrint(ast.rootNode)
},
actions(list) {
actions.innerHTML = Array.from(list).map(ea => ea.type
+ (ea.pos ? " pos: " + ea.pos : "")
+ " node: " + ea.node.type + " " + ea.node.id
+ (ea.parent ? (" parent: " + ea.parent.type + " " + ea.parent.id) : "")
+ (ea.other ? (" other: " + ea.other.type + " " + ea.other.id) : "")
+ (ea.value ? " value: " + ea.value : "")
).join("\n")
},
log(s) {
logList.appendChild(<div>{s}</div>)
}

})
domain2.innerHTML = "DOMAIN2\n" + root.debugPrint()
} catch(e) {
domain2.innerHTML = ""
domain2.appendChild(await (<lively-error>{e}</lively-error>))
}

}


let actions = <div style="white-space:pre;font-family:monospace"></div>
let domain1 = <div style="white-space:pre;font-family:monospace;display:inline-block;border:1px solid gray"></div>
let domain2 = <div style="white-space:pre;font-family:monospace;display:inline-block;border:1px solid gray"></div>
let newtree = <div style="white-space:pre;font-family:monospace;display:inline-block;border:1px solid gray"></div>
let logList = <div style="white-space:pre;font-family:monospace;border:1px solid gray"></div>

let pane = <div>
{editor1}{editor2}
{actions}
{domain1}{newtree}{domain2}
{logList}
</div>

update()

pane
</script>
6 changes: 4 additions & 2 deletions demos/tree-sitter/edit-history.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// editor1.value = `let a = 3 + 4`
editor1.value = `var a = 3`
// editor2.value = `let a = 3 + 4\na++`
editor2.value = `{var a = 3}`
editor2.value = `var a = 3\nl`

editor1.editor.on("change", (() => update()).debounce(500));
editor2.editor.on("change", (() => update()).debounce(500));
Expand All @@ -31,10 +31,12 @@

scriptGenerator.generate()

debugger

list.innerHTML = ""

for(let action of scriptGenerator.actions) {
list.appendChild(<li>{action.type} {action.node && action.node.type}
list.appendChild(<li>{action.type} {action.node && action.node.type} {action.pos} {action.parent && action.parent.type}
<button style="font-size:6pt" click={() => lively.openInspector(action)}>inspect</button>
</li>)
}
Expand Down
13 changes: 11 additions & 2 deletions demos/tree-sitter/matches.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@
var vis = await (<treesitter-matches></treesitter-matches>)

// editor1.value = `let a = 3 + 4`
editor1.value = `let a = 3`
editor1.value = `class Test {
foo(i) {
if (i == 0) return "Foo!"
}
}`
// editor2.value = `let a = 3 + 4\na++`
editor2.value = `{let a = 2+4}`
editor2.value = `class Test {
foo(i) {
if (i == 0) return "Bar"
else if (i == -1) return "Foo!"
}
}`

editor1.editor.on("change", (() => update()).debounce(500));
editor2.editor.on("change", (() => update()).debounce(500));
Expand Down
Loading

0 comments on commit 5baec81

Please sign in to comment.