-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCodeFormatter.sc
More file actions
69 lines (54 loc) · 1.41 KB
/
CodeFormatter.sc
File metadata and controls
69 lines (54 loc) · 1.41 KB
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
CodeFormatter {
classvar <>formatCmd = "/Users/Shared/_code/sclang-format/cmake-build-RelWithDebInfo/src/sclang-format";
var <tabSize=4, <insertSpaces=true;
var <inPipe, <outPipe, pid;
*new {
|tabSize=4, insertSpaces=true|
^super.newCopyArgs(tabSize, insertSpaces).init
}
isRunning {
^inPipe !? _.isOpen ?? false
}
init {
this.start();
ShutDown.add({
this.free()
});
}
start {
inPipe !? _.close;
outPipe !? _.close;
try {
#inPipe, outPipe = Pipe.argvReadWrite([
formatCmd,
"-i",
tabSize.asString,
insertSpaces.if("", "-t"),
"-w",
]);
1000.do {
if (inPipe.isOpen) { ^true };
0.01.wait;
};
} {
|e|
e.reportError;
};
^false;
}
format {
|code|
var read, result;
outPipe.putString(code);
outPipe.putChar(0.asAscii);
outPipe.flush();
while { read = inPipe.getChar(); read != 0.asAscii } {
result = result.add(read);
};
^result.join("")
}
free {
try { outPipe !? _.close };
try { inPipe !? _.close };
}
}