/
text_manipulation.v
97 lines (84 loc) · 2.63 KB
/
text_manipulation.v
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
89
90
91
92
93
94
95
96
97
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module c
import v.util
fn (mut g Gen) write(s string) {
$if trace_gen ? {
if g.file == unsafe { nil } {
eprintln('gen file: <nil> | last_fn_c_name: ${g.last_fn_c_name:-45} | write: ${s}')
} else {
eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | write: ${s}')
}
}
if g.indent > 0 && g.empty_line {
g.out.write_string(util.tabs(g.indent))
// g.out_parallel[g.out_idx].write_string(util.tabs(g.indent))
}
g.out.write_string(s)
////g.out_parallel[g.out_idx].write_string(s)
g.empty_line = false
}
fn (mut g Gen) writeln(s string) {
$if trace_gen ? {
if g.file == unsafe { nil } {
eprintln('gen file: <nil> | last_fn_c_name: ${g.last_fn_c_name:-45} | writeln: ${s}')
} else {
eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | writeln: ${s}')
}
}
if g.indent > 0 && g.empty_line {
g.out.write_string(util.tabs(g.indent))
// g.out_parallel[g.out_idx].write_string(util.tabs(g.indent))
}
// println('w len=$g.out_parallel.len')
g.out.writeln(s)
// g.out_parallel[g.out_idx].writeln(s)
g.empty_line = true
}
// Below are hacks that should be removed at some point.
fn (mut g Gen) go_back(n int) {
g.out.go_back(n)
// g.out_parallel[g.out_idx].go_back(n)
}
fn (mut g Gen) go_back_to(n int) {
g.out.go_back_to(n)
// g.out_parallel[g.out_idx].go_back_to(n)
}
[inline]
fn (g &Gen) nth_stmt_pos(n int) int {
return g.stmt_path_pos[g.stmt_path_pos.len - (1 + n)]
}
[inline]
fn (mut g Gen) set_current_pos_as_last_stmt_pos() {
g.stmt_path_pos << g.out.len
}
fn (mut g Gen) go_before_stmt(n int) string {
stmt_pos := g.nth_stmt_pos(n)
// g.out_parallel[g.out_idx].cut_to(stmt_pos)
return g.out.cut_to(stmt_pos)
}
[inline]
fn (mut g Gen) go_before_ternary() string {
return g.go_before_stmt(g.inside_ternary)
}
fn (mut g Gen) insert_before_stmt(s string) {
cur_line := g.go_before_stmt(g.inside_ternary)
g.writeln(s)
g.write(cur_line)
}
fn (mut g Gen) insert_at(pos int, s string) {
cur_line := g.out.cut_to(pos)
// g.out_parallel[g.out_idx].cut_to(pos)
g.writeln(s)
g.write(cur_line)
// After modifying the code in the buffer, we need to adjust the positions of the statements
// to account for the added line of code.
// This is necessary to ensure that autofree can properly insert string declarations
// in the correct positions, considering the surgically made changes.
for index, stmt_pos in g.stmt_path_pos {
if stmt_pos >= pos {
g.stmt_path_pos[index] += s.len + 1
}
}
}