LEFT | RIGHT |
1 /** | |
2 * This processes comments. | |
3 */ | |
4 function associate_comments(filename, scopeObject) { | |
5 // Read the script and give us line stuff | |
6 let file = read_file(filename).split(/\r\n|[\r\n]/); | |
7 // make file 1-based to avoid off-by-one errors... | |
8 file.unshift(""); | |
9 | |
10 // Now, get us a sorted list of all important AST locations | |
11 let locations = [{loc: {line: 0, column: -1}}]; | |
12 | |
13 function add_func(func) { | |
14 locations.push({loc: func.loc, obj: func, commentWanted: true}); | |
15 // The following will get us to the last line of the function | |
16 if (func.body.kids.length == 0) | |
17 return; | |
18 let last = func.body.kids[func.body.kids.length - 1]; | |
19 while (last.kids[last.kids.length - 1] && | |
20 last.kids[last.kids.length - 1].line > last.line) | |
21 last = last.kids[last.kids.length - 1]; | |
22 locations.push({loc: {line: last.line, column: last.column}}); | |
23 } | |
24 for (let v of scopeObject.variables) | |
25 locations.push({loc: v.loc, obj: v, commentWanted: true}); | |
26 for (let v of scopeObject.constants) | |
27 locations.push({loc: v.loc, obj: v, commentWanted: true}); | |
28 for (let v of scopeObject.functions) | |
29 add_func(v); | |
30 for (let v of scopeObject.code) | |
31 locations.push({loc: {line: v.line, column: v.column}, obj: v}); | |
32 for (let o of scopeObject.objects) { | |
33 locations.push({loc: o.loc, obj: o, commentWanted: true}); | |
34 for (let x of o.variables) | |
35 locations.push({loc: x.loc, obj: x, commentWanted: true}); | |
36 for (let x of o.functions) | |
37 add_func(x); | |
38 for (let x of o.getters) | |
39 add_func(x); | |
40 for (let x of o.setters) | |
41 add_func(x); | |
42 } | |
43 for (let o of scopeObject.classes) { | |
44 locations.push({loc: o.loc, obj: o, commentWanted: true}); | |
45 for (let x of o.variables) | |
46 locations.push({loc: x.loc, obj: x, commentWanted: true}); | |
47 for (let x of o.functions) | |
48 add_func(x); | |
49 for (let x of o.getters) | |
50 add_func(x); | |
51 for (let x of o.setters) | |
52 add_func(x); | |
53 } | |
54 locations.sort(function (a, b) { | |
55 if (a.loc.line == b.loc.line) | |
56 return a.loc.column - b.loc.column; | |
57 return a.loc.line - b.loc.line; | |
58 }); | |
59 | |
60 // With that list done, let's find comments in the range. | |
61 for (let i = 1; i < locations.length; i++) { | |
62 if (!locations[i].commentWanted) | |
63 continue; | |
64 let comment = find_comments(locations[i - 1].loc, locations[i].loc, file); | |
65 if (comment) | |
66 locations[i].obj.comment = comment; | |
67 } | |
68 } | |
69 | |
70 let comment_regex = /\/\*[\s\S]*?\*\/|\/\/.*?\n/mg; | |
71 function find_comments(start, end, file) { | |
72 let lines = []; | |
73 lines[0] = file[start.line].substring(start.column + 1).trim(); | |
74 for (let l = start.line + 1; l < end.line; l++) { | |
75 lines.push(file[l].trim()); | |
76 } | |
77 lines.push(file[end.line].substring(0, end.column).trim()); | |
78 | |
79 let goop = lines.join("\n"); | |
80 let match; | |
81 lines = []; | |
82 while ((match = comment_regex.exec(goop)) != null) { | |
83 lines.push(match); | |
84 } | |
85 return lines.join("\n"); | |
86 } | |
LEFT | RIGHT |