Pythonscript set-selection question
-
It is my experience that whenever you have selected text, one end or the other of it will have a flashing cursor. Thus, I was surprised to see that after executing some Pythonscript code that modifies a rectangular selection (user makes a column block, then invokes script), that what results (intended to be multiple single line blocks of various lengths, all starting at the original left column) has at least one odd selection which has no cursor touching it. See video, at about 8 seconds the script is run:
The odd line is line 78 (and maybe line 84). Is this a valid situation or some sort of bug?
-
Hi Alan,
the gif indicates that this happens after a replace operation, is this the case?
If so, did you recalculate the selection afterwards? Do you mess with virtualspaceoptions?Cheers
Claudia -
Hello Claudia!
Yes it is a replace operation and yes virtual space can be in play here. My goal is to analyze what changed from the replace and then set new multiple selections based upon that. Note that before the script is run we have a true rectangular block (which may contain virtual spaces). Afterward it won’t be that any more but will be “N” selections of various “widths”, on “N” consecutive lines (N=height of pre-script-run column selection). Maybe I’m just doing it wrong. Do I have to set the selection to “nothing” before beginning to set a different selection(s)?
I’m not sure what you mean by “recalculate the selection afterwards”. For each line I grab original content into a variable, then do the replace, then compare the line’s new content to the original to determine how I want to set the selection I will leave active for that line.
Any hints would be great as I find “selection-set” stuff a bit hard to understand in PS!
A related question is how to actually set a true rectangular selection in code. Whenever I’ve tried to do this, actions performed after it seem like they are acting on “N” different selections of stream type, not one selection of rectangular type. I can’t be more specific right now but I know this has come up for me in the past.
-
Alan, what I was thinking about it is something like this.
Assuming a text like''' abc-abc-abc b bb bbb bbbb bbb abc-abbbc-abc abc-abc-abc '''
with a rectangular selection of (8, 49)
and you want to replace ‘bb’ within the selection with a code like this
user_selection = editor.getUserCharSelection() for i in xrange(editor.getSelections()): editor.setTargetStart(editor.getSelectionNStart(i)) editor.setTargetEnd(editor.getSelectionNEnd(i)) if editor.searchInTarget('bb') > -1: editor.replaceTarget(editor.getTextRange(editor.getTargetStart(), editor.getTargetEnd()).replace('bb',''))
will result in
because parts of the selections have been replaced. Now it depends what you want to achieve
because if you want to have the initial selection parameters and add code likeeditor.setRectangularSelectionAnchor(user_selection[0]) editor.setRectangularSelectionCaret(user_selection[1])
after replacement code, selection will expand.
So you might consider reducing the resulting selection by the amount of text to be replaced.
Makes this sense to you? Or did I misunderstood your initial question?
Cheers
Claudia -
Well my original question was intended to be “How can I have some selected text without a blinking caret at one end of it or the other?” Like on ine 78 in my gif.
However, getting into my real goal…I’d like to do the replacement and have selections on each line remain that reflect what happened during the replacement.
The partial line selections could either:
- get shorter (so the section for that line would reduce), possibly even down to zero (in which case only a blinking caret would remain in the column of the original rect block’s left side)
- get longer (so the section for that line would extend)
- stay the same length (in which case the selection highlight for a given line wouldn’t change)
So maybe this text:
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzgets column-selected by the user like this:
then a script runs a replacement and the desired selection after running looks something like this (note, just a selection demo as the text doesn’t change):
In this demo each line is consistent…a selection (starting in the column of the original rectangular block’s left side column) with blinking cursor at the right-end of the selection.
I’m finding this difficult as I can get into situations like on line 78 and I don’t know what to think about that…I don’t know, I’ll keep experimenting with it.
-
I’d like to do the replacement and have selections on each line remain that reflect what happened during the replacement.
Not sure if this can be solved using selection functions but is it needed to have a selection
or could you use indicators (like smart highlighting) instead?Cheers
Claudia -
No worries, Claudia…I can let it go; it’s not that important…I withdraw the question(s).
:)