在Vim中使用ALE和Prettier格式化Jinja HTML模板

时间:2018-12-23 07:15:13

标签: vim prettier

我在Ubuntu 16.04 LTS上使用NVIM v0.3.2-208-g2841e89,并使用ALE作为Linting Engine插件。我在使用prettier格式化Jinja模板html文件时遇到问题。

<html>标记开头并包含jinja代码的html文件根本没有被格式化。当我在这些文件上运行ALEFix时,什么都没有发生。

{% extends %}行开头的html文件正在格式化,但格式不正确。当我在这些文件上运行ALEFix时,代码即开始显示。

原始文件

{% extends "home.html" %}
{% block body %}
<div class="jumbotron">
<p>Thank you {{ session['username'] }} for signing up !!</p>
</div>

{% endblock %}

格式化文件

{% extends "home.html" %} {% block body %}
<div class="jumbotron">
  <p>Thank you {{ session['username'] }} for signing up !!</p>
</div>

{% endblock %}

这是另一个例子。

原始文件

 {% extends "home.html" %}
{% block body %}
<form method="POST">
    {{ form.hidden_tag() }}
    {{ form.username.label }} {{ form.username }}
    {{ form.password.label }} {{ form.password }}
    {{ form.submit }}
</form>
{% endblock %}

格式化文件

{% extends "home.html" %} {% block body %}
<form method="POST">
  {{ form.hidden_tag() }} {{ form.username.label }} {{ form.username }} {{
  form.password.label }} {{ form.password }} {{ form.submit }}
</form>
{% endblock %}

我不确定这是jinja文件的正确格式,但是看起来不太可读。

这是我的配置外观

let g:ale_fixers = {
                    \ '*': ['remove_trailing_lines', 'trim_whitespace'],
                    \ 'html': ['prettier'],
                    \ 'javascript': ['eslint', 'prettier'],
                    \ 'css' : ['stylelint', 'prettier'],
                    \ 'python' : ['yapf', 'isort', 'autopep8']
                    \ }

以下是ALEInfo报告的Jinja模板文件。

 Current Filetype: jinja.html                                                                                                                                                                                      
Available Linters: ['alex', 'htmlhint', 'proselint', 'stylelint', 'tidy', 'writegood']                                                                                                                             
   Linter Aliases:                                                                                                                                                                                                 
'writegood' -> ['write-good']                                                                                                                                                                                      
  Enabled Linters: ['alex', 'htmlhint', 'proselint', 'stylelint', 'tidy', 'writegood']                                                                                                                             
 Suggested Fixers:                                                                                                                                                                                                 
  'prettier' - Apply prettier to a file.                                                                                                                                                                           
  'remove_trailing_lines' - Remove all blank lines at the end of a file.                                                                                                                                           
  'tidy' - Fix HTML files with tidy.                                                                                                                                                                               
  'trim_whitespace' - Remove all trailing whitespace characters at the end of every line.                                                                                                                          
 Linter Variables:                                                                                                                                                                                                 
let g:ale_html_htmlhint_executable = 'htmlhint'                                                                                                                                                                    
let g:ale_html_htmlhint_options = ''                                                                                                                                                                               
let g:ale_html_htmlhint_use_global = 0                                                                                                                                                                             
let g:ale_html_stylelint_executable = 'stylelint'                                                                                                                                                                  
let g:ale_html_stylelint_options = ''                                                                                                                                                                              
let g:ale_html_stylelint_use_global = 0                                                                                                                                                                            
let g:ale_html_tidy_executable = 'tidy'                                                                                                                                                                            
let g:ale_html_tidy_options = '-q -e -language en'                                                                                                                                                                 
 Global Variables:                                                                                                                                                                                                 
let g:ale_cache_executable_check_failures = v:null                                                                                                                                                                 
let g:ale_change_sign_column_color = 0                                                                                                                                                                             
let g:ale_command_wrapper = ''                                                                                                                                                                                     
let g:ale_completion_delay = v:null                                                                                                                                                                                
let g:ale_completion_enabled = 0                                                                                                                                                                                   
let g:ale_completion_max_suggestions = v:null                                                                                                                                                                      
let g:ale_echo_cursor = 1                                                                                                                                                                                          
let g:ale_echo_msg_error_str = 'E'                                                                                                                                                                                 
let g:ale_echo_msg_format = '[%linter%] %s [%severity%]'                                                                                                                                                           
let g:ale_echo_msg_info_str = 'Info'                                                                                                                                                                               
let g:ale_echo_msg_warning_str = 'W'                                                                                                                                                                               
let g:ale_enabled = 1                                                                                                                                                                                              
let g:ale_fix_on_save = 1                                                                                                                                                                                          
let g:ale_fixers = {'html': ['prettier'], '*': ['remove_trailing_lines', 'trim_whitespace'], 'javascript': ['eslint', 'prettier'], 'css': ['stylelint', 'prettier'], 'python': ['yapf', 'isort', 'autopep8']}      
let g:ale_history_enabled = 1                                                                                                                                                                                      
let g:ale_history_log_output = 1                                                                                                                                                                                   
let g:ale_keep_list_window_open = 0                                                                                                                                                                                
let g:ale_lint_delay = 200                                                                                                                                                                                         
let g:ale_lint_on_enter = 1                                                                                                                                                                                        
let g:ale_lint_on_filetype_changed = 1                                                                                                                                                                             
let g:ale_lint_on_insert_leave = 0                                                                                                                                                                                 
let g:ale_lint_on_save = 1                                                                                                                                                                                         
let g:ale_lint_on_text_changed = 'always'                                                                                                                                                                          
let g:ale_linter_aliases = {}                                                                                                                                                                                      
let g:ale_linters = {'css': ['stylelint'], 'python': ['flake8']}                                                                                                                                                   
let g:ale_linters_explicit = 0                     

1 个答案:

答案 0 :(得分:1)

更漂亮的不支持此功能。有人需要加紧编写一个插件。

更漂亮的问题:https://github.com/prettier/prettier/issues/5581

注意:它说的是Django,但在此情况下基本相同。