forked from jamiepg1/valencia
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathautocomplete.vala
More file actions
259 lines (209 loc) · 7.88 KB
/
autocomplete.vala
File metadata and controls
259 lines (209 loc) · 7.88 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* Copyright 2009-2015 Yorba Foundation
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/
using Gee;
using Valencia;
class AutocompleteDialog : Object {
weak Gedit.Window parent;
Gtk.Window window;
ListViewString list;
bool visible;
string partial_name;
bool inserting_text;
public AutocompleteDialog(Gedit.Window parent_win) {
parent = parent_win;
visible = false;
inserting_text = false;
list = new ListViewString(Gtk.TreeViewColumnSizing.AUTOSIZE, 100);
list.row_activated.connect(select_item);
window = new Gtk.Window(Gtk.WindowType.POPUP);
window.add(list.scrolled_window);
window.set_destroy_with_parent(true);
window.set_default_size(200, 1);
window.set_resizable(true);
window.set_title("");
window.set_border_width(1);
window.show_all();
window.hide();
}
string? get_completion_target(Gtk.TextBuffer buffer) {
Gtk.TextIter start = get_insert_iter(buffer);
Gtk.TextIter end = start;
while (true) {
start.backward_char();
unichar c = start.get_char();
if (!c.isalnum() && c != '.' && c != '_')
break;
}
// Only include characters in the ID name
start.forward_char();
if (start.get_offset() == end.get_offset())
return null;
return start.get_slice(end);
}
string strip_completed_classnames(string list_name, string completion_target) {
string result = list_name;
string[] classnames = completion_target.split(".");
int names = classnames.length;
// If the last classname is not explicitly part of the class qualification, then it
// should not be removed from the completion suggestion's name
if (!completion_target.has_suffix("."))
--names;
for (int i = 0; i < names; ++i) {
weak string name = classnames[i];
// If the name doesn't contain the current classname, it may be a namespace name that
// isn't part of the list_name string - we shouldn't stop the comparison early
if (result.contains(name)) {
// Add one to the offset of a string to account for the "."
long offset = name.length;
if (offset > 0)
++offset;
result = result.substring(offset);
}
}
return result;
}
string parse_single_symbol(Symbol symbol, string? completion_target, bool constructor) {
string list_name = "";
if (constructor) {
// Get the fully-qualified constructor name
Constructor c = symbol as Constructor;
assert(c != null);
list_name = c.parent.to_string();
if (c.name != null)
list_name += "." + c.name;
list_name += "()";
// If the user hasn't typed anything or if either the completion string or this
// constructor is not qualified, keep the original name
if (completion_target != null && completion_target.contains(".")
&& list_name.contains("."))
list_name = strip_completed_classnames(list_name, completion_target);
} else {
list_name = symbol.name;
if (symbol is Method && !(symbol is VSignal) && !(symbol is Delegate))
list_name = symbol.name + "()";
}
return list_name;
}
string[]? parse_symbol_names(HashSet<Symbol>? symbols) {
if (symbols == null)
return null;
string[] list = new string[symbols.size];
// If the first element is a constructor, all elements will be constructors
Iterator<Symbol> iter = symbols.iterator();
iter.next();
bool constructor = iter.get() is Constructor;
// match the extent of what the user has already typed with named constructors
string? completion_target = null;
if (constructor) {
completion_target = get_completion_target(parent.get_active_document());
}
int i = 0;
foreach (Symbol symbol in symbols) {
list[i] = parse_single_symbol(symbol, completion_target, constructor);
++i;
}
qsort(list, symbols.size, sizeof(string), (GLib.CompareFunc) compare_string);
return list;
}
public void show(SymbolSet symbol_set) {
if (inserting_text)
return;
list.clear();
visible = true;
partial_name = symbol_set.get_name();
weak HashSet<Symbol>? symbols = symbol_set.get_symbols();
string[]? symbol_strings = parse_symbol_names(symbols);
if (symbol_strings != null) {
foreach (string s in symbol_strings) {
list.append(s);
}
} else {
hide();
return;
}
// TODO: this must be updated to account for font size changes when adding ticket #560
int size = list.size();
if (size > 6) {
list.set_vscrollbar_policy(Gtk.PolicyType.AUTOMATIC);
window.resize(200, 140);
} else {
list.set_vscrollbar_policy(Gtk.PolicyType.NEVER);
window.resize(200, size * 23);
}
Gedit.Document document = parent.get_active_document();
Gtk.TextMark insert_mark = document.get_insert();
Gtk.TextIter insert_iter;
document.get_iter_at_mark(out insert_iter, insert_mark);
int x, y;
get_coords_at_buffer_offset(parent, insert_iter.get_offset(), false, true, out x, out y);
window.move(x, y);
window.show_all();
window.queue_draw();
select_first_cell();
}
public void hide() {
if (!visible)
return;
visible = false;
window.hide();
}
public bool is_visible() {
return visible;
}
public void select_first_cell() {
list.select_first_cell();
}
public void select_last_cell() {
list.select_last_cell();
}
public void select_previous() {
list.select_previous();
}
public void select_next() {
list.select_next();
}
public void page_up() {
list.page_up();
}
public void page_down() {
list.page_down();
}
public void select_item() {
string selection = list.get_selected_item();
Gedit.Document buffer = parent.get_active_document();
// delete the whole string to be autocompleted and replace it (the case may not match)
Gtk.TextIter start = get_insert_iter(buffer);
while (true) {
if (!start.backward_char())
break;
unichar c = start.get_char();
if (!c.isalnum() && c != '_')
break;
}
// don't include the nonalphanumeric character
start.forward_char();
Gtk.TextIter end = start;
while (true) {
unichar c = end.get_char();
if (c == '(') {
end.forward_char();
break;
}
if (!c.isalnum() && c != '_' && c != '.')
break;
if (!end.forward_char())
break;
}
// Text insertion/deletion signals have been linked to updating the autocomplete dialog -
// we don't want to do that if we're already inserting text.
inserting_text = true;
buffer.delete(ref start, ref end);
long offset = selection.has_suffix(")") ? 1 : 0;
buffer.insert_at_cursor(selection, (int) (selection.length - offset));
inserting_text = false;
hide();
}
}