Skip to content
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

Elixir Language Templating Commenting #57

Closed
Immortalin opened this issue Dec 8, 2015 · 22 comments
Closed

Elixir Language Templating Commenting #57

Immortalin opened this issue Dec 8, 2015 · 22 comments

Comments

@Immortalin
Copy link

When something like this:

<%= form_for @changeset, @action, fn f -> %>
  <%= if @changeset.action do %>
    <div class="alert alert-danger">
      <p>Oops, something went wrong! Please check the errors below:</p>
      <ul>
        <%= for {attr, message} <- f.errors do %>
          <li><%= humanize(attr) %> <%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="form-group">
    <%= submit "Submit", class: "btn btn-primary" %>
  </div>
<% end %>

is commented, you get this:

<!-- <%= form_for @changeset, @action, fn f -> %> -->
<!--   <%= if @changeset.action do %> -->
<!--     <div class="alert alert-danger"> -->
<!--       <p>Oops, something went wrong! Please check the errors below:</p> -->
<!--       <ul> -->
<!--         <%= for {attr, message} <- f.errors do %> -->
<!--           <li><%= humanize(attr) %> <%= message %></li> -->
<!--         <% end %> -->
<!--       </ul> -->
<!--     </div> -->
<!--   <% end %> -->

<!--   <div class="form-group"> -->
<!--     <%= submit "Submit", class: "btn btn-primary" %> -->
<!--   </div> -->
<!-- <% end %> -->

However, it doesn't actually comment out the templating tags as they have to be turned to the <%# %> form. One suggestion would be to keep the existing html style formatting as it helps the editor display things properly.

@redguardtoo
Copy link
Owner

what's your major-mode? web-mode?

@redguardtoo
Copy link
Owner

It's because web-mode does NOT define the comment operation for this language, a workaround:

(defalias 'web-mode-comment-elixir-block 'web-mode-comment-erb-block)
(defalias 'web-mode-uncomment-elixir-block 'web-mode-uncomment-erb-block)

You suggestion is interesting, let me think how to do it.

For now, I just notify the user which function web-mode not implemented yet.

@Immortalin
Copy link
Author

@redguardtoo the problem is that elixir and EEx doesn't have any built-in block comments like ruby as most documentation is done via module attributes. Also, there are three types of tags:

Tags

EEx.SmartEngine supports the following tags:

<% Elixir expression - inline with output %>
<%= Elixir expression - replace with result %>
<%% EEx quotation - returns the contents inside %>
<%# Comments - they are discarded from source %>

This means implementing comments is not easy as you have to keep in memory what the previous type the tag was in before turning them into comments. An alternative would be to wrap the whole line within a comment tag and remove them later when need. Sort of like a paredit slurp and barf. This might be easier as you could probably borrow some code from smartparens or paredit etc. However, I am not sure whether this would affect the compiler in anyway and whether it would escape the templates within comments etc. I am not sure whether the elixir-vim or sublime text modules support commenting, maybe you can take a look at their source if they do.

@redguardtoo
Copy link
Owner

can you send me a a complete eex file? for the workaround, defalias to the ruby should work in all three cases.

This had better be resolved in web-mode, I could send a pull request to web-mode.

Just double check, is below code valid:

<%# Elixir expression - inline with output %>
<%#= Elixir expression - replace with result %>
<%#% EEx quotation - returns the contents inside %>

@Immortalin
Copy link
Author

@redguardtoo the code snippet in the first post is a complete eex file. It is merged and compiled at runtime into a complete html file.

@Immortalin
Copy link
Author

@redguardtoo the defalias solution works for single line comments but not multi-line ones

@redguardtoo
Copy link
Owner

I can see that, I will fix that this weekend

@Immortalin
Copy link
Author

Thanks!

@redguardtoo
Copy link
Owner

770981c can comment/uncomment mixed code in web-mode (Chen Bin) v2.2

@Immortalin
Copy link
Author

@redguardtoo thanks a ton! :D

@Immortalin
Copy link
Author

@redguardtoo this fails for nested comments, e.g.:

  <script src="<%= static_path(@conn, "/js/en.js") %>"></script>

Commenting the above gives the following:

  <!-- <script src="<%= static_path(@conn, "/js/en.js") %>"></script> -->

The templating language in question is language agnostic i.e. it doesn't care whether you are using HTML, or YAML or JSON etc. it just does the templating.

@redguardtoo
Copy link
Owner

Just double check, so you want to only comment out the template language but keep the html, right?
That's doable. I can add a flag.

@Immortalin
Copy link
Author

@redguardtoo actually, both. I am using comment-or-uncomment-lines but due to how the templating language parser works, it doesn't recognize the commented out HTML so there needs to be double commenting.

@redguardtoo
Copy link
Owner

got, interesting. I will see what I can do.

@redguardtoo
Copy link
Owner

upgrade to v2.3 then,

(setq evilnc-comment-both-snippet-html t)

@Immortalin
Copy link
Author

@redguardtoo by the way, it fails for tags that span multilines e.g.

<h2>Edit trade</h2>

<%= render "form.html", changeset: @changeset,
                        action: trade_path(@conn, :update, @trade) %>

@Immortalin
Copy link
Author

@redguardtoo and it also fails for multiple tags on one line e.g.

<li><%= humanize(attr) %> <%= message %></li>

@redguardtoo
Copy link
Owner

single line is fine, you need (setq evilnc-comment-both-snippet-html t) at first.

for the multiple line tag, I think it's doable but need more complicated algorithm. For now, even web-mode's own command M-x web-mode-comment-or-uncomment does not work.

@Immortalin
Copy link
Author

@redguardtoo the single line fails when there's multiple EEx tags in-between the html tags

@redguardtoo
Copy link
Owner

Could you give me a sample file with multi-line and single line eex, and exact steps to reproduce the issue. and M-x evilnc-version to double check version.

@Immortalin
Copy link
Author

@redguardtoo I am on 2.3 and the following is the example that fails:

<li><%= humanize(attr) %> <%= message %></li>

@redguardtoo
Copy link
Owner

For multi-line snippet, the best way is do it in web-mode. I will raise a issue to web-mode developer. (fxbois/web-mode#670)

For single line, it works on my computer, could you re-install evil-nerd-commenter?

C-h v evilnc-comment-both-snippet-html to double check at first.

screencast

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants