forked from filhodanuvem/gitql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.go
187 lines (175 loc) · 4.12 KB
/
autocomplete.go
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
func suggestColumnsFromLatest(focused string) [][]rune {
return suggestLatest(focused[:len(focused)-1], [][]string{
[]string{"hash", "date", "author", "author_email", "committer", "committer_email", "message", "full_message"},
[]string{"name", "full_name", "type", "hash"},
[]string{"name", "url", "push_url", "owner"},
[]string{"name", "full_name", "hash"},
})
}
// Creates a candidate from the input previous character string.
func suggestLatest(focused string, candidacies [][]string) [][]rune {
var suggests [][]rune
for _, candidacy := range candidacies {
s := getPartsFromSlice(focused, candidacy)
if s != nil {
suggests = append(suggests, s...)
}
}
removeDuplicates(&suggests)
return suggests
}
func containColumns(focused string) bool {
_, ok := isContained(focused, []string{
"select", // gitql> select [tab
"distinct,",
"name,",
"url,",
"push_url,",
"owner,",
"full_name,",
"hash,",
"date,",
"author,",
"author_email,",
"committer,",
"committer_email,",
"message,",
"full_message,",
"type,",
})
return ok
}
// Remove duplicates elements in slice.
func removeDuplicates(s *[][]rune) {
found := make(map[string]bool)
j := 0
for i, x := range *s {
key := string(x)
if !found[key] {
found[key] = true
(*s)[j] = (*s)[i]
j++
}
}
*s = (*s)[:j]
}
func suggestQuery(inputs [][]rune, pos int) [][]rune {
ln := len(inputs)
if ln == 1 {
// When nothing is input yet
return [][]rune{[]rune("select")}
}
focused := string(inputs[ln-2])
if focused == "select" {
// gitql> select [tab
// In the case where the most recent input is "select"
return [][]rune{
[]rune("distinct"),
[]rune("*"),
[]rune("name"),
[]rune("url"),
[]rune("push_url"),
[]rune("owner"),
[]rune("full_name"),
[]rune("hash"),
[]rune("date"),
[]rune("author"),
[]rune("author_email"),
[]rune("committer"),
[]rune("committer_email"),
[]rune("message"),
[]rune("full_message"),
[]rune("type"),
}
} else if containColumns(focused) {
// gitql> select name, [tab
// gitql> select committer, [tab
// In the case where the most recent input is the column name and comma
return suggestColumnsFromLatest(focused)
} else if focused == "from" {
// gitql> select * from [tab
// In the case after inputted "from"
return [][]rune{
[]rune("tags"),
[]rune("branches"),
[]rune("commits"),
[]rune("refs"),
}
} else if focused == "order" {
return [][]rune{[]rune("by")}
} else if focused == "where" || focused == "by" || focused == "or" || focused == "and" {
// gitql> select name from commits where [tab
// gitql> select * from commits where committer = "K" order by [tab
// gitql> select * from commits where committer = "K" and [tab
// In the case is inputted after "where", "by", "and", "or"
var table string
for i := 0; i < len(inputs); i++ {
if string(inputs[i]) == "from" {
i++
table = string(inputs[i])
}
}
switch table {
case "commits":
return [][]rune{
[]rune("hash"),
[]rune("date"),
[]rune("author"),
[]rune("author_email"),
[]rune("committer"),
[]rune("committer_email"),
[]rune("message"),
[]rune("full_message"),
}
case "refs":
return [][]rune{
[]rune("name"),
[]rune("full_name"),
[]rune("type"),
[]rune("hash"),
}
case "branches", "tags":
return [][]rune{
[]rune("name"),
[]rune("full_name"),
[]rune("hash"),
}
}
}
return [][]rune{
[]rune("select"),
[]rune("from"),
[]rune("where"),
[]rune("order"),
[]rune("by"),
[]rune("or"),
[]rune("and"),
[]rune("limit"),
[]rune("in"),
[]rune("asc"),
[]rune("desc"),
}
}
func getPartsFromSlice(focused string, candidacy []string) [][]rune {
idx, ok := isContained(focused, candidacy)
if ok {
var suggests [][]rune
for i, v := range candidacy {
// Create slices other than what was focused
if i != idx {
suggests = append(suggests, []rune(v))
}
}
return suggests
}
return nil
}
func isContained(focused string, candidacy []string) (int, bool) {
for idx, val := range candidacy {
if focused == val {
return idx, true
}
}
return -1, false
}