摩纳哥编辑器registerCompletionItemProvider删除默认的局部变量完成

时间:2018-07-16 10:26:21

标签: monaco-editor

在摩纳哥编辑器中,使用标准初始化,例如:

monaco.editor.create(document.getElementById("container"), {
    value: "",
    language: "csharp"
});

将具有开箱即用的局部变量代码完成。例如,使用上面的标准初始化,然后输入如下代码:

string testVariable = "This is a string";
int aValue = 123;

代码完成功能将同时识别“ testVariable”和“ aValue”变量,并将其显示在“代码完成”列表中。

但是,如果我们向这样的初始化添加一个registerCompletionItemProvider:

//Custom Code Completion function
function createCompleters() {
    return [
        {
            label: 'customFunction1',
            kind: monaco.languages.CompletionItemKind.Function,
            documentation: "My first Custom Function",
            insertText: 'customFunction1()'
        },
        {
            label: 'customFunction2',
            kind: monaco.languages.CompletionItemKind.Function,
            documentation: "My second Custom Function",
            insertText: 'customFunction2()'
        }
    ];
}
//Register the custom completion function into Monaco Editor    
monaco.languages.registerCompletionItemProvider('csharp', {
    provideCompletionItems: function(model, position) {
        return createCompleters();
    }
});
//Continue with the Standard initialization here...
monaco.editor.create(document.getElementById("container"), {
    value: "",
    language: "csharp"
});

然后,不再识别局部变量,仅识别已注册的功能。

如何注册自定义代码完成但仍保留局部变量完成?谢谢!

1 个答案:

答案 0 :(得分:0)

您需要添加一些与您的功能匹配的东西。如果全部匹配,则只会找到createCompleters()函数中的函数。我还没有找到如何根据返回的建议中的内容进行搜索以及是否要查询局部变量的方法。下面的代码有效,请在操场上尝试并开始键入“ cu”。我知道这不是完整的答案,但希望它能帮助您前进。

from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy

from .models import Property

# Create your views here.
class PropertyListView(ListView):
        model = Property
        template_name = 'property_list.html'

class PropertyDetailView(DetailView):
        model = Property
        template_name = 'property_detail.html'

class PropertyCreateView(LoginRequiredMixin, CreateView):
        model = Property
        template_name = 'property_new.html'
        fields = [
                'property_type', 
                'is_for_sale', 
                'cost', 
                'location', 
                'num_of_bedrooms', 
                'num_of_bathrooms', 
                'num_of_parking_spaces', 
                'num_of_garages',
                'has_pool',
                'has_waterfront',
                'has_elevator',
                'added_on',
        ]

        login_url = 'login'

        def form_valid(self, form):
                form.instance.author = self.request.user
                return super().form_valid(form)

# class PropertyUpdateView(LoginRequiredMixin, UpdateView):
#       model = Property
#       fields = ['title', 'body', ]
#       template_name = 'property_edit.html'
#       login_url = 'login'

# class PropertyDeleteView(LoginRequiredMixin, DeleteView):
#       model = Property
#       template_name = 'property_delete.html'
#       success_url = reverse_lazy('property_list')
#       login_url = 'login'
#