SublimeText / TrailingSpaces

Highlight trailing spaces and delete them in a flash.
MIT License
896 stars 94 forks source link

TrailingSpaces fails to execute #156

Closed eblot closed 1 year ago

eblot commented 1 year ago

I've noticed recently that I'm not longer able to use TrailingSpaces.

I'm using ST4 Build 4150, Package Control and I've uninstalled/reinstalled TrailingSpaces several times w/o any success. I've deleted my user TrailingSpaces settings but I still can't make it work.

I got several issues but with the latest installation (v1.3.6) the console reports:

reloading settings Packages/TrailingSpaces/trailing_spaces.sublime-settings
Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime_plugin.py", line 1458, in run_
    return self.run()
  File "/Users/eblot/Library/Application Support/Sublime Text/Installed Packages/TrailingSpaces.sublime-package/trailing_spaces.py", line 380, in run
  File "./python3.3/collections/__init__.py", line 789, in __getitem__
  File "/Users/eblot/Library/Application Support/Sublime Text/Packages/sublime_lib/st3/sublime_lib/settings_dict.py", line 77, in __getitem__
    if key in self:
  File "/Users/eblot/Library/Application Support/Sublime Text/Packages/sublime_lib/st3/sublime_lib/settings_dict.py", line 101, in __contains__
    return self.settings.has(item)
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime.py", line 1856, in has
    return sublime_api.settings_has(self.settings_id, key)
TypeError: String required

What am I doing wrong? Thanks.

rchl commented 1 year ago

How does your TrailingSpaces settings file look like? Paste whole contents.

eblot commented 1 year ago

It is empty (since I deleted it):

cat /Users/eblot/Library/Application\ Support/Sublime\ Text/Packages/User/trailing_spaces.sublime-settings
{
}

The one that comes with the package is:

// Trailing Spaces' default settings.
//
// See Trailing Spaces' README for detailed instructions.
{
    // By default, Trailing Spaces is "live". It means the trailing spaces
    // regions will be matched in the background, and highlighted if a color
    // scope is defined, when the document is opened and edited.
    // Set to false to disable live matching and highlighting (the deletion
    // command remains available, so-called "lazy matching").
    "enabled" : true,

    // Highlight color is specified as a scope. You may define and use a custom
    // scope to better fit your colorscheme. A value of empty string "" will
    // make highlights invisible.
    "highlight_color" : "region.redish",

    // By default, empty lines are cleared as well when calling the deletion
    // command.
    // Set to false to ignore empty lines upon deletion.
    "include_empty_lines" : true,

    // By default, the line being currently edited will have its trailing
    // spaces highlighted.
    // Set to false to ignore trailing spaces on the edited line.
    "include_current_line" : true,

    // By default, any lines in the Find Results, Build output, Diff and Markdown views are ignored
    // Add scopes to this list if you need to ignore them.
    "scope_ignore": ["text.find-in-files", "source.build_output", "source.diff", "text.html.markdown"],

    // By default, trailing spaces are deleted within the whole document.
    // Set to true to affect only the lines you edited since last save.
    // Trailing spaces will still be searched for and highlighted in the whole
    // document.
    "modified_lines_only": false,

    // By default, nothing happens on save.
    // Set to true to trim trailing spaces before saving, with respect to the
    // other settings.
    "trim_on_save": false,

    // By default, deleting trailing spaces does not cause the document to be
    // saved.
    // Set to true to force saving after trailing spaces have been deleted.
    // This setting is irrelevant and will be ignored if trim_on_save is true.
    "save_after_trim": false,

    // ---- NEXT SETTINGS ARE FOR POWER USERS ONLY! ----

    // The number of characters before and after the visible region of text to
    // include in the highlighting. This is useful to also show the highlighting
    // immediately for text that just became visible through scrolling.
    // Adjust the value (in the number of characters) to whatever fits your
    // needs and performance.
    "non_visible_highlighting" : 500,

    // This is the interval at which the active view is tested for changes
    // (due to scrolling) to update the highlighting of the currently visible
    // region of text.
    // Adjust the value (in milliseconds) to whatever fits your needs and
    // performance.
    "update_interval" : 250,

    // Highlighting will be disabled if the edited file's size is larger than
    // this.
    // Adjust the value (in number of chars) to whatever fits your performance.
    "file_max_size" : 1048576,

    // By default, only simple spaces and tabs are matched as "trailing spaces".
    "regexp": "[ \t]+"
}
rchl commented 1 year ago

