Skip to content

Commit

Permalink
fix(CompletionProvider): refactor word capturing logic (#661)
Browse files Browse the repository at this point in the history
* fix(CompletionProvider): refactor word capturing logic

* fix: replace the word until the space and add a space to all providers

* fix: edge cases with word capturing
  • Loading branch information
GeopJr authored Dec 1, 2023
1 parent ca8e85e commit 36d1334
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
43 changes: 32 additions & 11 deletions src/Dialogs/Composer/Completion/CompletionProvider.vala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public abstract class Tuba.CompletionProvider: Object, GtkSource.CompletionProvi
public virtual bool is_trigger (Gtk.TextIter iter, unichar ch) {
if (this.trigger_char == null) {
return this.set_input_capture (true);
}
else if (ch.to_string () == this.trigger_char) {
} else if (ch.to_string () == this.trigger_char) {
return this.set_input_capture (true);
}
return false;
Expand All @@ -20,8 +19,7 @@ public abstract class Tuba.CompletionProvider: Object, GtkSource.CompletionProvi
this.is_capturing_input = state;
if (state) {
debug ("Capturing input");
}
else {
} else {
debug ("Stopped capturing input");
this.empty_triggers = 0;
}
Expand All @@ -37,8 +35,19 @@ public abstract class Tuba.CompletionProvider: Object, GtkSource.CompletionProvi
Gtk.TextIter end;
context.get_bounds (out start, out end);

// If end is ' ', it's already the
// end of the word. Proceeding will
// capture more than needed
if (end.get_char () != ' ')
// Go forwards until we find a space
// aka get the full string - even if
// it's not considered a word by pango
end.forward_find_char ((e) => e.isspace (), null);
// plus a space since we are appending one below
end.forward_char ();

var buffer = start.get_buffer ();
var new_content = proposal.get_typed_text ();
var new_content = proposal.get_typed_text () + " ";

buffer.begin_user_action ();
buffer.@delete (ref start, ref end);
Expand All @@ -58,13 +67,13 @@ public abstract class Tuba.CompletionProvider: Object, GtkSource.CompletionProvi
// is the trigger
Gtk.TextIter start;
context.get_bounds (out start, null);
if (start.backward_char ())
is_trigger (start, start.get_char ());
if (start.backward_char () && is_trigger (start, start.get_char ()))
return yield populate_async (context, cancellable);

return EMPTY;
}

var word = context.get_word ();
string word = get_whole_word (context);
if (word == "") {
debug ("Empty trigger");
this.empty_triggers++;
Expand All @@ -75,9 +84,9 @@ public abstract class Tuba.CompletionProvider: Object, GtkSource.CompletionProvi
return EMPTY;
}

var suggestions = yield this.suggest (context, cancellable);
var suggestions = yield this.suggest (word, cancellable);

if (word != context.get_word ())
if (word != get_whole_word (context))
return EMPTY;
return suggestions;
}
Expand All @@ -89,7 +98,19 @@ public abstract class Tuba.CompletionProvider: Object, GtkSource.CompletionProvi
);

public abstract async GLib.ListModel suggest (
GtkSource.CompletionContext context,
string word,
GLib.Cancellable? cancellable
) throws Error;

public string get_whole_word (GtkSource.CompletionContext context) {
Gtk.TextIter start;
Gtk.TextIter end;
context.get_bounds (out start, out end);

// If end is ':', everything until
// a newline will be treated as a word
if (end.get_char () != ':')
end.forward_word_end ();
return start.get_text (end);
}
}
4 changes: 1 addition & 3 deletions src/Dialogs/Composer/Completion/EmojiProvider.vala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ public class Tuba.EmojiProvider: Tuba.CompletionProvider {
}
}

public override async ListModel suggest (GtkSource.CompletionContext context, Cancellable? cancellable) throws Error {
var word = context.get_word ();

public override async ListModel suggest (string word, Cancellable? cancellable) throws Error {
var results = new GLib.ListStore (typeof (Object));
var emojis = accounts.active.instance_emojis;

Expand Down
6 changes: 2 additions & 4 deletions src/Dialogs/Composer/Completion/HandleProvider.vala
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ public class Tuba.HandleProvider: Tuba.CompletionProvider {
}

public override string? get_typed_text () {
return this.account.handle.offset (1) + " ";
return this.account.handle.offset (1);
}
}

public override async ListModel suggest (GtkSource.CompletionContext context, Cancellable? cancellable) throws Error {
var word = context.get_word ();

public override async ListModel suggest (string word, Cancellable? cancellable) throws Error {
var req = API.Account.search (word);
yield req.await ();

Expand Down
6 changes: 2 additions & 4 deletions src/Dialogs/Composer/Completion/HashtagProvider.vala
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ public class Tuba.HashtagProvider: Tuba.CompletionProvider {
}

public override string? get_typed_text () {
return this.tag.name + " ";
return this.tag.name;
}
}

public override async ListModel suggest (GtkSource.CompletionContext context, Cancellable? cancellable) throws Error {
var word = context.get_word ();

public override async ListModel suggest (string word, Cancellable? cancellable) throws Error {
var req = API.Tag.search (word);
yield req.await ();

Expand Down

0 comments on commit 36d1334

Please sign in to comment.