Last active
November 16, 2017 16:13
-
-
Save somini/26c845d4f7f6dc2630427a67cb371122 to your computer and use it in GitHub Desktop.
LanguageTool NeoMake Definitions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import sys | |
import subprocess | |
import json | |
LANGUAGETOOL = 'languagetool' | |
LANG = 'en-GB' | |
input_file = None | |
if len(sys.argv) > 1: | |
# Use the given filename | |
fname = sys.argv[1] | |
input_file = open(fname) | |
else: | |
# Use standart input | |
fname = '-' | |
input_file = sys.stdin | |
if len(sys.argv) > 2: | |
LANG = sys.argv[2] | |
in_file = ''.join(input_file.readlines()) | |
output = subprocess.check_output([LANGUAGETOOL, '--json', '-l', LANG, '-'], | |
input=in_file.encode(), | |
stderr=subprocess.DEVNULL) | |
output_json = json.loads(output) | |
for m in output_json['matches']: | |
offset = m['offset'] | |
length = m['length'] | |
rule_id = m['rule']['id'] | |
rule_type = m['rule']['issueType'] | |
message_long = m['message'] | |
message_short = m['shortMessage'] | |
extra_message = '' | |
error = False | |
if rule_type == 'misspelling': | |
error = True | |
word = in_file[offset:offset+length] | |
replacements = m['replacements'][:3] # Top 3 | |
message = '"%s"' % word | |
if replacements != list(): | |
replacement_values = map(lambda e: '"%s"' % e['value'], replacements) | |
message += ' => ' + '|'.join(replacement_values) | |
else: | |
message = message_long | |
line_num = in_file.count('\n', 0, offset) + 1 | |
column_num = offset - in_file.rfind('\n', 0, offset) | |
if error: | |
str_type = 'E' | |
else: | |
str_type = 'W' | |
line = '%s:%d:%d:%s %s' % (fname, line_num, column_num, str_type, message) | |
print(line) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function! neomake#makers#ft#text#languagetool() abort | |
return { | |
\ 'exe': 'languagetool-wrapper', | |
\ 'errorformat': '%f:%l:%c:%t %m', | |
\ } | |
endfunction | |
function! s:fn_languagetool_curl(jobinfo) abort dict | |
" XXX: update method to get filename! | |
let self.args = ['-s', | |
\ '--data-urlencode', printf('text@%s', fnameescape(fnamemodify(bufname(a:jobinfo.bufnr), ':p'))), | |
\ '--data-urlencode', printf('language=%s', get(split(&spelllang, ','), 0, 'en')), | |
\ '--data-urlencode', printf('motherTongue=%s', 'pt-PT'), | |
\ 'http://localhost:8081/v2/check'] | |
endfunction | |
function! neomake#makers#ft#text#GetEntriesForOutput_LanguagetoolCurl(context) abort | |
if a:context.source ==# 'stderr' | |
return [] | |
endif | |
let output = neomake#utils#JSONdecode(join(a:context.output, '')) | |
call neomake#utils#DebugObject('output', output) | |
if !len(output) | |
return [] | |
endif | |
let entries = [] | |
for _m in get(output, 'matches', []) | |
let offset = get(_m, 'offset') + 0 | |
let length = get(_m, 'length') + 0 | |
let rule = get(_m, 'rule') | |
let rule_id = get(rule, 'id') | |
let rule_type = get(rule, 'issueType') | |
let message_long = get(_m, 'message') | |
let message_short = get(_m, 'shortMessage') | |
let message = '' | |
if rule_type == 'misspelling' | |
let type = 'E' | |
let replacements = map(get(_m, 'replacements'), 'v:val.value') | |
if !len(replacements) | |
let message = message_long | |
else | |
let message = message_short . ' => ' . join((map(replacements,'"\"". v:val ."\""')), ' | ') | |
endif | |
else | |
let type = 'W' | |
let message = message_long | |
endif | |
let offset = offset | |
let line_maybe = byte2line(offset) | |
if line2byte(line_maybe + 1) == offset + 1 | |
" Next line starts in the next byte | |
" This means the error is in the first character of the next line | |
let line_num = line_maybe + 1 | |
let col_num = 1 | |
else | |
let line_num = line_maybe | |
let col_num = offset - line2byte(line_num) + 2 | |
endif | |
let entry = { | |
\ 'text': message, | |
\ 'lnum': line_num, | |
\ 'col': col_num, | |
\ 'length': length, | |
\ 'type': type, | |
\ 'bufnr': a:context.jobinfo.bufnr, | |
\ } | |
call add(entries, entry) | |
endfor | |
return entries | |
endfunction | |
function! neomake#makers#ft#text#languagetool_curl() abort | |
return { | |
\ 'exe': 'curl', | |
\ 'fn': function('s:fn_languagetool_curl'), | |
\ 'process_output': function('neomake#makers#ft#text#GetEntriesForOutput_LanguagetoolCurl'), | |
\ 'output_stream': 'stdout', | |
\ } | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment