2023-01-16 22:26:34 +00:00
|
|
|
ace.define("ace/ext/whitespace",["require","exports","module","ace/lib/lang"], function(require, exports, module){"use strict";
|
2013-12-30 15:00:16 +00:00
|
|
|
var lang = require("../lib/lang");
|
2023-01-16 22:26:34 +00:00
|
|
|
exports.$detectIndentation = function (lines, fallback) {
|
2013-12-30 15:00:16 +00:00
|
|
|
var stats = [];
|
|
|
|
var changes = [];
|
|
|
|
var tabIndents = 0;
|
|
|
|
var prevSpaces = 0;
|
|
|
|
var max = Math.min(lines.length, 1000);
|
|
|
|
for (var i = 0; i < max; i++) {
|
|
|
|
var line = lines[i];
|
|
|
|
if (!/^\s*[^*+\-\s]/.test(line))
|
|
|
|
continue;
|
2015-11-19 16:37:40 +00:00
|
|
|
if (line[0] == "\t") {
|
2013-12-30 15:00:16 +00:00
|
|
|
tabIndents++;
|
2015-11-19 16:37:40 +00:00
|
|
|
prevSpaces = -Number.MAX_VALUE;
|
2023-01-16 22:26:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-11-19 16:37:40 +00:00
|
|
|
var spaces = line.match(/^ */)[0].length;
|
|
|
|
if (spaces && line[spaces] != "\t") {
|
|
|
|
var diff = spaces - prevSpaces;
|
2023-01-16 22:26:34 +00:00
|
|
|
if (diff > 0 && !(prevSpaces % diff) && !(spaces % diff))
|
2015-11-19 16:37:40 +00:00
|
|
|
changes[diff] = (changes[diff] || 0) + 1;
|
|
|
|
stats[spaces] = (stats[spaces] || 0) + 1;
|
|
|
|
}
|
|
|
|
prevSpaces = spaces;
|
2013-12-30 15:00:16 +00:00
|
|
|
}
|
2014-03-16 18:05:39 +00:00
|
|
|
while (i < max && line[line.length - 1] == "\\")
|
2013-12-30 15:00:16 +00:00
|
|
|
line = lines[i++];
|
|
|
|
}
|
|
|
|
function getScore(indent) {
|
|
|
|
var score = 0;
|
|
|
|
for (var i = indent; i < stats.length; i += indent)
|
|
|
|
score += stats[i] || 0;
|
|
|
|
return score;
|
|
|
|
}
|
2023-01-16 22:26:34 +00:00
|
|
|
var changesTotal = changes.reduce(function (a, b) { return a + b; }, 0);
|
|
|
|
var first = { score: 0, length: 0 };
|
2013-12-30 15:00:16 +00:00
|
|
|
var spaceIndents = 0;
|
|
|
|
for (var i = 1; i < 12; i++) {
|
2014-07-08 20:15:22 +00:00
|
|
|
var score = getScore(i);
|
2013-12-30 15:00:16 +00:00
|
|
|
if (i == 1) {
|
2014-07-08 20:15:22 +00:00
|
|
|
spaceIndents = score;
|
|
|
|
score = stats[1] ? 0.9 : 0.8;
|
|
|
|
if (!stats.length)
|
2015-11-19 16:37:40 +00:00
|
|
|
score = 0;
|
2023-01-16 22:26:34 +00:00
|
|
|
}
|
|
|
|
else
|
2014-07-08 20:15:22 +00:00
|
|
|
score /= spaceIndents;
|
|
|
|
if (changes[i])
|
2013-12-30 15:00:16 +00:00
|
|
|
score += changes[i] / changesTotal;
|
|
|
|
if (score > first.score)
|
2023-01-16 22:26:34 +00:00
|
|
|
first = { score: score, length: i };
|
2013-12-30 15:00:16 +00:00
|
|
|
}
|
|
|
|
if (first.score && first.score > 1.4)
|
|
|
|
var tabLength = first.length;
|
2015-11-19 16:37:40 +00:00
|
|
|
if (tabIndents > spaceIndents + 1) {
|
|
|
|
if (tabLength == 1 || spaceIndents < tabIndents / 4 || first.score < 1.8)
|
|
|
|
tabLength = undefined;
|
2023-01-16 22:26:34 +00:00
|
|
|
return { ch: "\t", length: tabLength };
|
2015-11-19 16:37:40 +00:00
|
|
|
}
|
2014-07-08 20:15:22 +00:00
|
|
|
if (spaceIndents > tabIndents + 1)
|
2023-01-16 22:26:34 +00:00
|
|
|
return { ch: " ", length: tabLength };
|
2013-12-30 15:00:16 +00:00
|
|
|
};
|
2023-01-16 22:26:34 +00:00
|
|
|
exports.detectIndentation = function (session) {
|
2013-12-30 15:00:16 +00:00
|
|
|
var lines = session.getLines(0, 1000);
|
|
|
|
var indent = exports.$detectIndentation(lines) || {};
|
|
|
|
if (indent.ch)
|
|
|
|
session.setUseSoftTabs(indent.ch == " ");
|
|
|
|
if (indent.length)
|
|
|
|
session.setTabSize(indent.length);
|
|
|
|
return indent;
|
|
|
|
};
|
2023-01-16 22:26:34 +00:00
|
|
|
exports.trimTrailingSpace = function (session, options) {
|
2013-12-30 15:00:16 +00:00
|
|
|
var doc = session.getDocument();
|
|
|
|
var lines = doc.getAllLines();
|
2017-02-05 20:05:00 +00:00
|
|
|
var min = options && options.trimEmpty ? -1 : 0;
|
|
|
|
var cursors = [], ci = -1;
|
|
|
|
if (options && options.keepCursorPosition) {
|
|
|
|
if (session.selection.rangeCount) {
|
2023-01-16 22:26:34 +00:00
|
|
|
session.selection.rangeList.ranges.forEach(function (x, i, ranges) {
|
|
|
|
var next = ranges[i + 1];
|
|
|
|
if (next && next.cursor.row == x.cursor.row)
|
|
|
|
return;
|
|
|
|
cursors.push(x.cursor);
|
2017-02-05 20:05:00 +00:00
|
|
|
});
|
2023-01-16 22:26:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-02-05 20:05:00 +00:00
|
|
|
cursors.push(session.selection.getCursor());
|
|
|
|
}
|
|
|
|
ci = 0;
|
|
|
|
}
|
|
|
|
var cursorRow = cursors[ci] && cursors[ci].row;
|
2023-01-16 22:26:34 +00:00
|
|
|
for (var i = 0, l = lines.length; i < l; i++) {
|
2013-12-30 15:00:16 +00:00
|
|
|
var line = lines[i];
|
|
|
|
var index = line.search(/\s+$/);
|
2017-02-05 20:05:00 +00:00
|
|
|
if (i == cursorRow) {
|
|
|
|
if (index < cursors[ci].column && index > min)
|
2023-01-16 22:26:34 +00:00
|
|
|
index = cursors[ci].column;
|
2017-02-05 20:05:00 +00:00
|
|
|
ci++;
|
|
|
|
cursorRow = cursors[ci] ? cursors[ci].row : -1;
|
|
|
|
}
|
2013-12-30 15:00:16 +00:00
|
|
|
if (index > min)
|
|
|
|
doc.removeInLine(i, index, line.length);
|
|
|
|
}
|
|
|
|
};
|
2023-01-16 22:26:34 +00:00
|
|
|
exports.convertIndentation = function (session, ch, len) {
|
2013-12-30 15:00:16 +00:00
|
|
|
var oldCh = session.getTabString()[0];
|
|
|
|
var oldLen = session.getTabSize();
|
2023-01-16 22:26:34 +00:00
|
|
|
if (!len)
|
|
|
|
len = oldLen;
|
|
|
|
if (!ch)
|
|
|
|
ch = oldCh;
|
|
|
|
var tab = ch == "\t" ? ch : lang.stringRepeat(ch, len);
|
2013-12-30 15:00:16 +00:00
|
|
|
var doc = session.doc;
|
|
|
|
var lines = doc.getAllLines();
|
|
|
|
var cache = {};
|
|
|
|
var spaceCache = {};
|
2023-01-16 22:26:34 +00:00
|
|
|
for (var i = 0, l = lines.length; i < l; i++) {
|
2013-12-30 15:00:16 +00:00
|
|
|
var line = lines[i];
|
|
|
|
var match = line.match(/^\s*/)[0];
|
|
|
|
if (match) {
|
|
|
|
var w = session.$getStringScreenWidth(match)[0];
|
2023-01-16 22:26:34 +00:00
|
|
|
var tabCount = Math.floor(w / oldLen);
|
|
|
|
var reminder = w % oldLen;
|
2013-12-30 15:00:16 +00:00
|
|
|
var toInsert = cache[tabCount] || (cache[tabCount] = lang.stringRepeat(tab, tabCount));
|
|
|
|
toInsert += spaceCache[reminder] || (spaceCache[reminder] = lang.stringRepeat(" ", reminder));
|
|
|
|
if (toInsert != match) {
|
|
|
|
doc.removeInLine(i, 0, match.length);
|
2023-01-16 22:26:34 +00:00
|
|
|
doc.insertInLine({ row: i, column: 0 }, toInsert);
|
2013-12-30 15:00:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
session.setTabSize(len);
|
|
|
|
session.setUseSoftTabs(ch == " ");
|
|
|
|
};
|
2023-01-16 22:26:34 +00:00
|
|
|
exports.$parseStringArg = function (text) {
|
2013-12-30 15:00:16 +00:00
|
|
|
var indent = {};
|
|
|
|
if (/t/.test(text))
|
|
|
|
indent.ch = "\t";
|
|
|
|
else if (/s/.test(text))
|
|
|
|
indent.ch = " ";
|
|
|
|
var m = text.match(/\d+/);
|
|
|
|
if (m)
|
|
|
|
indent.length = parseInt(m[0], 10);
|
|
|
|
return indent;
|
|
|
|
};
|
2023-01-16 22:26:34 +00:00
|
|
|
exports.$parseArg = function (arg) {
|
2013-12-30 15:00:16 +00:00
|
|
|
if (!arg)
|
|
|
|
return {};
|
|
|
|
if (typeof arg == "string")
|
|
|
|
return exports.$parseStringArg(arg);
|
|
|
|
if (typeof arg.text == "string")
|
|
|
|
return exports.$parseStringArg(arg.text);
|
|
|
|
return arg;
|
|
|
|
};
|
|
|
|
exports.commands = [{
|
2023-01-16 22:26:34 +00:00
|
|
|
name: "detectIndentation",
|
|
|
|
description: "Detect indentation from content",
|
|
|
|
exec: function (editor) {
|
|
|
|
exports.detectIndentation(editor.session);
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
name: "trimTrailingSpace",
|
|
|
|
description: "Trim trailing whitespace",
|
|
|
|
exec: function (editor, args) {
|
|
|
|
exports.trimTrailingSpace(editor.session, args);
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
name: "convertIndentation",
|
|
|
|
description: "Convert indentation to ...",
|
|
|
|
exec: function (editor, arg) {
|
|
|
|
var indent = exports.$parseArg(arg);
|
|
|
|
exports.convertIndentation(editor.session, indent.ch, indent.length);
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
name: "setIndentation",
|
|
|
|
description: "Set indentation",
|
|
|
|
exec: function (editor, arg) {
|
|
|
|
var indent = exports.$parseArg(arg);
|
|
|
|
indent.length && editor.session.setTabSize(indent.length);
|
|
|
|
indent.ch && editor.session.setUseSoftTabs(indent.ch == " ");
|
|
|
|
}
|
|
|
|
}];
|
2013-12-30 15:00:16 +00:00
|
|
|
|
2020-04-14 12:14:48 +00:00
|
|
|
}); (function() {
|
|
|
|
ace.require(["ace/ext/whitespace"], function(m) {
|
|
|
|
if (typeof module == "object" && typeof exports == "object" && module) {
|
|
|
|
module.exports = m;
|
|
|
|
}
|
|
|
|
});
|
2014-07-08 20:15:22 +00:00
|
|
|
})();
|
|
|
|
|