GitHub

General purpose Language Server that integrate with linter to support diagnostic features

Main features

  • diagnostic with linters
  • document format

screenshot with neovim and coc image

Install

yarn global add diagnostic-languageserver

make sure your yarn's global bin path is include in PATH

for example export PATH="$(yarn global bin):$PATH"

Config & Document

languageserver config:

linters field:

filetypes field:

formatters field:

formatFiletypes field:

Args additional syntax

args: ["%text", "%filename", "%relativepath", "%file", "%filepath", "%dirname", "%tempfile"]

  • %filename will replace with basename of file
  • %text will replace with file content
  • %file will replace with full path to the file and not use stdio
  • %filepath will replace with full path to the file
  • %relativepath will replace with relative path of file
  • %dirname will replace with dirname of file
  • %tempfile will replace with the full path to a temporary file written with the contents of the document and not use stdio; this file will automatically be deleted when the command completes

How to config a new linter

shellcheck for example:

file test.sh:

#!/usr/bin/env bash
echo `ls -al`

then:

shellcheck --format=gcc test.sh

output:

t.sh:3:6: warning: Quote this to prevent word splitting. [SC2046]
t.sh:3:6: note: Useless echo? Instead of 'echo $(cmd)', just use 'cmd'. [SC2005]
t.sh:3:6: note: Use $(...) notation instead of legacy backticked `...`. [SC2006]

write pattern to match the line for line column message security:

const line = "t.sh:3:6: warning: Quote this to prevent word splitting. [SC2046]"
const formatPattern = "^[^:]+:(\\d+):(\\d+):\\s+([^:]+):\\s+(.*)$"
const match = line.match(new RegExp(formatPattern))
console.log(match)

output:

so you got:

  • line: match[1]
  • column: match[2]
  • message: match[4]
  • security: match[3]

and your formatPattern field will be:

Notes if the linter's message for per issue more then one line, you have to set the formatLines to fill your pattern, and you can view the languagetool pattern for example which formatLines = 2

Example with coc.nvim

Each LSP client should support initializationOptions option, all you need for diagnostic-languageserver is put the config in initializationOptions option.

  1. shellcheck for shell
  2. languagetool for grammer check
  3. more Linters config example.

coc-settings.json:

you can use this extension https://github.com/iamcco/coc-diagnostic

TODO

  • local node_modules linter support like eslint or textlint
  • diagnostic severity
  • root pattern
  • document format

References

Buy Me A Coffee ☕️

btc

image

Read the original on github.com ↗