<s> import os <EOL> import re <EOL> import subprocess <EOL> import sys <EOL> from . base import Base <EOL> from deoplete . util import charpos2bytepos <EOL> from deoplete . util import error <EOL> try : <EOL> current_dir = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> ujson_dir = os . path . dirname ( current_dir ) <EOL> sys . path . insert ( <NUM_LIT:0> , ujson_dir ) <EOL> from ujson import loads <EOL> except ImportError : <EOL> from json import loads <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 . gocode_binary = self . vim . vars [ '<STR_LIT>' ] <EOL> self . package_dot = self . vim . vars [ '<STR_LIT>' ] <EOL> self . sort_class = self . vim . vars [ '<STR_LIT>' ] <EOL> self . debug_enabled = self . vim . vars . get ( '<STR_LIT>' , <NUM_LIT:0> ) <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> process = subprocess . Popen ( [ self . GoCodeBinary ( ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> buf . name , <EOL> str ( offset ) ] , <EOL> stdin = subprocess . PIPE , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE , <EOL> start_new_session = True ) <EOL> process . stdin . write ( source ) <EOL> stdout_data , stderr_data = process . communicate ( ) <EOL> result = loads ( stdout_data . decode ( ) ) <EOL> try : <EOL> if result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ '<STR_LIT:class>' ] == '<STR_LIT>' : <EOL> error ( self . vim , '<STR_LIT>' ) <EOL> return [ ] <EOL> if self . sort_class : <EOL> class_dict = { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:type>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> out = [ ] <EOL> sep = '<STR_LIT:U+0020>' <EOL> for complete in result [ <NUM_LIT:1> ] : <EOL> word = complete [ '<STR_LIT:name>' ] <EOL> info = complete [ '<STR_LIT:type>' ] <EOL> _class = complete [ '<STR_LIT:class>' ] <EOL> abbr = str ( word + sep + info ) . replace ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> kind = _class <EOL> if _class == '<STR_LIT>' and self . package_dot : <EOL> word += '<STR_LIT:.>' <EOL> candidates = dict ( word = word , <EOL> abbr = abbr , <EOL> kind = kind , <EOL> info = info , <EOL> menu = self . mark , <EOL> dup = <NUM_LIT:1> <EOL> ) <EOL> if not self . sort_class or _class == '<STR_LIT>' : <EOL> out . append ( candidates ) <EOL> else : <EOL> class_dict [ _class ] . append ( candidates ) <EOL> if self . sort_class : <EOL> for c in self . sort_class : <EOL> for x in class_dict [ c ] : <EOL> out . append ( x ) <EOL> return out <EOL> except Exception : <EOL> return [ ] <EOL> def GoCodeBinary ( self ) : <EOL> try : <EOL> if os . path . isfile ( self . gocode_binary ) : <EOL> return self . gocode_binary <EOL> else : <EOL> raise <EOL> except Exception : <EOL> return self . FindBinaryPath ( '<STR_LIT>' ) <EOL> def FindBinaryPath ( 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 , '<STR_LIT>' ) </s>
