prism-code-editor icon indicating copy to clipboard operation
prism-code-editor copied to clipboard

Utility Request: insertTextWithTabStops

Open amxmln opened this issue 6 months ago • 5 comments

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!

amxmln avatar Aug 06 '25 22:08 amxmln

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.

jonpyt avatar Aug 07 '25 11:08 jonpyt

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.

amxmln avatar Aug 07 '25 11:08 amxmln

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.

jonpyt avatar Aug 07 '25 13:08 jonpyt

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? 🤔

amxmln avatar Aug 07 '25 20:08 amxmln

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.

jonpyt avatar Aug 07 '25 23:08 jonpyt