摩纳哥编辑获得 AST 访问权限

pas*_*luk 5 monaco-editor

我正在使用摩纳哥编辑器来编辑打字稿。有没有办法获得当前模型的 AST?是否可以修改树以便编辑器对更改做出反应?即我想为打字稿做简单的重构工具?

Dav*_*dio 0

Monaco 不公开其 AST,但您可以使用jscodeshift代替:

const editor = monaco.editor.create(
     document.querySelector("#editor"), {value: 'var foo;'})// editor content: var foo;
const newValue = jscodeshift(editor.getValue())
     .findVariableDeclarators('foo')
     .renameTo('bar')
     .toSource();
editor.setValue(newValue); // editor content: var bar;
Run Code Online (Sandbox Code Playgroud)