<s> from __future__ import unicode_literals <EOL> from lib . gherkin import GherkinParser , GherkinFormatter <EOL> class ViewFormatter ( object ) : <EOL> def __init__ ( self , sublime , view ) : <EOL> self . _sublime = sublime <EOL> self . _view = view <EOL> def format_view ( self , edit ) : <EOL> self . _store_caret_position ( ) <EOL> self . _format_and_replace ( edit ) <EOL> self . _reset_caret_position ( ) <EOL> def _format_and_replace ( self , edit ) : <EOL> for region in self . _view . sel ( ) : <EOL> if not region . empty ( ) : <EOL> self . _format_region ( region , edit ) ; <EOL> else : <EOL> entire_buffer = self . _sublime . Region ( <NUM_LIT:0> , self . _view . size ( ) ) <EOL> self . _format_region ( entire_buffer , edit ) <EOL> def _store_caret_position ( self ) : <EOL> self . _pos = self . _view . sel ( ) [ <NUM_LIT:0> ] . begin ( ) <EOL> def _reset_caret_position ( self ) : <EOL> self . _view . sel ( ) . clear ( ) <EOL> self . _view . sel ( ) . add ( self . _sublime . Region ( self . _pos , self . _pos ) ) <EOL> def _format_region ( self , region , edit ) : <EOL> parser = GherkinParser ( self . _view . substr ( region ) ) <EOL> parsed = parser . parse ( ) <EOL> result = GherkinFormatter ( ) . format ( parsed ) <EOL> self . _view . replace ( edit , region , result ) </s>
