gpt4 book ai didi

monaco-editor - 来自多个文件的 Monaco Editor 智能感知

转载 作者:行者123 更新时间:2023-12-04 06:16:29 28 4
gpt4 key购买 nike

我正在使用 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;
文件2
function newTest() {

}
module.exports.newTest = newTest;
编辑器文件
$(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'
});
});

最佳答案

我的用例几乎与您相同,但我有一个文件用作我的“定义”,用于填充主编辑器的自动完成功能。根据彼得提供的答案,我能够整理出以下工作示例。我相信您缺少的部分是创建模型,然后使用 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)
.editor {
margin: 1em 0;
border: 1px solid #999;
}
<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>

关于monaco-editor - 来自多个文件的 Monaco Editor 智能感知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57146485/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com