Automatically falling for utf-8

I started the evening working on the refactoring of the current help linting code in the authoring tools and quickly sent myself down a rabbit hole of refactoring in other areas entirely. As much fun as it is to write new code, it’s also quite enjoyable to go back over what you’ve done and clean it up a little bit.

This time around the changes are primarily for the loading of resources, which ties in with what I was originally going to be working on while at the same time knocking some items off of the TODO list and making things more robust to boot, so that’s nice.

Working backwards from the result to the work that was done, loading of help resources (index files or source help) now goes through a single function whose job it is to do the loading and normalize the line endings. The magic of this new method is that it always takes a resource specification, but if trying to load the resource doesn’t work, it falls back to trying to load the file manually directly from the Packages folder.

The practical upshot of this is that the authoring tools can now create files with impunity and then immediately try to load them from within the same command, using the same method as would be used everywhere else, and it will still load the file properly even if Sublime hasn’t indexed the new resource yet and added it to the list of resources.

Technically this is not needed since experimentation seems to indicate that even if find_resources doesn’t find anything, load_resource will still load it anyway. However that’s not really officially documented anywhere, and I’m loathe to rely on that sort of behaviour until there is official word, so that things don’t unexpectedly break in the future. Also this rolls all of the code for loading a resource nicely into a single place, which seems cleaner.

The new method also tries to load the resource as utf-8 (whichever way it loads) and is responsible for normalizing the line endings on the resulting output (if any) so that code everywhere can use a single function call to collect data and do all of their error checking all at once.

This ties in with the intended goal for this evening because one of the first things that I started looking at was the code for loading help files that aren’t already open into a hidden view so that their contents can be queried. This was manually loading the file, which annoyed me, and further was not taking into account that probably utf-8 should be used while loading the file. That sent me into wanting to use the existing help file loader, and down the rabbit hole we go.

The current goal is to do what I planned for tonight tomorrow, at least to see how well the idea I have in mind for the code organisation will work out for adding other linters. I think it should be OK, but the true test will be to try and implement a few and see where the problems lay.