Skip to content

Instantly share code, notes, and snippets.

@PatrickKennedy
Created April 24, 2010 23:22
Show Gist options
  • Select an option

  • Save PatrickKennedy/378039 to your computer and use it in GitHub Desktop.

Select an option

Save PatrickKennedy/378039 to your computer and use it in GitHub Desktop.
var sci = ko.views.manager.currentView.scimoz;
// If there's no block our cursor moves so we need to remember where it starts.
var curLine = sci.lineFromPosition(sci.currentPos);
ko.commands.doCommand("cmd_blockSelect");
if (sci.selText == sci.text) {
// We have just a single line selected
sci.gotoLine(curLine);
sci.vCHome();
sci.lineEndExtend();
var line = sci.selText;
line = line.replace(new RegExp("{ ", "g"), "{\n\t")
.replace(new RegExp("; ", "g"), ";\n\t")
.replace(new RegExp("\t}", "g"), "}");
sci.replaceSel(line);
} else {
// We have a block selected
var line = sci.selText;
line = line.replace(new RegExp(" ", "g"), "")
// We need a negative look-ahead so we don't erase
// the first bracket's indentation
.replace(new RegExp("\t(?![{\t])", "g"), "")
.replace(new RegExp("\n", "g"), " ");
sci.replaceSel(line+"\n");
sci.lineUp();
}
@PatrickKennedy
Copy link
Author

NOTE: This is an in-place operation. It's pseudo-folding but it works.
Also, this acts primarily on tabs. If you don't like tabs then replace all the \t's with two spaces, or your preferred number of spaces.

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