#include "./tree_sitter_utils.hpp"

namespace lsp::language::ast::ts
{
    std::string Text(TSNode node, std::string_view source)
    {
        uint32_t start = ts_node_start_byte(node);
        uint32_t end = ts_node_end_byte(node);

        if (start >= end || end > source.length())
            return "";

        return std::string(source.substr(start, end - start));
    }

    Location NodeLocation(TSNode node)
    {
        TSPoint start = ts_node_start_point(node);
        TSPoint end = ts_node_end_point(node);

        return Location{
            static_cast<uint32_t>(start.row),
            static_cast<uint32_t>(start.column),
            static_cast<uint32_t>(end.row),
            static_cast<uint32_t>(end.column),
            ts_node_start_byte(node),
            ts_node_end_byte(node)
        };
    }

}
