Sublime Forum

Are variables available across sublime_* files?

#1

Disclaimer: Newbie to altering ST eg. new syntaxes, completions etc. New to YAML and other data storage formats

I’m working on setting up an extended syntax from YAML.

I have a large list of words that I would like to be able to use across syntax and completion files. Ideally by creating that list of words in a suitable format that can be used across multiple sublime files.

The goal is to have the core list of words able to edited by users who are not familiar with the intricacies of the underlying systems. So the list needs to be able to be reliably edited without complicated data structures being involved in the first instance.

Either by creating a variable in a sublime file that can then be accessed from OTHER sublime files.

OR perhaps by creating a single master file that can be imported into each sublime file that needs it.

The goal being to have one authoritative place to make changes to that master list.

One stumbling block is that different sublime files use different interpreters.

eg. sublime-syntax uses YAML
sublime-completions uses JSON

That may not matter since my understanding is that JSON is a sub-set of YAML, so perhaps compatibility is easy.

If my list of words is:

Rule
View
Views
Look

Then I need to be able to present that list in different ways to different sublime files.

For instance, a syntax file will need those words in a regex format so it can find any of them.

Ignoring the surrounding regex code, I would need to be able to present this list in the syntax file as:

Rule|View|Views|Look

And it would need to be presented to the sublime-completions file in a different way again.

I’m currently stuck on how to get completions to operate reliably with one word.

So I don’t know how to even make a set of completions that would present the above list as options for completing in my files.

In any case, I FEEL like the point of YAML is to enable this kind of interoperability.

Perfectly glad to be pointed at pages or given keywords to search for.

Thanks!

0 Likes

#2

Syntax definitions (.sublime-syntax) and completions (.sublime-completions) are completely unrelated. They don’t share any information.

Extending syntaxes can re-use or override variables (and contexts) of their base syntax. That’s all.

To achieve your goal, you’d need to go the JSCustom way and write a template syntax definition, which is then used to create a real syntax using the list of words maintained in a dedicated settings file.

Same would work to create sublime-completions files using a template, but alternatively you could write a plugin with an EventListener, that handles on_query_completions() to return your words.

1 Like

#3

Ah thank you!

I was literally just coming back here to ask if I could make the contents of a settings file available to a sublime.syntax file.

And you have already suggested such a thing.

Based on other conversations in this forum, I think to do what I would like to do with completions, it’s going to require Python, and an EventListener.

I shall look into templates…

0 Likes

#4

Because I’m a newbie…

What is JSCustom? Is that a package?

I’ve tried searching for template syntax definition but I haven’t spotted anything that looks likely.

Sublime Text doesn’t seem to have templates.

Thanks for any clairification

0 Likes

#5

I found the JSCustom package and installed it.

However, JSCustom docs are incredibly minimal.

There is no explanation of where it gets any of the syntax information from. No explanation of the configurations and the values.

There ARE docs, but they assume massive amounts of knowledge that I don’t have, and don’t know what to look for.

I appreciate that these projects are done out of love, not for money.

I like learning these things. But its very disheartening when every solution is a dead end, because no context is supplied for the packages.

0 Likes

#6

You mentioned JSCustom, and writing a template syntax definition.

Having looked around JSCustom, and followed some breadcrumbs, is JSCustom a way of creating your own templates, then using them to create your own syntax files?

Or is it specifically limited to JSX etc? And just used to create a few specific customized syntax files?

If that’s the case, then are you saying that I would need to write my own template engine for the syntax files I want to create?

0 Likes

#7

For some basic information on how syntaxes work in Sublime, as well as some plugin work such as creating a custom completion event handler and more, you may be interested in the 5 video series linked below. It starts from first principles and builds up a package from scratch, with a running commentary on how everything works.

0 Likes

#8

JS Custom uses a technologiy called YAMLMacros to dynamically create sublime-syntax files with configurable feature sets. It takes ST’s original JavaScript syntax, injects syntax rules configured via sublime-settings files and stores the result as plain sublime-syntax file. It let’s users create various variants of JavaScript with maybe conflicting features required for different frameworks.

You can’t use it directly for your package. It is just an example, how to dynamically create syntax files.

YAMLMacros package may be the general approach, which can be used by packages to do that. Maybe Vue is a better example to start learning.

It uses https://github.com/vuejs/vue-syntax-highlight/blob/st4/Vue%20Component.sublime-syntax.yaml-macros as base, which is compiled to https://github.com/vuejs/vue-syntax-highlight/blob/st4/Vue%20Component.sublime-syntax.

Macros (see: https://github.com/vuejs/vue-syntax-highlight/blob/ec6e5e72d38e97f6122688a1cef3ddb123f437c1/Vue%20Component.sublime-syntax.yaml-macros#L135-L137) call python functiosn (see https://github.com/vuejs/vue-syntax-highlight/blob/ec6e5e72d38e97f6122688a1cef3ddb123f437c1/src/macros.py#L3), which dynamically create syntax rules.

Python modules are bound to YAML-macro templates via https://github.com/vuejs/vue-syntax-highlight/blob/ec6e5e72d38e97f6122688a1cef3ddb123f437c1/Vue%20Component.sublime-syntax.yaml-macros#L2

it would also work to just write a syntax definition with some jinja2 placeholders, which are filled with your pre-defined lists of words finally.

0 Likes

#9

Thank you so much for the ideas and pointers to some options.

I really appreciate it. I have a chunk of learning to do now.

Cheers!

0 Likes