<s> import os <EOL> import re <EOL> import subprocess <EOL> import sys <EOL> import json <EOL> import tempfile <EOL> from . base import Base <EOL> from deoplete . util import charpos2bytepos <EOL> from deoplete . util import error <EOL> class Source ( Base ) : <EOL> def __init__ ( self , vim ) : <EOL> Base . __init__ ( self , vim ) <EOL> self . name = '<STR_LIT>' <EOL> self . mark = '<STR_LIT>' <EOL> self . filetypes = [ '<STR_LIT>' ] <EOL> self . input_pattern = r'<STR_LIT>' <EOL> self . rank = <NUM_LIT> <EOL> self . _source_kitten_binary = self . vim . vars [ '<STR_LIT>' ] <EOL> def get_complete_position ( self , context ) : <EOL> m = re . search ( r'<STR_LIT>' , context [ '<STR_LIT:input>' ] ) <EOL> return m . start ( ) if m else - <NUM_LIT:1> <EOL> def gather_candidates ( self , context ) : <EOL> line = self . vim . current . window . cursor [ <NUM_LIT:0> ] <EOL> column = context [ '<STR_LIT>' ] <EOL> buf = self . vim . current . buffer <EOL> offset = self . vim . call ( '<STR_LIT>' , line ) + charpos2bytepos ( self . vim , context [ '<STR_LIT:input>' ] [ : column ] , column ) - <NUM_LIT:1> <EOL> source = '<STR_LIT:\n>' . join ( buf ) . encode ( ) <EOL> fp = tempfile . NamedTemporaryFile ( delete = False , suffix = "<STR_LIT>" ) <EOL> fp . write ( source ) <EOL> fp . flush ( ) <EOL> fp . close ( ) <EOL> args = [ self . source_kitten_binary ( ) , "<STR_LIT>" , "<STR_LIT>" , fp . name , "<STR_LIT>" , str ( offset ) ] <EOL> process = subprocess . Popen ( args , <EOL> stdin = subprocess . PIPE , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE , <EOL> start_new_session = True ) <EOL> stdout_data , stderr_data = process . communicate ( ) <EOL> result = stdout_data . decode ( ) <EOL> os . remove ( fp . name ) <EOL> if stderr_data != b'<STR_LIT>' : <EOL> raise Exception ( ( args , stderr_data . decode ( ) ) ) <EOL> results = json . loads ( result ) <EOL> return self . identifiers_from_result ( results ) <EOL> def identifiers_from_result ( self , result ) : <EOL> out = [ ] <EOL> candidates = [ ] <EOL> longest_desc_length = <NUM_LIT:0> <EOL> longest_desc = '<STR_LIT>' <EOL> for complete in result : <EOL> candidates . append ( complete ) <EOL> desc_len = len ( complete [ '<STR_LIT:name>' ] ) <EOL> if desc_len > longest_desc_length : <EOL> longest_desc = complete [ '<STR_LIT:name>' ] <EOL> longest_desc_length = desc_len <EOL> for completion in candidates : <EOL> description = completion [ '<STR_LIT:name>' ] <EOL> _type = completion [ '<STR_LIT>' ] <EOL> abbr = description + '<STR_LIT:U+0020>' + _type . rjust ( ( len ( description ) - longest_desc_length ) + <NUM_LIT:1> ) <EOL> info = _type <EOL> candidate = dict ( word = description , <EOL> abbr = abbr , <EOL> info = info , <EOL> dup = <NUM_LIT:1> <EOL> ) <EOL> out . append ( candidate ) <EOL> return out <EOL> def calltips_from_result ( self , result ) : <EOL> out = [ ] <EOL> result = result [ <NUM_LIT:1> : ] <EOL> for calltip in result : <EOL> candidate = dict ( <EOL> abbr = calltip , <EOL> word = self . parse_function_parameters ( calltip ) , <EOL> info = calltip <EOL> ) <EOL> out . append ( candidate ) <EOL> return out <EOL> def parse_function_parameters ( self , decl ) : <EOL> """<STR_LIT>""" <EOL> last_lparen = decl . rfind ( '<STR_LIT:(>' ) <EOL> last_rparen = decl . rfind ( '<STR_LIT:)>' ) <EOL> param_list = decl [ last_lparen + <NUM_LIT:1> : last_rparen ] <EOL> param_list = param_list . split ( '<STR_LIT:U+0020>' ) <EOL> param_list = param_list [ <NUM_LIT:1> : : <NUM_LIT:2> ] <EOL> return '<STR_LIT:U+0020>' . join ( param_list ) <EOL> def source_kitten_binary ( self ) : <EOL> try : <EOL> if os . path . isfile ( self . _source_kitten_binary ) : <EOL> return self . _source_kitten_binary <EOL> else : <EOL> raise <EOL> except Exception : <EOL> return self . find_binary_path ( '<STR_LIT>' ) <EOL> def find_binary_path ( self , cmd ) : <EOL> def is_exec ( fpath ) : <EOL> return os . path . isfile ( fpath ) and os . access ( fpath , os . X_OK ) <EOL> fpath , fname = os . path . split ( cmd ) <EOL> if fpath : <EOL> if is_exec ( cmd ) : <EOL> return cmd <EOL> else : <EOL> for path in os . environ [ "<STR_LIT>" ] . split ( os . pathsep ) : <EOL> path = path . strip ( '<STR_LIT:">' ) <EOL> binary = os . path . join ( path , cmd ) <EOL> if is_exec ( binary ) : <EOL> return binary <EOL> return error ( self . vim , cmd + '<STR_LIT>' ) </s>
