pythongtkgnomegeditgedit-plugin

Gedit plugin not showing custom context menu items with Python


I'm developing a Gedit plugin in Python using Gtk. The plugin is supposed to add "🔮 Generate" and "📝 Summarize" items to the right-click context menu in the editor. However, the items are not showing up.

Here are related code:

...
    def on_populate_popup(self, view, menu):
        generate_item = Gtk.MenuItem(label="🔮 Generate")
        summarize_item = Gtk.MenuItem(label="📝 Summarize")
        generate_item.connect("activate", self.on_generate_clicked, view)
        summarize_item.connect("activate", self.on_summarize_clicked, view)
        menu.append(Gtk.SeparatorMenuItem())
        menu.append(generate_item)
        menu.append(summarize_item)
        menu.show_all()
...

I've confirmed the plugin loads and activates, but the menu items don't appear when right-clicking in the editor. Any ideas on what I might be missing?

Here is the related snap: https://imgur.com/AVYDmH2

And here is the full code: https://github.com/maifeeulasad/gedit-localllama/blob/e4bfb2a909924a2d71a0414eadee9d2880a4b8ef/geditlocalllama.py


Solution

  • On my Linux Mint 22 problem makes isinstance(file, Gio.File) because print(file) shows it is GtkSource.File and it exits _connect_doc before it runs connect("populate-popup",...). (I checked it with print()).

    Problem makes also tab = self.window.get_tab_from_location(file) because it can't work with GtkSource.File (it needs Gio.File).

    I can see items in menu if I skip all _connect_doc and use only

    def on_tab_added(self, window, tab):
        #doc = tab.get_document()
        #self._connect_doc(doc)
    
        view = tab.get_view()
        #print('[DEBUG] view:', view)
    
        if view and view not in self._handler_ids:
            #print('[DEBUG] populate-popup')
            handler_id = view.connect("populate-popup", self.on_populate_popup)
            self._handler_ids[view] = handler_id
    

    enter image description here


    BTW: problem makes emoji in

    print(f"🔮 Generate clicked with: {selected_text}")
    

    because Gedit tries to convert it to ascii before sending to console.


    Command gedit --version gives me gedit - Version 46.2