dense-analysis / ale

Check syntax in Vim/Neovim asynchronously and fix files, with Language Server Protocol (LSP) support
BSD 2-Clause "Simplified" License
13.52k stars 1.43k forks source link

Add support for latexindent #2065

Open kevinywlui opened 5 years ago

kevinywlui commented 5 years ago

Name: latexindent URL: https://github.com/cmhughes/latexindent.pl

This is a tool that fixes the indentation of a latex file. It can read from stdin.

GohioAC commented 5 years ago

IMO, latexindent should be considered a fixer, not a linter!

ethanabrooks commented 5 years ago

Just want to follow up on this. I am not very experienced with vimscript, but I would be happy to write a pull request. I might need some guidance from the maintainers.

RyanSquared commented 5 years ago

For the most part if you look at fixers that already exist you should be able to find one that is close enough to what you need, and use code from there.

ethanabrooks commented 5 years ago

So I notice that latexindent is actually already included among the tex fixers. However, when I look at :ALEInfo, I see

(finished - exit code 2) ['/usr/local/bin/zsh', '-c', '''latexindent'' -l -w '
'/var/folders/bd/8nh4kyg933zcxm7n90n2m98r0000gn/T/nvimaLyXGD/29/algorithms.tex
''']

Would you like me to start a new issue for this?

RyanSquared commented 5 years ago

What is the actual thing that it does to the input, and what do you expect it to do? We can rename this issue if there is an issue with it.

ethanabrooks commented 5 years ago

There is no change to the input. I expect it to make my tex file pretty. I assume that the script is failing for some reason. Maybe this helps:

❯ /usr/local/bin/zsh -c latexindent
Please enter text to be indented: (press CTRL+D when finished)
❯ /usr/local/bin/zsh -c 'latexindent -l -w /var/folders/bd/8nh4kyg933zcxm7n90n2m98r0000gn/T/nvimaLyXGD/29/algorithms.tex'
Could not find directory /var/folders/bd/8nh4kyg933zcxm7n90n2m98r0000gn/T/nvimaLyXGD/29
Exiting, no indentation done. at /usr/local/texlive/2019basic/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm line 95.

Maybe that var/ file is not accessible from the command line. Not exactly sure how that works.

RyanSquared commented 5 years ago

That file is most likely made only for the purpose of ALE, and then afterwards deleted. What happens if you use the -l -w <filename> options on a copy of the file?

ethanabrooks commented 5 years ago

That works.

❯ /usr/local/bin/zsh -c 'latexindent -l -w algorithms.tex'
\documentclass{article}

\usepackage{amsmath}
\usepackage{algpseudocode,algorithm,algorithmicx}
\newcommand*\DNA{\textsc{dna}}

