-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathcompletion-provider.vala
More file actions
142 lines (119 loc) · 5.28 KB
/
Copy pathcompletion-provider.vala
File metadata and controls
142 lines (119 loc) · 5.28 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
/*
* Copyright (c) 2013 Mario Guerriero <mario@elementaryos.org>
*
* This is a free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; see the file COPYING. If not,
* write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
public class Scratch.Plugins.CompletionProvider : Gtk.SourceCompletionProvider, Object {
public string name;
public int priority;
public const string COMPLETION_END_MARK_NAME = "ScratchWordCompletionEnd";
public const string COMPLETION_START_MARK_NAME = "ScratchWordCompletionStart";
private Gtk.TextView? view;
private Gtk.TextBuffer? buffer;
private Euclide.Completion.Parser parser;
private Gtk.TextMark completion_end_mark;
private Gtk.TextMark completion_start_mark;
public signal void can_propose (bool b);
public CompletionProvider (Scratch.Plugins.Completion completion) {
this.view = completion.current_view as Gtk.TextView;
this.buffer = completion.current_view.buffer;
this.parser = completion.parser;
Gtk.TextIter iter;
buffer.get_iter_at_offset (out iter, 0);
completion_end_mark = buffer.create_mark (COMPLETION_END_MARK_NAME, iter, false);
completion_start_mark = buffer.create_mark (COMPLETION_START_MARK_NAME, iter, false);
}
public string get_name () {
return this.name;
}
public int get_priority () {
return this.priority;
}
public bool match (Gtk.SourceCompletionContext context) {
Gtk.TextIter iter;
buffer.get_iter_at_offset (out iter, buffer.cursor_position);
string text = parser.get_word_immediately_before (iter);
return parser.match (text);
}
public void populate (Gtk.SourceCompletionContext context) {
/*Store current insertion point for use in activate_proposal */
GLib.List<Gtk.SourceCompletionItem>? file_props;
bool no_minimum = (context.get_activation () == Gtk.SourceCompletionActivation.USER_REQUESTED);
get_proposals (out file_props, no_minimum);
context.add_proposals (this, file_props, true);
}
public bool activate_proposal (Gtk.SourceCompletionProposal proposal, Gtk.TextIter iter) {
Gtk.TextIter start;
Gtk.TextIter end;
Gtk.TextMark mark;
mark = buffer.get_mark (COMPLETION_END_MARK_NAME);
buffer.get_iter_at_mark (out end, mark);
mark = buffer.get_mark (COMPLETION_START_MARK_NAME);
buffer.get_iter_at_mark (out start, mark);
buffer.@delete (ref start, ref end);
buffer.insert (ref start, proposal.get_text (), proposal.get_text ().length);
return true;
}
public Gtk.SourceCompletionActivation get_activation () {
return Gtk.SourceCompletionActivation.INTERACTIVE |
Gtk.SourceCompletionActivation.USER_REQUESTED;
}
public int get_interactive_delay () {
return 0;
}
private bool get_proposals (out GLib.List<Gtk.SourceCompletionItem>? props, bool no_minimum) {
string to_find = "";
string completion = "";
bool have_selection = true;
Gtk.TextBuffer temp_buffer = buffer;
props = null;
Gtk.TextIter start, end;
buffer.get_selection_bounds (out start, out end);
to_find = temp_buffer.get_text (start, end, true);
if (to_find.length == 0) {
have_selection = false;
Gtk.TextIter iter;
temp_buffer.get_iter_at_offset (out iter, buffer.cursor_position);
to_find = parser.get_word_immediately_before (iter);
}
buffer.move_mark_by_name (COMPLETION_END_MARK_NAME, end);
buffer.move_mark_by_name (COMPLETION_START_MARK_NAME, start);
/* There is no minimum length of word to find if the user requested a completion */
if (no_minimum || to_find.length >= Euclide.Completion.Parser.MINIMUM_WORD_LENGTH) {
/* Get proposals, if any */
List<string> prop_word_list;
if (parser.get_for_word (to_find, out prop_word_list)) {
foreach (var word in prop_word_list) {
// If there is a selection the start mark is at start of completion
// otherwise it is at the cursor position but we want to replace from
// the start of the word
if (have_selection) {
completion = word;
} else {
completion = word.substring (to_find.length);
}
var item = new Gtk.SourceCompletionItem ();
item.label = word;
item.text = completion;
props.append (item);
}
return true;
}
}
return false;
}
}