From 866f4391ff65e5033dad3cc62208bdc704a6e2e9 Mon Sep 17 00:00:00 2001 From: Pooria Attarzadeh Date: Fri, 7 Aug 2015 16:32:54 +0430 Subject: [PATCH] adding smart curly bracket features --- code/editor.js | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/code/editor.js b/code/editor.js index 7eeb3c3..0c85885 100644 --- a/code/editor.js +++ b/code/editor.js @@ -196,6 +196,10 @@ var _ = window.Editor = function(pre) { return false; } + break; + case 219: // bracket + evt.shiftKey && that.action('bracket'); + break; } }, @@ -203,10 +207,11 @@ var _ = window.Editor = function(pre) { keypress: function(evt) { var cmdOrCtrl = evt.metaKey || evt.ctrlKey, code = evt.charCode, + isBracket = evt.charCode === 123, ss = this.selectionStart, se = this.selectionEnd; - if(code && !cmdOrCtrl) { + if(code && !cmdOrCtrl && !isBracket) { var character = String.fromCharCode(code); that.undoManager.action({ @@ -537,24 +542,54 @@ _.actions = { }, newline: function(state) { - var ss = state.ss, + var DEFAULT_INDENT=' ', + ss = state.ss, lf = state.before.lastIndexOf('\n') + 1, - indent = (state.before.slice(lf).match(/^\s+/) || [''])[0]; + indent = (state.before.slice(lf).match(/^\s+/) || [''])[0], + plf,after='',pIndent = '',indentUnit = indent; + + // Bracket smart indenting + if(state.before.slice(ss-1,ss) === '{'){ + after = '\n' + indent; + if (indent !== ''){ + // Finding user indent per line (nested lines) + plf = state.before.slice(0,lf-1).lastIndexOf('\n')+1; + pIndent = (state.before.slice(plf).match(/^[^\S\n]+/) || [''])[0]; + indentUnit = indentUnit.replace(pIndent,'') || DEFAULT_INDENT; + indent+=indentUnit; + } + else + indent=DEFAULT_INDENT; + } state.before += '\n' + indent; + state.after = after + state.after; var selection = state.selection; - state.selection = ''; + state.selection = ''; state.ss += indent.length + 1; state.se = state.ss; return { - add: '\n' + indent, + add: '\n' + indent + after, del: selection, start: ss }; }, + + bracket: function(state) { + var brkClose = ''; + + if(state.after.match(/^\n/)) brkClose='}'; + state.after = state.selection + brkClose + state.after; + + return { + add: '{' + state.selection + brkClose, + del: state.selection, + start: state.ss + } + }, comment: function(state, options) { var open = options.lang === 'css'? '/*' : '