📜 ⬆️ ⬇️

QScintilla: Deeper and Deeper

Good day!

I almost forgot. The first and second article of the cycle.

This is me again, with my new article. Today I will tell you how to work with qscintilla almost at full power. And more specifically: I will show why it is useful to inherit QsciScintilla with your class, rather than use its copy and tell you how to write some kind of auto-complete code for your editor.
')
I’ll say right away that the article will be small, but I will show you how to work with all this rubbish using the example of my still poorly developing Web Development IDE Galactica project.

Let's start with the second. What do we want to do? We want to make an editor that autocomplete the text. And it is obvious that the best option for this is Ctrl + Space. But here the file is waiting for us - after the coveted bind, the NULL character is inserted into the editor. For me, the obvious solution is to inherit QsciScintilla and redefine keyPressEvent ().

I will bring the listing keyPressEvent ():
void MainEditor::keyPressEvent(QKeyEvent *e) { if((e->modifiers() == Qt::CTRL) && (e->key() == Qt::Key_Space)) { // Ctrl+<Space> autoCompleteFromAll(); return; //    NULL } //... QsciScintilla::keyPressEvent(e); } 


Fine. Now when we click on Ctrl + Space - we call autocomplete text, and only. And now I will bring the constructor listing:
 MainEditor::MainEditor(QWidget *parent) : QsciScintilla(parent), lexerHTML(new QsciLexerHTML) { //! To accept cyrillics setUtf8(true); setLexer(lexerHTML); //    HTML, PHP //... //! Autocompleting setAutoCompletionSource(QsciScintilla::AcsAll); //     setAutoCompletionCaseSensitivity(true); //     setAutoCompletionReplaceWord(true); //     ( ) setAutoCompletionUseSingle(AcusExplicit); //     ,    setAutoCompletionThreshold(1); //   1   //... } 


This provides us with autocomplete HTML and PHP. Good, yes? Everything is really that simple. But not interesting. Now, if you could set up autocomplete for your lexer ... And yet, it’s not difficult:
 MyLexer::MyLexer(QObject *parent) : QsciLexerCustom(parent) { //... //   QsciAPIs *api = new QsciAPIs(this); //     foreach(const QString &word, listWithKeywords) { //     api->add(word); //    } api->prepare(); //  setAPIs(api); } 


And here, I would like to pause. Here we create an API object from the current API and supplement it with words from our list. I do not list the code for the list. Then we just set the new API.

In practice, this should all be greatly enhanced, for example, analyzing the code and supplementing it as it is written. For example complementing new variables. Those. Add UserList variables from AST (Abstract Syntax Tree). But this is only in practice).

Here’s what my IDE editor looks like:



And by the way, here is its repository .

This concludes the series of articles about QScintilla. I apologize in advance if the article turned out to be weak. It's just all I wanted, but I didn't have time to tell.

Thanks for attention.

Source: https://habr.com/ru/post/144855/


All Articles