wakatime / sublime-wakatime

Sublime Text 2 & 3 plugin for automatic time tracking and metrics generated from your programming activity.
https://wakatime.com/sublime-text
BSD 3-Clause "New" or "Revised" License
526 stars 47 forks source link

Random errors due to no selection #116

Closed rchl closed 1 year ago

rchl commented 1 year ago

I'm randomly getting errors like:

Traceback (most recent call last):
  File "../Packages/LSP/plugin/symbols.py", line 106, in <lambda>
    lambda response: sublime.set_timeout(lambda: self.handle_response(response)),
  File "../Packages/LSP/plugin/symbols.py", line 121, in handle_response
    self.view.run_command("lsp_selection_clear")
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime.py", line 1349, in run_command
    sublime_api.view_run_command(self.view_id, cmd, args)
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime_plugin.py", line 1496, in run_
    self.view.end_edit(edit)
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime.py", line 1310, in end_edit
    sublime_api.view_end_edit(self.view_id, edit.edit_token)
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime_plugin.py", line 932, in on_selection_modified
    run_view_callbacks('on_selection_modified', view_id)
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime_plugin.py", line 708, in run_view_callbacks
    callback(v, *args)
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime_plugin.py", line 152, in profiler
    return event_handler(*args)
  File "../Packages/WakaTime/WakaTime.py", line 620, in on_selection_modified
    handle_activity(view)
  File "../Packages/WakaTime/WakaTime.py", line 409, in handle_activity
    append_heartbeat(entity, timestamp, is_write, view, project, folders)
  File "../Packages/WakaTime/WakaTime.py", line 426, in append_heartbeat
    rowcol = view.rowcol(selections[0].begin())
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime.py", line 1044, in __getitem__
    raise IndexError()
IndexError

It's not really that important how it's triggered IMO because in the ST API it's not guaranteed that selection is non-empty so plugin should never assume so and always check first.

alanhamlett commented 1 year ago

Can you add this after line 424 in your WakaTime.py file:

print("SELECTIONS: ", selections)

Then check your ST console for the output? What I want to find is the value of selections because right before selections[0] the code checks to make sure selections is defined which should guarantee it has at least one item in the list. So, an IndexError doesn't make sense.

rchl commented 1 year ago

I don't have a way to reproduce it at the moment but "truthy" check is not enough due to how Selections object is implemented in ST. It defines:

    def __bool__(self):
        return self.view_id != 0

which doesn't check number of selections. The 3.8 plugin host does:

    def __bool__(self) -> bool:
        """ The selection is ``True`` when not empty. """
        return self.view_id != 0 and len(self) > 0

but that fix didn't trickle down to the old host.

So the explicit len(...) check is needed.

alanhamlett commented 1 year ago

Oh, that's not normal in Python but I was assuming it behaved like a Python list. I'll submit a patch now.

alanhamlett commented 1 year ago

Fixed in v11.0.8 released just now.