Skip to content

Adding completion to gscb #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions sourcecodebrowser/completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from gi.repository import GObject, Gtk, GtkSource, Gedit

class Proposal(GObject.Object, GtkSource.CompletionProposal):
__gtype_name__ = "GeditSourceCodeProposal"


def __init__(self, tag):
GObject.Object.__init__(self)
self._tag = tag

# Interface implementation
def do_get_markup(self):
return self._tag.name

"""
def do_get_info(self):
return self._tag.name
"""

class Provider(GObject.Object, GtkSource.CompletionProvider):
__gtype_name__ = "GeditSourceCodeProvider"

def __init__(self, sourcetree):
GObject.Object.__init__(self)
self.name = 'SourceCodeProvider'
self.sourcetree = sourcetree
"""
theme = Gtk.IconTheme.get_default()
f, w, h = Gtk.icon_size_lookup(Gtk.IconSize.MENU)

try:
self.icon = theme.load_icon(Gtk.STOCK_JUSTIFY_LEFT, w, 0)
except:
self.icon = None
"""
self.icon = self.sourcetree.get_pixbuf('source-code-browser')

def get_word(self, context):
it = context.get_iter()

if it.starts_word() or it.starts_line() or not it.ends_word():
return None

start = it.copy()

if start.backward_word_start():
return start.get_text(it)
else:
return None

def do_match(self, context):
return True

def do_populate(self, context):
word = self.get_word(context)
proposals = []
print word
for tag in self.sourcetree.tags:
if not word or tag.name.startswith(word):
proposals.append(Proposal(tag))
context.add_proposals(self, proposals, True)

def do_get_name(self):
return self.name

def do_activate_proposal(self, proposal, piter):
buf = piter.get_buffer()
buf.insert_at_cursor(proposal._tag.name, -1)
return True

def do_get_icon(self):
return self.icon

def do_get_activation(self):
return GtkSource.CompletionActivation.USER_REQUESTED

# ex:ts=8:et:
12 changes: 10 additions & 2 deletions sourcecodebrowser/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import tempfile
import ctags
import completion
from gi.repository import GObject, GdkPixbuf, Gedit, Gtk, PeasGtk, Gio

logging.basicConfig()
Expand Down Expand Up @@ -179,9 +180,9 @@ def load(self, kinds, tags, uri):
# TODO: We need to go at least one more level to deal with the inline
# classes used in many python projects (eg. Models in Django)
# Recursion would be even better.

self.tags = tags
# sort
if self.sort_list:
if self.sort_list:
self._store.set_sort_column_id(1, Gtk.SortType.ASCENDING)

# expand
Expand Down Expand Up @@ -393,8 +394,11 @@ def _load_active_document_symbols(self):
self._is_loaded = False
# do not load if not the active tab in the panel
panel = self.window.get_side_panel()
#Disabled because completion needs the symbols
"""
if not panel.item_is_active(self._sourcetree):
return
"""

document = self.window.get_active_document()
if document:
Expand Down Expand Up @@ -424,6 +428,10 @@ def _load_active_document_symbols(self):

def on_active_tab_changed(self, window, tab, data=None):
self._load_active_document_symbols()

if not hasattr(self.window.get_active_view(), "sc_completion"):
self.window.get_active_view().get_completion().add_provider(completion.Provider(self._sourcetree))
self.window.get_active_view().sc_completion = True

def on_setting_changed(self, settings, key, data=None):
"""
Expand Down