Utility Request: insertTextWithTabStops
Hi!
I’m currently building an extension that allows wrapping a selection with a snippet, which is very useful for example when working with HTML and wanting to wrap a word in <strong /> tags.
The snippets are using the same format as the autocomplete ones, so they have an insert and an array of tabStops, but unfortunately to recreate the insertion from the autocomplete plugin, I would have to duplicate a lot of the logic.
Would it be possible to create a new insertTextWithTabStops function that would allow inserting text into an editor, but also handle the tabStops?
This could then be used in insertOption() in the autocomplete plugin and exposed via an export in the utils module.
As always, thank you for this awesome library!
Wrapping the current selection with <strong></strong> can easily be done with just insertText() shown in the following example:
const before = "<strong>"
const after = "</strong>"
const [start, end] = editor.getSelection()
insertText(
editor,
before + editor.value.slice(start, end) + after,
start,
end,
start + before.length,
end + before.length,
)
editor.cursor?.scrollIntoView()
I'm not sure I understand why you need "tab stops" to achieve this.
I’m sorry, I probably didn’t explain it right.
The <strong /> was just a very simple example. My extension is supposed to be able to wrap the selection in an arbitrary snippet which may contain tab-stops.
For example wrapping a JS statement in a trycatch-snippet from javascript/snippets.ts. I could insert it with the example above, but I think it would be nice UX and match user expectations if they could tab to the catch-block after the insertion.
The tab stops are very tighly coupled with the auto completion extension and separating them wouldn't be very feasible. What could be done is adding an insertCompletion() method to the extension which can insert an arbitrary completion that doesn't need to be in the list of current completions.
Oh that could work! Since snippets and the autocomplete extension are tightly coupled already, I don’t think I would mind my wrapping extension to be tightly coupled to the autocomplete extension as well!
So I’m guessing inserCompletion() would be exported from the autocomplete extension? 🤔
So I’m guessing
inserCompletion()would be exported from the autocomplete extension? 🤔
No, I would add methods to the extension returned by autoComplete(). Could probably add a startQuery() method too. The extension would also be stored on editor.extensions.autoComplete too, so you don't need to store a reference to the extension.