Re: [code] Python and events.FILE_AFTER_SAVE

From: Robert Gieseke <rob.g.att.web.de>
Date: Tue, 29 Apr 2014 11:56:52 +0200

Hi Mitchell,

on my machine the syntax check is the same for Python 2.7 and 3.4, so it
doesn't work for 2.7.

The following should work for the Python module's init.lua (without the
version check):

-- Show syntax errors as annotations.
events.connect(events.FILE_AFTER_SAVE, function()
   if buffer:get_lexer() ~= 'python' then return end
   buffer:annotation_clear_all()
   p = io.popen('python -m py_compile "'..buffer.filename..'" 2>&1')
   local out = p:read('*all')
   p:close()
   if out:match('^%s*File') then
     local line = out:match('^%s*File ".-", line (%d+)')
     if line and tonumber(line) > 0 then
       line = tonumber(line) - 1
       local msg = out:match('^[^\r\n]+[\r\n]+(.-)%s*$')
       -- If the error line is not onscreen, annotate the current line.
       if (line < buffer.first_visible_line or
           line > buffer.first_visible_line + buffer.lines_on_screen) then
         msg = 'line '..(line + 1)..'\n'..msg
         line = buffer:line_from_position(buffer.current_pos)
       end
       buffer.annotation_visible = 2
       buffer.annotation_text[line] = msg
       buffer.annotation_style[line] = 8 -- error style number
       buffer:goto_line(line)
     end
   end
end)

I came across a syntax output backport
(http://bugs.python.org/issue7842) so maybe that's where it changed.

Cheers,
Robert

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Tue 29 Apr 2014 - 05:56:52 EDT

This archive was generated by hypermail 2.2.0 : Tue 29 Apr 2014 - 06:51:46 EDT