Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to trigger formatting and select the correct formatter #39

Closed
yngwi opened this issue Nov 20, 2020 · 9 comments
Closed

How to trigger formatting and select the correct formatter #39

yngwi opened this issue Nov 20, 2020 · 9 comments

Comments

@yngwi
Copy link

yngwi commented Nov 20, 2020

Describe the bug
I can't seem to get the formatting to work for TypeScript in the most recent Neovim, the linting seems to work fine.
I'm trying the configuration listed below. When I try to call :lua vim.lsp.buf.formatting() just nothing happens. I have disabled the typescript language server to make sure nothing interferes and when I inspect the attached clients, diagnostics-languageserver is there as expected. Do you have an idea how to use the formatter in Neovim? And assuming the formatting works, is there a way to make sure, nvim-lsp uses diagnostics-languageserver instead of tsserver for formatting?

Thank you!
Daniel

lspconfig.diagnosticls.setup{
  on_attach=custom_attach,
  filetypes = { 'javascript', 'javascriptreact', 'typescript', 'typescriptreact', 'css', 'scss', 'markdown', 'pandoc' },
  init_options = {
    linters = {
      eslint = {
        command = 'eslint',
        rootPatterns = { '.git' },
        debounce = 100,
        args = { '--stdin', '--stdin-filename', '%filepath', '--format', 'json' },
        sourceName = 'eslint',
        parseJson = {
          errorsRoot = '[0].messages',
          line = 'line',
          column = 'column',
          endLine = 'endLine',
          endColumn = 'endColumn',
          message = '[eslint] ${message} [${ruleId}]',
          security = 'severity'
        },
        securities = {
          [2] = 'error',
          [1] = 'warning'
        }
      },
      markdownlint = {
        command = 'markdownlint',
        rootPatterns = { '.git' },
        isStderr = true,
        debounce = 100,
        args = { '--stdin' },
        offsetLine = 0,
        offsetColumn = 0,
        sourceName = 'markdownlint',
        securities = {
          undefined = 'hint'
        },
        formatLines = 1,
        formatPattern = {
          '^.*:(\\d+)\\s+(.*)$',
          {
            line = 1,
            column = -1,
            message = 2,
          }
        }
      }
    },
    filetypes = {
      javascript = 'eslint',
      javascriptreact = 'eslint',
      typescript = 'eslint',
      typescriptreact = 'eslint',
      markdown = 'markdownlint',
      pandoc = 'markdownlint'
    },
    formatters = {
      prettierEslint = {
        command = 'prettier-eslint',
        args = { '--stdin' },
        rootPatterns = { '.git' },
      },
      prettier = {
        command = 'prettier',
        args = { '--stdin-filepath', '%filename' }
      }
    },
    formatFiletypes = {
       css = 'prettier',
       javascript = 'prettierEslint',
       javascriptreact = 'prettierEslint',
       json = 'prettier',
       scss = 'prettier',
       typescript = 'prettierEslint',
       typescriptreact = 'prettierEslint'
    }
  }
}
@iamcco
Copy link
Owner

iamcco commented Nov 23, 2020

Open the log channel to see if LSP client send format request to LSP server.

@yngwi
Copy link
Author

yngwi commented Nov 23, 2020

Thank you I will try.

@alex-popov-tech
Copy link

hey guys, any updates on that?
also, @iamcco can you please assist on where to search for diagnostic logs?
I'm having problems with formatting large files, basically i toggle formatting and it does nothing...
using eslint and prettier btw, here is the config:

lsp_config.diagnosticls.setup({
  on_attach = lsp_completion.on_attach,
  filetypes={
    'javascript',
    'typescript',
    'javascriptreact',
    'typescriptreact',
  },
  init_options = {
    filetypes = {
      javascript = 'eslint',
      typescript = 'eslint',
      javascriptreact = 'eslint',
      typescriptreact = 'eslint',
    },
    formatters = {
      prettier = {
        command = './node_modules/.bin/prettier',
        args = {
          '--stdin-filepath',
          '%filepath',
        }
      }
    },
    formatFiletypes = {
      javascript = 'prettier',
      typescript = 'prettier',
      javascriptreact = 'prettier',
      typescriptreact = 'prettier'
    },
    linters = {
      eslint = {
        command = './node_modules/.bin/eslint',
        rootPatterns = { '.git' },
        debounce = 100,
        args = {
          '--stdin',
          '--stdin-filename',
          '%filepath',
          '--format',
          'json'
        },
        sourceName = 'eslint',
        parseJson = {
          errorsRoot = '[0].messages',
          line = 'line',
          column = 'column',
          endLine = 'endLine',
          endColumn = 'endColumn',
          message = '${message} [${ruleId}]',
          security = 'severity'
        },
        securities = {
          [2] = 'error',
          [1] = 'warning',
        },
      },
    },
  }
})

@iamcco
Copy link
Owner

iamcco commented Dec 9, 2020

@alex-popov-tech Open the diagnostic-languageserver output channel. The way it's related to the LSP client.

@annguyenwasd
Copy link

@yngwi
I realize that you guys missed the rootPatterns in formatters configuration. After I add

 prettier = {
        rootPatterns = {'.git'},
...

it works for me.

To test prettier. Just edit a rule in .prettierrc and format your document. Such as I toggle singleQuote to true and false.

@alex-popov-tech
Copy link

oh, thanks @annguyenwasd !
Have no idea why it was working on file project/tmp.ts and not on project/tests/base/filename.ts tho...
For me rootPatterns did the trick

@yngwi yngwi closed this as completed Dec 11, 2020
@lakshya-sky
Copy link

@alex-popov-tech Open the diagnostic-languageserver output channel. The way it's related to the LSP client.

I am trying to get the log but I couldn't manage to access logs for the diagnostic-language server. A little help with what is output channel and how to access it will be appreciated.

Thank you!

@pacokwon
Copy link

@alex-popov-tech Open the diagnostic-languageserver output channel. The way it's related to the LSP client.

I am trying to get the log but I couldn't manage to access logs for the diagnostic-language server. A little help with what is output channel and how to access it will be appreciated.

Thank you!

Maybe try :lua print(vim.lsp.get_log_path())!

Source - :help lsp

@lakshya-sky
Copy link

lakshya-sky commented May 18, 2021

Well, I tried that earlier but it's the same path to the lsp.log file which doesn't include anything about the diagnostic-language server.

I am trying to use Eslint with an explicit path to an eslintrc.json file but the language server is not working otherwise it works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants