Skip to content
This repository has been archived by the owner on Feb 1, 2019. It is now read-only.

Add textDocument/formatting. #13

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
aac18f2
.gitignore: Update gitignore
ksdme Jul 5, 2018
b1f5ae4
.vscode-client: Drop vscode client
ksdme Jun 7, 2018
e0e4f3c
README: Update README.md and assets
ksdme Jun 8, 2018
305a441
coala_langserver: Drop codebase and tests
ksdme Jun 6, 2018
6bbad87
tests: Add test helpers and resources
ksdme Jun 8, 2018
023120d
conftest.py: Add conftest.py
ksdme Jun 8, 2018
5d4c230
coalals.utils.files: Add files module
ksdme Jun 8, 2018
7a6ebac
coala.utils.log: Add log configuration
ksdme Jun 8, 2018
2ccfa25
coalals.utils.wrappers: Add wrappers
ksdme Jun 8, 2018
832f76e
coalals.concurrency: Add concurrency module
ksdme Jun 8, 2018
3e7defd
coalals.interface: Add initial coala wrapper
ksdme Jun 8, 2018
f101593
coalals.results: Add result processing module
ksdme Jun 8, 2018
9e84731
coala.langserver: Add language server module
ksdme Jun 8, 2018
1a5f1c0
coalals.main: Add main.py
ksdme Jun 8, 2018
b6dc724
__main__.py: Make module executable
ksdme Jun 8, 2018
8c64f2a
requirements.txt: Update requirements
ksdme Jun 8, 2018
5c5f87f
test-requirements.txt: Update test requirements
ksdme Jun 8, 2018
d56adf2
*: Update test configuration
ksdme Jun 8, 2018
c60a565
coala-ls.sh: Add coala-ls.sh
ksdme Jun 8, 2018
32fe61c
coalals.results: Refactor to submodule
ksdme Jul 5, 2018
e9745d3
coalals.utils.files: Add get_disk_contents()
ksdme Jul 5, 2018
88d7983
requirements.txt: Add whatthepatch
ksdme Jun 25, 2018
9cef173
coalals.results.fixes: Add fixes parsing & lsp models
ksdme Jul 5, 2018
7b4d59d
coalals.results.diagnostics: Support fixes collection
ksdme Jul 5, 2018
8e75ead
coalals.langserver: Add support for textDocument/formatting
ksdme Jul 5, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
coalals.results: Add result processing module
Add module to process the results and convert coala
diagnostic messages to language server supported
messages. Supports code fixing messages also.
ksdme committed Jul 5, 2018
commit f10159391befe2f184c453183932e5e3747a2c34
105 changes: 105 additions & 0 deletions coalals/results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from json import loads

import logging
logger = logging.getLogger(__name__)


class Diagnostics:
"""
Handle the diagnostics format transformation
and processing.
"""

@classmethod
def from_coala_json(cls, json_op):
"""
Transform coala json output into valid
diagnostic messages following LSP protocol
structure.

:param json_op:
coala json output as string.
:return:
Instance of Diagnostics class.
"""
coala_op = loads(json_op)['results']
warnings, fixes = [], []

def convert_offset(x):
return x - 1 if x else x

for section, coala_warnings in coala_op.items():
for warning in coala_warnings:
"""
Transform RESULT_SEVERITY of coala to DiagnosticSeverity of LSP
coala: INFO = 0, NORMAL = 1, MAJOR = 2
LSP: Error = 1, Warning = 2, Information = 3, Hint = 4
"""
severity = 3 - warning['severity']
message = warning['message']
origin = warning['origin']
full_message = '[{}] {}: {}'.format(section, origin, message)

# TODO Handle results for multiple files
for code in warning['affected_code']:
start_line = convert_offset(code['start']['line'])
start_char = convert_offset(code['start']['column'])
end_line = convert_offset(code['end']['line'])
end_char = convert_offset(code['end']['column'])

if start_char is None or end_char is None:
start_char, end_char = 0, 0
end_line = start_line + 1

warnings.append({
'severity': severity,
'range': {
'start': {
'line': start_line,
'character': start_char,
},
'end': {
'line': end_line,
'character': end_char,
},
},
'source': 'coala',
'message': full_message,
})

# TODO Handle results for multiple files
# and also figure out a way to resolve
# overlapping patches.

# for file, diff in warning['diffs'].items():
# for parsed_diff in parse_patch(diff):
# pass

logger.debug(warnings)
return cls(warnings, fixes=fixes)

def __init__(self, warnings=[], fixes=[]):
"""
:param warnings:
A list of initial warnings to initialize
instance with.
:param fixes:
A list of initial code fixes to initialize
instance with.
"""
self._warnings = warnings
self._fixes = fixes

def warnings(self):
"""
:return:
Returns a list of warnings.
"""
return self._warnings

def fixes(self):
"""
:return:
Returns a list of fixes.
"""
return self._fixes
34 changes: 34 additions & 0 deletions tests/tests_coalals/test_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
from json import load, dumps

from coalals.results import Diagnostics
from helpers.resources import sample_diagnostics


class DummyDiff:
pass


def get_all_samples():
with open(sample_diagnostics) as samples:
return load(samples)['samples']


def test_diagnostics_init():
diagnostics = Diagnostics(warnings=[{}])
assert diagnostics.warnings() == [{}]


def test_diagnostics_fixes():
diagnostics = Diagnostics(fixes=[DummyDiff()])
assert len(diagnostics.fixes()) == 1


@pytest.mark.parametrize('sample', get_all_samples())
def test_from_coala_op_json(sample):
coala = sample['coala']
exp_langserver = sample['langserver']

coala_json_op = dumps(coala)
gen_diags = Diagnostics.from_coala_json(coala_json_op)
assert gen_diags.warnings() == exp_langserver