sublime_text icon indicating copy to clipboard operation
sublime_text copied to clipboard

Package Authoring: Error when setting a syntax file immediately after auto-generating it

Open mechatroner opened this issue 4 years ago • 3 comments

Description

Hello, I am developing and supporting the rainbow_csv package. Recently I added an option to autogenerate syntax files when users select a text of the CSV separator, so it could be not a single character but a sequence of characters. My extension generates the syntax file and writes it to "Packages/User" folder e.g. Packages/User/Rainbow_CSV_hex_657665_Simple.sublime-syntax You can see the code here: https://github.com/mechatroner/sublime_rainbow_csv/blob/421a305569ea372f63fe6a05e6c4676088431850/main.py#L426

The problem is, when my extension calls view.set_syntax_file(rainbow_syntax_file) immediately after generating it, Sublime Text shows this error:

Error loading syntax file "Packages/User/Rainbow_CSV_hex_657665_Simple.sublime-syntax"
Unable to read Packages/User/Rainbow_CSV_hex_657665_Simple.sublime-syntax

But, if I click OK, Sublime correctly applies the newly-generated syntax file as if no error had occurred.

I actually found a workaround for this problem, If I set the syntax file with 2.5 seconds delay after generating it, everything seems to be working as expected (no error is getting shown). Although I have already published rainbow_csv with this workaround I would prefer to find out what is the root cause of this and it would be great if you could fix this.

Steps to reproduce

  1. Clone the failure demo branch of rainbow_csv package: https://github.com/mechatroner/sublime_rainbow_csv/tree/syntax_failure_demo
  2. Copy the package into the Sublime Packages dir as rainbow_csv! Do not copy as "sublime_rainbow_csv"
  3. Start the Sublime, open any file, select some characters with the cursor (more than one, otherwise rainbow_csv will use a built-in syntax file instead of autogenerating one), "Right-click" -> "Rainbow CSV" -> "Enable ... "
  4. Observe the error
  5. After clicking OK see the file getting highlighted

Expected behavior

Same as above but without step 4.

Actual behavior

Sublime Text 3 shows an error in step 4.

Environment

  • Build: 3211
  • Operating system and version: Windows 10

mechatroner avatar Jul 18 '20 03:07 mechatroner

I have a similar issue when generating syntaxes for JS Custom's unit tests. I plan to address this by polling sublime.list_syntaxes() and running the tests when the generated syntax is included in the list. I haven't tried this yet, but I think you should be able to do something similar.

Thom1729 avatar Jul 18 '20 05:07 Thom1729

@Thom1729 Thank you for the suggestion. I ended up using sublime.find_resources() which is what list_syntaxes() uses under the hood. Also between the polling sublime.find_resources() I am waiting with sublime.set_timeout(). If I try to wait with time.sleep() instead, it doesn't work for some reason.

mechatroner avatar Jul 18 '20 21:07 mechatroner

time.sleep() will block.

wbond avatar Jul 18 '20 22:07 wbond