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

Completion after Enter or paste #918

Closed
ascoders opened this issue Jun 16, 2018 · 22 comments
Closed

Completion after Enter or paste #918

ascoders opened this issue Jun 16, 2018 · 22 comments

Comments

@ascoders
Copy link

monaco-editor version: 0.13.1
Browser: Chrome
OS: Mac
Steps or JS usage snippet reproducing the issue:

Hello there! If there is a way to make editor request completion items after the "enter" or "cmd+v" was typed?

For dot using triggerCharacters: ["."], but how to trigger completion after "enter" or "cmd+v"?

@alexdima
Copy link
Member

You could implement this by:

@ascoders
Copy link
Author

Thanks @alexandrudima ! It's workable to listening enter or cmd+v using editor.onDidType or editor.onDidPaste, It's fantastic! But i also have one question.

How to trigger suggest specific provideCompletionItems?

As you can say, editor.trigger('anything', 'editor.action.triggerSuggest') can show completion items directly, but the model filled No suggestions, that isn't what i want.

I have registered a completionItemProvider:

monaco.language.registerCompletionItemProvider('sql', provideCompletionItems: (model, position) => {
  return myCustomItems
})

Using the object model and position, i create myCustomItems. My question is, how to do provideCompletionItems effects using editor.trigger('anything', 'editor.action.triggerSuggest')? I can't find any docs, I guess I can get model and position some where, and return new completion items?

@ascoders
Copy link
Author

My goal is to achieve this effect:

  1. When type a, show completion items a ab abc.
  2. And if select abc, whatever using enter or tab, or even paste abc directly, will auto show the next completion items d de def.

Here is a gif:

102

@alexdima
Copy link
Member

What you show consists of two pieces:

  1. constantly triggering the suggest widget. The code I've shown above could be used, or you could also implement triggering of the suggestions again by using the command field in CompletionItem.
  2. having a completion item provider. This is something you need to implement, as the editor is language agnostic, it could not possibly know what completion items to show where. It is up to you to parse the SQL query and understand where the position sits within the SQL query and propose only what makes sense. For example, if you are before FROM you should suggest column names. If you are after FROM you should not suggest column names.

@ascoders
Copy link
Author

Thanks for your reply! Can you give me a simple example how triggering suggestions by command in CompletionItem? Thanks a lot!

I don't know how to trigger provideCompletionItems again by using command field in CompletionItem..

@alexdima
Copy link
Member

For each completion item, you would use the command field and use editor.action.triggerSuggest as the value for the command. Then, when the completion item is accepted, that command will also be invoked.

At least I think that's how that should work. We can ask @jrieken for further help if it does not.

@ascoders
Copy link
Author

Thank @alexandrudima, you helped me find the key to solving the problem! But i still need help because nothing happens when choose any completion item when using this code:

resolveCompletionItem(item: monaco.languages.CompletionItem) {
  item.command = {
    id: 'editor.action.triggerSuggest',
    title: '123'
  };
  return item;
}

What should i trigger new suggestion when choose completion items, and set custom suggest items? @jrieken I would like to get your help very much!

@jrieken
Copy link
Member

jrieken commented Jun 21, 2018

The command id is correct and I know that css uses this successfully. Try setting the command during provideCompletionItems and not in resolve...

@ascoders
Copy link
Author

Hello @jrieken , i add command to provideCompletionItems, but nothing happened. Here is my code:

provideCompletionItems: () => {
  return [{label: 'test', command: {id: 'editor.action.triggerSuggest', title: 123}}]
}

@jrieken
Copy link
Member

jrieken commented Jun 22, 2018

, but nothing happened

Something always happens ;-) Is it that your provider isn't called or that your completions aren't showing? Is there an error? You can debug this on suggestController.js#_onDidSelectItem

@ascoders
Copy link
Author

Thank you for your quick reply, but I'm still in doubt.

When trigger command in provideCompletionItems, where should i handle it? Or it will recall provideCompletionItems itself?

@jrieken
Copy link
Member

jrieken commented Jun 22, 2018

Yes. You don't trigger the command you tell the editor to trigger the command once a completion is selected and then that will eventually call provideCompletionItems

@ascoders
Copy link
Author

Thanks, i'll try it.

@ascoders
Copy link
Author

Thanks @jrieken ! It now works fine! Another question, how can i change point position after a completion is selected?

I want both trigger provideCompletionItems(Using command) and change cursor position after select a completion.

@jrieken
Copy link
Member

jrieken commented Jun 25, 2018

Use a SnippetString as insert text

@ascoders
Copy link
Author

Thanks @alexandrudima and @jrieken! All works fine for me now!

@suhasdeshpande
Copy link

@ascoders I'm going through a similar use-case. Would it be possible for you to share some gist on how you used Editor APIs?

@ascoders
Copy link
Author

@suhasdeshpande What's your problem?

@DoubleYellowEgg
Copy link

@alexandrudima @jrieken
How does excuse me multilevel clew use
monaco.languages.registerCompletionItemProvider('javascript', { provideCompletionItems: function () { return { suggestions: [ { label: 'May', kind: monaco.languages.CompletionItemKind['Function'], insertText: 'Mayname', detail: '名字', command: {id: 'editor.action.triggerSuggest', title: 123} { label: 'Cay', kind: monaco.languages.CompletionItemKind['Function'], insertText: 'CayNmae', detail: 'cay', } ] } }, triggerCharacters: ['M', 'C'] })

@gunn4r
Copy link

gunn4r commented Aug 5, 2019

+1 would love to see a final code example solution for this. I've got a very similar usecase but even though my completionItemProvider is returning suggestions, they are in some cases either not triggering the suggestion widget at all, or if manually triggered, widget displays "No suggestions" even though I can see the suggestions logged to console.

Seems it will only actually display the suggestions if there is a space preceding the suggestion trigger.

@ascoders @alexandrudima @jrieken

@wzhudev
Copy link

wzhudev commented Aug 22, 2019

How to resolve this problem since SnippetString is removed?

@gunn4r
Copy link

gunn4r commented Aug 22, 2019

I was able to generally get this working fine by adding a space to the end of each suggestion item.

@vscodebot vscodebot bot locked and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants