summaryrefslogtreecommitdiff
path: root/clformat/src/main/java/bjc/utils/ioutils/format/directives/IndentDirective.java
blob: 4ccf627275f2555ee374478cea06edde6a1919ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package bjc.utils.ioutils.format.directives;

import bjc.utils.ioutils.format.*;

/**
 * Implement the I directive.
 * 
 * @author student
 *
 */
public class IndentDirective implements Directive {
	@Override
	public Edict compile(CompileContext compCTX) {
		CLParameters params = compCTX.decr.parameters;
		CLModifiers mods = compCTX.decr.modifiers;

		if (mods.dollarMod) return new IndentConfigureEdict();

		CLValue indentCount = CLValue.nil();
		if (params.length() >= 1) {
			params.mapIndices("count");

			indentCount = params.resolveKey("count");
		}

		return new IndentEdict(indentCount, mods.colonMod);
	}
}

class IndentEdict implements Edict {
	private CLValue numIndentsVal;

	private boolean isRelative;

	public IndentEdict(CLValue numIndents, boolean isRelative) {
		this.numIndentsVal = numIndents;

		this.isRelative = isRelative;
	}

	@Override
	public void format(FormatContext formCTX) {
		int numIndents = numIndentsVal.asInt(formCTX.items, "indent count", "I", 1);

		boolean dedent = false;
		if (numIndents < 0) {
			numIndents = -numIndents;

			dedent = true;
		}

		if (isRelative) {
			if (dedent) formCTX.writer.dedent(numIndents);
			else        formCTX.writer.indent(numIndents);
		} else {
			if (dedent) {
				throw new IllegalArgumentException("Cannot have negative indent level");
			}

			formCTX.writer.setLevel(numIndents);
		}
	}
}

class IndentConfigureEdict implements Edict {
	@Override
	public void format(FormatContext formCTX) {
		// @TODO implement me - Ben Culkin, 1/5/20
	}
}