<commit_before>import neovim
import os, subprocess
import json
import threading
import time
from functools import partial

class RPC:
    def __init__(self, infile, outfile, handler):
        self.infile = infile
        self.outfile = outfile
        self.handler = handler

    def call(self, method, params, mid=None):
        content = {
                "jsonrpc": "2.0",
                "method": method,
                "params": params
                }
        if mid is not None:
            content["id"] = mid
        content = json.dumps(content)
        message = (
                "Content-Length: {}\r\n\r\n"
                "{}".format(len(content), content)
                )
        print(content)
        print()
        self.outfile.write(message)
        self.outfile.flush()

    def serve(self):
        while True:
            line = self.infile.readline()
            if line:
                contentLength = int(line.split(":")[1])
                self.infile.readline()
                content = self.infile.read(contentLength)
                print(content)
                print()
                self.handler.handle(json.loads(content))

@neovim.plugin
class LanguageClient:
    def __init__(self, nvim):
        self.nvim = nvim
        self.server = subprocess.Popen(
            ["/bin/bash", "/opt/rls/wrapper.sh"],
            # ["cargo", "run", "--manifest-path=/opt/rls/Cargo.toml"],
            # ['langserver-go', '-trace', '-logfile', '/tmp/langserver-go.log'],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True)
        self.rpc = RPC(self.server.stdout, self.server.stdin, self)
        threading.Thread(target=self.rpc.serve, name="RPC Server", daemon=True).start()
        self.mid = 0
        self.queue = {}
        self.capabilities = {}

    @neovim.command('LanguageClientInitialize')
    def initialize(self, rootPath=None):
        if rootPath is None:
            rootPath = getRootPath(self.nvim.current.buffer.name)

        mid = self.mid
        self.mid += 1
        self.queue[mid] = partial(self.handleInitializeResponse, mid);

        self.rpc.call('initialize', {
            "processId": os.getpid(),
            "rootPath": rootPath,
            "capabilities":{},
            "trace":"verbose"
            }, mid)

    def handleInitializeResponse(self, mid, result):
        del self.queue[mid]
        self.capabilities = result['capabilities']
        self.nvim.command('echom "LanguageClient started."')

    @neovim.function('LanguageClient_textDocument_didOpen')
    def textDocument_didOpen(self, filename=None):
        if filename is None:
            filename = self.nvim.current.buffer.name

        uri = convertToURI(filename)
        languageId = self.nvim.eval('&filetype')
        with open(filename) as f:
            text = f.read()

        self.rpc.call('textDocument/didOpen', {
            "uri": uri,
            "languageId": languageId,<commit_msg>Get and increment mid.<commit_after>import neovim
import os, subprocess
import json
import threading
import time
from functools import partial

class RPC:
    def __init__(self, infile, outfile, handler):
        self.infile = infile
        self.outfile = outfile
        self.handler = handler

    def call(self, method, params, mid=None):
        content = {
                "jsonrpc": "2.0",
                "method": method,
                "params": params
                }
        if mid is not None:
            content["id"] = mid
        content = json.dumps(content)
        message = (
                "Content-Length: {}\r\n\r\n"
                "{}".format(len(content), content)
                )
        print(content)
        print()
        self.outfile.write(message)
        self.outfile.flush()

    def serve(self):
        while True:
            line = self.infile.readline()
            if line:
                contentLength = int(line.split(":")[1])
                self.infile.readline()
                content = self.infile.read(contentLength)
                print(content)
                print()
                self.handler.handle(json.loads(content))

@neovim.plugin
class LanguageClient:
    def __init__(self, nvim):
        self.nvim = nvim
        self.server = subprocess.Popen(
            ["/bin/bash", "/opt/rls/wrapper.sh"],
            # ["cargo", "run", "--manifest-path=/opt/rls/Cargo.toml"],
            # ['langserver-go', '-trace', '-logfile', '/tmp/langserver-go.log'],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True)
        self.rpc = RPC(self.server.stdout, self.server.stdin, self)
        threading.Thread(target=self.rpc.serve, name="RPC Server", daemon=True).start()
        self.mid = 0
        self.queue = {}
        self.capabilities = {}

    def incMid(self) -> int:
        mid = self.mid
        self.mid += 1
        return mid

    @neovim.command('LanguageClientInitialize')
    def initialize(self, rootPath=None):
        if rootPath is None:
            rootPath = getRootPath(self.nvim.current.buffer.name)

        mid = self.incMid()
        self.queue[mid] = partial(self.handleInitializeResponse, mid);

        self.rpc.call('initialize', {
            "processId": os.getpid(),
            "rootPath": rootPath,
            "capabilities":{},
            "trace":"verbose"
            }, mid)

    def handleInitializeResponse(self, mid, result):
        del self.queue[mid]
        self.capabilities = result['capabilities']
        self.nvim.command('echom "LanguageClient started."')

    @neovim.function('LanguageClient_textDocument_didOpen')
    def textDocument_didOpen(self, filename=None):
        if filename is None:
            filename = self.nvim.current.buffer.name

        uri = convertToURI(filename)
        languageId = self.nvim.eval('&filetype')
        with open(filename) as f:
            text = f.read()

        self.rpc.call('textDocument/didOpen', {
            "uri": uri,
            "languageId": languageId,
{'fraction_non_alphanumeric': 0.12556929082628496, 'fraction_numerical': 0.0016265452179570592, 'mean_word_length': 1.8827941865916549, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 4, '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': '6135188', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1757, 'n_words': 349}