ビットの海

ゆるふわソフトウェアエンジニアしゃぜのブログ

coc-python が archive されたので、coc-jedi と coc-diagnostic に乗り換えた

はじめに

元々 coc-python でLSPとして以下のような設定をして使っていたが、coc-python が停止してしまったので、乗り換えたというお話。

coc-settings.json

   
{
  "languageserver": {
    "python": {
      "command": "pyls",
      "filetypes": [
        "python"
      ],
      "trace.server": "verbose",
      "args": [
        "-vv",
        "--log-file",
        "/tmp/foo.log"
      ],
      "settings": {
        "pyls": {
          "trace": {
            "server": "verbose"
          },
          "configurationSources": [
            "flake8"
          ],
          "plugins": {
            "pydocstyle": {
              "enabled": false
            },
            "pycodestyle": {
              "enabled": false
            },
            "yapf": {
              "enabled": false
            },
            "pylint": {
              "enabled": false
            }
          }
        }
      }
    }
  }
}

やりたいこと

  • Language Server によるアシスト
  • Flake8 での Lint
  • Black での Format

どうするか

coc-jedi と coc-diagnostic を使います。

Language Server によるアシストは coc-jedi を使う

coc-jedi の導入

:CocInstall coc-jedi

Language Server は coc-python 時代は、 pip install 'python-language-server[all]' していたのですが、jedi の場合は以下のようになる。

pip install jedi-language-server

python-language-server(pyls) と同じく、vim が参照できる path を jedi-language-server も設定しておく。

そして、coc-settings.json にも設定を書く

  "jedi": {
    "enable": true,
    "startupMessage": false,
    "executable.command": "jedi-language-server"
  }

coc-diagnostic で Lint と Format

インストール

:CocInstall coc-diagnostic
pip install flake8 black

coc-settings.json の設定

  "diagnostic-languageserver.filetypes": {
    "python": ["flake8"]
  },
  "diagnostic-languageserver.formatFiletypes": {
    "python": ["black"]
  },
  "diagnostic-languageserver.linters": {
    "flake8": {
      "command": "flake8",
      "debounce": 100,
      "args": [ "--format=%(row)d,%(col)d,%(code).1s,%(code)s: %(text)s", "-" ],
      "offsetLine": 0,
      "offsetColumn": 0,
      "sourceName": "flake8",
      "formatLines": 1,
      "formatPattern": [
        "(\\d+),(\\d+),([A-Z]),(.*)(\\r|\\n)*$",
        {
          "line": 1,
          "column": 2,
          "security": 3,
          "message": 4
        }
      ],
      "securities": {
        "W": "warning",
        "E": "error",
        "F": "error",
        "C": "error",
        "N": "error"
      }
    }
  },
  "diagnostic-languageserver.formatters": {
    "black": {
      "command": "black",
      "args": ["--quiet", "-"]
    }
  },

フォーマットするばあいは、以下のコマンドを呼び出す

:call CocAction('format')

参考にしています

coc-pythonがArchive入りしていることに気がついたのでcoc-jediに変えてみた | DevelopersIO