Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Sublime Text 2 like completion in Python
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.