sublimehq / sublime_text

Issue tracker for Sublime Text
https://www.sublimetext.com
811 stars 39 forks source link

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

Open mechatroner opened 4 years ago

mechatroner commented 4 years ago

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

Thom1729 commented 4 years ago

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.

mechatroner commented 4 years ago

@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.

wbond commented 4 years ago

time.sleep() will block.