gpt4 book ai didi

jquery - 使用 RESTful Webservice 完成 Monaco 编辑器代码

转载 作者:行者123 更新时间:2023-12-03 21:29:46 25 4
gpt4 key购买 nike

我想为 Monaco Editor 0.8.3 实现一个自定义 CompletionItemProvider,即从 RESTful Web 服务中获取完成建议。为此,我想使用 jQuery 3.2.1。

  • 我已经实现了一个实现 monaco.languages.CompletionItemProvider 的类界面。
  • 由于对 webservice 的调用是异步的,我不能直接返回 CompletionItem[]来自 provideCompletionItems()方法。因此我想我必须返回一个 monaco.Thenable<monaco.languages.CompletionItem[]> -目的。
  • 不幸的是documentation regarding the use of Thenable 很稀疏。我猜,monaco.Promise类是 Thenable 的默认实现界面。正如我发现的 one example using this class , 我试了一下...

  • 这是相关代码(以下是完整代码)

                    return new monaco.Promise<monaco.languages.CompletionItem[]>(function (c, e, p) {
    req = $.getJSON("/codeeditorapi/objectcompletions/" + objAndProp[0]);
    req.done(function (data) {
    console.log("objectcompletions c(data) with " + data);
    c(data);
    });
    req.fail((data) => e(data));
    }, function () {
    req.abort();
    });

    现在是该类的完整代码

    class MMSObjectModelCompletionProvider implements monaco.languages.CompletionItemProvider
    {

    private PVAN:string = "v";//PlantVariableAccessor Name
    public provideCompletionItems(model: monaco.editor.IReadOnlyModel, position: monaco.Position, token: monaco.CancellationToken): monaco.Thenable<monaco.languages.CompletionItem[]> | monaco.languages.CompletionItem[]{
    var line = model.getValueInRange({ startLineNumber: position.lineNumber, startColumn: 1, endLineNumber: position.lineNumber, endColumn: position.column });
    let pos = line.lastIndexOf(this.PVAN+".");
    if (pos < 0 || line.length < pos + this.PVAN.length+1) //+1 for the dot behind
    {
    return [];
    }

    let objAndProp: string[] = line.substr(pos + this.PVAN.length + 1).split(".");
    let ret: monaco.languages.CompletionItem[];
    var req: JQueryXHR;
    switch (objAndProp.length)
    {
    case 3:
    //Only append the value
    return [
    {
    label: "Value",
    kind: monaco.languages.CompletionItemKind.Property,
    detail: "Access Value of plant variable",
    },
    ];
    case 2:
    //objectname finished, search for property
    if (objAndProp[0].length == 0 || objAndProp[1].length == 0) return [];
    return new monaco.Promise<monaco.languages.CompletionItem[]>(function (c, e, p) {
    req = $.getJSON("/codeeditorapi/propertycompletions/" + objAndProp[0] + "/" + objAndProp[1]);
    req.done(function (data) {
    console.log("propertycompletions c(data) with " + data);
    c(data);
    });
    req.fail((data) => e(data));
    }, function () {
    req.abort();
    });
    case 1:
    //search for object name
    if (objAndProp[0].length == 0) return [];
    return new monaco.Promise<monaco.languages.CompletionItem[]>(function (c, e, p) {
    req = $.getJSON("/codeeditorapi/objectcompletions/" + objAndProp[0]);
    req.done(function (data) {
    console.log("objectcompletions c(data) with " + data);
    c(data);
    });
    req.fail((data) => e(data));
    }, function () {
    req.abort();
    });
    default: return [];
    }
    }
    }

    好吧,webservice 被正确调用并返回带有 CompletionItems 的预期数组。我可以看到 console.log 输出。然而,电话 c(data)在库深处的某个地方引发以下错误。
    Uncaught Error: Cannot read property 'length' of undefined

    TypeError: Cannot read property 'length' of undefined
    at Object.S [as compareIgnoreCase] (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:33:4808)
    at _ (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:74:21918)
    at C (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:74:22403)
    at Array.sort (native)
    at http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:74:21249
    at Object.g [as _notify] (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:35:5529)
    at Object.enter (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:35:9107)
    at n.Class.derive._creator._run (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:35:10935)
    at n.Class.derive._creator._completed (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:35:10376)
    at n.Class.define.cancel.then (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:35:12368)
    at Object.S [as compareIgnoreCase] (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:33:4808)
    at _ (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:74:21918)
    at C (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:74:22403)
    at Array.sort (native)
    at http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:74:21249
    at Object.g [as _notify] (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:35:5529)
    at Object.enter (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:35:9107)
    at n.Class.derive._creator._run (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:35:10935)
    at n.Class.derive._creator._completed (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:35:10376)
    at n.Class.define.cancel.then (http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:35:12368)
    at http://localhost:58254/Scripts/monaco-editor-0.8.3/min/vs/editor/editor.main.js:33:25360

    我不知道,现在该怎么办。任何帮助或提示表示赞赏。非常感谢!

    最佳答案

    对于仍在寻找答案的人来说,它看起来像 provideCompletionItems方法只能返回一个带有期望值的 Promise, Monaco Editor 显示 Loading...在完成框中,直到 promise 解决。
    因此,特别是对于您的情况,只需返回一个 Promise,您可以在其中向 Web 服务发出请求。
    另外,provideCompletionItems方法应该返回一个带有 suggestions 的对象属性与您的完成数组。return { suggestions: [...] }

    关于jquery - 使用 RESTful Webservice 完成 Monaco 编辑器代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44775541/

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