我正在使用 monaco-editor,我想包含来自多个文件的建议。我不确定什么是最好的方法,但基本上,我希望当我在 file2.js 中导出一些函数时,能够从建议中的另一个 file1.js 访问它。
关于如何实现这一目标的任何想法?谢谢 !
文件 1
var express = require('express');
var pug = require('pug');
var config = require('./config');
var fs = require('fs');
var router = express.Router();
var utils = require('/utils');
// Here I would like to use the function newTest from the other file
but it does not show in the suggestions
router.get('/', function (req, res) {
console.log("ip - ", req.connection.remoteAddress)
res.send(pug.compileFile('views/main.pug')({
config
}))
});
module.exports = router;
Run Code Online (Sandbox Code Playgroud)
文件 2
function newTest() {
}
module.exports.newTest = newTest;
Run Code Online (Sandbox Code Playgroud)
编辑器文件
$(document).ready(function() {
// I prefetch my models, then I have a callback to create an instance of the editor
preFetchAllModels(function() {
var models = monaco.editor.getModels();
// I check that I have my models (file1 and file2) prefetched before creating the editor
console.log("models", models);
monaco.languages.typescript.javascriptDefaults.setEagerModelSync(true)
monacoEditor = monaco.editor.create(document.getElementById("editor"), {
value: "loading...",
language: "javascript",
theme: 'monokai',
lineHeight: 20,
fontSize: 16,
wordWrap: "bounded",
automaticLayout: true,
wrappingIndent: 'indent'
});
});
Run Code Online (Sandbox Code Playgroud)
Pet*_*ter 12
要实现跨多个文件的 IntelliSense 目标,您需要monaco.editor在应用程序启动期间使用单个实例并为每个文件使用 IntelliSense,初始化一个新模型。此外,对于自动完成支持,您必须实现一个完成项提供程序。阅读下文了解更多详情。
const myEditorInstance = monaco.editor.create({ ... , model: null })创建一个单独的编辑器实例。monaco.editor.createModel(...)wheren是文件数创建 n-model 实例。monaco.languages.typescript.javascriptDefaults.setEagerModelSync(true)。monaco.editor.getModels()。myEditorInstance.setModel(model).myEditorInstance.saveViewState().myEditorInstance.restoreState().我和你有几乎一样的用例,除了我有一个文件作为我的“定义”来填充主编辑器的自动完成。根据 Peter 提供的答案,我能够整理出以下工作示例。我相信您缺少的部分是创建模型,然后将editor.setModel(model)其分配给您的编辑器。
const editorOptions = {
minimap: { enabled: false },
scrollBeyondLastLine: false,
model: null,
language: "javascript"
}
const initEditor = () => {
monaco.languages.typescript.javascriptDefaults.setEagerModelSync(true)
const defModel = monaco.editor.createModel(
"const helloWorld = () => { return \"Hello World!\"; }",
"javascript"
)
const textModel = monaco.editor.createModel(
"helloWorld()",
"javascript"
)
const e1 = monaco.editor.create(
document.getElementById("editor1"), editorOptions
)
e1.setModel(defModel)
const e2 = monaco.editor.create(
document.getElementById("editor2"), editorOptions
)
e2.setModel(textModel)
}
require.config({
paths: {
vs: "https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.18.0/min/vs"
}
})
require(["vs/editor/editor.main"], initEditor)Run Code Online (Sandbox Code Playgroud)
.editor {
margin: 1em 0;
border: 1px solid #999;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.18.0/min/vs/loader.js"></script>
<h3>Definitions</h3>
<div class="editor" id="editor1" style="width: 100%; height: 40px"></div>
<h3>Editor</h3>
<div class="editor" id="editor2" style="width: 100%; height: 300px"></div>Run Code Online (Sandbox Code Playgroud)
看起来下面的代码成功了:
monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
allowNonTsExtensions: true
});
Run Code Online (Sandbox Code Playgroud)