\newcommand*\Let[2]{\State #1 $\gets$ #2}
\algrenewcommand\algorithmicrequire{\textbf{Precondition:}}
\algrenewcommand\algorithmicensure{\textbf{Postcondition:}}
\DeclareMathOperator{\softmax}{Softmax}

\begin{document}
\begin{algorithm}[H]
    \caption{Current algorithm}
    \begin{algorithmic}[1]
        \State $s_t \gets \text{Conv}(x_t)$
        \State $c_r \gets \sigma\left(\phi^{update}\left(s_t \otimes a_{t-1} \otimes
                r_{t-1}^{(1)} \otimes  r_{t-1}^{(2)}\right)\right)$
        \State $c_g \gets \sigma\left(\phi^{update}\left(s_t\otimes a_{t-1}\otimes
                g_{t-1}^{(1)}\otimes g_{t-1}^{(2)}\right)\right)$
        \State $\tilde{p} \gets [0, 0, 1] \ast p_{t-1}$
        \State $p_t \gets c_r\tilde{p} + \left(1-c_r\right) p_{t-1}$
        \State $r_t \gets Mp_t$
        \State $g_t \sim c_gp_t + \left(1-c_g\right)\text{one-hot}\left(g_{t-1}\right)$
    \end{algorithmic}
\end{algorithm}
\begin{algorithm}
    \caption{Counting mismatches between two packed \DNA{} strings
        \label{alg:packed-dna-hamming}}
    \begin{algorithmic}[1]
        \State  $D \gets \left(\phi^{mux}\left(\softmax\left(x_t\otimes N\right)\right)\right)$
        \State $\tilde{p} \gets Dp$
    \end{algorithmic}
\end{algorithm}
\end{document}

It also changes the file in place.

RyanSquared commented 5 years ago

So you can confirm that when using the fixer, it doesn't change anything? Can you get a minimum example test-case?

ethanabrooks commented 5 years ago

Ok. This is my vimrc:

scriptencoding utf-8
set encoding=utf-8

let g:ale_fixers = {'tex': ['latexindent']}
let g:ale_fix_on_save = 1

call plug#begin('~/.vim/bundle')
Plug 'w0rp/ale'
call plug#end()

This is file:

\documentclass{article}

\usepackage{amsmath}
\usepackage{algpseudocode,algorithm,algorithmicx}
\newcommand*\DNA{\textsc{dna}}

\newcommand*\Let[2]{\State #1 $\gets$ #2}
\algrenewcommand\algorithmicrequire{\textbf{Precondition:}}
\algrenewcommand\algorithmicensure{\textbf{Postcondition:}}
\DeclareMathOperator{\softmax}{Softmax}

\begin{document}
\setcounter{algorithm}{4}
\begin{algorithm}
    \caption{Counting mismatches between two packed \DNA{} strings
    \begin{algorithmic}[1]
        \State $\tilde{p} \gets Dp$

    \end{algorithmic}
\end{algorithm}
\end{document}
This is the output of ALEInfo ``` Current Filetype: tex Available Linters: ['alex', 'chktex', 'lacheck', 'proselint', 'redpen', 'texlab', 'textlint', 'vale', 'writegood'] Linter Aliases: 'writegood' -> ['write-good'] Enabled Linters: ['alex', 'chktex', 'lacheck', 'proselint', 'redpen', 'texlab', 'textlint', 'vale', 'writegood'] Suggested Fixers: 'latexindent' - Indent code within environments, commands, after headings and within special code blocks. 'remove_trailing_lines' - Remove all blank lines at the end of a file. 'textlint' - Fix text files with textlint --fix 'trim_whitespace' - Remove all trailing whitespace characters at the end of every line. Linter Variables: let g:ale_tex_chktex_executable = 'chktex' let g:ale_tex_chktex_options = '-I' let g:ale_tex_lacheck_executable = 'lacheck' let g:ale_tex_texlab_executable = 'texlab' let g:ale_tex_texlab_options = '' 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 = 'Error' let g:ale_echo_msg_format = '%code: %%s' let g:ale_echo_msg_info_str = 'Info' let g:ale_echo_msg_warning_str = 'Warning' let g:ale_enabled = 1 let g:ale_fix_on_save = 1 let g:ale_fixers = {'tex': ['latexindent']} 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 = 1 let g:ale_lint_on_save = 1 let g:ale_lint_on_text_changed = 'normal' let g:ale_linter_aliases = {} let g:ale_linters = {} let g:ale_linters_explicit = 0 let g:ale_list_vertical = 0 let g:ale_list_window_size = 10 let g:ale_loclist_msg_format = '%code: %%s' let g:ale_lsp_root = {} let g:ale_max_buffer_history_size = 20 let g:ale_max_signs = -1 let g:ale_maximum_file_size = v:null let g:ale_open_list = 0 let g:ale_pattern_options = v:null let g:ale_pattern_options_enabled = v:null let g:ale_set_balloons = 0 let g:ale_set_highlights = 1 let g:ale_set_loclist = 1 let g:ale_set_quickfix = 0 let g:ale_set_signs = 1 let g:ale_sign_column_always = 0 let g:ale_sign_error = '>>' let g:ale_sign_info = '--' let g:ale_sign_offset = 1000000 let g:ale_sign_style_error = '>>' let g:ale_sign_style_warning = '--' let g:ale_sign_warning = '--' let g:ale_statusline_format = v:null let g:ale_type_map = {} let g:ale_use_global_executables = v:null let g:ale_virtualtext_cursor = 0 let g:ale_warn_about_trailing_blank_lines = 1 let g:ale_warn_about_trailing_whitespace = 1 Command History: (executable check - failure) alex (executable check - failure) chktex (executable check - success) lacheck (finished - exit code 0) ['/bin/zsh', '-c', '''lacheck'' ''/var/folders/bd/8nh4kyg933zcxm7n90n2m98r0000gn/T/nvimHZxGCi/2/simple.tex'''] <<>> "/var/folders/bd/8nh4kyg933zcxm7n90n2m98r0000gn/T/nvimHZxGCi/2/simple.tex", line 20: <- unmatched "\end{algorithm}" "/var/folders/bd/8nh4kyg933zcxm7n90n2m98r0000gn/T/nvimHZxGCi/2/simple.tex", line 15: -> unmatched "{" "/var/folders/bd/8nh4kyg933zcxm7n90n2m98r0000gn/T/nvimHZxGCi/2/simple.tex", line 21: <- unmatched "\end{document}" "/var/folders/bd/8nh4kyg933zcxm7n90n2m98r0000gn/T/nvimHZxGCi/2/simple.tex", line 14: -> unmatched "\begin{algorithm}" "/var/folders/bd/8nh4kyg933zcxm7n90n2m98r0000gn/T/nvimHZxGCi/2/simple.tex", line 22: <- unmatched "end of file /var/folders/bd/8nh4kyg933zcxm7n90n2m98r0000gn/T/nvimHZxGCi/2/simple.tex" "/var/folders/bd/8nh4kyg933zcxm7n90n2m98r0000gn/T/nvimHZxGCi/2/simple.tex", line 12: -> unmatched "\begin{document}" <<>> (executable check - failure) proselint (executable check - failure) redpen (executable check - failure) textlint (executable check - failure) vale (executable check - failure) write-good ```
vindex10 commented 10 months ago

Hi! I managed to make it work.

In my case ALEInfo is like this:

(finished - exit code 0) ['/bin/bash', '-c', '''latexindent'' -l /abs/path/to/latexindent.yaml -m - < ''/tmp/nvimsfgF05/5/myfile.tex'']

As you can see, ALE feeds the tex file via stream redirection. Therefore, inside the fixer there should be "-" to read the input file from stdin.

I configure fixer per project with .exrc. If you use global latexindent.yaml, you can omit this hardcoded abspath.

So here is the part that made it work:

let g:ale_tex_latexindent_options='/abs/path/to/latexindent.yaml -m -'
let g:ale_fixers['tex'] = get(g:ale_fixers, 'latexindent', []) + ['latexindent']

fixed: However, I noticed that the output, at least for my config was different between stdin and file input: https://github.com/cmhughes/latexindent.pl/issues/493