Last active
March 17, 2016 19:19
-
-
Save gooocho/644e15d8f868b499db54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// referring: | |
// https://github.com/itbaby/brackets-expand-selection-to-quotes/blob/master/main.js | |
define(function(require,exports,module){ | |
var AppInit = brackets.getModule('utils/AppInit'), | |
KeyBindingManager = brackets.getModule('command/KeyBindingManager'), | |
CommandManager = brackets.getModule('command/CommandManager'), | |
EditorManager = brackets.getModule('editor/EditorManager'); | |
var EXPAND_SELECTION_TO_QUOTES = 'Expand selection to quotes', | |
CMD_SELECT_TO_QUOTES = 'quotes.select'; | |
function selectBetweenQutoes(){ | |
function last(array) { | |
return array[array.length - 1]; | |
} | |
var editor = EditorManager.getFocusedEditor(); | |
var pos = editor.getCursorPos(), | |
document = editor.document, | |
line = document.getLine(pos.line); | |
var leftPart = line.substring(0, pos.ch); | |
var rightPart = line.substr(pos.ch); | |
var quoteReg = /['"]/g; | |
var stack = []; | |
var exec; | |
// 前から順にクォートを覚える | |
// 同じクォートが2つ連続したら、1つ忘れる | |
while (exec = quoteReg.exec(leftPart)) { | |
if ( | |
stack.length && | |
last(stack)[0] === exec[0] | |
) { | |
stack.pop(); | |
} else { | |
stack.push(exec); | |
} | |
} | |
if (stack.length) { | |
// クォートの中にいる | |
// aaaa"bbbb'c|ccc'dddd"eeee | |
var quoteChar = last(stack)[0]; | |
var leftQuoteIndex = last(stack).index; | |
var rightQuoteIndex; | |
if (rightPart.indexOf(quoteChar) !== -1) { | |
// +1 しないとヘンかも? | |
rightQuoteIndex = pos.ch + rightPart.indexOf(quoteChar); | |
} else { | |
// 開きクォートがあるけど、対応する閉じクォートがない場合 | |
// とりあえず行末まで選択するようにしています、気に入らなければ直してください | |
rightQuoteIndex = line.length - 1; | |
} | |
editor.setSelection( | |
{ | |
line: pos.line, | |
ch: leftQuoteIndex | |
}, | |
{ | |
line: pos.line, | |
ch: rightQuoteIndex | |
} | |
); | |
} else { | |
// クォートの中にいない | |
// aaaa"bbbb"c|ccc"dddd"eeee | |
// do nothing | |
} | |
} | |
CommandManager.register( | |
EXPAND_SELECTION_TO_QUOTES, | |
CMD_SELECT_TO_QUOTES, | |
selectBetweenQutoes | |
); | |
KeyBindingManager.addBinding(CMD_SELECT_TO_QUOTES,"Ctrl-'"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment