* Replaces tabs with smart spaces. * * @param {String} code Code to fix the tabs in. * @param {Number} tabSize Number of spaces in a column. * @return {String} Returns code with all tabs replaces with roper amount of spaces.
(code, tabSize)
| 865 | * @return {String} Returns code with all tabs replaces with roper amount of spaces. |
| 866 | */ |
| 867 | function processSmartTabs(code, tabSize) |
| 868 | { |
| 869 | var lines = splitLines(code), |
| 870 | tab = '\t', |
| 871 | spaces = '' |
| 872 | ; |
| 873 | |
| 874 | // Create a string with 1000 spaces to copy spaces from... |
| 875 | // It's assumed that there would be no indentation longer than that. |
| 876 | for (var i = 0; i < 50; i++) |
| 877 | spaces += ' '; // 20 spaces * 50 |
| 878 | |
| 879 | // This function inserts specified amount of spaces in the string |
| 880 | // where a tab is while removing that given tab. |
| 881 | function insertSpaces(line, pos, count) |
| 882 | { |
| 883 | return line.substr(0, pos) |
| 884 | + spaces.substr(0, count) |
| 885 | + line.substr(pos + 1, line.length) // pos + 1 will get rid of the tab |
| 886 | ; |
| 887 | }; |
| 888 | |
| 889 | // Go through all the lines and do the 'smart tabs' magic. |
| 890 | code = eachLine(code, function(line) |
| 891 | { |
| 892 | if (line.indexOf(tab) == -1) |
| 893 | return line; |
| 894 | |
| 895 | var pos = 0; |
| 896 | |
| 897 | while ((pos = line.indexOf(tab)) != -1) |
| 898 | { |
| 899 | // This is pretty much all there is to the 'smart tabs' logic. |
| 900 | // Based on the position within the line and size of a tab, |
| 901 | // calculate the amount of spaces we need to insert. |
| 902 | var spaces = tabSize - pos % tabSize; |
| 903 | line = insertSpaces(line, pos, spaces); |
| 904 | } |
| 905 | |
| 906 | return line; |
| 907 | }); |
| 908 | |
| 909 | return code; |
| 910 | }; |
| 911 | |
| 912 | /** |
| 913 | * Performs various string fixes based on configuration. |
no test coverage detected