«Method help re-ordering utility» by jamshark70

on 22 Jul'16 14:34 in utilityscdochelpmethodsortorder

If you have a large class with a lot of methods, you probably want to sort the methods in the auto-generated SCDoc template into an order that's meaningful for the end user (related methods grouped together, more important methods toward the top).

But SCDoc syntax, with method::, argument:: and returns:: tags, makes it a bit harder to see where entries begin and end, and provides no overview of the list of methods.

This GUI reads the METHOD:: sections from the auto-generated template and presents a list box containing the method names.

  1. Get the list of METHOD:: sections, as a string, into the variable 'm'. If the string is in a disk file, use the upper code block to read it.

  2. Run the second block.

  3. Initially, the methods are sorted in ASCII order. The ^/v buttons allow you to change the order, or use "a" and "z" keys while focusing on the list box.

  4. Close the window or click the "P" button to reprint the full templates, in the new order. Then you can copy/paste these back into the schelp file.

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
(
Dialog.openPanel { |path|
	var f = File(path, "r");
	protect {
		if(f.isOpen) {
			m = f.readAllString;
		} {
			"Couldn't read %".format(path).warn;
		};
	} { f.close };
};
)

(
var split, sorted, w, list, upBtn, dnBtn, printBtn, printFunc, centerX = Window.screenBounds.center.x;

split = m.findRegexp("METHOD:: *([A-Za-z0-9_]+)");
split = split.separate { |a, b| b[1].contains("METHOD::") };
sorted = split.copy.sort { |a, b| a.last[1] < b.last[1] };

w = Window("Methods", Rect(centerX - 150, 0, 300, Window.screenBounds.height));
w.layout = HLayout(
	list = ListView(),
	VLayout(
		nil,
		upBtn = Button().fixedSize_(Size(30, 20)),
		dnBtn = Button().fixedSize_(Size(30, 20)),
		printBtn = Button().fixedSize_(Size(30, 20)),
		nil
	)
);

list.items_(sorted.collect { |pair| pair.last[1] })
.keyDownAction_({ |view, char|
	switch(char)
	{ $a } {
		upBtn.doAction;
		true  // do not do normal item matching
	}
	{ $z } {
		dnBtn.doAction;
		true
	}
});

upBtn.states_([["^"]])
.action_({ |view|
	var items = list.items, i = list.value;
	if(i > 0) {
		items = items.swap(i - 1, i);
		sorted = sorted.swap(i - 1, i);
		list.items = items;
		list.value = i - 1;
	};
});

dnBtn.states_([["v"]])
.action_({ |view|
	var items = list.items, i = list.value;
	if(i < (items.size - 1)) {
		items = items.swap(i, i + 1);
		sorted = sorted.swap(i, i + 1);
		list.items = items;
		list.value = i + 1;
	};
});

printBtn.states_([["P"]])
.action_({ printFunc.value });

printFunc = {
	sorted.do { |pair|
		var j = split.detectIndex { |item| item == pair };
		if(j.isNil) {
			"FAILURE: %\n".postf(pair);
		} {
			if(j == (split.size - 1)) {
				m[pair[0][0]..].postln;
			} {
				m[split[j][0][0] .. split[j+1][0][0] - 1].postln;
			};
		}
	};
};

w.front;
w.onClose = printFunc;
)
raw 1857 chars (focus & ctrl+a+c to copy)
reception
comments