Community
    • Login

    PythonScript: editor.setProperty() and editor.getProperty()

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    12 Posts 3 Posters 318 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Alan KilbornA
      Alan Kilborn
      last edited by

      I’m not big on doing this, but I’ve seen it done in some scripts: You want to save something that is relevant to the current tab. By doing editor.setProperty('foo', 'value'), then “foo” is only defined for the current tab; if the user switches to another tab, then editor.getProperty('foo') wouldn’t have “value” assigned to it. If the user comes back to the original tab, the “foo” again has its “value”.

      So I was trying this (PythonScript console):

      >>> editor.getProperty('fold')
      '0'
      

      Ok, good, this is a known property (used by the lexer).

      Next:

      >> editor.setProperty('fold', '1')
      
      >>> editor.getProperty('fold')
      '1'
      

      Again, all good, I can set the property.

      Next:

      >>> editor.setProperty('foo', '1')
      
      >>> editor.getProperty('foo')
      ''
      

      So…no good. I can’t retrieve the value I set. Of course, “foo” isn’t a known lexer property, just a “general purpose value”, but I know I’ve seen this done in other scripts.

      Does anyone have any idea about this?

      EkopalypseE 1 Reply Last reply Reply Quote 0
      • EkopalypseE
        Ekopalypse @Alan Kilborn
        last edited by

        @Alan-Kilborn

        As far as I remember, this does no longer work due to changes from scintilla.

        Alan KilbornA 1 Reply Last reply Reply Quote 1
        • Alan KilbornA
          Alan Kilborn @Ekopalypse
          last edited by

          @Ekopalypse said in PythonScript: editor.setProperty() and editor.getProperty():

          this does no longer work due to changes from scintilla

          Ah, maybe stopped working when Lexilla split off from Scintilla…

          EkopalypseE 1 Reply Last reply Reply Quote 0
          • EkopalypseE
            Ekopalypse @Alan Kilborn
            last edited by

            @Alan-Kilborn

            I’m not sure, but I have something in my head that was done deliberately.

            1 Reply Last reply Reply Quote 0
            • Alan KilbornA
              Alan Kilborn
              last edited by

              Any other ideas on how to have a custom bit of data “tag along” with each tab?

              (I could of course have a Python dict, keyed on “buffer id”, I suppose…)

              EkopalypseE 1 Reply Last reply Reply Quote 0
              • EkopalypseE
                Ekopalypse @Alan Kilborn
                last edited by

                @Alan-Kilborn said in PythonScript: editor.setProperty() and editor.getProperty():

                (I could of course have a Python dict, keyed on “buffer id”, I suppose…)

                I think that’s the best solution.

                EkopalypseE 1 Reply Last reply Reply Quote 1
                • EkopalypseE
                  Ekopalypse @Ekopalypse
                  last edited by

                  but if I remember correctly, buffer IDs are reused, so you have to make sure you remove them from the dict once the file is closed. Hmm … but … what happens to cloned documents …?

                  Alan KilbornA CoisesC 2 Replies Last reply Reply Quote 0
                  • Alan KilbornA
                    Alan Kilborn @Ekopalypse
                    last edited by Alan Kilborn

                    @Ekopalypse said in PythonScript: editor.setProperty() and editor.getProperty():

                    what happens to cloned documents

                    Cloned docs share buffer id value, so just more code needed to do things correctly.

                    1 Reply Last reply Reply Quote 1
                    • CoisesC
                      Coises @Ekopalypse
                      last edited by

                      @Ekopalypse said in PythonScript: editor.setProperty() and editor.getProperty():

                      but if I remember correctly, buffer IDs are reused, so you have to make sure you remove them from the dict once the file is closed. Hmm … but … what happens to cloned documents …?

                      I had to deal with that in Columns++ (though in C++, not Python).

                      Adding to the confusion is that Scintilla notifications have a Scintilla document ID but no Notepad++ buffer ID, and Notepad++ notifications for opening and closing files have a buffer ID but no document ID.

                      I remember a lot of trial and error working it out, and I’m still not certain that it’s bullet-proof because there is no documentation of how it expected to work, and past a point, I just get lost trying to read Notepad++ code.

                      I know I wound up with what I consider messy code. I have a map (like a Python dict) from document IDs to structures which contain the buffer ID along with the information I need to keep per document, and some code processing Notepad++ messages that came from just watching messages until I was as sure I was going to get that I had discerned a pattern.

                      What I wound up with removes the entry in the map if a file is opened with the same buffer ID as an existing file or if a file is closed and the buffer ID can’t be found in either view; skips NPPN_BUFFERACTIVATED processing between NPPN_FILEBEFOREOPEN and NPPN_FILEOPENED; and does new document processing in NPPN_BUFFERACTIVATED if the document ID is not in the map or if the buffer ID in the map for that document ID doesn’t match the one returned by NPPM_GETCURRENTBUFFERID.

                      EkopalypseE 1 Reply Last reply Reply Quote 3
                      • EkopalypseE
                        Ekopalypse @Coises
                        last edited by

                        @Coises

                        Yes, cloned documents are a nightmare from a plugin perspective. When developing my LSP client, I also played around with it a lot and found problems such as missing notifications.
                        I honestly don’t understand the implementation of the concept of a cloned document.
                        I would assume that this only makes sense if the cloned document is practically the page before or after the original document so that you can see more of the code. Arbitrary view in a cloned document doesn’t seem helpful to me.
                        Maybe I just didn’t understand the point behind it but honestly I just hope, that nobody who uses my plugins wants to use cloned documents.

                        CoisesC 1 Reply Last reply Reply Quote 2
                        • CoisesC
                          Coises @Ekopalypse
                          last edited by

                          @Ekopalypse said in PythonScript: editor.setProperty() and editor.getProperty():

                          I honestly don’t understand the implementation of the concept of a cloned document.

                          It’s pretty much straight lifted from how Scintilla works. Scintilla has documents and controls. Notepad++ uses two visible Scintilla controls, one for each view, and one Scintilla document for each file or new, unsaved file.

                          Scintilla keeps visible controls containing the same document up to date. The tricky parts come because while the content is associated with the document, many elements of the display are associated only with the control. (So, in my Elastic tabstops implementation, if a document is visible in both views and tab positions change, I must update both views, since custom tab positions are a property of a Scintilla control, not of a Scintilla document.)

                          You can observe this in vanilla Notepad++ if you open a file, make a multiple selection, open a new tab and then switch back to the first tab. The multiple selection will be gone. That’s because selection is a property of the control, not the document. Notepad++ goes out of its way to save and restore the primary selection or a rectangular selection, but it doesn’t trouble with multiple selections.

                          problems such as missing notifications

                          If it’s Scintilla notifications, I’m fairly sure notifications for things associated with the document (like SCN_MODIFIED) will occur once per document, while those associated with the control (like SCN_UPDATEUI) will occur once per view.

                          Maybe I just didn’t understand the point behind it

                          It’s helpful if there are multiple sections of a document that should correspond in some way (for example, every item listed in an overview should be addressed, in order, in a later section, using exactly the same wording) but it won’t all fit on a single screen.

                          Sometimes it makes it easier to move passages from one part of a document to another if you can see both the source and the target at the same time.

                          EkopalypseE 1 Reply Last reply Reply Quote 5
                          • EkopalypseE
                            Ekopalypse @Coises
                            last edited by

                            @Coises

                            Maybe I just didn’t understand the meaning behind it.

                            So that’s right. :-)

                            It looks like I don’t need to do this kind of work that would benefit from a cloned view, or not often enough. In the cases where I need to jump to different places, I use bookmarks, but I understand that you’d rather have the other area in the second view if you need to do that repeatedly.

                            As for notifications, I’d have to look in my notes, which I don’t have access to at the moment, but from what I remember it was about both npp and scintilla notifications. I suspect this will be one of the issues that will be addressed when everything else is working and the plugin is officially released.

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post
                            The Community of users of the Notepad++ text editor.
                            Powered by NodeBB | Contributors