【问题标题】:Auto formating of a .liquid-file (a Shopify theme file).liquid 文件(Shopify 主题文件)的自动格式化
【发布时间】:2018-07-26 06:28:39
【问题描述】:

我正在编辑一些第 3 方代码(Shopify 主题),代码完全不可读。

理想情况下,我希望能够自动格式化.liquid-files,但似乎无法做到。

我在 PhpStorm 中的尝试

我尝试在 PhpStorm 中插入代码并按下 CMD + Option + l (after doing this),它会做一些事情,但会在随机的地方换行,如下所示:

{% unless current_tags %}<h1
class="text-center text-uppercase title-under">{{ 'blogs.general.title_main' | t }}</h1>{% else %}<h1

我在 Atom 中的尝试

在 Atom 中,我尝试安装 atom-beautify,但无法识别文件类型。我可以看到others have had my issue as well。由于.liquid.blade.php 共享了很多内容,因此我尝试查看是否有针对该格式的解决方案,结果是open thread as well

我尝试查看adding support for a new language(使用 atom-beautify),但它很快就变得非常耗时。

我对@9​​87654326@的尝试

这有点接近了。但是,如果我有一个完整的 Shopify 主题,在将代码移动到我的编辑器之前,将代码复制到该网站中仍然很麻烦。如果 PrettyDiff 绝对可以自动格式化,那么我可能会接受它,但它仍然会做这样的事情:

{% if settings.show_blog_sidebar %}
  <div class="col-xl-8 col-lg-8 col-md-12" id="centerColumn">
    {% endif %}

我想要达到的目标

做这个废话:

{% unless current_tags %}<h1
class="text-center text-uppercase title-under">{{ 'blogs.general.title_main' | t }}</h1>{% else %}<h1
class="text-center text-uppercase title-under">{{ 'blogs.general.title_result' | t }} {{ current_tags.first | replace: '-', ' ' }}</h1>{% endunless %}
    {% for article in blog.articles %}{% if article.excerpt contains "SOMEVAR" %}<p>
    test</p>{% capture _article %}{% include "get-content-with-key" content: article.excerpt key: "[SOMEVAR]" %}{% endcapture %}{% capture _title %}{% include "get-content-with-key" content: _article key: "[title]" %}{% endcapture %}{% capture author %}{% include "get-content-with-key" content: _article key: "[author]" %}{% endcapture %}{% capture image %}{% include "get-content-with-key" content: _article key: "[image]" %}{% endcapture %}{% capture content %}{% include "get-content-with-key" content: _article key: "[content]" %}{% endcapture %}{% if _title == '' %}{% assign _title = article.title %}{% endif %}{% if author == '' %}{% assign author = article.author %}{% endif %}
    {% if article.comments.size > 0 %}{% assign comment_url = article.url | append: '#comments' %}{% else %}{% assign comment_url = article.url | append: '#addcomment' %}{% endif %}
        <div class="{{ grid }}">
        ...
        ...

看起来像这样:

{% unless current_tags %}
  <h1 class="text-center text-uppercase title-under">{{ 'blogs.general.title_main' | t }}</h1>
{% else %}
  <h1 class="text-center text-uppercase title-under">{{ 'blogs.general.title_result' | t }} {{ current_tags.first | replace: '-', ' ' }}</h1>
{% endunless %}

{% for article in blog.articles %}
  {% if article.excerpt contains "SOMEVAR" %}
    <p>test</p>
    {% capture _article %}
      {% include "get-content-with-key" content: article.excerpt key: "[SOMEVAR]" %}
    {% endcapture %}
    {% capture _title %}
      {% include "get-content-with-key" content: _article key: "[title]" %}
    {% endcapture %}
    {% capture author %}
      {% include "get-content-with-key" content: _article key: "[author]" %}
    {% endcapture %}
    {% capture image %}
      {% include "get-content-with-key" content: _article key: "[image]" %}
    {% endcapture %}
    {% capture content %}
      {% include "get-content-with-key" content: _article key: "[content]" %}
    {% endcapture %}
    {% if _title == '' %}
      {% assign _title = article.title %}
    {% endif %}

    {% if author == '' %}
      {% assign author = article.author %}
    {% endif %}

    {% if article.comments.size > 0 %}
      {% assign comment_url = article.url | append: '#comments' %}
    {% else %}
      {% assign comment_url = article.url | append: '#addcomment' %}
    {% endif %}
      <div class="{{ grid }}">
            ...
            ...

我真的是这个星球上第一个想要自动格式化.liquid 文件的人吗?

【问题讨论】:

    标签: formatting phpstorm shopify atom-editor liquid


    【解决方案1】:

    尚不能在 PhpStorm 中以这种方式进行格式化:https://youtrack.jetbrains.com/issue/WI-12782https://youtrack.jetbrains.com/issue/WI-39065 - 相应的功能请求以添加您可以投票支持的正确格式。

    【讨论】:

      【解决方案2】:

      使用 Vim 或 GVim 语法高亮和命令行标志来重新格式化您的代码。

      vim -V -se +"filetype plugin indent on|set syntax=liquid|filetype|normal gg=G" -cwq test.liquid

      -s is silent mode
      -e is execution mode (not normal mode)
      +"" executes the string inside the quotes, separated by pipes
      -cwq is short-hand for command :wq
      filetype is just for debugging
      normal gg=G goes to the top of the file and re-indents all the way to the end.
      

      如果您只是将 Vim 用作 IDE,您只需在普通(不是编辑器)模式下输入 gg=G(假设您打开了基于文件类型的缩进)

      【讨论】:

      • 我无法让它工作。您是否在某处(插件或其他东西)定义了您的.liquid-syntax?你测试过吗?我会给你赏金,因为你是我迄今为止最接近解决方案的人。我在终端(Mac)上使用 Vim 8.0。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      相关资源
      最近更新 更多