<s> import sublime <EOL> import sublime_plugin <EOL> class JasmineScaffoldCommand ( sublime_plugin . TextCommand ) : <EOL> DESCRIBE_LINE = '<STR_LIT>' <EOL> IT_LINE = '<STR_LIT>' <EOL> def translatingTabsToSpaces ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . view . settings ( ) . get ( '<STR_LIT>' ) <EOL> def spacingSetting ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . view . settings ( ) . get ( '<STR_LIT>' ) <EOL> def countLineWhitespace ( self , line , type ) : <EOL> """<STR_LIT>""" <EOL> return len ( line ) - len ( line . lstrip ( type ) ) <EOL> def getSelectedRegionStart ( self , view , point ) : <EOL> """<STR_LIT>""" <EOL> lineRegion = view . line ( point ) <EOL> pos = lineRegion . a <EOL> end = lineRegion . b <EOL> while pos < end : <EOL> ch = view . substr ( pos ) <EOL> if ch != self . spacingSetting ( ) : <EOL> break <EOL> pos += <NUM_LIT:1> <EOL> return pos <EOL> def buildScaffold ( self , lines , spacingCount , spacingType , usingSpaces ) : <EOL> """<STR_LIT>""" <EOL> scaffold = [ ] <EOL> for index , line in enumerate ( lines ) : <EOL> lineText = line . lstrip ( spacingType ) <EOL> currentWhitespace = self . countLineWhitespace ( line , spacingType ) <EOL> if index < len ( lines ) - <NUM_LIT:1> : <EOL> nextWhitespace = self . countLineWhitespace ( <EOL> lines [ index + <NUM_LIT:1> ] , spacingType ) <EOL> else : <EOL> nextWhitespace = self . countLineWhitespace ( <EOL> lines [ <NUM_LIT:0> ] , spacingType ) <EOL> descRepl = self . DESCRIBE_LINE % lineText <EOL> indented = descRepl . rjust ( <EOL> len ( descRepl ) + currentWhitespace , spacingType ) <EOL> if currentWhitespace >= nextWhitespace : <EOL> itRepl = self . IT_LINE % lineText <EOL> decreasingWhitespace = currentWhitespace <EOL> itReplIndent = itRepl . rjust ( <EOL> len ( itRepl ) + currentWhitespace , spacingType ) <EOL> indented = [ ] <EOL> indented . append ( itReplIndent + '<STR_LIT>' . rjust ( <EOL> <NUM_LIT:3> + currentWhitespace , spacingType ) + '<STR_LIT>' ) <EOL> while decreasingWhitespace > nextWhitespace : <EOL> decreasingWhitespace -= spacingCount if usingSpaces else <NUM_LIT:1> <EOL> closeBrackets = '<STR_LIT>' . rjust ( <EOL> <NUM_LIT:3> + decreasingWhitespace , spacingType ) + '<STR_LIT>' <EOL> indented . append ( closeBrackets ) <EOL> scaffold . extend ( indented ) <EOL> return scaffold <EOL> def run ( self , edit ) : <EOL> lines = [ ] <EOL> fullFile = True <EOL> if len ( self . view . sel ( ) [ <NUM_LIT:0> ] ) == <NUM_LIT:0> : <EOL> region = sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) <EOL> else : <EOL> fullFile = False <EOL> for sel in self . view . sel ( ) : <EOL> start = self . getSelectedRegionStart ( <EOL> self . view , min ( sel . a , sel . b ) ) <EOL> selectionEnd = self . view . line ( max ( sel . a , sel . b ) ) . b <EOL> region = sublime . Region ( self . view . line ( start ) . a , selectionEnd ) <EOL> file = self . view . substr ( region ) <EOL> for line in file . splitlines ( ) : <EOL> lines . append ( line ) <EOL> if self . translatingTabsToSpaces ( ) : <EOL> scaffolded = self . buildScaffold ( <EOL> lines , self . spacingSetting ( ) , '<STR_LIT:U+0020>' , True ) <EOL> else : <EOL> scaffolded = self . buildScaffold ( <EOL> lines , self . spacingSetting ( ) , '<STR_LIT:\t>' , False ) <EOL> self . view . sel ( ) . clear ( ) <EOL> self . view . sel ( ) . add ( <EOL> sublime . Region ( self . view . size ( ) if fullFile else selectionEnd ) ) <EOL> self . view . replace ( edit , region , '<STR_LIT>' . join ( scaffolded ) ) </s>
