TextEditor - Indices to Point object


#1

I’m trying to get the Range of a script (source.js.embedded.html)tag inside a .html file. Firstly, is there an easier way of getting all the specified scopes? I’m now finding the indices of <script> and </script>, but I couldn’t find any information in the API on how to convert the index to a Point object. How would I do that?


#2

There are a few ways to potentially get the range depending on where you’re starting from. If you have the cursor inside the tag, you could use editor.bufferRangeForScopeAtCursor('.source.js.embedded.html'). If you don’t know where the cursor will be, you’d want to start with editor.scan() to find the tag.


#4

It’s kind of weird there. What you want is to make a selection and then use editor.insertText() or selection.insertText() (which sounds like it should append, but what it actually does is replace the selected text with something else). Here’s an example. If you use editor.addSelectionForBufferRange(), replace the text, and then remove the selection, it will all happen fast enough that there won’t even be a flicker.


#5

What’s working to convert an index to a Point is editor.buffer.positionForCharacterIndex. Thanks for the input on modifying the text. I was testing in the atom’s developer console, and was using the native Range implementation, rather than Atom’s. Now it’s all working fine :slight_smile: thank you!