import re
from .base import Base


CompleteResults = "g:LanguageClient_completeResults"


def simplify_snippet(snip: str) -> str:
    return re.sub(r'(?<!\\)\$\d+', '', snip)


class Source(Base):
    def __init__(self, vim):
        super().__init__(vim)

        self.name = "LanguageClient"
        self.mark = "[LC]"
        self.rank = 1000
        self.filetypes = vim.eval(
            "get(g:, 'LanguageClient_serverCommands', {})").keys()
        self.min_pattern_length = 1
        self.input_pattern = r'(\.|::)\w*'

    def gather_candidates(self, context):
        if not context["is_async"]:
            context["is_async"] = True
            self.vim.funcs.LanguageClient_omniComplete()
            return []
        elif self.vim.funcs.eval("len({})".format(CompleteResults)) == 0:
            return []

        context["is_async"] = False
        result = self.vim.funcs.eval("remove({}, 0)".format(CompleteResults))
        if result is None:
            result = []
        return result

@@ -131,40 +131,168 @@
     
-return re.sub(r'(?%3C!%5C%5C)%5C$%5Cd+', '
+snip = re.sub(r'(?%3C!%5C%5C)%5C$(?P%3Cnum%3E%5Cd+)', '%3C%60%5Cg%3Cnum%3E%60%3E', snip)%0A    return re.sub(r'(?%3C!%5C%5C)%5C$%7B(?P%3Cnum%3E%5Cd+):(?P%3Cdesc%3E.+?)%7D',%0A                  '%3C%60%5Cg%3Cnum%3E:%5Cg%3Cdesc%3E%60%3E
 ', s

Replace placeholders in completion text