-
-
Save NapalmHorn/1ac7e13673b38ffe8be9 to your computer and use it in GitHub Desktop.
Textadept: Check Python syntax after saving.
This file contains 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
events.connect('file_after_save', | |
function() -- shows all Python errors as annotations | |
if buffer:get_lexer() == 'python' then | |
local lfs = require 'lfs' | |
local buffer = buffer | |
buffer:annotation_clear_all() | |
local filepath = buffer.filename:iconv(_CHARSET, 'UTF-8') | |
local filedir, filename = '', filepath | |
if filepath:find('[/\\]') then | |
filedir, filename = filepath:match('^(.+[/\\])([^/\\]+)$') | |
end | |
local current_dir = lfs.currentdir() | |
lfs.chdir(filedir) | |
local command = 'flake8 ' .. filedir .. filename | |
local p = io.popen(command..' 2>&1') | |
mylines = p:read("*a") | |
p:close() | |
line_end = string.find(mylines, "\n") | |
while line_end and mylines do | |
out, mylines = string.sub(mylines, 0, line_end), string.sub(mylines, line_end + 1) | |
lfs.chdir(current_dir) | |
local line, err_type, err_msg = | |
string.match(out, "(%d+):%d+: ([A-Z]%d%d%d) (.*)$") | |
line = tonumber(line) | |
if line and not buffer.annotation_text[line - 1] then | |
buffer.annotation_visible = buffer.ANNOTATION_BOXED | |
buffer.annotation_text[line - 1] = err_type .. ":" .. err_msg | |
buffer.annotation_style[line - 1] = 8 -- error style number | |
buffer:goto_line(line - 1) | |
elseif line then | |
buffer.annotation_text[line - 1] = buffer.annotation_text[line - 1] .. err_type .. ":" .. err_msg | |
buffer.annotation_style[line - 1] = 8 -- error style number | |
end | |
line_end = string.find(mylines, "\n") | |
end | |
end | |
end) |
Shows all errors not just the first one.
Fixed off by one error because of how textadept and python number lines.
Handles more than 1 error per line.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fixed to work with current Text Adept API and use flake8 for style and syntax checks