-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathcode.go
More file actions
165 lines (151 loc) · 3.32 KB
/
code.go
File metadata and controls
165 lines (151 loc) · 3.32 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package source
import (
"bufio"
"fmt"
"sort"
"strings"
"unicode"
)
type Edit struct {
Location int
Old string
New string
OldFunc func(string) int
}
type CommentSyntax struct {
Dash bool
Hash bool
SlashStar bool
}
func LineNumber(source string, head int) (int, int) {
// Calculate the true line and column number for a query, ignoring spaces
var comment bool
var loc, line, col int
for i, char := range source {
loc += 1
col += 1
// TODO: Check bounds
if char == '-' && source[i+1] == '-' {
comment = true
}
if char == '\n' {
comment = false
line += 1
col = 0
}
if loc <= head {
continue
}
if unicode.IsSpace(char) {
continue
}
if comment {
continue
}
break
}
return line + 1, col
}
func Pluck(source string, location, length int) (string, error) {
head := location
tail := location + length
return source[head:tail], nil
}
func Mutate(raw string, a []Edit) (string, error) {
if len(a) == 0 {
return raw, nil
}
sort.Slice(a, func(i, j int) bool { return a[i].Location > a[j].Location })
s := raw
for idx, edit := range a {
start := edit.Location
if start > len(s) || start < 0 {
return "", fmt.Errorf("edit start location is out of bounds")
}
var oldLen int
if edit.OldFunc != nil {
oldLen = edit.OldFunc(s[start:])
} else {
oldLen = len(edit.Old)
}
stop := edit.Location + oldLen
if stop > len(s) {
return "", fmt.Errorf("edit stop location is out of bounds")
}
// If this is not the first edit, (applied backwards), check if
// this edit overlaps the previous one (and is therefore a developer error)
if idx != 0 {
prevEdit := a[idx-1]
if prevEdit.Location < edit.Location+oldLen {
return "", fmt.Errorf("2 edits overlap")
}
}
s = s[:start] + edit.New + s[stop:]
}
return s, nil
}
func StripComments(sql string) (string, []string, error) {
s := bufio.NewScanner(strings.NewReader(strings.TrimSpace(sql)))
var lines, comments []string
for s.Scan() {
t := s.Text()
if strings.HasPrefix(t, "-- name:") {
continue
}
if strings.HasPrefix(t, "/* name:") && strings.HasSuffix(t, "*/") {
continue
}
if strings.HasPrefix(t, "# name:") {
continue
}
if strings.HasPrefix(t, "--") {
comments = append(comments, strings.TrimPrefix(t, "--"))
continue
}
if strings.HasPrefix(t, "/*") && strings.HasSuffix(t, "*/") {
t = strings.TrimPrefix(t, "/*")
t = strings.TrimSuffix(t, "*/")
comments = append(comments, t)
continue
}
if strings.HasPrefix(t, "#") {
comments = append(comments, strings.TrimPrefix(t, "#"))
continue
}
lines = append(lines, t)
}
return strings.Join(lines, "\n"), comments, s.Err()
}
func CleanedComments(rawSQL string, cs CommentSyntax) ([]string, error) {
s := bufio.NewScanner(strings.NewReader(strings.TrimSpace(rawSQL)))
var comments []string
for s.Scan() {
line := s.Text()
var prefix string
if strings.HasPrefix(line, "--") {
if !cs.Dash {
continue
}
prefix = "--"
}
if strings.HasPrefix(line, "/*") {
if !cs.SlashStar {
continue
}
prefix = "/*"
}
if strings.HasPrefix(line, "#") {
if !cs.Hash {
continue
}
prefix = "#"
}
if prefix == "" {
continue
}
rest := line[len(prefix):]
rest = strings.TrimSuffix(rest, "*/")
comments = append(comments, rest)
}
return comments, s.Err()
}