Last active
August 29, 2015 14:02
-
-
Save colelawrence/b19321cbe81c1ba57ddf to your computer and use it in GitHub Desktop.
CodeMirror 4 use spaces instead of tabs
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
cm_config = {} | |
cm_config['extraKeys'] = { | |
"Tab": checkToUseSpacesInsteadofTabs | |
} | |
CodeMirror($(".code")[0], cm_config) |
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
checkToUseSpacesInsteadofTabs = (cm) -> | |
if(cm.getOption("indentWithTabs")) | |
return CodeMirror.Pass | |
indentUnit = cm.getOption("indentUnit") | |
getYoSpaceCount = (head) -> | |
# Use preceding to determine what room is taken by tabs | |
preceding = cm.getRange {line:head.line, ch:0}, head | |
tabCount = 0 | |
tabreg = /\t/g | |
while(tabreg.exec preceding) | |
tabCount++ | |
return indentUnit - (head.ch + tabCount*(indentUnit - 1)) % indentUnit | |
makeYoSpaces = (spaceCount) -> | |
spaces = "" | |
for [1..spaceCount] | |
spaces += " " | |
return spaces | |
putYoSpacesHere = (range) -> | |
{anchor, head} = range | |
if anchor.line isnt head.line or anchor.ch isnt head.ch | |
for line in [anchor.line..head.line] | |
curLine = cm.getLine(line) | |
curPos = {line, ch:0} | |
cm.replaceRange makeYoSpaces(indentUnit), curPos, curPos | |
#anchor.ch += indentUnit | |
#head.ch += indentUnit | |
else | |
cm.replaceRange makeYoSpaces(getYoSpaceCount(head)), anchor, head | |
cm.doc.sel.ranges.forEach putYoSpacesHere |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment