import json

from coalib.bearlib.abstractions.Lint import Lint
from coalib.bears.LocalBear import LocalBear
from coalib.results.Result import Result
from coalib.results.Diff import Diff
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY


class HaskellLintBear(LocalBear, Lint):
    executable = 'hlint'
    arguments = '--json {filename}'
    severity_map = {
        "Error": RESULT_SEVERITY.MAJOR,
        "Warning": RESULT_SEVERITY.NORMAL,
        "Suggestion": RESULT_SEVERITY.INFO}
    gives_corrected = True

    def run(self, filename, file):
        '''
        Checks the given file with hlint.
        '''
        return self.lint(filename=filename, file=file)

    def _process_corrected(self, output, filename, file):
        output = json.loads("".join(output))

        for issue in output:
            assert issue["startLine"] == issue["endLine"]
            diff = Diff(file)
            line_nr = issue["startLine"]
            line_to_change = file[line_nr-1]
            newline = line_to_change.replace(issue["from"], issue["to"])
            diff.change_line(line_nr, line_to_change, newline)

            yield Result.from_values(
                origin=self,
                message=issue["hint"],
                file=filename,
                severity=self.severity_map[issue["severity"]],
                line=issue["startLine"],
                diffs={filename: diff})

@@ -43,24 +43,26 @@
 ons.Lint
+er
  import 
 Lint%0Afro
@@ -57,56 +57,13 @@
 ort 
-Lint%0Afrom coalib.bears.LocalBear import LocalBea
+linte
 r%0Afr
@@ -203,108 +203,117 @@
 Y%0A%0A%0A
-class HaskellLintBear(LocalBear, Lint):%0A    executable = '
+@linter(executable='hlint')%0Aclass HaskellLintBear:%0A    %22%22%22%0A    Checks the given file with %60%60
 hlint
-'
+%60%60.
 %0A    
-arguments = '--json %7Bfilename%7D'
+%22%22%22%0A
 %0A   
@@ -329,25 +329,16 @@
 _map = %7B
-%0A        
 %22Error%22:
@@ -361,16 +361,28 @@
 .MAJOR,%0A
+            
         
@@ -416,16 +416,28 @@
 NORMAL,%0A
+            
         
@@ -476,191 +476,115 @@
 FO%7D%0A
+%0A
     
-gives_corrected = True%0A%0A    def run(self, filename, file):%0A        '''%0A        Checks the given file with hlint.%0A        '''%0A        return self.lint(filename=filename, file=file)
+@staticmethod%0A    def create_arguments(filename, file, config_file):%0A        return '--json', filename
 %0A%0A  
@@ -593,17 +593,16 @@
 def 
-_
 process_
 corr
@@ -601,17 +601,14 @@
 ess_
-corrected
+output
 (sel
@@ -667,24 +667,15 @@
 ads(
-%22%22.join(
 output)
-)
 %0A%0A  

Refactor to Linter v2 API