| 1 | ace.define("ace/ext/whitespace",["require","exports","module","ace/lib/lang"], function(require, exports, module){"use strict"; |
| 2 | var lang = require("../lib/lang"); |
| 3 | exports.$detectIndentation = function (lines, fallback) { |
| 4 | var stats = []; |
| 5 | var changes = []; |
| 6 | var tabIndents = 0; |
| 7 | var prevSpaces = 0; |
| 8 | var max = Math.min(lines.length, 1000); |
| 9 | for (var i = 0; i < max; i++) { |
| 10 | var line = lines[i]; |
| 11 | if (!/^\s*[^*+\-\s]/.test(line)) |
| 12 | continue; |
| 13 | if (line[0] == "\t") { |
| 14 | tabIndents++; |
| 15 | prevSpaces = -Number.MAX_VALUE; |
| 16 | } |
| 17 | else { |
| 18 | var spaces = line.match(/^ */)[0].length; |
| 19 | if (spaces && line[spaces] != "\t") { |
| 20 | var diff = spaces - prevSpaces; |
| 21 | if (diff > 0 && !(prevSpaces % diff) && !(spaces % diff)) |
| 22 | changes[diff] = (changes[diff] || 0) + 1; |
| 23 | stats[spaces] = (stats[spaces] || 0) + 1; |
| 24 | } |
| 25 | prevSpaces = spaces; |
| 26 | } |
| 27 | while (i < max && line[line.length - 1] == "\\") |
| 28 | line = lines[i++]; |
| 29 | } |
| 30 | function getScore(indent) { |
| 31 | var score = 0; |
| 32 | for (var i = indent; i < stats.length; i += indent) |
| 33 | score += stats[i] || 0; |
| 34 | return score; |
| 35 | } |
| 36 | var changesTotal = changes.reduce(function (a, b) { return a + b; }, 0); |
| 37 | var first = { score: 0, length: 0 }; |
| 38 | var spaceIndents = 0; |
| 39 | for (var i = 1; i < 12; i++) { |
| 40 | var score = getScore(i); |
| 41 | if (i == 1) { |
| 42 | spaceIndents = score; |
| 43 | score = stats[1] ? 0.9 : 0.8; |
| 44 | if (!stats.length) |
| 45 | score = 0; |
| 46 | } |
| 47 | else |
| 48 | score /= spaceIndents; |
| 49 | if (changes[i]) |
| 50 | score += changes[i] / changesTotal; |
| 51 | if (score > first.score) |
| 52 | first = { score: score, length: i }; |
| 53 | } |
| 54 | if (first.score && first.score > 1.4) |
| 55 | var tabLength = first.length; |
| 56 | if (tabIndents > spaceIndents + 1) { |
| 57 | if (tabLength == 1 || spaceIndents < tabIndents / 4 || first.score < 1.8) |
| 58 | tabLength = undefined; |
| 59 | return { ch: "\t", length: tabLength }; |
| 60 | } |
| 61 | if (spaceIndents > tabIndents + 1) |
| 62 | return { ch: " ", length: tabLength }; |
| 63 | }; |
| 64 | exports.detectIndentation = function (session) { |
| 65 | var lines = session.getLines(0, 1000); |
| 66 | var indent = exports.$detectIndentation(lines) || {}; |
| 67 | if (indent.ch) |
| 68 | session.setUseSoftTabs(indent.ch == " "); |
| 69 | if (indent.length) |
| 70 | session.setTabSize(indent.length); |
| 71 | return indent; |
| 72 | }; |
| 73 | exports.trimTrailingSpace = function (session, options) { |
| 74 | var doc = session.getDocument(); |
| 75 | var lines = doc.getAllLines(); |
| 76 | var min = options && options.trimEmpty ? -1 : 0; |
| 77 | var cursors = [], ci = -1; |
| 78 | if (options && options.keepCursorPosition) { |
| 79 | if (session.selection.rangeCount) { |
| 80 | session.selection.rangeList.ranges.forEach(function (x, i, ranges) { |
| 81 | var next = ranges[i + 1]; |
| 82 | if (next && next.cursor.row == x.cursor.row) |
| 83 | return; |
| 84 | cursors.push(x.cursor); |
| 85 | }); |
| 86 | } |
| 87 | else { |
| 88 | cursors.push(session.selection.getCursor()); |
| 89 | } |
| 90 | ci = 0; |
| 91 | } |
| 92 | var cursorRow = cursors[ci] && cursors[ci].row; |
| 93 | for (var i = 0, l = lines.length; i < l; i++) { |
| 94 | var line = lines[i]; |
| 95 | var index = line.search(/\s+$/); |
| 96 | if (i == cursorRow) { |
| 97 | if (index < cursors[ci].column && index > min) |
| 98 | index = cursors[ci].column; |
| 99 | ci++; |
| 100 | cursorRow = cursors[ci] ? cursors[ci].row : -1; |
| 101 | } |
| 102 | if (index > min) |
| 103 | doc.removeInLine(i, index, line.length); |
| 104 | } |
| 105 | }; |
| 106 | exports.convertIndentation = function (session, ch, len) { |
| 107 | var oldCh = session.getTabString()[0]; |
| 108 | var oldLen = session.getTabSize(); |
| 109 | if (!len) |
| 110 | len = oldLen; |
| 111 | if (!ch) |
| 112 | ch = oldCh; |
| 113 | var tab = ch == "\t" ? ch : lang.stringRepeat(ch, len); |
| 114 | var doc = session.doc; |
| 115 | var lines = doc.getAllLines(); |
| 116 | var cache = {}; |
| 117 | var spaceCache = {}; |
| 118 | for (var i = 0, l = lines.length; i < l; i++) { |
| 119 | var line = lines[i]; |
| 120 | var match = line.match(/^\s*/)[0]; |
| 121 | if (match) { |
| 122 | var w = session.$getStringScreenWidth(match)[0]; |
| 123 | var tabCount = Math.floor(w / oldLen); |
| 124 | var reminder = w % oldLen; |
| 125 | var toInsert = cache[tabCount] || (cache[tabCount] = lang.stringRepeat(tab, tabCount)); |
| 126 | toInsert += spaceCache[reminder] || (spaceCache[reminder] = lang.stringRepeat(" ", reminder)); |
| 127 | if (toInsert != match) { |
| 128 | doc.removeInLine(i, 0, match.length); |
| 129 | doc.insertInLine({ row: i, column: 0 }, toInsert); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | session.setTabSize(len); |
| 134 | session.setUseSoftTabs(ch == " "); |
| 135 | }; |
| 136 | exports.$parseStringArg = function (text) { |
| 137 | var indent = {}; |
| 138 | if (/t/.test(text)) |
| 139 | indent.ch = "\t"; |
| 140 | else if (/s/.test(text)) |
| 141 | indent.ch = " "; |
| 142 | var m = text.match(/\d+/); |
| 143 | if (m) |
| 144 | indent.length = parseInt(m[0], 10); |
| 145 | return indent; |
| 146 | }; |
| 147 | exports.$parseArg = function (arg) { |
| 148 | if (!arg) |
| 149 | return {}; |
| 150 | if (typeof arg == "string") |
| 151 | return exports.$parseStringArg(arg); |
| 152 | if (typeof arg.text == "string") |
| 153 | return exports.$parseStringArg(arg.text); |
| 154 | return arg; |
| 155 | }; |
| 156 | exports.commands = [{ |
| 157 | name: "detectIndentation", |
| 158 | description: "Detect indentation from content", |
| 159 | exec: function (editor) { |
| 160 | exports.detectIndentation(editor.session); |
| 161 | } |
| 162 | }, { |
| 163 | name: "trimTrailingSpace", |
| 164 | description: "Trim trailing whitespace", |
| 165 | exec: function (editor, args) { |
| 166 | exports.trimTrailingSpace(editor.session, args); |
| 167 | } |
| 168 | }, { |
| 169 | name: "convertIndentation", |
| 170 | description: "Convert indentation to ...", |
| 171 | exec: function (editor, arg) { |
| 172 | var indent = exports.$parseArg(arg); |
| 173 | exports.convertIndentation(editor.session, indent.ch, indent.length); |
| 174 | } |
| 175 | }, { |
| 176 | name: "setIndentation", |
| 177 | description: "Set indentation", |
| 178 | exec: function (editor, arg) { |
| 179 | var indent = exports.$parseArg(arg); |
| 180 | indent.length && editor.session.setTabSize(indent.length); |
| 181 | indent.ch && editor.session.setUseSoftTabs(indent.ch == " "); |
| 182 | } |
| 183 | }]; |
| 184 | |
| 185 | }); (function() { |
| 186 | ace.require(["ace/ext/whitespace"], function(m) { |
| 187 | if (typeof module == "object" && typeof exports == "object" && module) { |
| 188 | module.exports = m; |
| 189 | } |
| 190 | }); |
| 191 | })(); |
| 192 |