[QScintilla] Clickable functions and variables

Matic Kukovec kukovecmatic at hotmail.com
Wed Oct 12 23:19:35 BST 2016


Hi Kristof,


Please, no Mr., just call me Matic.

Yes, you can use my responses on StackOverflow, I do not care about the points.

Thanks for mentioning me in the answer!

Now:


1.) I accidentally  left the 'send_scintilla = ' line in, just remove it. The code in my editor is 'send_scintilla = self.SendScintilla', then send_scintilla is used, to save me some typing.


2.) To connect to the signal use the code below in the QScintilla editors __init__ function:

def hotspot_click(position, modifiers):

    print("Hotspot click!")


self.SCN_HOTSPOTRELEASECLICK.connect(hotspot_click)

Check the attached example. I just added the hotspot code to your StackOverflow example and disabled the lexer. Hover the mouse over the SimplePythonEditor word in line 18 and click on it.

When you click on it, it will print to the console the index you clicked on.

!! Note that for this example to work, YOU HAVE TO DISABLE THE LEXER, as hotspots should be styled by the lexer if you are using it with your QScintilla editor !!


3.) As λuser<http://stackoverflow.com/users/6541288/%ce%bbuser> already mentioned, you have to parse the text for the functions yourself and then style the hotspots manually. QScintilla has no functionality to parse code, it only styles the text according to predefined tokens. This is done in the underlying C++ code for the built-in lexers like C or Python.

If you want to make functions clickable and have syntax highlighting at the same time, you have to make your custom lexer by inheriting from the QsciLexerCustom class and writing you custom styleText function.

Check my editor's Oberon lexer and its styleText function in the lexers.py module (link<https://github.com/matkuki/ExCo/blob/master/lexers.py#L280>). This function is called everytime you change the text in the editor. In the Oberon lexer, the text is just split using a list of separator characters into tokens, then the tokens are iterated over and styled according to what type of token it is. Here is where you would have to add your custom hotspot styling code.

So if you're parsing C code for your GNEB editor, your custom lexer's styleText function would have to do the following:

- parse the editor text for C functions and variables

