Untitled

mail@pastecode.io avatar
unknown
python
a year ago
669 B
3
Indexable
Never
from codetext.utils import parse_code
from codetext.parser.python_parser import PythonParser
from codetext.parser.language_parser import get_node_by_kind
from tree_sitter import Node


def doc_code_combine(doc: str, code: str)->str:
    tree = parse_code(code, 'python')

    function_node : Node = PythonParser.get_function_list(
        tree.root_node
    )[0]
    docstring_start_line = get_node_by_kind(function_node, kind=':')[0].end_point[0]
    out = code.strip("\n").split("\n")
    doc = "\n".join(["    " + line for line in doc.split("\n")])
    out.insert(docstring_start_line, doc)
    out = "\n".join(out)
    return out

print(doc_code_combine(doc, code))