点击查看html编辑器说明文档

tree-sitter 测试edit icon

|
|
Fork(复制)
|
|
作者:
lynn-ssk
提交反馈
嵌入
设置
下载
HTML
CSS
JS
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ...
展开
</head>
<body>
            
            <script src="https://uovol-resources.sunshineknows.com/tree-sitter/tree-sitter.js"></script>

<script>
  const Parser = window.TreeSitter;
  Parser.init().then(async () => { 
    console.log(123);
    const parser = new Parser;

    // tree-sitter-javascript2 是自己编译出来的。
    // tree-sitter-javascript 是 github 仓库复制的
    const JavaScript = await Parser.Language.load('https://uovol-resources.sunshineknows.com/tree-sitter/tree-sitter-javascript2.wasm');
    parser.setLanguage(JavaScript);

    const sourceCode = 'let x = 1; console.log(x);';
    const tree = parser.parse(sourceCode);

    console.log(tree.rootNode.toString());

    const callExpression = tree.rootNode.child(1).firstChild;
    console.log(callExpression);



    // Replace 'let' with 'const'
    const newSourceCode = 'const x = 1; console.log(x);';
    
    tree.edit({
      startIndex: 0,
      oldEndIndex: 3,
      newEndIndex: 5,
      startPosition: {row: 0, column: 0},
      oldEndPosition: {row: 0, column: 3},
      newEndPosition: {row: 0, column: 5},
    });
    
    const newTree = parser.parse(newSourceCode, tree);
    console.log(newTree.rootNode.toString());

  });
</script>
        
</body>
预览
控制台