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

Syntax highlighting break for </template and > not in same line in template #1211

Closed
nibytes opened this issue Apr 18, 2019 · 10 comments · Fixed by #1669
Closed

Syntax highlighting break for </template and > not in same line in template #1211

nibytes opened this issue Apr 18, 2019 · 10 comments · Fixed by #1669

Comments

@nibytes
Copy link

nibytes commented Apr 18, 2019

  • [*] I have searched through existing issues
  • [*] I have read through docs
  • [*] I have read FAQ

Info

  • Platform: Win
  • Vetur version: 0.18.1
  • VS Code version: 1.33.1

Problem

изображение

Reproducible Case

I don't know, this behavior in one file.
I restarted VS, rename file, and reinstalled vetur. But highlighting not work in this file,

@nibytes
Copy link
Author

nibytes commented Apr 18, 2019

Ok something in template broken highlighting

<template>
  <div class="l-content l-content_fill">
    <div class="ipr-add__status">
      <div class="ipr-add__status-item">
        <span class="ipr-add__status-cell">
          <i class="fa fa-heartbeat" aria-hidden="true"></i>
        </span>
        <span class="ipr-add__status-cell">
          <strong>Статус: </strong>в процессе
        </span>
      </div>
      <div class="ipr-add__status-item">
        <span class="ipr-add__status-cell">
          <i class="fa fa-spinner" aria-hidden="true"></i>
        </span>
        <span class="ipr-add__status-cell">
          <strong>Рассмотрение: </strong>
          <template v-if="task.ReviewDueDate">{{
            task.ReviewDueDate | moment("from", "now")
          }}</template>
          <template v-else
            >Не установлено
            </template>
        </span>
      </div>
      <div class="ipr-add__status-item">
        <span class="ipr-add__status-cell">
          <i class="fa fa-fire" aria-hidden="true"></i>
        </span>
        <span class="ipr-add__status-cell">
          <strong>Дедлайн: </strong>
          <template v-if="task.DueDate">{{
            task.DueDate | moment("from", "now")
          }}</template>
          <template v-else
            >Не установлено</template
          >
        </span>
      </div>
    </div>
  </div>
</template>

If reformat template all work fine, but this format I get from lint --fix

@dombavetta
Copy link

I'm experiencing the same issue, script and style sections for one file no longer have syntax highlighting.

@dombavetta
Copy link

I have created a reproducible repo for this case, which can be pulled down and ran from here /~https://github.com/dombavetta/vetur-template-interpolation-issues.

The problematic file -> /~https://github.com/dombavetta/vetur-template-interpolation-issues/blob/master/pages/test.vue

The issue appears to be caused by this line and the way it is formatted (24) <span v-show=" formDirtyFlags.reason && (userHasNonSchedualableProvider || someLongVariableNameThatIsGoingToGetNewLined) " class="error" >

Removing that span resolves the highlighting issues.

@octref octref added the grammar label Apr 24, 2019
@octref
Copy link
Member

octref commented Apr 24, 2019

          <template v-else
            >Не установлено</template
          >

Man, why would you write your code like this...
Vetur looks for </template> to close your opening <template>. I'm not sure if there's an easy fix for the textmate grammar.

@octref octref added the bug label Apr 24, 2019
@octref octref changed the title Syntax highlighting not work in one file Syntax highlighting break for </template and > not in same line in template Apr 24, 2019
@dombavetta
Copy link

@octref I doubt he purposely wrote it like that. eslint/prettier will format it like that to prevent unnecessary whitespace from making it into the markup. Thats what it does for me at least.. I know its ugly but I guess theres some good reasoning behind it. For me its easier to just submit to the formatters that fight them or clutter my code with disable and ignore comments.

@octref
Copy link
Member

octref commented May 8, 2019

This doesn't seem to be possible with TextMate:

@fabsenet
Copy link

this formatting is a result of whitespace-sensitive-formatting in prettier 1.15 and later

also, it only breaks the syntax highlighting if it is a template tag.

breaks highlighting:

<template v-else
    >abc</template
>

works fine (replaced template with div tag only):

<div v-else
    >abc</div
>

@Tsuyoshi84
Copy link

Tsuyoshi84 commented Jan 11, 2020

I just faced the same problem. The following code breaks highlightings of VS Code.

<template v-if="value"
  >Some message</template
>
<template v-else
  >Another message</template
>

But thanks to @fabsenet, I figured out some workarounds of this problem.

1. Use display: block comment

As Prettier mentions, we can use display: block comment to prevent <template> from being broken.

<!-- display: block -->
<template v-if="value">
  Some message
</template>
<!-- display: block -->
<template v-else>
  Another message
</template>

2. Use ignore option

Another option is to set htmlWhitespaceSensitivity to ignore in prettier config.

  "htmlWhitespaceSensitivity": "ignore"

However, this change would affect the entire code formating, so we should keep in mind that.

@ssorallen
Copy link
Contributor

ssorallen commented Jan 21, 2020

Since Prettier enabled whitespace-sensitive-formatting by default, more and more of my Vue SFCs have become blobs of all-white JavaScript because of this bug. I don't yet have a solution, but here are my notes:

  • The <template> tag both applies a new grammar and is a valid tag within that grammar. The grammars currently do not differentiate between a closing </template> within the grammar block and a closing </template> that ends the grammar block. <template> tags recursively apply the Vue HTML grammar because of

    "include": "text.html.vue-html"

    Potential solution:
    Match the closing </template> at the beginning of a line to mark the close of the "text.html.vue-html" grammar block.

    For example:

    <template>
      <div>
        <!-- NOT beginning of line, is a simple closing tag -->
        <template v-if="true">Foo</template>
      </div>
    </template> <!-- beginning of line, closes grammar -->
    

    This would enable removing the recursive grammar application in the above vue-html.tmLanguage.json file.

    What I tried:
    I updated L268 to be the following (added the caret):

    "end": "^(</)(template)(>)",
    

    this effectively matched only the outermost </template> to close the grammar block, but then </template> tags within the grammar did not match regular HTML closing tags.

This would introduce a new restriction on how SFCs must be formatted, but it would enable differentiating the two types of closing </template> tags.

ssorallen pushed a commit to ssorallen/vetur that referenced this issue Jan 22, 2020
End the vue-html only with a `</template>` at the beginning of a line.
Because the `<template>` tag starts a grammar in source.vue files and
can also be used a a tag *within* that grammar, the closing
`</template>` of the grammar must be distinguishable from the same
closing tag within the vue-html grammar.

This enables removing the recursive application of the vue-html grammar
within the vue-html grammar because source.vue can reliably know when
a `</template>` should end the grammar.

* New restriction: there can be only 1 closing `</template>` within a
  Vue single-file component (SFC) otherwise the vue-html grammar will
  be closed early. That closing `</template>` must also appear at the
  beginning of a line. Because this follows Vue formatting guidelines,
  this should have zero impact on users who following those guidelines.

Fixes vuejs#1211
@octref octref mentioned this issue Feb 10, 2020
3 tasks
octref added a commit that referenced this issue Feb 10, 2020
@octref octref added this to the February 2020 milestone Feb 18, 2020
@yacoob
Copy link

yacoob commented Jul 11, 2021

As I've just bumped into this: syntax highlighting for <script> breaks also if the entire <template>...</template> block is on a single line :)

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

Successfully merging a pull request may close this issue.

7 participants