Wrap text with quotes or brackets

Hi. Is it possible to make selected text to wrap when I call symbols like ", ', [, {, (, `? SublimeText style. Last symbol would be useful for SQL. I have to insert quotes and brackets manually so often, it became a rutine.

Also, usually when I press some quote, it doubles automaticaly. But when I do it inside of string, it’s not. Example - quote after = will not double when you call it:

$query = mysql_query("SELECT `user_id` FROM `wp_loans` WHERE `some_text`='test'");

Same stuff with iverted quotes - " will not double:

$string = 'Dwayne "The Rock" Johnson';
1 Like

Not natively possible (I’d be interested in adding it but afraid that it might confuse people). Easily doable as a macro though; I went ahead and took the liberty:

/**
 * @fileoverview    Automatically adds quotes around selection when selecting
 *                  text and pressing single/double quote
 * @author          Nathan Rijksen
 * @version         0.1
 */

if (typeof(extensions) === 'undefined')
    extensions = {};
if (extensions.AutoQuotes && extensions.AutoQuotes.onKeyPress) {
    // Remove the existing trigger handler, we'll re-instate it.
    var editor_pane = ko.views.manager.topView;
    editor_pane.removeEventListener('keypress', extensions.AutoQuotes.onKeyPress, true);
}
extensions.AutoQuotes = {};

(function() {

    var charCodes = [39, 34, 91, 93, 123, 125, 40, 41];

    this.onKeyPress = function(e) {
        if (charCodes.indexOf(e.charCode) == -1) return;

        var strLeft = strRight = String.fromCharCode(e.charCode);
        if (strLeft == '(') strRight = ')';
        else if (strRight == ')') strLeft = '(';
        else if (strLeft == '[') strRight = ']';
        else if (strRight == ']') strLeft = '[';
        else if (strLeft == '{') strRight = '}';
        else if (strRight == '}') strLeft = '{';

        // Get scimoz API
        var editor = ko.views.manager.currentView.scimoz;
        if ( ! editor || editor.selText == "") return;

        // Prepare our auto-completion
        var fakeSnippet = {
            hasAttribute: function(name) {
                return name in this;
            },
            getStringAttribute: function(name) {
                return this[name];
            },
            name: "autoquote snippet",
            value: strLeft + "[[%w]]" + strRight
        };

        // Insert auto-completion
        ko.projects.snippetInsert(fakeSnippet);

        // Prevent default auto-completion
        e.preventDefault();
        return false;
    }

    // Bind keypress listener
    var editor_pane = ko.views.manager.topView;
    editor_pane.addEventListener('keypress', this.onKeyPress, true);

}).apply(extensions.AutoQuotes);

Add to your toolbox, set to run on startup and run the macro. Now pressing ’ or " while having text selected will wrap it with those characters.

Edit: Added to resources section: http://komodoide.com/resources/macros/komodo--wrapselection/

1 Like

Thanks so much for this!

Urgh, why you have to go and reimplement it :stuck_out_tongue:
http://komodoide.com/blog/2014-06/brace-wrap-selection/

2 Likes

Lmao I had totally forgotten you did that. Please use @toddw’s instead guys, my version has bugs and is evidently redudant :wink:

That’s awesome, it’s even possible to make it work with `.

toddw, maybe you can help with this bug too? I even suggesed JS solution there, but I don’t know how to implement this in Komodo. Sorry for offtop.

Is this (@toddw’s macro) the basis for the wrapping in IDE 9.3? I feel like 9.3’s is lacking, and I’m trying to decide whether or not to complain :wink:

Nope, this is a new feature in 9.3 unrelated to the macro’s mentioned here.

You can control it from Preferences > Editor > Smart Editing > Wrap Selection with typed delimiter instead of overwriting it.

Got it. I had it set for plain text only. Thanks for the quick reply. Looks like I misread the date on the post I replied to. -Clark

This is a GREAT feature. I am forever needing to wrap code examples with backticks in Markdwon, or put quotes around strings I’ve typed out but forgotten to put quotes around.

But there’s a fly in the ointment. It only works for the simplest of cases. It doesn’t work if there are any punctuation characters in the text to be wrapped.

WORKS

Fabulous
Good ship Lollypop
counter[error] += 1 # for the “error” index

FAILS

Fabulous, slightly
slightly-fabulous


XML::Twig

This is by design, as it would annoy most people if it did. However we have you covered too:

Preferences > Editor > Smart Editing > Only wrap plain text selections

Just uncheck that option.

If you run into an issue like this in Komodo your first thought should always be “I’ll check if there is a pref for this”, cause odds are there is.

1 Like

This feature is the best!

I always used Komodo IDE and Sublime Text, I always edit my codes in sublime then move it to komodo, but lately Komodo IDE is turning into a really good text editor.

1 Like

If you have more feature requests, let us know @stramin! Thanks for the props!

  • Carey

1 Like