Menu

Trouble using annotationSetStyles(line,styles)

Help
2017-07-12
2017-07-12
  • David Hansen

    David Hansen - 2017-07-12

    I am trying to use annotationSetStyles() to create annotations using different styles, e.g. http://www.scintilla.org/annotations.png

    I thought it would be similar to adding styled text to the console, but the following is not working:

    editor.setLexer(LEXER.CONTAINER)
    editor.styleSetFore(20,(255,255,0))    
    editor.styleSetBack(20,(0,0,0))
    
    editor.annotationClearAll()
    editor.annotationSetVisible(2)
    
    editor.annotationSetText(1, 'Boo') 
    editor.annotationSetStyle(1, 20)
    editor.annotationSetStyles(2, Cell('Hoo\n',[20]))
    

    It shows the annotation for line 1 (Boo), but nothing for line 2 (Hoo). No errors in appear in the console, but the annotationSetStyles() statement has no effect.

    Can anyone explain what I'm missing?

     
  • CFrank

    CFrank - 2017-07-12

    I haven't used Cell class (to be honest wasn't even aware that it exists) but I use it
    like this

    editor.annotationClearAll()
    editor.annotationSetText(1,'hello\nworld')
    editor.annotationSetStyles(1, "\1\1\1\2\2\2\3\3\3\4\5")
    editor.annotationSetVisible(2)
    

    Hope this is helpful for you.
    Be aware that the document needs to get the focus to make it work.

    Cheers
    Claudia

     

    Last edit: CFrank 2017-07-12
  • David Hansen

    David Hansen - 2017-07-12

    Claudia,
    Thank you for your prompt reply, and putting me on the right path. I've got my demo working now with your help. :D

    BTW, here is the thread detailing how to add styled text to the console using Cell().

    I'm still curious though whether its possible to use Cell() with annotationSetStyles()... Perhaps Mr Brotherstone might chime in?

     
  • CFrank

    CFrank - 2017-07-12

    David,
    I don't think that Cell can be used in such a case as the Cell class seems not to provide any
    functions which could convert the input and in addition scintilla documentation states,
    that setting the text must be prior to setting styles. Let's see what Dave thinks.

    My strategy is the following

    ERROR_MSG_ID = 20
    WARNING_MSG = 21
    INFO_MSG = 22
    editor.styleSetFore(ERROR_MSG_ID,(255,0,0))    
    editor.styleSetBack(ERROR_MSG_ID,(0,0,0))
    editor.styleSetFore(WARNING_MSG,(255,255,0))    
    editor.styleSetBack(WARNING_MSG,(0,0,0))
    editor.styleSetFore(INFO_MSG,(128,255,0))    
    editor.styleSetBack(INFO_MSG,(0,0,0))
    
    def SetAnnotation(text, line):
        _text = u''
        styles = u''
        for l,s in sorted(text, key=lambda tup: tup[1]):
            _text += l
            styles += '{}'.format(chr(s))*len(l)
    
        editor.annotationSetText(line, _text[:-1])
        editor.annotationSetStyles(line, styles)
        editor.annotationSetVisible(2)
    
    annotation_text = [(u'error line\n', ERROR_MSG_ID), 
                       (u'warning line\n', WARNING_MSG), 
                       (u'info line\n', INFO_MSG),
                       (u'another error line\n', ERROR_MSG_ID), 
                       (u'and another warning line\n', WARNING_MSG),
                      ]
    
    editor.annotationClearAll()
    editor.annotationSetVisible(0)
    
    SetAnnotation(annotation_text, 20) # example annotation message at line 19
    

    Of course annotation_text gets created by a message parser automatically.

    Cheers
    Claudia

     
  • David Hansen

    David Hansen - 2017-07-14

    Yep, you're right, from the Scintilla Docs : "The text must be set first as it specifies how long the annotation is so how many bytes of styling to read."

    And a big "thank you" for sharing your example Claudia, I hadn't been able to find any for annotationSetStyles.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.