Skip to content

Commit f704619

Browse files
Jeremy WoottenJeremy Wootten
authored andcommitted
Code style, cleanup
1 parent 2341656 commit f704619

File tree

3 files changed

+33
-43
lines changed

3 files changed

+33
-43
lines changed

plugins/word-completion/completion-provider.vala

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
*
2020
*/
2121

22-
public class Scratch.Plugins.CompletionProvider : Gtk.SourceCompletionProvider, Object {
23-
public string name;
24-
public int priority;
22+
public class Scratch.Plugins.CompletionProvider : Gtk.SourceCompletionProvider, GLib.Object {
23+
public string name { get; construct; }
24+
public int priority { get; construct; }
25+
public int interactive_delay { get; construct; }
26+
public Gtk.SourceCompletionActivation activation { get; construct; }
2527

2628
public const string COMPLETION_END_MARK_NAME = "ScratchWordCompletionEnd";
2729
public const string COMPLETION_START_MARK_NAME = "ScratchWordCompletionStart";
@@ -43,30 +45,25 @@ public class Scratch.Plugins.CompletionProvider : Gtk.SourceCompletionProvider,
4345

4446
public CompletionProvider (
4547
Euclide.Completion.Parser _parser,
46-
Gtk.TextView _view
48+
Scratch.Services.Document _doc
4749
) {
4850

4951
Object (
5052
parser: _parser,
51-
view: _view
53+
view: _doc.source_view,
54+
name: _("%s - Word Completion").printf (_doc.get_basename ())
5255
);
5356
}
5457

5558
construct {
59+
interactive_delay = (int) Completion.INTERACTIVE_DELAY;
60+
activation = INTERACTIVE | USER_REQUESTED;
5661
Gtk.TextIter iter;
5762
view.buffer.get_iter_at_offset (out iter, 0);
5863
completion_end_mark = buffer.create_mark (COMPLETION_END_MARK_NAME, iter, false);
5964
completion_start_mark = buffer.create_mark (COMPLETION_START_MARK_NAME, iter, false);
6065
}
6166

62-
public override string get_name () {
63-
return this.name;
64-
}
65-
66-
public override int get_priority () {
67-
return this.priority;
68-
}
69-
7067
public override bool match (Gtk.SourceCompletionContext context) {
7168
int end_pos = buffer.cursor_position;
7269
int start_pos = end_pos;
@@ -114,15 +111,6 @@ public class Scratch.Plugins.CompletionProvider : Gtk.SourceCompletionProvider,
114111
return true;
115112
}
116113

117-
public override Gtk.SourceCompletionActivation get_activation () {
118-
return Gtk.SourceCompletionActivation.INTERACTIVE |
119-
Gtk.SourceCompletionActivation.USER_REQUESTED;
120-
}
121-
122-
public override int get_interactive_delay () {
123-
return 500;
124-
}
125-
126114
public override bool get_start_iter (Gtk.SourceCompletionContext context,
127115
Gtk.SourceCompletionProposal proposal,
128116
out Gtk.TextIter iter) {
@@ -132,6 +120,9 @@ public class Scratch.Plugins.CompletionProvider : Gtk.SourceCompletionProvider,
132120
return true;
133121
}
134122

123+
public override string get_name () {
124+
return name;
125+
}
135126

136127
private bool get_proposals (out GLib.List<Gtk.SourceCompletionItem>? props, bool no_minimum) {
137128
string to_find = "";

plugins/word-completion/plugin.vala

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
public class Scratch.Plugins.Completion : Peas.ExtensionBase, Peas.Activatable {
2222

2323
public const int MAX_TOKENS = 1000000;
24+
public const uint INTERACTIVE_DELAY = 500;
25+
2426
private const uint [] ACTIVATE_KEYS = {
2527
Gdk.Key.Return,
2628
Gdk.Key.KP_Enter,
@@ -36,8 +38,10 @@ public class Scratch.Plugins.Completion : Peas.ExtensionBase, Peas.Activatable {
3638
public Object object { owned get; construct; }
3739

3840
private List<Gtk.SourceView> text_view_list = new List<Gtk.SourceView> ();
39-
private Euclide.Completion.Parser parser {get; private set;}
40-
private Gtk.SourceView? current_view {get; private set;}
41+
private Euclide.Completion.Parser parser;
42+
private Gtk.SourceView? current_view;
43+
private Gtk.SourceCompletion? current_completion;
44+
private Scratch.Plugins.CompletionProvider current_provider;
4145
private Scratch.Services.Document current_document {get; private set;}
4246
private MainWindow main_window;
4347
private Scratch.Services.Interface plugins;
@@ -76,29 +80,30 @@ public class Scratch.Plugins.Completion : Peas.ExtensionBase, Peas.Activatable {
7680

7781
current_document = doc;
7882
current_view = doc.source_view;
83+
current_completion = current_view.completion;
7984
current_view.buffer.insert_text.connect (on_insert_text);
8085
current_view.buffer.delete_range.connect (on_delete_range);
8186

82-
current_view.completion.show.connect (() => {
87+
current_completion.show.connect (() => {
8388
completion_in_progress = true;
8489
});
8590

86-
current_view.completion.hide.connect (() => {
91+
current_completion.hide.connect (() => {
8792
completion_in_progress = false;
8893
});
8994

9095
if (text_view_list.find (current_view) == null) {
9196
text_view_list.append (current_view);
9297
}
9398

94-
var comp_provider = new Scratch.Plugins.CompletionProvider (parser, current_view);
95-
comp_provider.priority = 1;
96-
comp_provider.name = provider_name_from_document (doc);
99+
current_provider = new Scratch.Plugins.CompletionProvider (parser, doc);
97100

98101
try {
99-
current_view.completion.add_provider (comp_provider);
100-
current_view.completion.show_headers = true;
101-
current_view.completion.show_icons = true;
102+
current_completion.add_provider (current_provider);
103+
current_completion.show_headers = true;
104+
current_completion.show_icons = true;
105+
current_completion.accelerators = 9;
106+
current_completion.select_on_show = true;
102107
} catch (Error e) {
103108
critical (
104109
"Could not add completion provider to %s. %s\n",
@@ -180,10 +185,7 @@ public class Scratch.Plugins.Completion : Peas.ExtensionBase, Peas.Activatable {
180185
return Source.REMOVE;
181186
});
182187
}
183-
}
184188

185-
private string provider_name_from_document (Scratch.Services.Document doc) {
186-
return _("%s - Word Completion").printf (doc.get_basename ());
187189
}
188190

189191
private void cleanup () {
@@ -195,13 +197,10 @@ public class Scratch.Plugins.Completion : Peas.ExtensionBase, Peas.Activatable {
195197
current_view.buffer.delete_range.disconnect (on_delete_range);
196198
// Disconnect show completion??
197199

198-
current_view.completion.get_providers ().foreach ((p) => {
200+
current_completion.get_providers ().foreach ((p) => {
199201
try {
200202
/* Only remove provider added by this plug in */
201-
if (p.get_name () == provider_name_from_document (current_document)) {
202-
debug ("removing provider %s", p != null ? p.get_name () : "null");
203-
current_view.completion.remove_provider (p);
204-
}
203+
current_completion.remove_provider (current_provider);
205204
} catch (Error e) {
206205
warning (e.message);
207206
}

plugins/word-completion/prefix-tree.vala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
private GLib.StringBuilder sb;
2525
private uint reaper_timeout_id = 0;
2626
private bool delay_reaping = false;
27-
private const uint REAPING_THROTTLE_MS = 500;
27+
// Reaping need not occur before completions shown since non-occurring words are
28+
// not shown anyway.
29+
private const uint REAPING_THROTTLE_MS = Completion.INTERACTIVE_DELAY * 2;
2830
private bool reaping_cancelled = false;
2931

3032
public bool completed { get; set; default = false; }
@@ -85,9 +87,7 @@
8587
delay_reaping = false;
8688
return Source.CONTINUE;
8789
} else {
88-
debug ("reaping timeout");
8990
reaper_timeout_id = 0;
90-
// var words_to_remove = get_words_to_remove ();
9191
debug ("reaping");
9292
words_to_remove.foreach ((end_node) => {
9393
if (reaping_cancelled) {

0 commit comments

Comments
 (0)