<s> import os <EOL> import re <EOL> from os . path import exists , dirname <EOL> from . base import Base <EOL> from deoplete . util import set_default , get_simple_buffer_config <EOL> class Source ( Base ) : <EOL> def __init__ ( self , vim ) : <EOL> Base . __init__ ( self , vim ) <EOL> self . name = '<STR_LIT:file>' <EOL> self . mark = '<STR_LIT>' <EOL> self . min_pattern_length = <NUM_LIT:0> <EOL> self . rank = <NUM_LIT> <EOL> set_default ( self . vim , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> def get_complete_position ( self , context ) : <EOL> pos = context [ '<STR_LIT:input>' ] . rfind ( '<STR_LIT:/>' ) <EOL> return pos if pos < <NUM_LIT:0> else pos + <NUM_LIT:1> <EOL> def gather_candidates ( self , context ) : <EOL> p = self . __longest_path_that_exists ( context [ '<STR_LIT:input>' ] ) <EOL> if p in ( None , [ ] ) or p == '<STR_LIT:/>' or re . search ( '<STR_LIT>' , p ) : <EOL> return [ ] <EOL> complete_str = self . __substitute_path ( dirname ( p ) + '<STR_LIT:/>' ) <EOL> if not os . path . isdir ( complete_str ) : <EOL> return [ ] <EOL> hidden = context [ '<STR_LIT>' ] . find ( '<STR_LIT:.>' ) == <NUM_LIT:0> <EOL> dirs = [ x for x in os . listdir ( complete_str ) <EOL> if os . path . isdir ( complete_str + x ) and <EOL> ( hidden or x [ <NUM_LIT:0> ] != '<STR_LIT:.>' ) ] <EOL> files = [ x for x in os . listdir ( complete_str ) <EOL> if not os . path . isdir ( complete_str + x ) and <EOL> ( hidden or x [ <NUM_LIT:0> ] != '<STR_LIT:.>' ) ] <EOL> return [ { '<STR_LIT>' : x , '<STR_LIT>' : x + '<STR_LIT:/>' } for x in sorted ( dirs ) <EOL> ] + [ { '<STR_LIT>' : x } for x in sorted ( files ) ] <EOL> def __longest_path_that_exists ( self , input_str ) : <EOL> data = re . split ( self . vim . call ( <EOL> '<STR_LIT>' , <EOL> self . vim . options [ '<STR_LIT>' ] ) , input_str ) <EOL> pos = [ "<STR_LIT:U+0020>" . join ( data [ i : ] ) for i in range ( len ( data ) ) ] <EOL> existing_paths = list ( filter ( lambda x : exists ( <EOL> dirname ( self . __substitute_path ( x ) ) ) , pos ) ) <EOL> if existing_paths and len ( existing_paths ) > <NUM_LIT:0> : <EOL> return sorted ( existing_paths ) [ - <NUM_LIT:1> ] <EOL> return None <EOL> def __substitute_path ( self , path ) : <EOL> buffer_path = get_simple_buffer_config ( <EOL> self . vim , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> m = re . match ( r'<STR_LIT>' , path ) <EOL> if m : <EOL> h = self . vim . funcs . repeat ( '<STR_LIT>' , len ( m . group ( <NUM_LIT:1> ) ) ) <EOL> return re . sub ( r'<STR_LIT>' , <EOL> self . vim . funcs . fnamemodify ( <EOL> ( self . vim . funcs . bufname ( '<STR_LIT:%>' ) <EOL> if buffer_path <EOL> else self . vim . funcs . getcwd ( ) ) , '<STR_LIT>' + h ) , <EOL> path ) <EOL> m = re . match ( r'<STR_LIT>' , path ) <EOL> if m and os . environ . get ( '<STR_LIT>' ) : <EOL> return re . sub ( r'<STR_LIT>' , os . environ . get ( '<STR_LIT>' ) , path ) <EOL> m = re . match ( r'<STR_LIT>' , path ) <EOL> if m and os . environ . get ( m . group ( <NUM_LIT:1> ) ) : <EOL> return re . sub ( r'<STR_LIT>' , os . environ . get ( m . group ( <NUM_LIT:1> ) ) , path ) <EOL> return path </s>
