-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathhighlight-word-selection.vala
More file actions
156 lines (129 loc) · 5.48 KB
/
highlight-word-selection.vala
File metadata and controls
156 lines (129 loc) · 5.48 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
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
/***
BEGIN LICENSE
Copyright (C) 2013 Madelynn May <madelynnmay@madelynnmay.com>
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, 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. If not, see <http://www.gnu.org/licenses/>
END LICENSE
***/
public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Scratch.Services.ActivatablePlugin {
Scratch.Widgets.SourceView current_source;
Scratch.MainWindow? main_window = null;
Gtk.SourceSearchContext? current_search_context = null;
// Consts
// Pneumonoultramicroscopicsilicovolcanoconiosis longest word in a major dictionary @ 45
private const uint SELECTION_HIGHLIGHT_MAX_CHARS = 45;
Scratch.Services.Interface plugins;
public Object object { owned get; construct; }
public void update_state () {}
public void activate () {
plugins = (Scratch.Services.Interface) object;
plugins.hook_document.connect ((doc) => {
if (current_source != null) {
current_source.deselected.disconnect (on_deselection);
current_source.selection_changed.disconnect (on_selection_changed);
}
current_source = doc.source_view;
current_source.deselected.connect (on_deselection);
current_source.selection_changed.connect (on_selection_changed);
});
plugins.hook_window.connect ((w) => {
main_window = w;
});
}
public void on_selection_changed (ref Gtk.TextIter start, ref Gtk.TextIter end) {
var window_search_context = main_window != null ? main_window.search_bar.search_context : null;
if (window_search_context == null ||
window_search_context.settings.search_text == "" ||
window_search_context.get_occurrences_count () == 0) {
// Perform plugin selection when there is no ongoing and successful search
current_search_context = new Gtk.SourceSearchContext (
(GtkSource.Buffer)current_source.buffer,
null
);
current_search_context.settings.search_text = "";
current_search_context.set_highlight (false);
var original_start = start.copy ();
// Ignore leading space
start.forward_find_char (
(uc) => {
return !uc.isspace ();
},
end
);
// Extend backwards to start of word
start.backward_find_char (
(uc) => {
var break_type = uc.break_type ();
if (break_type == UnicodeBreakType.ALPHABETIC ||
break_type == UnicodeBreakType.WORD_JOINER) {
return false;
} else {
return true;
}
},
null
);
// Do not include leading punctuation unless originally selected by user
if (start.compare (original_start) < 0) {
start.forward_char ();
}
// Ignore trailing spaces
end.backward_find_char (
(uc) => {
return !uc.isspace ();
},
start
);
// Extend forward to end of word
end.forward_find_char (
(uc) => {
var break_type = uc.break_type ();
if (break_type == UnicodeBreakType.ALPHABETIC ||
break_type == UnicodeBreakType.WORD_JOINER) {
return false;
} else {
return true;
}
},
null
);
// Ensure no leading or trailing space
var selected_text = start.get_text (end).strip ();
if (selected_text.char_count () > SELECTION_HIGHLIGHT_MAX_CHARS) {
return;
}
current_search_context.settings.search_text = selected_text;
current_search_context.set_highlight (true);
} else if (current_search_context != null) {
// Cancel existing search
current_search_context.set_highlight (false);
current_search_context = null;
}
}
public void on_deselection () {
if (current_search_context != null) {
current_search_context.set_highlight (false);
current_search_context = null;
}
}
public void deactivate () {
if (current_source != null) {
current_source.deselected.disconnect (on_deselection);
current_source.selection_changed.disconnect (on_selection_changed);
}
}
}
[ModuleInit]
public void peas_register_types (TypeModule module) {
var objmodule = module as Peas.ObjectModule;
objmodule.register_extension_type (typeof (Scratch.Services.ActivatablePlugin),
typeof (Scratch.Plugins.HighlightSelectedWords));
}