Skip to content

Instantly share code, notes, and snippets.

@Naatan
Created May 7, 2014 17:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Naatan/e82011550823421e7013 to your computer and use it in GitHub Desktop.
Save Naatan/e82011550823421e7013 to your computer and use it in GitHub Desktop.
Komodo IDE - Auto Braces
/**
* @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);
@mook-as
Copy link

mook-as commented May 9, 2014

if (e.charCode != '{'.charCodeAt(0) || !e.shiftKey) return; ? Though I'm not sure what the shift is doing there; some keyboard layouts might have that as an unshifted character.

@Naatan
Copy link
Author

Naatan commented May 9, 2014

Hrm good point, any higher level API we can tunnel that through?

@mook-as
Copy link

mook-as commented May 9, 2014

Dunno, they're adding all sorts of fancy things. Devmo might have something. I think they recommend that you use an unimplemented API, or something silly like that.... 😞

@Naatan
Copy link
Author

Naatan commented May 10, 2014

Damn for a second there I thought Devmo was some fancy new API.

@Defman21
Copy link

Defman21 commented Jul 2, 2014

value: "\n{\n [[%tabstop:]]\n}": as you can see - space instead of tab. I think value: "\n{\n\t[[%tabstop:]]\n}" is more logical.

@Naatan
Copy link
Author

Naatan commented Jul 9, 2014

It's not meant to put a tab, ko.projects.snippetInsert will take care of that as per your preferences.

@tomByrer
Copy link

Note to those who prefer 1TBS curlies, use the following:
value: " {\n [[%tabstop:]]\n}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment