Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on_selection_modified should be called only once and with the view where the selection was modified #2990

Open
evandrocoan opened this issue Sep 19, 2019 · 11 comments

Comments

@evandrocoan
Copy link

Sub-issue from: Multiple event hooks don't work with clones #289

Actual behavior

If I have 10 clones into a view, I get on_selection_modified called 10 times, and each time it always have the primary view as the view parameter.

Expected behavior

If I have 10 clones into a view, the on_selection_modified event should be called only 1 time, with the view on which the selection was modified (not the primary view).

Environment

  • Operating system and version:
    • Windows 10 build 15063 x64
    • Resolution 1920x1080
    • dpi_scale 1.0
    • Sublime Text:
    • Build 3207
    • 64 bit

Related threads

  1. on_selection_modified gets called twice when left clicking on_selection_modified gets called twice when left clicking #1254
  2. Sublime Text cannot call the plugins forwards as on_selection_modified(1) before the plugin goes through the forward plugin_loaded(0) EventHandler hooks (e.g. on_selection_modified) called before plugin_loaded #1508
@deathaxe
Copy link
Collaborator

Each cloned view receives the on_selection_modified() with the correct view object being passed, if the selection was modified in one of them. So it is called 10 times if 10 cloned views into one file exist.

It's up to a plugin to handle or ignore the event for all or the active view only.

class MyListener(sublime_plugin.EventListener):

    def on_selection_modified(self, view):
        if not view.window():
            return
        if view == view.window().active_view():
            print(f"on_selection_modified called for active View {view.id()} : {view.file_name()}.")
        else:
            print(f"on_selection_modified called for inactive View {view.id()} : {view.file_name()}.")

@rwols
Copy link

rwols commented Feb 29, 2020

I doubt this is an excellent solution to the "cloned view" problem. This puts the responsibility in the hands of plugin developers. It's a "gotcha" that's easily overlooked. On the other hand, it's certainly a solution that we can work with.

@wbond
Copy link
Member

wbond commented Feb 29, 2020

Let’s reopen so I can take another look

@wbond wbond reopened this Feb 29, 2020
@deathaxe
Copy link
Collaborator

ST can't know whether a plugin wants to be informed about the active view or all views, so this is propably the most flexible solution.

@deathaxe
Copy link
Collaborator

With regards to #289 it would mean to propably implement a special behavior for on_selection_modified so only that is called for the active view only?

How about adding a ViewEventListener.applies_to_active_view_only()?

@rwols
Copy link

rwols commented Feb 29, 2020

ST can't know whether a plugin wants to be informed about the active view or all views, so this is propably the most flexible solution.

But each view maintains its own selection whether cloned or not. So calling on_selection_modified for a (cloned) view for which the selection was not modified is a bit of a lie IMO ;)

@wbond wbond added this to the Build 4069 milestone Mar 4, 2020
@wbond wbond removed their assignment Mar 4, 2020
@wbond wbond modified the milestones: Build 4069, Build 4070 Mar 27, 2020
@deathaxe
Copy link
Collaborator

deathaxe commented Apr 3, 2020

ST 4070 still fires the event for all clones.

@wbond
Copy link
Member

wbond commented Apr 3, 2020

This branch hasn't been merged yet, I'll update the milestone

@wbond wbond modified the milestones: Build 4070, Build 4071 Apr 3, 2020
@wbond wbond added the R: fixed label Apr 8, 2020
@wbond
Copy link
Member

wbond commented Apr 8, 2020

This should be fixed in 4072

@wbond wbond closed this as completed Apr 8, 2020
@wbond wbond added the T: bug label Apr 8, 2020
@deathaxe
Copy link
Collaborator

deathaxe commented Apr 8, 2020

IMHO, this issue is only partly fixed.

It works, if the caret is moved in one of the views without modification to the content.

But if the content changes, the on_selection_modified event is still fired for both the primary and the cloned view.

It might be logical if the caret of the inactive was moved by the modification, but that's not the case.

Please see how the caret of the primary (left) view is moved to the beginning of the file and how the on_selection_modified is still fired for that view (19), if new lines (or characters) are added to the buffer in the cloned (right) view (22) at a later position of the file.

Animation

@wbond
Copy link
Member

wbond commented Apr 8, 2020

It is probably that we aren’t checking all of the selections in the inactive view to see if at least one is after the modification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants