Skip to content

Instantly share code, notes, and snippets.

@centaurialpha
Last active August 20, 2016 22:21
Show Gist options
  • Save centaurialpha/448df0209532ea076adbcc819f46c68c to your computer and use it in GitHub Desktop.
Save centaurialpha/448df0209532ea076adbcc819f46c68c to your computer and use it in GitHub Desktop.
Test calltips in scintilla with Python
# CallTips Python Scintilla
import sys
from PyQt4.QtGui import *
from PyQt4.Qsci import QsciScintilla, QsciLexerPython
CALLTIPS = True
class Lexer(QsciLexerPython):
def __init__(self, p):
super(Lexer, self).__init__(p)
class Editor(QsciScintilla):
def __init__(self):
super(Editor, self).__init__()
self.setAutoIndent(True)
lex = Lexer(self)
self.setLexer(lex)
self._calltip = False
if CALLTIPS:
self._calltip = CallTip(self)
def keyPressEvent(self, event):
super(Editor, self).keyPressEvent(event)
if event.text() == '(' and self._calltip:
self._calltip.showtip()
elif event.text() == ')' and self._calltip:
self._calltip.close()
class CallTip:
def __init__(self, editor):
self._editor = editor
def close(self):
self._editor.SendScintilla(QsciScintilla.SCI_CALLTIPCANCEL)
def showtip(self):
expression = self.__get_expression()
if not expression:
return
if expression.find('(') != -1:
return
argspec = self.__get_argspec(expression)
if not argspec:
return
pos = self._editor.SendScintilla(QsciScintilla.SCI_GETCURRENTPOS)
self._editor.SendScintilla(QsciScintilla.SCI_CALLTIPSHOW,
pos, argspec.encode())
def __get_expression(self):
line, index = self._editor.getCursorPosition()
exp = self._editor.text(line)[:index].split()[-1][:-1]
return exp
def __get_argspec(self, exp):
obj = self.__get_entity(exp)
string_signature = ""
try:
obj.__call__
except AttributeError:
return string_signature
doc = obj.__doc__
if doc:
lines = []
for line in doc.splitlines():
line = line.strip()
if not line:
break
lines.append(line)
string_signature = '\n'.join(lines)
return string_signature
def __get_entity(self, exp):
if exp:
namespace = sys.modules.copy()
try:
return eval(exp, namespace)
except Exception:
return None
if __name__ == "__main__":
app = QApplication([])
w = Editor()
w.show()
w.resize(700, 500)
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment