-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathengine.vala
More file actions
113 lines (95 loc) · 3.58 KB
/
Copy pathengine.vala
File metadata and controls
113 lines (95 loc) · 3.58 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
/*
* Copyright (c) 2011 Lucas Baudin <xapantu@gmail.com>
*
* 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 Euclide.Completion.Parser : GLib.Object {
public const int MINIMUM_WORD_LENGTH = 1;
public const int MAX_TOKENS = 1000000;
private Scratch.Plugins.PrefixTree? prefix_tree = null;
public const string DELIMITERS = " .,;:?{}[]()0123456789+=&|<>*\\/\r\n\t\'\"`";
public static bool is_delimiter (unichar c) {
return DELIMITERS.index_of_char (c) >= 0;
}
public static void back_to_word_start (ref Gtk.TextIter iter) {
iter.backward_find_char (is_delimiter, null);
iter.forward_char ();
}
public Gee.HashMap<Gtk.TextView,Scratch.Plugins.PrefixTree> text_view_words;
public bool parsing_cancelled = false;
public Parser () {
text_view_words = new Gee.HashMap<Gtk.TextView,Scratch.Plugins.PrefixTree> ();
prefix_tree = new Scratch.Plugins.PrefixTree ();
}
public bool match (string to_find) {
return prefix_tree.find_prefix (to_find);
}
public bool get_for_word (string to_find, out List<string> list) {
list = prefix_tree.get_all_matches (to_find);
return list.first () != null;
}
public void rebuild_word_list (Gtk.TextView view) {
prefix_tree.clear ();
if (view.buffer.text.length > 0) {
parse_string (view.buffer.text);
text_view_words.@set (view, prefix_tree);
}
}
public bool select_current_tree (Gtk.TextView view) {
bool pre_existing = true;
if (!text_view_words.has_key (view)) {
var new_treemap = new Scratch.Plugins.PrefixTree ();
text_view_words.@set (view, new_treemap);
pre_existing = false;
}
lock (prefix_tree) {
prefix_tree = text_view_words.@get (view);
parsing_cancelled = false;
}
return pre_existing && get_initial_parsing_completed ();
}
public void set_initial_parsing_completed (bool completed) requires (prefix_tree != null) {
lock (prefix_tree) {
prefix_tree.completed = completed;
}
}
public bool get_initial_parsing_completed () requires (prefix_tree != null) {
return prefix_tree.completed;
}
public void add_word (string word) {
if (word.length < MINIMUM_WORD_LENGTH)
return;
lock (prefix_tree) {
prefix_tree.insert (word);
}
}
public void cancel_parsing () {
parsing_cancelled = true;
}
private bool parse_string (string text) {
parsing_cancelled = false;
string [] word_array = text.split_set (DELIMITERS, MAX_TOKENS);
foreach (var current_word in word_array ) {
if (parsing_cancelled) {
debug ("Cancelling parse");
return false;
}
add_word (current_word);
}
return true;
}
}