<commit_before>import vim, urllib2, urllib, urlparse, logging, json, os, os.path, cgi, types
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from SocketServer import ThreadingMixIn


logger = logging.getLogger('omnisharp')
logger.setLevel(logging.WARNING)

log_dir = os.path.join(vim.eval('expand("<sfile>:p:h")'), '..', 'log')
if not os.path.exists(log_dir):
    os.makedirs(log_dir)
hdlr = logging.FileHandler(os.path.join(log_dir, 'python.log'))
logger.addHandler(hdlr) 

formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)


def getResponse(endPoint, additionalParameters=None):
    parameters = {}
    parameters['line'] = vim.eval('line(".")')
    parameters['column'] = vim.eval('col(".")')
    parameters['buffer'] = '\r\n'.join(vim.eval("getline(1,'$')")[:])
    if(vim.eval('exists("+shellslash") && &shellslash')):
        parameters['filename'] = vim.current.buffer.name.replace('/', '\\')
    else:
        parameters['filename'] = vim.current.buffer.name

    if(additionalParameters != None):
        parameters.update(additionalParameters)

    target = urlparse.urljoin(vim.eval('g:OmniSharp_host'), endPoint)
    parameters = urllib.urlencode(parameters)
    try:
        response = urllib2.urlopen(target, parameters, timeout=int(vim.eval('g:OmniSharp_timeout')))
        return response.read()
    except Exception as e:
        print("OmniSharp : Could not connect to " + target + ": " + str(e))
        return ''


#All of these functions take vim variable names as parameters
def getCompletions(ret, column, partialWord):
    parameters = {}
    parameters['column'] = vim.eval(column)
    parameters['wordToComplete'] = vim.eval(partialWord)

    parameters['buffer'] = '\r\n'.join(vim.eval('s:textBuffer')[:])
    js = getResponse('/autocomplete', parameters)

    command_base = ("add(" + ret +
            ", {'word': '%(CompletionText)s', 'abbr': '%(DisplayText)s', 'info': \"%(Description)s\", 'icase': 1, 'dup':1 })")
    enc = vim.eval('&encoding')
    if(js != ''):
        completions = json.loads(js)
        for completion in completions:
            try:
                completion['Description'] = completion['Description'].replace('\r\n', '')
                command = command_base % completion
                if type(command) == types.StringType:
                    vim.eval(command)
                else:
                    vim.eval(command.encode(enc))
            except:
                logger.error(command)

def findUsages(ret):
    js = getResponse('/findusages')
    if(js != ''):<commit_msg>default completion displays same as YCM plugin<commit_after>import vim, urllib2, urllib, urlparse, logging, json, os, os.path, cgi, types
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from SocketServer import ThreadingMixIn


logger = logging.getLogger('omnisharp')
logger.setLevel(logging.WARNING)

log_dir = os.path.join(vim.eval('expand("<sfile>:p:h")'), '..', 'log')
if not os.path.exists(log_dir):
    os.makedirs(log_dir)
hdlr = logging.FileHandler(os.path.join(log_dir, 'python.log'))
logger.addHandler(hdlr) 

formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)


def getResponse(endPoint, additionalParameters=None):
    parameters = {}
    parameters['line'] = vim.eval('line(".")')
    parameters['column'] = vim.eval('col(".")')
    parameters['buffer'] = '\r\n'.join(vim.eval("getline(1,'$')")[:])
    if(vim.eval('exists("+shellslash") && &shellslash')):
        parameters['filename'] = vim.current.buffer.name.replace('/', '\\')
    else:
        parameters['filename'] = vim.current.buffer.name

    if(additionalParameters != None):
        parameters.update(additionalParameters)

    target = urlparse.urljoin(vim.eval('g:OmniSharp_host'), endPoint)
    parameters = urllib.urlencode(parameters)
    try:
        response = urllib2.urlopen(target, parameters, timeout=int(vim.eval('g:OmniSharp_timeout')))
        return response.read()
    except Exception as e:
        print("OmniSharp : Could not connect to " + target + ": " + str(e))
        return ''


#All of these functions take vim variable names as parameters
def getCompletions(ret, column, partialWord):
    parameters = {}
    parameters['column'] = vim.eval(column)
    parameters['wordToComplete'] = vim.eval(partialWord)

    parameters['buffer'] = '\r\n'.join(vim.eval('s:textBuffer')[:])
    js = getResponse('/autocomplete', parameters)

    command_base = ("add(" + ret +
            ", {'word': '%(CompletionText)s', 'menu': '%(DisplayText)s', 'info': \"%(Description)s\", 'icase': 1, 'dup':1 })")
    enc = vim.eval('&encoding')
    if(js != ''):
        completions = json.loads(js)
        for completion in completions:
            try:
                completion['Description'] = completion['Description'].replace('\r\n', '')
                command = command_base % completion
                if type(command) == types.StringType:
                    vim.eval(command)
                else:
                    vim.eval(command.encode(enc))
            except:
                logger.error(command)

def findUsages(ret):
    js = getResponse('/findusages')
    if(js != ''):
{'fraction_non_alphanumeric': 0.1667620683075749, 'fraction_numerical': 0.0019080328181644724, 'mean_word_length': 3.4161752316764953, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5481623', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1598, 'n_words': 300}