iamcco / coc-diagnostic

diagnostic-languageserver extension for coc.nvim
245 stars 22 forks source link

coc-action-format does not call Black formatter for python #80

Closed dajuno closed 3 years ago

dajuno commented 3 years ago

I set up coc-diagnostic with the python formatters black and isort. isort works but black does not seem to be invoked by :call CocAction("format").

nvim/coc setup

NVIM v0.4.4
Build type: Release
LuaJIT 2.0.5
Compilation: /usr/bin/cc -D_FORTIFY_SOURCE=2 -march=x86-64 -mtune=generic -O2 -pipe -fno-plt -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -O2 -DNDEBUG -DMIN_LOG_LEVEL=3 -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=always -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -I/build/neovim/src/build/config -I/build/neovim/src/neovim-0.4.4/src -I/usr/include -I/build/neovim/src/build/src/nvim/auto -I/build/neovim/src/build/include
Compiled by builduser

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
     fall-back for $VIM: "/usr/share/nvim"

     Run :checkhealth for more info

init.vim

if &compatible
  set nocompatible
endif

filetype plugin indent on

call plug#begin('~/.config/nvim/plugged')
    Plug 'neoclide/coc.nvim', {'branch': 'release'}
call plug#end()

black: version 20.8b1 isort: 5.7.0 coc-plugins: coc-jedi, coc-diagnostic, coc-json

coc-settings.json

{
    "jedi.enable": true,
    "jedi.startupMessage": false,
    "jedi.markupKindPreferred": "plaintext",
    "jedi.trace.server": "off",
    "jedi.jediSettings.autoImportModules": [],
    "jedi.executable.command": "jedi-language-server",
    "jedi.executable.args": [],
    "jedi.completion.disableSnippets": false,
    "jedi.completion.resolveEagerly": false,
    "jedi.diagnostics.enable": true,
    "jedi.diagnostics.didOpen": true,
    "jedi.diagnostics.didChange": true,
    "jedi.diagnostics.didSave": true,
    "jedi.workspace.extraPaths": [],
    "diagnostic-languageserver.filetypes": {
        "python": "flake8",
        "cpp": "cpplint",
        "sh": "shellcheck"
    },
    "diagnostic-languageserver.formatFiletypes": {
        "sh": "shfmt",
        "python": ["black", "isort"]
    },
    "diagnostic-languageserver.formatters": {
        "black": {
            "command": "black",
            "args": ["--quiet", "--line-length 80", "-"]
        },
        "isort": {
            "command": "isort",
            "args": ["--quiet", "-"]
        }
    },
    "diagnostic.checkCurrentLine": true,
    "diagnostic.virtualText": false,
    "diagnostic.displayByAle": false
}

:CocInfo after calling :call CocAction("format") in MWE below

vim version: NVIM v0.4.4
node version: v15.8.0
coc.nvim version: 0.0.80-6e5a2aaeb5
coc.nvim directory: /home/david/.config/nvim/plugged/coc.nvim
term: xterm-256color
platform: linux

## Log of coc.nvim

2021-02-17T13:58:21.771 INFO (pid:469777) [services] - registered service "diagnostic-languageserver"
2021-02-17T13:58:21.773 INFO (pid:469777) [services] - diagnostic language service state change: stopped => starting
2021-02-17T13:58:21.782 INFO (pid:469777) [services] - registered service "jedi"
2021-02-17T13:58:21.782 INFO (pid:469777) [services] - jedi-language-server state change: stopped => starting
2021-02-17T13:58:21.784 INFO (pid:469777) [plugin] - coc.nvim 0.0.80-6e5a2aaeb5 initialized with node: v15.8.0 after 107ms
2021-02-17T13:58:21.792 INFO (pid:469777) [language-client-index] - diagnostic-languageserver started with 469790
2021-02-17T13:58:21.804 INFO (pid:469777) [language-client-index] - Language server "jedi" started with 469796
2021-02-17T13:58:21.952 INFO (pid:469777) [services] - diagnostic language service state change: starting => running
2021-02-17T13:58:21.957 INFO (pid:469777) [services] - service diagnostic-languageserver started
2021-02-17T13:58:21.994 INFO (pid:469777) [services] - jedi-language-server state change: starting => running
2021-02-17T13:58:22.001 INFO (pid:469777) [services] - service jedi started
2021-02-17T13:58:30.042 INFO (pid:469777) [attach] - receive notification: showInfo []
2021-02-17T13:58:30.064 INFO (pid:469777) [attach] - receive request: CocAutocmd [ 'BufReadCmd', 'output', 'output:///info' ]
2021-02-17T13:58:38.049 INFO (pid:469777) [attach] - receive request: format []
2021-02-17T13:58:45.450 INFO (pid:469777) [attach] - receive notification: showInfo []

MWE:

import scipy.interpolate as si
import numpy as np

x = 'hello'

isort reorders the imports, black should change the single quotes to double quotes. However, :call CocAction("format") produces:

import numpy as np
import scipy.interpolate as si

x = 'hello'

I.e., the black formatting is ignored (see :CocInfo above).

Any advice is appreciated! And thank you for your work.

yaegassy commented 3 years ago

@dajuno I think it is due to a mistake in your black "args" setting.

Your setting:

"args": ["--quiet", "--line-length 80", "-"]

Change to this setting and try it:

"args": ["--quiet", "--line-length", "80", "-"]

dajuno commented 3 years ago

Oh indeed, you are right! I thought I had tested without this setting, but apparently not. Thank you :)

yaegassy commented 3 years ago

@dajuno Has this issue been resolved? If it is, please close the issue.