1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 module utils.IndentedStringBuilder; 21 22 import std.algorithm; 23 import std.string; 24 25 /* Keeps track of indentation level while building up a string */ 26 27 public class IndentedStringBuilder 28 { 29 string tabs; 30 31 this(string t) 32 { 33 tabs = t; 34 } 35 36 this() 37 { 38 this(""); 39 } 40 41 /** 42 * Formats the input lines while keeping track of indentation 43 * Params: 44 * lines = The lines to format 45 */ 46 public string format(string[] lines) 47 { 48 string text = ""; 49 foreach(string line ; lines ) 50 { 51 string ln = std..string.strip(line); 52 if ( endsWith(ln, '}') 53 || endsWith(ln, "};") 54 || startsWith(ln, "}") 55 || startsWith(ln, "* }") 56 || startsWith(ln, "// }") 57 ) 58 { 59 if ( !canFind(ln, '{') && tabs.length > 0 ) tabs.length = tabs.length -1; 60 } 61 if ( startsWith(ln, '*') ) 62 { 63 text ~= tabs ~" "~ ln ~ "\n"; 64 } 65 else 66 { 67 text ~= tabs ~ ln ~ "\n"; 68 } 69 if ( endsWith(ln,'{') && !startsWith(line, "}") ) 70 { 71 tabs ~= '\t'; 72 } 73 } 74 75 return text; 76 } 77 78 public void setIndent(string t) 79 { 80 tabs = t; 81 } 82 }