PythonScript: editor.setProperty() and editor.getProperty()
-
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, theneditor.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?
-
As far as I remember, this does no longer work due to changes from scintilla.
-
@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…
-
I’m not sure, but I have something in my head that was done deliberately.
-
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…)
-
@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.
-
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 …?
-
@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.
-
@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.
-
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.