Howto register/unregister words completion provider on buffer change in textview?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Howto register/unregister words completion provider on buffer change in textview?

David C. Rankin
All,

  I have an editor that holds the open files on the left in a
treeview/treemodel and displays the buffer containing each file in a textview
(sourceview) window on the right.

  Each treemodel entry has a struct containing a pointer to its
GtkSourceCompletionWords provider. The word-completion works fine for the
first buffer shown in the window, but when I change files and display a
different buffer in the textview window, I am not able to register/unregister
the current buffer from the words provider and use the new buffer as a source?

  Essentially I create the initial completion object as:

    GtkSourceCompletion *completion;
    GtkSourceCompletionWords *prov_words;

    completion = gtk_source_view_get_completion (view);
    prov_words = gtk_source_completion_words_new (NULL, NULL);
    gtk_source_completion_words_register (prov_words,
                    gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
    gtk_source_completion_add_provider (completion,
                    GTK_SOURCE_COMPLETION_PROVIDER (prov_words), NULL);
    /* store a pointer to the completions words provider as part of
     * the editor instance.
     */
    einst->prov_words = prov_words;

  When displaying a new buffer in the textview, I use the treeview "changed"
signal to display the new buffer, update the window title, etc.. and I tried
to unregister the current buffer from the prov_words provider and then
register the new buffer as the word source with:

    GtkSourceCompletionWords *prov_words = einst->prov_words;

    /* unregister current buffer from words completion provider */
    gtk_source_completion_words_unregister (prov_words,
                                        GTK_TEXT_BUFFER(oldbuf));

    /* register new buffer with words completion provider */
    gtk_source_completion_words_register (prov_words,
                                        GTK_TEXT_BUFFER(newbuf));

  However, I am obviously missing something as the new buffer displayed does
not provide any completion functionality at all.

  Since the completion object (GtkSourceCompletion) is part of the sourceview
(GtkSourceView) and the completion provider (GtkSourceCompletionWords) is part
of the completion object, there should be no need to create another object or
provider as the sourceview window is the same. The only thing that I see that
needs doing is to change the buffer registered as the source of words for the
provider on buffer change -- but so far I have not met with any success...

  Is there something I am missing when changing the buffer in the sourceview
that is needed to change the completion so that word completion continues to
function but simply drawing words from the new buffer instead of the old?

  When I switch focus back to the original buffer -- completion works fine
again, it's something in the switch to the new that I must be missing.

  Help?

--
David C. Rankin, J.D.,P.E.
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Howto register/unregister words completion provider on buffer change in textview?

David C. Rankin
On 09/16/2018 03:21 AM, David C. Rankin wrote:
>   Each treemodel entry has a struct containing a pointer to its
> GtkSourceCompletionWords provider.

Uugh...

That should read the editor instance struct associated with the sourceview has
a pointer to its GtkSourceCompletionWords provider. (the tree model just holds
a struct containing a pointer to its GtkSourceBuffer which is what allows
updating the textview/sourceview window on the right with the new buffer).

So when I click a different file in the treeview, I want to associate that
buffer as the word-source for the GtkSourceCompletionWords provider for the
textview by unregistering the existing buffer as the word-source and
registering the new buffer as the source for the provider in the textview.

--
David C. Rankin, J.D.,P.E.
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Howto register/unregister words completion provider on buffer change in textview?

David C. Rankin
On 09/16/2018 03:53 AM, David C. Rankin wrote:

> On 09/16/2018 03:21 AM, David C. Rankin wrote:
>>   Each treemodel entry has a struct containing a pointer to its
>> GtkSourceCompletionWords provider.
>
> Uugh...
>
> That should read the editor instance struct associated with the sourceview has
> a pointer to its GtkSourceCompletionWords provider. (the tree model just holds
> a struct containing a pointer to its GtkSourceBuffer which is what allows
> updating the textview/sourceview window on the right with the new buffer).
>
> So when I click a different file in the treeview, I want to associate that
> buffer as the word-source for the GtkSourceCompletionWords provider for the
> textview by unregistering the existing buffer as the word-source and
> registering the new buffer as the source for the provider in the textview.
>

Let me bump this thread again and update with the next approach I have taken
that also failed. By way of short background, I have an editor where multiple
files are opened and displayed in a Treeview on the left of the main window.
Selecting a file will display its buffer in the textview, e.g.

    +===================================================+
    |menu                                               |
    +------------+--------------------------------------+
    | file_a     |                                      |
    | file_b     |                                      |
    | file_c     |             TextView                 |
    |            |               with                   |
    |            |          WordCompletion              |
    |            |                                      |
    |            |                                      |
    |            |                                      |
    |            |                                      |
    +------------+--------------------------------------+

I enable word completion within the textview on the first file when the window
is created. What I need to do is be able to make the word completion continue
to work with the new word-provider associated with each file when a new file
is displayed in the textview.

As noted in the earlier post, the completion object is associated with the
Textview itself, but the words provider is associated with each file. Earlier
I attempted to use the same provider, by 'registering' and 'unregistering' the
words contained within a single provider.

In this test, I have created a new words completion provider associated with
each file instance and have attempted to remove the current provider and add
the provider associated with the new buffer to be displayed with the following:

    GList *providers = gtk_source_completion_get_providers (completion);

    while (providers) {
        /* TODO add validation and GError */
        gtk_source_completion_remove_provider (completion,
                    GTK_SOURCE_COMPLETION_PROVIDER (providers->data), NULL);
        providers = providers->next;
    }

    /* register the words in the current buffer for provider */
    if (!inst->prov_words) {
        /* create the words provider */
        inst->prov_words = gtk_source_completion_words_new (NULL, NULL);

        gtk_source_completion_words_register (inst->prov_words,
                    gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
    }
    /* add provider to completion - make sure to unref when done */
    gtk_source_completion_add_provider (completion,
                    GTK_SOURCE_COMPLETION_PROVIDER (inst->prov_words), NULL);


However, the results are the same. Word completion works fine for the first
buffer associated with the textview (say 'file_a' above). Switching buffers
(say to 'file_b'), there is no word-completion triggered at all within the
same textview for the new buffer. However, switching back to the first buffer
(file_a) and word completion begins working fine again.

Anybody have any other ideas how to make word completion continue to work
within a textview when the buffer displayed is changed?

Thanks for any thoughts or ideas you may have.

--
David C. Rankin, J.D.,P.E.
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list