|
| 1 | +# Copyright 2019 Palantir Technologies, Inc. |
| 2 | +"""Linter pluging for flake8""" |
| 3 | +import logging |
| 4 | +from flake8.api import legacy as flake8 |
| 5 | +from pyls import hookimpl, lsp |
| 6 | + |
| 7 | +log = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +@hookimpl |
| 11 | +def pyls_settings(): |
| 12 | + # Default flake8 to disabled |
| 13 | + return {'plugins': {'flake8': {'enabled': False}}} |
| 14 | + |
| 15 | + |
| 16 | +@hookimpl |
| 17 | +def pyls_lint(config, document): |
| 18 | + settings = config.plugin_settings('flake8') |
| 19 | + log.debug("Got flake8 settings: %s", settings) |
| 20 | + |
| 21 | + opts = { |
| 22 | + 'exclude': settings.get('exclude'), |
| 23 | + 'filename': settings.get('filename'), |
| 24 | + 'hang_closing': settings.get('hangClosing'), |
| 25 | + 'ignore': settings.get('ignore'), |
| 26 | + 'max_line_length': settings.get('maxLineLength'), |
| 27 | + 'select': settings.get('select'), |
| 28 | + } |
| 29 | + |
| 30 | + # Build the flake8 checker and use it to generate a report from the document |
| 31 | + kwargs = {k: v for k, v in opts.items() if v} |
| 32 | + style_guide = flake8.get_style_guide(quiet=4, verbose=0, **kwargs) |
| 33 | + report = style_guide.check_files([document.path]) |
| 34 | + |
| 35 | + return parse_report(document, report) |
| 36 | + |
| 37 | + |
| 38 | +def parse_report(document, report): |
| 39 | + """ |
| 40 | + Build a diagnostics from a report, it should extract every result and format |
| 41 | + it into a dict that looks like this: |
| 42 | + { |
| 43 | + 'source': 'flake8', |
| 44 | + 'code': code, # 'E501' |
| 45 | + 'range': { |
| 46 | + 'start': { |
| 47 | + 'line': start_line, |
| 48 | + 'character': start_column, |
| 49 | + }, |
| 50 | + 'end': { |
| 51 | + 'line': end_line, |
| 52 | + 'character': end_column, |
| 53 | + }, |
| 54 | + }, |
| 55 | + 'message': msg, |
| 56 | + 'severity': lsp.DiagnosticSeverity.*, |
| 57 | + } |
| 58 | +
|
| 59 | + Args: |
| 60 | + document: The document to be linted. |
| 61 | + report: A Report object returned by checking the document. |
| 62 | + Returns: |
| 63 | + A list of dictionaries. |
| 64 | + """ |
| 65 | + |
| 66 | + file_checkers = report._application.file_checker_manager.checkers |
| 67 | + # No file have been checked |
| 68 | + if not file_checkers: |
| 69 | + return [] |
| 70 | + # There should be only a filechecker since we are parsing using a path and not a pattern |
| 71 | + if len(file_checkers) > 1: |
| 72 | + log.error("Flake8 parsed more than a file for '%s'", document.path) |
| 73 | + |
| 74 | + diagnostics = [] |
| 75 | + file_checker = file_checkers[0] |
| 76 | + for error in file_checker.results: |
| 77 | + code, line, character, msg, physical_line = error |
| 78 | + diagnostics.append( |
| 79 | + { |
| 80 | + 'source': 'flake8', |
| 81 | + 'code': code, |
| 82 | + 'range': { |
| 83 | + 'start': { |
| 84 | + 'line': line, |
| 85 | + 'character': character |
| 86 | + }, |
| 87 | + 'end': { |
| 88 | + 'line': line, |
| 89 | + # no way to determine the column |
| 90 | + 'character': len(physical_line) |
| 91 | + } |
| 92 | + }, |
| 93 | + 'message': msg, |
| 94 | + # no way to determine the severity using the legacy api |
| 95 | + 'severity': lsp.DiagnosticSeverity.Warning, |
| 96 | + } |
| 97 | + ) |
| 98 | + |
| 99 | + return diagnostics |
0 commit comments