Change text from multiple selection


#1

Hi everyone,

I want to write a package which changes the text selected by a multiple selection. I hereby mean if you eg select all occurrences of a phrase, I then want to change these multiple selected phrases.

I’ve searched the API for some time now, especially in TextEditor and TextBuffer docs but I’m still not sure what’s the appropriate way to do this.

The easiest way I could think of would be to call getSelectedText() on the TextEditor but this seems to only return the latest addition of a multiple selection. I couldn’t find a command to return like an array of strings, like the selected texts line wise.

I’m thankful for any hint. Thanks a lot!

Update:
Writing a question like this seems to clear the mind. I think I’ve probably found a way to do this. Please correct me if it’s wrong.

I could call getSelectedScreenRanges() on the TextEditor and then getTextInRange() on the ScreenBuffer to get the actual text. After changing the strings I could the use TextBuffer’s setTextInRamge() to overwrite the old texts.

Is this a good way to do it? Thanks again.


#2

You can use the TextEditor.getSelections() method.

Also, the API documentation displays the commonly used methods by default, but you can expand the extended methods by clicking these:


#3

Thank you so much @leedohm I totally overlooked this. It’s exactly what I was looking for.


#4

I’m not sure, if I’m understanding you correctly, but doesn’t this already work?

Most of the time, when I want to replace multiple occurrences of a word I use this workflow:

  • select word
  • ctrl-f
  • f3
  • alt-enter
  • type new word

#5

He was asking about how to do it programmatically:


#6

Yes, I do understand that, but I wanted to make sure, that he’s aware of the fact that it’s already possible. So if he only wants to write the package, because he needs that functionality or he wants to contribute something new to the atom ecosystem he doesn’t waste his time.


#7

Thanks for the hint. I actually just wanted to make my package compatible to multiple selections.