no python keywords in autocomplete suggestions using QScintilla and PyQt

724 Views Asked by At

I was expecting autocomplete suggestions of Python keywords (i.e. for, range, lambda) and hoping for autocomplete suggestions with standard library modules and functions (i.e. sys, os.path, etc.) when setting up an api with the QsciLexerPython lexer, but in the code example below, compressed and updated from this website, the only auto-completion that happens are the four strings added to the list.

Am I doing something wrong, and if so how do I get python keywords to register in the autocomplete? (My main question here of course)

If rather this is the expected behavior, what role does the lexer play in the autocomplete and why are the keywords from the lexer not being assigned to the autocomplete lists in this example?

Lastly, is it possible to have python standard library modules and sub-modules in the autocomplete, and what does that require?

"""Base code originally from: http://kib2.free.fr/tutos/PyQt4/QScintilla2.html"""

import sys
from PyQt5 import QtWidgets, Qsci

app = QtWidgets.QApplication(sys.argv)
editor = Qsci.QsciScintilla()
lexer = Qsci.QsciLexerPython()
editor.setLexer(lexer)

## setup autocompletion
api = Qsci.QsciAPIs(lexer)
api.add("aLongString")
api.add("aLongerString")
api.add("aDifferentString")
api.add("sOmethingElse")
api.prepare()
editor.setAutoCompletionThreshold(1)
editor.setAutoCompletionSource(Qsci.QsciScintilla.AcsAPIs)

editor.show()
editor.setText(open(sys.argv[0]).read())
sys.exit(app.exec_())
2

There are 2 best solutions below

19
On BEST ANSWER

Thanks to ekhumoro for guiding me to the answer. The QsciAPIs class has a load method, and PyQt comes with a set of api files. Below is the code that does proper autocompletion in the manner I was looking for:

"""Base code originally from: http://kib2.free.fr/tutos/PyQt4/QScintilla2.html"""

import sys
import os
import PyQt5
from PyQt5 import QtWidgets, Qsci

app = QtWidgets.QApplication(sys.argv)
editor = Qsci.QsciScintilla()
lexer = Qsci.QsciLexerPython(editor)
editor.setLexer(lexer)

## setup autocompletion
api = Qsci.QsciAPIs(lexer)

# import the desired api file
pyqt_path = os.path.dirname(PyQt5.__file__)
api.load(os.path.join(pyqt_path, "Qt/qsci/api/python/Python-3.6.api"))

api.prepare()
editor.setAutoCompletionThreshold(1)
editor.setAutoCompletionSource(Qsci.QsciScintilla.AcsAll)

editor.show()
editor.setText(open(sys.argv[0]).read())
sys.exit(app.exec_())
4
On

qscintilla does not know the keywords of python nor of the libraries, QsciAPIs hopes that you provide information, in the following part I show some functions that return the keywords and the name of the standard libraries. qscintilla will only autocomplete with the words you provide, if you want an intelligent autocomplete, I recommend you do a logic that recognizes the points or parenthesis and filter the words you provide to QsciAPIs.

import sys
from PyQt5 import QtWidgets, Qsci
import keyword
import pkgutil

app = QtWidgets.QApplication(sys.argv)
editor = Qsci.QsciScintilla()
lexer = Qsci.QsciLexerPython()
editor.setLexer(lexer)

## setup autocompletion
api = Qsci.QsciAPIs(lexer)

for key in keyword.kwlist + dir(__builtins__):
    api.add(key)

for importer, name, ispkg in pkgutil.iter_modules():
    api.add(name)

api.prepare()

editor.setAutoCompletionThreshold(1)
editor.setAutoCompletionSource(Qsci.QsciScintilla.AcsAPIs)

editor.show()
editor.setText(open(sys.argv[0]).read())
sys.exit(app.exec_())