<s> import sublime_plugin <EOL> import sublime <EOL> import re <EOL> import time <EOL> from os . path import basename <EOL> MIN_WORD_SIZE = <NUM_LIT:3> <EOL> MAX_WORD_SIZE = <NUM_LIT:50> <EOL> MAX_VIEWS = <NUM_LIT:20> <EOL> MAX_WORDS_PER_VIEW = <NUM_LIT:100> <EOL> MAX_FIX_TIME_SECS_PER_VIEW = <NUM_LIT> <EOL> class AllAutocomplete ( sublime_plugin . EventListener ) : <EOL> def on_query_completions ( self , view , prefix , locations ) : <EOL> words = [ ] <EOL> other_views = [ v for v in sublime . active_window ( ) . views ( ) if v . id != view . id ] <EOL> views = [ view ] + other_views <EOL> views = views [ <NUM_LIT:0> : MAX_VIEWS ] <EOL> for v in views : <EOL> if len ( locations ) > <NUM_LIT:0> and v . id == view . id : <EOL> view_words = v . extract_completions ( prefix , locations [ <NUM_LIT:0> ] ) <EOL> else : <EOL> view_words = v . extract_completions ( prefix ) <EOL> view_words = filter_words ( view_words ) <EOL> view_words = fix_truncation ( v , view_words ) <EOL> words += [ ( w , v ) for w in view_words ] <EOL> words = without_duplicates ( words ) <EOL> matches = [ ] <EOL> for w , v in words : <EOL> trigger = w <EOL> contents = w . replace ( '<STR_LIT:$>' , '<STR_LIT>' ) <EOL> if v . id != view . id and v . file_name ( ) : <EOL> trigger += '<STR_LIT>' % basename ( v . file_name ( ) ) <EOL> matches . append ( ( trigger , contents ) ) <EOL> return matches <EOL> def filter_words ( words ) : <EOL> words = words [ <NUM_LIT:0> : MAX_WORDS_PER_VIEW ] <EOL> return [ w for w in words if MIN_WORD_SIZE <= len ( w ) <= MAX_WORD_SIZE ] <EOL> def without_duplicates ( words ) : <EOL> result = [ ] <EOL> used_words = [ ] <EOL> for w , v in words : <EOL> if w not in used_words : <EOL> used_words . append ( w ) <EOL> result . append ( ( w , v ) ) <EOL> return result <EOL> def fix_truncation ( view , words ) : <EOL> fixed_words = [ ] <EOL> start_time = time . time ( ) <EOL> for i , w in enumerate ( words ) : <EOL> match = view . find ( r'<STR_LIT>' + re . escape ( w ) + r'<STR_LIT>' , <NUM_LIT:0> ) <EOL> truncated = is_empty_match ( match ) <EOL> if truncated : <EOL> extended_words = [ ] <EOL> view . find_all ( r'<STR_LIT>' + re . escape ( w ) + r'<STR_LIT>' , <NUM_LIT:0> , "<STR_LIT>" , extended_words ) <EOL> if len ( extended_words ) > <NUM_LIT:0> : <EOL> fixed_words += extended_words <EOL> else : <EOL> fixed_words . append ( w ) <EOL> else : <EOL> fixed_words . append ( w ) <EOL> if time . time ( ) - start_time > MAX_FIX_TIME_SECS_PER_VIEW : <EOL> return fixed_words + words [ i + <NUM_LIT:1> : ] <EOL> return fixed_words <EOL> if sublime . version ( ) >= '<STR_LIT>' : <EOL> def is_empty_match ( match ) : <EOL> return match . empty ( ) <EOL> else : <EOL> def is_empty_match ( match ) : <EOL> return match is None </s>
