public
Last active

Sublime Text 2 like completion in Python

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
from gi.repository import Gtk, Gio
import re
 
text = """
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(400, 300);
glutCreateWindow("Hello World");
//glutIdleFunc(&update_fade_factor);
glutIdleFunc(&update_timer);
glutDisplayFunc(&render);
 
glewInit();
if (!GLEW_VERSION_2_0) {
fprintf(stderr, "OpenGL 2.0 not available\n");
return 1;
}
if
(!make_resources(argc >= 2 ? argv[1] : "hello-gl.v.glsl")) {
fprintf(stderr, "Failed to load resources\n");
return 1;
}
 
glutMainLoop();
return 0;
"""
 
words = [w for w in re.split(r'\W+', text) if w]
max_length = max(words, key=len)
print words
 
class CompletionApp(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self, application_id="apps.test.completion",
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.connect("activate", self.on_activate)
def on_activate(self, data=None):
entry = Gtk.Entry()
entrybuffer = entry.get_buffer()
entrybuffer.connect("inserted-text", self.on_entry_inserted_text)
entrybuffer.connect("deleted-text", self.on_entry_deleted_text)
 
scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_hexpand(True)
scrolledwindow.set_vexpand(True)
textview = Gtk.TextView()
self.textbuffer = textview.get_buffer()
self.textbuffer.set_text("\n".join(words))
scrolledwindow.add(textview)
 
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
vbox.pack_start(entry, False, True, 5)
vbox.pack_start(scrolledwindow, True, True, 5)
 
window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
window.set_title("Word completion")
window.set_default_size(300, 500)
window.set_position(Gtk.WindowPosition.CENTER)
window.add(vbox)
window.show_all()
self.add_window(window)
 
def on_entry_inserted_text(self, buffer, position, chars, n_chars, user_data=None):
self._complete(buffer)
 
def on_entry_deleted_text(self, buffer, position, n_chars, user_data=None):
self._complete(buffer)
 
def _complete(self, buffer):
search = buffer.get_text()
re_string = ""
for letter in search.lower():
re_string += "%s\w*" % letter
text = ""
for word in words:
if re.match(re_string, word.lower()):
text += word+"\n"
self.textbuffer.set_text(text)
if __name__ == "__main__":
app = CompletionApp()
app.run(None)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.