Maybe go to:

File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime.py", line 1856

and add print(self.settings_id, key) just before sublime_api.settings_has(self.settings_id, key).

Apparently the key is not a string as it should so that indicates some weird issue with your settings.

FichteFoll commented 1 year ago

The source code looks a bit sketchy here if I'm honest...

https://github.com/SublimeText/TrailingSpaces/blob/c09a3f0e4da01033f5fa6e5f3df4eba6e4bc1812/trailing_spaces.py#L380

All invocation of save() do it in this pattern, settings[0].save(), but settings is a ChainMap and I see no reason for settings[0] to ever be defined.

eblot commented 1 year ago

Well, I had to quit and restart ST to run the modified code, and ... the error disappeared and TrailingSpaces instantly started doing its job, highlighting the extra spaces... No idea what happened here. I'm sure I already quitted and restarted the app while trying to debug this issue, maybe in a different order with uninstalling/editing the settings/reinstalling TrailingSpaces.

Update: it is back and seems to depend on how I start ST: when using a sublime-project file, the bug shows up. Investigating... even weirder

eblot commented 1 year ago
Package Control: Skipping automatic upgrade, last run at 2023-06-04 10:19:58, next run at 2023-06-04 11:19:58 or after
26 0 <class 'int'>
<module 'sublime_api' (built-in)>
Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime_plugin.py", line 1458, in run_
    return self.run()
  File "/Users/eblot/Library/Application Support/Sublime Text/Installed Packages/TrailingSpaces.sublime-package/trailing_spaces.py", line 380, in run
  File "./python3.3/collections/__init__.py", line 789, in __getitem__
  File "/Users/eblot/Library/Application Support/Sublime Text/Packages/sublime_lib/st3/sublime_lib/settings_dict.py", line 77, in __getitem__
    if key in self:
  File "/Users/eblot/Library/Application Support/Sublime Text/Packages/sublime_lib/st3/sublime_lib/settings_dict.py", line 101, in __contains__
    return self.settings.has(item)
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime.py", line 1857, in has
    return sublime_api.settings_has(self.settings_id, key)
TypeError: String required

with

    def has(self, key):
        try:
            return sublime_api.settings_has(self.settings_id, key)
        except Exception:
            from pprint import pprint
            print(self.settings_id, key, type(key))
            pprint(sublime_api)
            raise
eblot commented 1 year ago

It's getting weirder with the latest change (#157):

  1. Quit ST4
  2. Remove $HOME/Library/Application\ Support/Sublime Text/Packages/User/trailing_spaces.sublime-settings
  3. Restart ST4 with a project file (with a single open C file containing trailing space)
  4. Use TrailingSpace toggle from the command panel creates a new user setting file, with a single entry "highlight_color" that toggles from "" to "region.redish"
  5. However whatever the setting nothing is highlighted and
  6. "Delete trailing spaces" from ST command panel does not work and reports "No trailing spaces to delete!"
  7. However "Trim trailing white space" does work

Same behavior with loading the C file w/o loading any sublime-project file.

rchl commented 1 year ago

By default highlighting is enabled so by executing step 4 you are disabling it. Thus my question is: are trailing spaces not highlighted before you do the step 4? That would indicate some other issue affecting this.

That said, I think with @FichteFoll's fix the changes as saved psychically in the settings file while previously those were only saved in memory... I think so at least. Not sure if that's expected or not.

rchl commented 1 year ago

Actually seems like there is also an issue now where it's not possible to enable highlighting back using the Toggle option.

eblot commented 1 year ago

By default highlighting is enabled so by executing step 4 you are disabling it. Thus my question is: are trailing spaces not highlighted before you do the step 4? That would indicate some other issue affecting this.

~No, whatever I do - or here do not do - trailing spaces are neither highlighted nor removed...~

Update: I just discovered that they are highlighted in .c files and plain text file, but not in .md. So maybe there is a secondary issue where another plugin could override TrailingSpaces highlighting and removal: in the same ST4 session, using a .md file in a window and a plain text file in another window:

rchl commented 1 year ago

Markdown is excluded by default - you have to override this option to change that: https://github.com/SublimeText/TrailingSpaces/blob/5695f7327991a163858860b6fb2cdd4e4af6246f/trailing_spaces.sublime-settings#L29

eblot commented 1 year ago

Ok, I did miss that option, sorry about that. I think I got misled with the "no trailing spaces to delete!" message, I would think this would only apply to the highlighting feature.