Hi
I often have to swap 2 words that are not adjacent to eachother
So I wrote a little python script and figured I'd share it
(There are 2 other threads that have similar but different functionalities. see one by Dev Player and another by kadner)
This script works only with 2 selections
If the selection has a length, then the script uses those chars, otherwise, it will use the word under the cursor
After the script swaps the words, it sets a full selection on each of the 2 words swapped (just to show the actual swap)
There is an undo actions started at the beginning of the script, and ended at the end. This way, one control+z will undo the action (instead of 2)
Yes, you can shorten the code as you suggested, good work. However, I happen to prefer a little longer code that offers a little more clarity. [Being able to see return(word, position, position) makes it very clear what the function is doing.] And especially when writing for other people.
By the way, I don't mind helping as much as I can.... but I am in no way a mentor!
I simply don't know enough nor do I have enough experience (in outside languages)...
Get someone who knows more and better (like Dave...if you can!) :)
All the best
Davey
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi
I often have to swap 2 words that are not adjacent to eachother
So I wrote a little python script and figured I'd share it
(There are 2 other threads that have similar but different functionalities. see one by Dev Player and another by kadner)
This script works only with 2 selections
If the selection has a length, then the script uses those chars, otherwise, it will use the word under the cursor
After the script swaps the words, it sets a full selection on each of the 2 words swapped (just to show the actual swap)
There is an undo actions started at the beginning of the script, and ended at the end. This way, one control+z will undo the action (instead of 2)
Hope others can find this helpful as well
Davey
Last edit: Davey 2015-02-24
Hello Davey,
It took me a while to grasp your moves. :)
- When I did, I was impressed; it's beautiful.
Thank you.
Understanding "getWord_Sel(selNum)", I've realized that we both could shorten the code slightly.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def getWord_Sel(selNum): # tuple of: word, word_start, word_end
sPos = editor.getSelectionNStart(selNum)
ePos = editor.getSelectionNEnd(selNum)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
And in Select and Find First:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def getExpression():
mainSel = editor.getMainSelection()
sPos = editor.getSelectionNStart(mainSel)
ePos = editor.getSelectionNEnd(mainSel)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thanks again, mentor.
Hi Yaron
Thanks
Yes, you can shorten the code as you suggested, good work. However, I happen to prefer a little longer code that offers a little more clarity. [Being able to see return(word, position, position) makes it very clear what the function is doing.] And especially when writing for other people.
By the way, I don't mind helping as much as I can.... but I am in no way a mentor!
I simply don't know enough nor do I have enough experience (in outside languages)...
Get someone who knows more and better (like Dave...if you can!) :)
All the best
Davey
Hello Davey,
Your code is indeed much clearer. Great work.
Regards.