- tokenize the editor text (for each token you need it's starting index and length)

- loop over the tokens and style them either normally or if the token is a C function or variable as a hotspot

You also have to add a function that will handle the clicks on functions and variables.


I know it probably looks complicated looking at the code, but after a while you'll get the hang of how the lexers work. If you need more help with any detail, just ask and I'll help if I can.


The other thing you can do is what I did in Ex.Co.. Instead of making functions and variables clickable, parse the code and show a list on the side with all the functions and variables in it. Then when you click on a function or variable in the list, the editor jumps to the function definition.



As for GNEB, it a great idea! At one point I wanted to add in-built support for the SDCC compiler into Ex.Co., but simply couldn't find the time.

I looked at your website a couple of days ago, but couldn't find any references if you will support 16-bit PIC's? I will try GNEB this week and mail you.


I'm from Slovenia, so yes, we're in the same timezone.


Hope this info helps.


Matic


________________________________
From: kristof.mulier at telenet.be <kristof.mulier at telenet.be>
Sent: Wednesday, October 12, 2016 9:58 PM
To: Matic Kukovec
Subject: Re: [QScintilla] Clickable functions and variables

Dear Mr. Kukovec,

About QScintilla
--------------------------
First of all, a big thank you for your help. Your answer is very useful. I have taken the freedom to post it on StackOverflow, with of course your name mentioned. If you would like to, you can post it yourself (and I'll remove my post), such that you can earn the StackOverflow points you deserve ;-)
Anyway, a big big thank you!
Could you take a quick look at the StackOverflow post?

http://stackoverflow.com/questions/40002373/qscintilla-based-text-editor-in-pyqt5-with-clickable-functions-and-variables/40006957#40006957

I have added a few questions below the post, to ask for clarification of a few things in your answer. I lack some experience in QScintilla, so certain things are a bit hard for me to grasp.

About your project
-----------------------------
I have downloaded the IDE from your github account, and I am truly amazed. This is a wonderful work. You have certainly spent a lot of time on it!
Could you take a quick look at my project?
www.gneb.io<http://www.gneb.io>
I am making an IDE that is targeted at microcontroller programming in C. What do you think about it?

I hope we could exchange some thoughts. Do you also live in the European timezone? I'm from Belgium :-)

Kind greetings,

Kristof





________________________________
Van: "Matic Kukovec" <kukovecmatic at hotmail.com>
Aan: "QScintilla" <qscintilla at riverbankcomputing.com>
Verzonden: Woensdag 12 oktober 2016 20:08:55
Onderwerp: Re: [QScintilla] Clickable functions and variables


Hey Kristof,


Hotspots make text clickable. You have to style it manualy using the QScintilla.SendScintilla function.

Example function I used in my editor Ex.Co. (https://github.com/matkuki/ExCo):


def style_hotspot(self, index_from, length, color=0xff0000):
            """Style the text from/to with a hotspot"""
            send_scintilla =
            #Use the scintilla low level messaging system to set the hotspot
            self.SendScintilla(PyQt4.Qsci.QsciScintillaBase.SCI_STYLESETHOTSPOT, 2, True)
            self.SendScintilla(PyQt4.Qsci.QsciScintillaBase.SCI_SETHOTSPOTACTIVEFORE, True, color)
            self.SendScintilla(PyQt4.Qsci.QsciScintillaBase.SCI_SETHOTSPOTACTIVEUNDERLINE, True)
            self.SendScintilla(PyQt4.Qsci.QsciScintillaBase.SCI_STARTSTYLING, index_from, 2)
            self.SendScintilla(PyQt4.Qsci.QsciScintillaBase.SCI_SETSTYLING, length, 2)


This makes text in the QScintilla editor clickable when you hover the mouse over it.

The number 2 in the above functions is the hotspot style number.

To catch the event that fires when you click the hotspot, connect to these signals:


QScintilla.SCN_HOTSPOTCLICK
QScintilla.SCN_HOTSPOTDOUBLECLICK
QScintilla.SCN_HOTSPOTRELEASECLICK



For more details look at Scintilla hotspot documentation:

http://www.scintilla.org/ScintillaDoc.html#SCI_STYLESETHOTSPOT

and QScintilla hotspot events:

http://pyqt.sourceforge.net/Docs/QScintilla2/classQsciScintillaBase.html#a5eff383e6fa96cbbaba6a2558b076c0b



Try it. Hope it helps.


Matic




________________________________
From: QScintilla <qscintilla-bounces at riverbankcomputing.com> on behalf of kristof.mulier at telenet.be <kristof.mulier at telenet.be>
Sent: Wednesday, October 12, 2016 5:33 PM
To: qscintilla at riverbankcomputing.com
Subject: [QScintilla] Clickable functions and variables

Dear QScintilla users and developers,

Can you please take a look at this StackOverflow question?

http://stackoverflow.com/questions/40002373/qscintilla-based-text-editor-in-pyqt5-with-clickable-functions-and-variables

I am still trying to build an IDE in PyQt5, and use QScintilla for the syntax highlighting. I wonder if certain features are available in QScintilla. The official documentation is way too short.

Kind greetings,

Kristof Mulier

PS: The IDE I am building can be found on my website: www.gneb.io<http://www.gneb.io>


_______________________________________________
QScintilla mailing list
QScintilla at riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/qscintilla

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/qscintilla/attachments/20161012/e10721e1/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: hotspot_example.py
Type: text/x-python
Size: 4368 bytes
Desc: hotspot_example.py
URL: <https://www.riverbankcomputing.com/pipermail/qscintilla/attachments/20161012/e10721e1/attachment-0001.py>


More information about the QScintilla mailing list