Skip to content

Commit

Permalink
Rework word completion: Start to prepare PrefixNode for dynamic updat…
Browse files Browse the repository at this point in the history
…ing (#1532)
  • Loading branch information
jeremypw authored Feb 6, 2025
1 parent 7dde1f5 commit 857a871
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
42 changes: 38 additions & 4 deletions plugins/word-completion/prefix-tree-node.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,50 @@
*/

public class Scratch.Plugins.PrefixNode : Object {
public GLib.List<PrefixNode> children;
public enum NodeType {
ROOT,
CHAR,
WORD_END
}

private const unichar WORD_END_CHAR = '\0';
private uint occurrences; // Only used for WORD_END nodes

public unichar uc { get; construct; }
public NodeType node_type { get; construct; }
public PrefixNode? parent { get; construct; }
public unichar value { get; construct; }

public PrefixNode (unichar c = '\0') {
public Gee.ArrayList<PrefixNode> children;

public PrefixNode.from_unichar (unichar c, PrefixNode? _parent) requires (c != WORD_END_CHAR) {
Object (
value: c
value: c,
parent: _parent,
uc: c,
node_type: NodeType.CHAR
);
}

public PrefixNode.root () {
Object (
parent: null,
uc: WORD_END_CHAR,
node_type: NodeType.ROOT
);
}

public PrefixNode.word_end (PrefixNode _parent) {
Object (
parent: _parent,
uc: WORD_END_CHAR,
node_type: NodeType.WORD_END
);

occurrences = 1;
}

construct {
children = new List<PrefixNode> ();
children = new Gee.ArrayList<PrefixNode> ();
}
}
5 changes: 3 additions & 2 deletions plugins/word-completion/prefix-tree.vala
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ namespace Scratch.Plugins {
}
}

var new_child = new PrefixNode (curr);
node.children.insert_sorted (new_child, (c1, c2) => {
var new_child = new PrefixNode.from_unichar (curr, null);
node.children.insert (0, new_child);
node.children.sort ((c1, c2) => {
if (c1.value > c2.value) {
return 1;
} else if (c1.value == c2.value) {
Expand Down

0 comments on commit 857a871

Please sign in to comment.