我正在使用monaco编辑器,并且我希望包含来自多个文件的建议。我不确定什么是最好的方法,但是基本上,我希望在导出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;


editorFile

$(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'
    });
});

最佳答案

为了在多个文件中实现IntelliSense的目标,您需要使用monaco.editor的单个实例,并且对于每个要IntelliSense的文件,在应用程序启动期间初始化一个新模型。此外,为了支持自动补全功能,
您必须实现一个完成项提供程序。请阅读下面的详细信息。

应用程序初始负载


使用const myEditorInstance = monaco.editor.create({ ... , model: null })创建单个编辑器实例。
通过monaco.editor.createModel(...)创建n模型实例,其中n是文件数。
eager model syncmonaco.languages.typescript.javascriptDefaults.setEagerModelSync(true)配置语言默认值。
要启用自动补全功能,您需要根据CompletionItemProvider api实现一个方法。


初始加载后处理各种事件


通过monaco.editor.getModels()获取现有模型的列表。
打开文件时(通过用户单击按钮或通过编写的某些代码以编程方式),将编辑器实例的模型设置为要使用myEditorInstance.setModel(model)打开的文件。
关闭文件时,应使用myEditorInstance.saveViewState()保存该模型的视图状态(包括光标位置等)。
如果再次打开该文件,则可以使用myEditorInstance.restoreState()恢复该先前的模型状态。
如果文件被重命名或删除,则需要正确处理相应的模型,并且在文件被重命名的情况下,请重新初始化模型。

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

10-11 12:50
查看更多