Python-like selectable templating laguages

How can i susbstitute some a language for any user-specified file types. Like Python makes html/xml files acts as Django?

I know how to implement everyting with my own file type and extension, but want to be able to choose language the same way as python does. 

10 comments
Comment actions Permalink
Official comment

Python uses LanguageSubstitutor extension for that. Please be aware that this would make your filetype, language, parser and thus indexing results be dependent on some complicated logic of yours, that would very easily lead to issues where index content doesn't correspond to the current file's AST. To avoid that, you must be very careful and reindex all your files whenever anything affecting your LanguageSubstitutor changes. You might want to use a FilePropertyPusher for that.

Comment actions Permalink

I thought about it, but been confused with LanguageSubstitutor being LanguageEP, so requires language to be registered. But Pythons binds to any file you specify in configuration dialog. So it should be able to bind this extension to any language. 

0
Comment actions Permalink

Ok, i've implemented runtime registering and unregistering of LanguageSubstitutors. But i've encountered some caching inside the platform and can't find it myself.

For example, If i start the IDEA and opening html file - it's ok, just html.

Than my plugin registering substitutor, that should change html language to my own. And debugging says that requests are coming and substitutor returns proper value.  But html file is still being parsed as html. Re-opening or editing it doesn't help. Looks like language is cached somewhere and sequential substitutions just being ignored.

If I restart the idea and my substitutor being added on startup - substitution works. But vice versa, after removing substitutor IDEA still acts like it's in there and works untill restart (but debugger says opposite).

Need help here.

0
Comment actions Permalink

Did you call pushers which should've resulted in FileContentUtilCore.reparseFiles being called for your files?

0
Comment actions Permalink

As far as I understood from answers to some of my prvious questions, pushers related to indexing. But atm there are no indexes. Only parsing.  

But seems FileContentUtilCore.reparseOpenedFiles() solved the problem. Thanks. 

Seems that opened files doesn't mean really opened in editor.

0
Comment actions Permalink

FileContentUtil#reparseOpenedFiles does mean "opened in editor", the implementation says so. And you need to reparse all files affected by language substitutor changes, regardless of whether they're open. And they need to be reindexed, because a lot of indices depend on the parser, file language and AST structure. Pushers solve this issue.

0
Comment actions Permalink

I see. Thanks you!

0
Comment actions Permalink

Well, i've made a pusher and i've solved my problem. But now i've got a main question:

What is pusher here for?

As far as i understand, all the pusher thing is kinda map storage, where you can put any serializable object and access it using a virtualfile as a key. This may be useful in some occasions.

But, file properties themselves is not anyhow related to filebased indexes or stubs or anything else. It's just persistent user data.

So, related to my question: seems i just need to re-parse all affected files and I don't need a pusher. 

I've re-read explanation of pushers work from Maxim Moiseenko  in other thread: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206102159-Perl5-plugin-for-Intellij-IDEA?page=8#community_comment_206652329

But it is still unclear what is pusher here for?

0
Comment actions Permalink

To be honest: pusher is a hack. Our indices are supposed to depend only on file and its content. But there are situations when they depend on something else: project language level for Java, template language settings for Python, etc. Whenever such things change, files must be reindexed, otherwise index data will not match actual parsed trees and you'll get exceptions like "stub-ast mismatch".

We don't yet have explicit mechanism for such dependencies inside indices. We'd very much like to, but we haven't yet invented an API. For now we have pushers instead. They remember (persistently) the information important for parsing, and when it's changed, ensure that the caches are properly updated. It only remains to invoke pusher explicitly when any related settings change.

2

Please sign in to leave a comment.