You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A common way to implement TextChangeListener.is_applicable is to consult the buffer's primary view's settings:
classMyViewEventListener(sublime_plugin.ViewEventListener):
@classmethoddefis_applicable(cls, settings):
return# ... check some property of the settings ...classMyTextChangeListener(sublime_plugin.TextChangeListener):
@classmethoddefis_applicable(cls, buffer):
returnMyViewEventListener.is_applicable(buffer.primary_view().settings())
For instance, this pattern was mentioned and upvoted here.
However, ST does not re-check TextChangeListener.is_applicable when the settings change, even though it does re-check ViewEventListener.is_applicable when the settings change.
This is a problem for plugins that only want text change events on certain kinds of syntax. Consider e.g. Calvin-L/sublime-coq-plugin#9, in which the following sequence of actions thwarts the plugin's attempts to listen to text changes in Coq files:
make a new file
set the syntax to Coq
interact with the plugin --- does not work properly since ST only checks TextChangeListener.is_applicable after step 1, when the file is not yet a Coq file
Steps to reproduce
Create text_change_plugin_example.py in <ST>/Packages/User/:
importsublimeimportsublime_pluginclassMyViewEventListener(sublime_plugin.ViewEventListener):
@classmethoddefis_applicable(cls, settings):
# only listen to events on Python3 filesprint(f"Checking applicability with syntax={settings.get('syntax')}")
returnsettings.get("syntax", "").endswith("/Python.sublime-syntax")
classMyTextChangeListener(sublime_plugin.TextChangeListener):
@classmethoddefis_applicable(cls, buffer):
returnMyViewEventListener.is_applicable(buffer.primary_view().settings())
defon_text_changed(self, changes):
print(f"Handling {len(changes)} changes")
Then
Create a new file
Set the syntax to Python
Make some modifications
Expected behavior
There should be "Handling X changes" messages in the console.
Actual behavior
There are no "Handling X changes" messages, since MyTextChangeListener was not given a second chance to advertise its applicability after step 2.
Sublime Text build number
4152
Operating system & version
macOS 12.6.8
(Linux) Desktop environment and/or window manager
No response
Additional information
No response
OpenGL context information
No response
The text was updated successfully, but these errors were encountered:
This is intentional. TextChangeListener.is_applicable can change its output due to any number of things; you're expected to use TextChangeListener.attach and detach to manage the listener yourself if more complex behavior is needed.
Ah, gotcha. If that's the official word then I'm prepared to close this and adopt attach/detach in my plugins.
But, if you have time to clarify a little:
In my current understanding, I don't see any correct implementation of TextChangeListener.is_applicable except return True or return False. Every interesting property of a buffer except its id() is mutable---if ST never re-checks is_applicable, then error-prone attach/detach code looks like the only correct way to restrict which buffers are being listened to.
Obviously ST can't re-check is_applicable for every change to every possible property of a buffer or its views, but re-checking on settings changes seems (to me) like a reasonable middle-ground. It would have nice symmetry with how ST already re-checks ViewEventListener.is_applicable on settings changes.
Description of the bug
A common way to implement
TextChangeListener.is_applicable
is to consult the buffer's primary view's settings:For instance, this pattern was mentioned and upvoted here.
However, ST does not re-check
TextChangeListener.is_applicable
when the settings change, even though it does re-checkViewEventListener.is_applicable
when the settings change.This is a problem for plugins that only want text change events on certain kinds of syntax. Consider e.g. Calvin-L/sublime-coq-plugin#9, in which the following sequence of actions thwarts the plugin's attempts to listen to text changes in Coq files:
TextChangeListener.is_applicable
after step 1, when the file is not yet a Coq fileSteps to reproduce
Create
text_change_plugin_example.py
in<ST>/Packages/User/
:Then
Expected behavior
There should be "Handling X changes" messages in the console.
Actual behavior
There are no "Handling X changes" messages, since
MyTextChangeListener
was not given a second chance to advertise its applicability after step 2.Sublime Text build number
4152
Operating system & version
macOS 12.6.8
(Linux) Desktop environment and/or window manager
No response
Additional information
No response
OpenGL context information
No response
The text was updated successfully, but these errors were encountered: