iamcco / diagnostic-languageserver

diagnostic language server integrate with linters
MIT License
418 stars 28 forks source link

Feature: Clear formatter error logs #36

Open pappasam opened 4 years ago

pappasam commented 4 years ago

Code formatters can fail when the code they're formatting is not valid. For example, "black" fails on invalid Python. It would be great if there were a feature that saved formatter errors for pretty display to the end user; it seems like these error messages are discarded at present (see https://github.com/iamcco/diagnostic-languageserver/pull/11)

I've implemented my own Vim formatter plugin a while ago that provides a version of this, but I'd like to migrate to your general language server for formatting if possible. The error logging feature is an extremely nice to have, so I'm opening this issue in case anyone has the time / inclination to implement something helpful (custom action show stderr on failed formatters, etc). I think formatter debugging capabilities is a fundamental part of a tool that provides code formatting.

Reference: https://github.com/pappasam/vim-filetype-formatter/blob/845aaa95dc00efa20a9aa2c8615a47bbd4bf50f7/doc/filetype_formatter.txt#L128

pappasam commented 4 years ago

Any recommendations for how to develop this effectively locally? I keep running into this error when I try to create a mon-repo with this + coc-diagnostic: https://stackoverflow.com/questions/57422458/error-ts6059-file-is-not-under-rootdir-rootdir-is-expected-to-contain-al

My hunch is that something like this could be a simple path forward for now. If you're able to test more-easily than me, might be worth giving this a try:

  diff --git a/src/handles/handleFormat.ts b/src/handles/handleFormat.ts
  index 376a05c..a1841f5 100644
  --- a/src/handles/handleFormat.ts
  +++ b/src/handles/handleFormat.ts
  @@ -3,6 +3,7 @@ import { TextEdit, TextDocument, CancellationToken, Range, Position } from 'vsco
   import { URI } from 'vscode-uri';

   import { IFormatterConfig } from '../common/types';
  +import logger from '../common/logger';
   import { findWorkDirectory, findCommand, executeFile, checkAnyFileExists } from '../common/util';
   import HunkStream from '../common/hunkStream';

  @@ -49,6 +50,9 @@ async function handleFormat(
     let output = '';
     if (code > 0) {
       output = text
  +    if (stderr) {
  +      logger.warn(stderr)
  +    }
     } else if (config.doesWriteToFile) {
       output = fs.readFileSync(URI.parse(textDocument.uri).fsPath, 'utf8')
     } else if (isStdout === undefined && isStderr === undefined) {
iamcco commented 4 years ago

logger.warn(stderr) only output to log channel.