Function/Expression Structure

Komodo only seems to support this structure for function/expressions:

if (checkCondition()) {
  handleCondition();
} else {
  doSomething();
}

I have need for this structure.

if (checkCondition())
{
  handleCondition();
}
else
{
  doSomething();
}

I can’t find a way to change it. Is it possible?

Thank you.

I’m guessing you mean templates in JavaScript. You can
find the template in the toolbox at Samples/Abbreviations/JavaScript-common/*/keywords/if, edit it, and put in newlines to form the template to your preference.

  • Eric

No, not really. I’m talking more about the core language formatting. Think php or javascript or any c-style language. If I enter while (0) into the editor, then a left curly brace, it adds the right curly brace right after. End result is while (0) {} with the cursor between the curly braces.

Other IDEs … when enterint a language construct such as this, you’d type while ( and it would add the closing paren and then the curly braces based on a preferred style - the left on the same row or next and the right on the next or a few down with room for code. That’s a time saver that I miss from others and maybe I’ve missed something and komodo can do it…?

I write my code the same way as you do (curly braces on new line) and have not had any issues with this. To take your example, I would just wrote the following

while (0)
{

then this would auto complete to:

while (0)
{|}

Where “|” represents the cursor. Then when you press enter you get:

while (0)
{
    |
}

Which pretty much covers your use-case. I’d be interested to hear exactly how you wish to perform these actions though.

You’re not wrong, but … As I explained, other IDEs would do the complete structure and that’s super awesome.

Meaning type
while (

and it inserts

)
{

}

And puts the active cursor in between the parens (|).

That means no enter keystrokes, no curly braces, just gets it done and a conditional entry, a couple arrow keys later you’re writing code.

So… Doesn’t do it, that kinda sucks, I hope the activestate folks watch this and consider it.

Well I “am” one of the activestate folks, so consider this watched :wink:

I think it’s an interesting feature to have, but I like how our current implementation leaves this flexibility to the user.

Is it just curly braces that you want to do this for or are there other similar auto-completions that you’d like to see? If it’s just the curly braces then this could be easily implemented with a macro.

I could see a simple setting that provides 3 choices. 1 is to do it as is currently done. 2 would be formatting with braces on same line. 3 would be braces on separate lines. That would rock.

I don’t know that it really applies to anything else - not for my needs in php and javascript, anyway. Perhaps other languages have their construct where this would be useful, but I’m no expert on the others.

I’m not familiar with macros in komodo. My concern with macros would be… Do you have to do a different one for each language construct… if ( while ( for ( foreach ( do { switch ( etc. Know what I mean?

I love komodo, but it seems in supporting all languages, it’s lost some of the niceties that could be applied to each one, as is the case here. This might only apply to php and javascript or perl.

Keep in mind that what you may consider to be a nicety others may consider to be an annoyance. What you’re asking for would to me personally be a very annoying feature. That’s not to say it shouldn’t be offered as a feature, I’m just making an argument for why it’s not currently there.

I’ll cook up a simple macro as a prototype. If it works out well we can integrate it as a feature in the editor.

Thanks for continued engagement on this. 2 points…

  1. A setting, as described, satisfies both those who might find it annoying and people like me. :slight_smile:

  2. This setting is consistent with other editors, from the old allaire homesite to zend studio. It’s not my idea, just re-expression of an old one.

Love to see it!

Here’s a first draft of the macro, I have not done any testing beyond the base use-case

/**
 * @fileoverview  Auto completes curly braces to start on a new line - experimental
 * @author        Nathan Rijksen
 * @version       0.1
 */

/**
 * Komodo Extensions Namespace.
 * This namespace was suggested by JeffG. More information is available at:
 *    {@link http://community.activestate.com/forum-topic/extension-s-namespace}
 *
 * @type  Object
 */
if (typeof(extensions) === 'undefined')
    extensions = {};
if (extensions.AutoBraces && extensions.AutoBraces.onKeyPress) {
    // Remove the existing trigger handler, we'll re-instate it.
    var editor_pane = ko.views.manager.topView;
    editor_pane.removeEventListener('keypress', extensions.AutoBraces.onKeyPress, true);
}
extensions.AutoBraces = {};

(function() {

    this.onKeyPress = function(e) {
        if (e.charCode != 123 /* { */ || !e.shiftKey) return;

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

        // Validate our context
        var currentPos = editor.currentPos;
        var strRight = editor.getTextRange(currentPos, currentPos + 1);
        if ([0,10].indexOf(strRight.charCodeAt(0))===-1) return;

        // Prepare our auto-completion
        var fakeSnippet = {
            hasAttribute: function(name) {
                return name in this;
            },
            getStringAttribute: function(name) {
                return this[name];
            },
            name: "autobrace snippet",
            indent_relative: "true",
            value: "\n{\n    [[%tabstop:]]\n}"
        };

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

        // Prevent default auto-completion
        e.preventDefault();
    }
    
    // Bind keypress listener
    var editor_pane = ko.views.manager.topView;
    editor_pane.addEventListener('keypress', this.onKeyPress, true);

}).apply(extensions.AutoBraces);

fork: https://gist.github.com/Naatan/e82011550823421e7013

This will auto complete the second you write the left curly brace, provided there is nothing else to the right of the current caret position on the current line.

It’s not a very big snippet, most of what you see is boilerplate stuff.

Pretty sweet. Works like a charm. Good enough solution for me, I think. If it does it somewhere it shouldn’t, can always ctrl-z and back it out. Well done.

Please let me know if you run into bugs, I’d like to see if this is something we can ship with Komodo (albeit not enabled by default).

I will do that. Seems good for now, though I’ve only tested with fake code and at various levels of indentation.

FYI - I use the Allman style when doing C-style languages. The standard in komodo is old K&R, which is fine, but not for me. I suspect there are a lot who could go either way on this. http://en.wikipedia.org/wiki/Indent_style#BSD.2FAllman_style

Thanks a lot! This is a great little bit of code.

I use the same style myself (eg. https://github.com/Komodo/KomodoEdit/blob/trunk/src/modules/openfiles/content/openfiles.js). Komodo doesn’t force you into a coding style, but we have to set default prefs. If those do not work for your coding style you will have to tweak them to your liking.

No, it doesn’t really force a style, but this macro makes a big difference - it helps komodo in making that style easier.

Thanks again and I’ll let you know if I find issues or tweak it!

I have the same issue. I can use auto-abbreviations to get the behavior I want, but I’d rather have a setting. Even more, though, I imagine these things are actually snippets somewhere? Maybe people could just edit them if we knew where to look…