I want a snippet that puts the selected text if any in Python triple quotes. This was my attempt:
<% var scimoz = ko.views.manager.currentView.scimoz; scimoz.cut(); %>
<% scimoz.paste() %>
This does not work as expected. Starting with selected text “Hello World” I get:
So what is to be done?
scimoz.paste() operates directly on the scimoz buffer the moment it is called. It is not aware of the snippet template that it is called from, so the output makes no sense.
Here is an EJS snippet that works:
EDIT: Another way of looking at snippets: the body of the snippet is evaluated apart from the insertion. The
paste() call would have worked if the evaluation and insertion were handled line-by-line. But in fact the
paste() call happened during evaluation and before insertion – it just cut and pasted the text in the same place (evaluation phase), then inserted the triple quotes (insertion phase).
@ervumlens is correct about when the code is running and that’s why it’s showing up in the wrong place.
scimoz.selText will grab all selected text on the page (multi cursor stuff). Just something to be aware of. You can use
scimoz.multipleSelection to see if there is multiselection.
Also, please use the new API,
var scimoz = require("ko/editor").scimoz();
Thanks @ervumlens and @careyh that works fine. Final verrsion is
<% var scimoz=require("ko/editor").scimoz();%>
<%= scimoz.selText %>
BTW the function scimoz.getTextRange is in the docs but when I try to use it is declared not to exist.
Try not to use scimoz() or scintilla() in ko/editor when a viable function exists for what you are trying to do, eg.
<% var editor = require("ko/editor") %>
<%= editor.getSelection() %>
The scimoz() and scintilla() functions are helper methods, and while they are unlikely to change they should not be relied on.
By the way you do not need to use any EJS for this snippet at all, Snippets have the “Selection” mechanic built-in. Just press the little arrow next to the input field.
Example snippet for your use-case:
Search the Komodo help
getSelection and it finds nothing and the Macro Api help is completely out of date so I had no way of knowing about
[[%s]] does not quite do because if there is no selection it throws an error. I wanted a macro that used the selection if it was there but otherwise not. How do I find out what the editor methods are? The docs really need bringing up to date!
Defman thank you - the frustration with the old docs had become extreme.
I agree, the old docs were very much due for an overhaul. Fwiw if you sign up for the beta docs site you will get wiki-like editing rights.