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

标签 monaco-editor

我正在使用 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/

相关文章:

visual-studio-code - 有没有办法在 google cloud shell 中使用 VS code 插件?

svelte - 在 Svelte 中使用 Monaco 编辑器和 Rollup

json - 在 Monaco Editor 中点击按钮折叠展开

javascript - Monaco Editor : only show part of document

javascript - 如何使 monaco-editor 像 codemirror 自动调整大小模式一样自动适应内容?

visual-studio-code - 如何在 monaco-editor 中使用 VSC 主题?

tokenize - Monaco Editor - 使用递归状态匹配同一行上的任意数量的参数?

reactjs - Monaco Editor deltaDecorations 更改整个文本的样式,而不仅仅是给定范围

monaco-editor - Monaco Editor : Configure libs by editor

typescript - 如何从 monaco 编辑器获取转译代码?