<s> import sublime <EOL> import sublime_plugin <EOL> import re <EOL> from . gotools_util import Buffers <EOL> from . gotools_util import GoBuffers <EOL> from . gotools_util import Logger <EOL> from . gotools_util import ToolRunner <EOL> from . gotools_settings import GoToolsSettings <EOL> class GotoolsFormatOnSave ( sublime_plugin . EventListener ) : <EOL> def on_pre_save ( self , view ) : <EOL> if not GoBuffers . is_go_source ( view ) : return <EOL> if not GoToolsSettings . get ( ) . format_on_save : return <EOL> view . run_command ( '<STR_LIT>' ) <EOL> class GotoolsFormat ( sublime_plugin . TextCommand ) : <EOL> def is_enabled ( self ) : <EOL> return GoBuffers . is_go_source ( self . view ) <EOL> def run ( self , edit ) : <EOL> command = "<STR_LIT>" <EOL> args = [ ] <EOL> if GoToolsSettings . get ( ) . format_backend == "<STR_LIT>" : <EOL> command = "<STR_LIT>" <EOL> args = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> elif GoToolsSettings . get ( ) . format_backend in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> command = "<STR_LIT>" <EOL> args = [ "<STR_LIT>" ] <EOL> stdout , stderr , rc = ToolRunner . run ( command , args , stdin = Buffers . buffer_text ( self . view ) ) <EOL> self . view . erase_regions ( "<STR_LIT>" ) <EOL> if rc == <NUM_LIT:2> : <EOL> self . show_syntax_errors ( stderr ) <EOL> return <EOL> if rc != <NUM_LIT:0> : <EOL> Logger . log ( "<STR_LIT>" + str ( rc ) + "<STR_LIT>" + stderr ) <EOL> return <EOL> if GoToolsSettings . get ( ) . format_backend == "<STR_LIT>" : <EOL> command = "<STR_LIT>" <EOL> args = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> stdout , stderr , rc = ToolRunner . run ( command , args , stdin = stdout . encode ( '<STR_LIT:utf-8>' ) ) <EOL> self . view . erase_regions ( "<STR_LIT>" ) <EOL> if rc == <NUM_LIT:2> : <EOL> self . show_syntax_errors ( stderr ) <EOL> return <EOL> if rc != <NUM_LIT:0> : <EOL> Logger . log ( "<STR_LIT>" + str ( rc ) + "<STR_LIT>" + stderr ) <EOL> return <EOL> self . view . window ( ) . run_command ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . prev_viewport_pos = self . view . viewport_position ( ) <EOL> self . view . replace ( edit , sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) , stdout ) <EOL> sublime . set_timeout ( self . restore_viewport , <NUM_LIT:0> ) <EOL> def restore_viewport ( self ) : <EOL> self . view . set_viewport_position ( self . prev_viewport_pos , False ) <EOL> def show_syntax_errors ( self , stderr ) : <EOL> output_view = self . view . window ( ) . create_output_panel ( '<STR_LIT>' ) <EOL> output_view . set_scratch ( True ) <EOL> output_view . settings ( ) . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> output_view . run_command ( "<STR_LIT>" ) <EOL> output_view . run_command ( "<STR_LIT>" ) <EOL> syntax_output = stderr . replace ( "<STR_LIT>" , self . view . file_name ( ) ) <EOL> output_view . run_command ( '<STR_LIT>' , { '<STR_LIT>' : syntax_output } ) <EOL> self . view . window ( ) . run_command ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> marks = [ ] <EOL> for error in stderr . splitlines ( ) : <EOL> match = re . match ( "<STR_LIT>" , error ) <EOL> if not match or not match . group ( <NUM_LIT:2> ) : <EOL> Logger . log ( "<STR_LIT>" + error + "<STR_LIT>" + str ( match ) ) <EOL> continue <EOL> row = int ( match . group ( <NUM_LIT:2> ) ) <EOL> pt = self . view . text_point ( row - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> Logger . log ( "<STR_LIT>" + str ( row ) ) <EOL> marks . append ( sublime . Region ( pt ) ) <EOL> if len ( marks ) > <NUM_LIT:0> : <EOL> self . view . add_regions ( "<STR_LIT>" , marks , "<STR_LIT>" , "<STR_LIT>" , sublime . DRAW_STIPPLED_UNDERLINE | sublime . PERSISTENT ) </s>
