Package: IndentTracker

IndentTracker

nameinstructionbranchcomplexitylinemethod
IndentTracker()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
decrIndent()
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getIndentation()
M: 0 C: 20
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
incrIndent()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
indent(String)
M: 0 C: 38
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
resetIndent()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%~
3: * New Pretty Printer
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.core.npp;
23:
24: /**
25: * The Class IndentTracker keeps track of indentation levels when printing ASTs. The indentation level is initially zero
26: * (no indentation). It can be increased by one level at a time with {@link incrIndent} and reset with
27: * {@link resetIndent}. <br>
28: * Indentation is done exclusively with tabs. This allows for easy rewriting of indents in the final string.
29: */
30: public class IndentTracker
31: {
32:
33:         /** The indentation counter. */
34:         private int count;
35:
36:         // indent character.
37:         private String indent = "\t";
38:
39:         /**
40:          * Instantiates a new indent tracker.
41:          */
42:         public IndentTracker()
43:         {
44:                 count = 0;
45:         }
46:
47:         /**
48:          * Decrease the level of indentation by one. Does nothing if level is zero.
49:          */
50:         void decrIndent()
51:         {
52:•                if (count > 0)
53:                 {
54:                         count--;
55:                 }
56:         }
57:
58:         /**
59:          * Get the current indentation, computed as <code>indentLevel x indendtString</code>.
60:          *
61:          * @return the indentation
62:          */
63:         String getIndentation()
64:         {
65:                 StringBuilder sb = new StringBuilder();
66:
67:•                for (int i = 0; i < count; i++)
68:                 {
69:                         sb.append(indent);
70:                 }
71:
72:                 return sb.toString();
73:         }
74:
75:         /**
76:          * Increase the level of indentation by one.
77:          */
78:         void incrIndent()
79:         {
80:                 count++;
81:         }
82:
83:         /**
84:          * Indent a string. Inserts an indentation at the beginning of the string. For strings with line breaks, a new
85:          * indentation will also be inserted after each line break <b>(including the last)</b>.
86:          *
87:          * @param s
88:          * the string to indent
89:          * @return the indented string: " s"
90:          */
91:         String indent(String s)
92:         {
93:•                if (count > 0)
94:                 { // don't bother indenting if the level is zero
95:                         StringBuilder sb = new StringBuilder();
96:                         sb.append("\n");
97:                         sb.append(getIndentation());
98:                         StringBuilder r = new StringBuilder();
99:                         r.append(getIndentation());
100:                         r.append(s.replaceAll("\n", sb.toString()));
101:                         return r.toString();
102:                 } else
103:                 {
104:                         return s;
105:                 }
106:         }
107:
108:         /**
109:          * Reset the indentation level to 0.
110:          */
111:         void resetIndent()
112:         {
113:                 count = 0;
114:         }
115:
116: }