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.mul...@telenet.be <kristof.mul...@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" <kukovecma...@hotmail.com>
Aan: "QScintilla" <qscintilla@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-boun...@riverbankcomputing.com> on behalf of 
kristof.mul...@telenet.be <kristof.mul...@telenet.be>
Sent: Wednesday, October 12, 2016 5:33 PM
To: qscintilla@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@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/qscintilla

#-------------------------------------------------------------------------
# qsci_simple_pythoneditor.pyw
#
# QScintilla sample with PyQt
#
# Eli Bendersky (eli...@gmail.com)
# This code is in the public domain
#-------------------------------------------------------------------------
import sys

import sip
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qsci import QsciScintilla, QsciLexerPython, QsciScintillaBase


class SimplePythonEditor(QsciScintilla):
    ARROW_MARKER_NUM = 8

    def __init__(self, parent=None):
        super(SimplePythonEditor, self).__init__(parent)

        # Set the default font
        font = QFont()
        font.setFamily('Courier')
        font.setFixedPitch(True)
        font.setPointSize(10)
        self.setFont(font)
        self.setMarginsFont(font)

        # Margin 0 is used for line numbers
        fontmetrics = QFontMetrics(font)
        self.setMarginsFont(font)
        self.setMarginWidth(0, fontmetrics.width("00000") + 6)
        self.setMarginLineNumbers(0, True)
        self.setMarginsBackgroundColor(QColor("#cccccc"))

        # Clickable margin 1 for showing markers
        self.setMarginSensitivity(1, True)
#        self.connect(self,
#            SIGNAL('marginClicked(int, int, Qt::KeyboardModifiers)'),
#            self.on_margin_clicked)
        self.markerDefine(QsciScintilla.RightArrow,
            self.ARROW_MARKER_NUM)
        self.setMarkerBackgroundColor(QColor("#ee1111"),
            self.ARROW_MARKER_NUM)

        # Brace matching: enable for a brace immediately before or after
        # the current position
        #
        self.setBraceMatching(QsciScintilla.SloppyBraceMatch)

        # Current line visible with special background color
        self.setCaretLineVisible(True)
        self.setCaretLineBackgroundColor(QColor("#ffe4e4"))

        # Set Python lexer
        # Set style for Python comments (style number 1) to a fixed-width
        # courier.
        #

#        lexer = QsciLexerPython()
#        lexer.setDefaultFont(font)
#        self.setLexer(lexer)

        text = bytearray(str.encode("Arial"))    
        self.SendScintilla(QsciScintilla.SCI_STYLESETFONT, 1, text)

        # Don't want to see the horizontal scrollbar at all
        # Use raw message to Scintilla here (all messages are documented
        # here: http://www.scintilla.org/ScintillaDoc.html)
        self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)

        # not too small
        self.setMinimumSize(600, 450)
        
        # Hotspot Example
        def hotspot_click(position, modifiers):
            print("Hotspot click at position: " + str(position))
        self.SCN_HOTSPOTCLICK.connect(hotspot_click)
        
    
    def init_hotspot(self):
        # Style the text with the hotspot.
        # You have to know the starting index and the length of the word
        # you wish to be clickable! In this example we'll make the class 
        # definition name 'SimplePythonEditor' clickable.
        index_from = self.text().find("SimplePythonEditor")
        length = len("SimplePythonEditor")
        self.style_hotspot(index_from, length, color=0xffff00)
    
    def style_hotspot(self, index_from, length, color=0xff0000):
        """Style the text from/to with a hotspot"""
        #Use the scintilla low level messaging system to set the hotspot
        style_number = 2
        self.SendScintilla(QsciScintillaBase.SCI_STYLESETHOTSPOT, style_number, True)
        self.SendScintilla(QsciScintillaBase.SCI_SETHOTSPOTACTIVEFORE, True, color)
        self.SendScintilla(QsciScintillaBase.SCI_SETHOTSPOTACTIVEUNDERLINE, True)
        self.SendScintilla(QsciScintillaBase.SCI_STARTSTYLING, index_from, style_number)
        self.SendScintilla(QsciScintillaBase.SCI_SETSTYLING, length, style_number)

    def on_margin_clicked(self, nmargin, nline, modifiers):
        # Toggle marker for the line the margin was clicked on
        if self.markersAtLine(nline) != 0:
            self.markerDelete(nline, self.ARROW_MARKER_NUM)
        else:
            self.markerAdd(nline, self.ARROW_MARKER_NUM)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    editor = SimplePythonEditor()
    editor.show()
    editor.setText(open(sys.argv[0]).read())
    editor.init_hotspot()
    editor.init_hotspot()
    app.exec_()
_______________________________________________
QScintilla mailing list
QScintilla@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/qscintilla

Reply via email to