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

时间:2017-06-27 08:28:44

标签: jquery typescript promise monaco-editor

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

  • 我实现了一个实现monaco.languages.CompletionItemProvider接口的类。
  • 由于对Web服务的调用是异步的,我无法直接从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

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

1 个答案:

答案 0 :(得分:0)

对于仍在寻找答案的任何人,看来provideCompletionItems方法只能返回具有期望值的Promise,并且Monaco编辑器在完成框中显示Loading...直到诺言得以解决。 / p>

因此,特别是对于您的情况,只需返回一个Promise,即可在其中向Web服务发出请求。 同样,provideCompletionItems方法应返回一个带有suggestions属性的对象,该对象带有您的补全数组。 return { suggestions: [...] }