Skip to content

Instantly share code, notes, and snippets.

@rgieseke
Created November 30, 2010 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rgieseke/722253 to your computer and use it in GitHub Desktop.
Save rgieseke/722253 to your computer and use it in GitHub Desktop.
Textadept: Check Python syntax after saving.
events.connect('file_after_save',
function() -- show Python syntax 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 = 'python -c \"import py_compile; py_compile.compile(r\''
.. filename .. '\')\"'
local p = io.popen(command..' 2>&1')
local out = p:read('*line')
p:close()
lfs.chdir(current_dir)
if out then
local err_type, err_msg, line =
out:match("(.*:%s)%('(.*)',%s%(.+',%s(%d+)")
if line then
buffer.annotation_visible = 2
buffer:annotation_set_text(line - 1, err_type..err_msg)
buffer.annotation_style[line - 1] = 8 -- error style number
buffer:goto_line(line - 1)
end
end
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment