Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegisterCompletionItemProvider problem #138

Closed
elgransan opened this issue Aug 16, 2024 · 2 comments
Closed

RegisterCompletionItemProvider problem #138

elgransan opened this issue Aug 16, 2024 · 2 comments

Comments

@elgransan
Copy link

Hi, thanks for the port It's awesome.
I want to get the code selection from the provideCompletionItems delegate, like the javascript example on the Monaco playground:

monaco.languages.registerCompletionItemProvider("json", {
	provideCompletionItems: function (model, position) {
		var word = model.getWordUntilPosition(position);   <-- this
		var range = {
			startLineNumber: position.lineNumber,
			endLineNumber: position.lineNumber,
			startColumn: word.startColumn,
			endColumn: word.endColumn,
		};
		return {
			suggestions: createDependencyProposals(range),
		};
	},
});

But the wrapper sends a "modelUri" not the model itself, how can I convert this string to the real object? I tried using
BlazorMonaco.Editor.Global.GetModel(JsRuntime, modelUri); or also _editor.GetSelection() but there are not allowed async operations.

Thanks!

@brettwinters
Copy link

I have no idea also, but it seems you have to do something like this:

 await Global.RegisterCompletionItemProvider(JsRuntime, "sql", (model, position, context) =>
        {
            ArgumentNullException.ThrowIfNull(_editor);
            
            var text = _editor.GetModel().Result;
            var pos = _editor.GetPosition().Result;
            var word = text.GetWordUntilPosition(pos).Result.Word;

            if (word is not null &&
                word.Trim().EndsWith("FROM", StringComparison.InvariantCultureIgnoreCase))
            {
                   // return your list 
            }
}

In blazor this doesn't work because you can't call .Result on async methods so I'm trying to figure that out now...

@serdarciplak
Copy link
Owner

In v3.3.0, async support is added to the RegisterCompletionItemProvider method. So, you can do something like below. I'll release that version in a few days.

await BlazorMonaco.Languages.Global.RegisterCompletionItemProvider(jsRuntime, "javascript", async (modelUri, position, context) =>
{
    var model = await BlazorMonaco.Editor.Global.GetModel(jsRuntime, modelUri);
    var word = await model.GetWordUntilPosition(position);
    var range = new BlazorMonaco.Range
    {
        StartLineNumber = position.LineNumber,
        EndLineNumber = position.LineNumber,
        StartColumn = word.StartColumn,
        EndColumn = word.EndColumn,
    };
    return new CompletionList
    {
        Suggestions = createDependencyProposals(range)
    };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants