Skip to content

Commit

Permalink
Update construction of completion provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Wootten authored and Jeremy Wootten committed Jan 31, 2025
1 parent 6056e9a commit d1c3f7a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
61 changes: 33 additions & 28 deletions plugins/word-completion/completion-provider.vala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright 2024 elementary, Inc. <https://elementary.io>
* Copyright (c) 2013 Mario Guerriero <[email protected]>
*
* This is a free software; you can redistribute it and/or
Expand All @@ -18,37 +19,50 @@
*
*/

public class Scratch.Plugins.CompletionProvider : Gtk.SourceCompletionProvider, Object {
public string name;
public int priority;
public class Scratch.Plugins.CompletionProvider : Gtk.SourceCompletionProvider, GLib.Object {
private const int MAX_COMPLETIONS = 10;
public string name { get; construct; }
public int priority { get; construct; }
public int interactive_delay { get; construct; }
public Gtk.SourceCompletionActivation activation { get; construct; }

public const string COMPLETION_END_MARK_NAME = "ScratchWordCompletionEnd";
public const string COMPLETION_START_MARK_NAME = "ScratchWordCompletionStart";

private Gtk.TextView? view;
private Gtk.TextBuffer? buffer;
private Euclide.Completion.Parser parser;
public Gtk.TextView? view { get; construct; }
public Euclide.Completion.Parser parser { get; construct; }

private unowned Gtk.TextBuffer buffer {
get {
return view.buffer;
}
}

private Gtk.TextMark completion_end_mark;
private Gtk.TextMark completion_start_mark;
private string current_text_to_find = "";

public signal void can_propose (bool b);

public CompletionProvider (Scratch.Plugins.Completion completion) {
this.view = completion.current_view as Gtk.TextView;
this.buffer = completion.current_view.buffer;
this.parser = completion.parser;
Gtk.TextIter iter;
buffer.get_iter_at_offset (out iter, 0);
completion_end_mark = buffer.create_mark (COMPLETION_END_MARK_NAME, iter, false);
completion_start_mark = buffer.create_mark (COMPLETION_START_MARK_NAME, iter, false);
}
public CompletionProvider (
Euclide.Completion.Parser _parser,
Scratch.Services.Document _doc
) {

public string get_name () {
return this.name;
Object (
parser: _parser,
view: _doc.source_view,
name: _("%s - Word Completion").printf (_doc.get_basename ())
);
}

public int get_priority () {
return this.priority;
construct {
interactive_delay = (int) Completion.INTERACTIVE_DELAY;
activation = INTERACTIVE | USER_REQUESTED;
Gtk.TextIter iter;
view.buffer.get_iter_at_offset (out iter, 0);
completion_end_mark = buffer.create_mark (COMPLETION_END_MARK_NAME, iter, false);
completion_start_mark = buffer.create_mark (COMPLETION_START_MARK_NAME, iter, false);
}

public bool match (Gtk.SourceCompletionContext context) {
Expand Down Expand Up @@ -85,15 +99,6 @@ public class Scratch.Plugins.CompletionProvider : Gtk.SourceCompletionProvider,
return true;
}

public Gtk.SourceCompletionActivation get_activation () {
return Gtk.SourceCompletionActivation.INTERACTIVE |
Gtk.SourceCompletionActivation.USER_REQUESTED;
}

public int get_interactive_delay () {
return 0;
}

public bool get_start_iter (Gtk.SourceCompletionContext context,
Gtk.SourceCompletionProposal proposal,
out Gtk.TextIter iter) {
Expand Down
6 changes: 3 additions & 3 deletions plugins/word-completion/plugin.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/

public class Scratch.Plugins.Completion : Peas.ExtensionBase, Peas.Activatable {
public const uint INTERACTIVE_DELAY = 500;

public Object object { owned get; construct; }

private List<Gtk.SourceView> text_view_list = new List<Gtk.SourceView> ();
Expand Down Expand Up @@ -88,9 +90,7 @@ public class Scratch.Plugins.Completion : Peas.ExtensionBase, Peas.Activatable {
if (text_view_list.find (current_view) == null)
text_view_list.append (current_view);

var comp_provider = new Scratch.Plugins.CompletionProvider (this);
comp_provider.priority = 1;
comp_provider.name = provider_name_from_document (doc);
var comp_provider = new Scratch.Plugins.CompletionProvider (parser, doc);

try {
current_view.completion.add_provider (comp_provider);
Expand Down

0 comments on commit d1c3f7a

Please sign in to comment.