-
Notifications
You must be signed in to change notification settings - Fork 847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.tmLanguage & VS Code Support #293
Comments
This should be community-supported IMO. We could consider linking to a community project from the README perhaps. |
Community-supported would be ideal -- it seems like a non-trivial project unto itself. We're definitely happy to help and provide support however possible. |
Can we just fork the // Place your settings in this file to overwrite the default settings
{
"files.associations": {
"*.ejs": "erb"
}
} |
Interesting idea! Would you like to take a crack at an adapted-for-JS fork? |
I gave it a try at vscode-ejs, but I can't seem to get partial blocks (e.g. Edit: what we'll probably need to do is partially reimplement the JS grammar within the EJS grammar, fixing any multiline expectations when between EJS tags. Right now, it seems that those expectations are what's causing issues with the grammar e.g. <% if (true) { /* The vscode JS grammar takes over here and expects a closing bracket */ %>
<h1>Test</h1>
<% } %> The same issue is apparent something like this as well, <% [1, 2, 3].map(num => { /* The vscode JS grammar takes over here and expects a closing bracket AND a closing parenthesis */ %>
<span><%= num %></span>
<% }) %> |
Of course, it's two (or is it three?) interleaved grammars. Seems like a really interesting problem to solve. :) |
@ezekg That is the exact point that I created this issue! I had a similar thought to yours where I wanted to dig up Handlebars support and see if it handles this in any way that we could fork or re-implement, but it's still on my to-do list unfortunately. I wonder if there are any template engines who have done something similar to what you've mentioned already? I honestly don't even know what's out there these days - a quick Google brought up this list which I can try to look into. I honestly feel like we're this close. |
Right now this extension (EJS language definition) is utterly useless. Wrong syntax highlighting when you use IF statements and whats worse, it doesnt mark closing tags in the html. |
All, I have created a new EJS Language definition. If you want to test it out and let me know if you have any issues. https://github.com/Digitalbrainstem/ejs-grammar For now, if you open VSCode installation directory, find the other languages and create a new folder called "ejs" and place the code from my repo in there. I will be working on getting it integrated into VSCode. But you should now have access to select "Javascript EJS" from the languages. I tried to get as much as I could in it. Let me know if I should add anything. |
I have made this an actual extension in VSCode, Search ejs, the name is "Javascript EJS" Let me know if there are issues. |
@Betanu701 👏 👍 😄 I'll install this and check it out a bit later! Very excited! |
You're awesome, @Betanu701. Looks great for me. 👏 |
This is awesome. :) Would be great to get this added to the README and Web site. |
Any link to the extension? The only ones I found are:
And none of them highlights my code properly: https://cl.ly/0f290P110G2m |
There are several issues you are encountering. The first one, is that you are using multiple lines within tags. Technically this does render, but as far as I am aware, EJS is not meant for this. It is meant for single lines. For instance
What is happening with the parser which is causing your issues is the language sees the open braket and wants to interpret it as javascript. From then on, it is using javascript and there is not anything built in to see the stopping delimiter. We will have to write an entire javascript parser to see the ending delimiter and break javascript to signal the end of ejs. tmlanguage servers do not see multiple lines. They only read 1 line at a time. This is what makes what you are wanting very difficult. I would need to essentially hold the lines after an open tag in memory until I see the closing tag. In theory this seems like a basic practice, but the parser itself is not powerful enough (yet) to do that. This requires significant work with an actual language server to make this possible. There is another contributor that is working on the language server for ejs. I am not sure how through it is, and it is not ready for prod, but you are welcome to test it. The other issue you are encountering, is ejs tags within strings of html. This is a known issue, but again not easy to solve, as it requires writing an entire html parser as well to find anything within html tags and to find <%%>. HTML natively takes the string and hides it. If you know of a way to parser this out without the language server you are welcome to edit the language files and submit a pull request. I don't know of a way without a server though. I spent many many hours trying to solve that. |
@tbergeron The issue with the old ejs server, was that on any given line if you had an open bracket, it would automatically have javascript take over and you could not get out of javascript parser. The new language file will actually stop javascript parsing at the end of a line if there is a closing ejs tag.
The only issue really is the open brackets with multiline things. If you don't have multiline brackets, you can use 1 single pair of ejs tags. This will work: ` <% for(foo=0;foo<bar;foo++) { %> '
Hope this gives further clarity. Edit: The other option you have is putting the open bracket for the control statements on the next line. ` <% for(foo=0;foo<bar;foo++) %>
|
I completely get what is happening now. Thanks for such a detail response! I've recently switched to VSCode and I'm looking more and more about extending it and learning how to develop extensions etc. Your explanation really helped wrapping my head around the whole language server concept (this also helped a lot https://code.visualstudio.com/docs/extensions/example-language-server) My EJS usage might be quite funky in the provided example, but in my humble opinion, I believe the parser shouldn't expect a proper syntax but act on whatever you throw at it. Nobody uses a syntax in a lint-esque manner all the time, so this shouldn't actually break the rendering (though I understand why this is happening at the moment hehehe) I'm not ranting at all, what's been done so far is already fantastic! I see you seem to be developing the update on the |
This is desperately needed for |
This is more to start a community discussion, but support for VS Code is lacking. There is one plugin out there, but it's just a fork of TextMate support for EJS.
It looks like VS Code needs a
.tmLanguage
file to offer support for a language. This a grammar format TextMate uses to identify keywords, comments, control flow, etc. when rendering an.ejs
file. The documentation seemed vague to me. This is why the above and only EJS extension forked the TextMate.tmLanguage
file.I have had issues with EJS support for a while. I moved from Sublime Text to Atom because of the EJS support breaking in Sublime. The best support I've seen for EJS is in WebStorm via a Java plugin from JetBrains.
I've found a list of various
.tmLanguage
files for EJS and tried them all in VS Code. They all have various features that kind of work. I am proposing we build one officially supported.tmLanguage
file that can be shared by Sublime, VS Code, TextMate, and any other editor that supports this format (I think Atom and Webstorm also do).I'm not sure if this is something best suited for the official EJS language repo or organization or done as a community project. I'm willing to help out and even do a bulk of the work, it's when I got into the guts of it that things got tricky and I needed some help and brainstorming. Hence me reaching out to the community to see if there is already something in works, interest in getting this off the ground, etc.
List of
EJS.tmLanguage
files:Please let me know thoughts, next steps, or anything I'm missing or overlooking.
In my opinion, these would be 'must haves':
include
's%>
and opening<%
<img src="<%= ejs %>">
Current Screenshot of EJS in VS Code:

Current Screenshot of EJS in WebStorm:

The text was updated successfully, but these errors were encountered: