Skip to content

Instantly share code, notes, and snippets.

@NapalmHorn
Forked from rgieseke/python_syntax_check.lua
Last active January 3, 2016 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NapalmHorn/1ac7e13673b38ffe8be9 to your computer and use it in GitHub Desktop.
Save NapalmHorn/1ac7e13673b38ffe8be9 to your computer and use it in GitHub Desktop.
Textadept: Check Python syntax after saving.
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)
@NapalmHorn
Copy link
Author

fixed to work with current Text Adept API and use flake8 for style and syntax checks

@NapalmHorn
Copy link
Author

Shows all errors not just the first one.

@NapalmHorn
Copy link
Author

Fixed off by one error because of how textadept and python number lines.

@NapalmHorn
Copy link
Author

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