Cut and paste in snippet

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:

Hello world
"""
"""

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:

"""
<%= ko.views.manager.currentView.scimoz.selText%>
"""

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).

1 Like

Hi @peter2108,

@ervumlens is correct about when the code is running and that’s why it’s showing up in the wrong place.

Two things, 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();

  • Carey

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.

1 Like

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:

"""
[[%s]]
"""
1 Like

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 editor.getSelection. Also [[%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!

1 Like

Source code of SDK components: https://github.com/Komodo/KomodoEdit/tree/master/src/chrome/komodo/content/sdk
New docs (in beta, Komodo devs are working on it and they are looking for community contributions ;)): http://beta.docs.komodoide.com/SDK/api/commonjs/editor#editor_functions_getselection

Defman thank you :heart_eyes: - 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.