|
| 1 | +extends TextForgeMode |
| 2 | + |
| 3 | + |
| 4 | +func _generate_outline(text: String) -> Array: |
| 5 | + var outline: Array |
| 6 | + var i := 0 |
| 7 | + for l in text.split("\n"): |
| 8 | + if l.strip_edges() != "": |
| 9 | + outline.append([_split_line(l)[0], i]) |
| 10 | + i += 1 |
| 11 | + return outline |
| 12 | + |
| 13 | + |
| 14 | +func _generate_preview(text: String) -> String: |
| 15 | + var preview := "" |
| 16 | + var i := 0 |
| 17 | + for l in text.split("\n"): |
| 18 | + if i == 0: |
| 19 | + preview += "[table={0}]\n".format([l.get_slice_count(",")]) |
| 20 | + for c in _split_line(l): |
| 21 | + preview += "[cell bg=00000000,f0f0f020 padding=8,2,8,2]" + c.strip_edges() + "[/cell]" |
| 22 | + preview += "\n" |
| 23 | + i += 1 |
| 24 | + preview += "[/table]" |
| 25 | + return preview |
| 26 | + |
| 27 | + |
| 28 | +func _lint_file(text: String) -> Array[Dictionary]: |
| 29 | + var problems: Array[Dictionary] = [] |
| 30 | + var columns_count := _split_line(text.split("\n")[0]).size() |
| 31 | + var i := 0 |
| 32 | + for l in text.split("\n"): |
| 33 | + var c_count := _split_line(l).size() |
| 34 | + if columns_count != c_count: |
| 35 | + problems.append({ |
| 36 | + "line": i, |
| 37 | + "column": l.length(), |
| 38 | + "error": true, |
| 39 | + "title": "Invalid value count!", |
| 40 | + "details": "This row has {0} values when it should have {1} values.".format([c_count, columns_count]), |
| 41 | + }) |
| 42 | + i += 1 |
| 43 | + return problems |
| 44 | + |
| 45 | + |
| 46 | +func _split_line(line: String) -> Array[String]: |
| 47 | + var output: Array[String] = [] |
| 48 | + var regex := RegEx.new() |
| 49 | + regex.compile(r",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)") |
| 50 | + var matches := regex.search_all(line) |
| 51 | + |
| 52 | + if not matches or matches.size() == 0: |
| 53 | + output.append(line.strip_edges()) |
| 54 | + return output |
| 55 | + |
| 56 | + var last_index := 0 |
| 57 | + for m in matches: |
| 58 | + var part := line.substr(last_index, m.get_start() - last_index) |
| 59 | + output.append(part.strip_edges()) |
| 60 | + last_index = m.get_end() |
| 61 | + |
| 62 | + output.append(line.substr(last_index).strip_edges()) |
| 63 | + |
| 64 | + return output |
0 commit comments