def METHOD_NAME(self, completion, truncate_before_pattern):
    def find_re(string, pattern, start_pos):
        m = pattern.search(string, start_pos)
        return m.start() if m else -1
    re = try_import("regex")
    terminals = [re.compile(pattern, re.MULTILINE) for pattern in truncate_before_pattern]
    prints = list(re.finditer("^print", completion, re.MULTILINE))
    if len(prints) > 1:
        completion = completion[: prints[1].start()]
    defs = list(re.finditer("^def", completion, re.MULTILINE))
    if len(defs) > 1:
        completion = completion[: defs[1].start()]
    start_pos = 0
    terminals_pos = [
        pos for pos in [find_re(completion, terminal, start_pos) for terminal in terminals] if pos != -1
    ]
    if len(terminals_pos) > 0:
        return completion[: min(terminals_pos)]
    else:
        return completion