Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

code-chunking

БесплатноБез исполняемых скриптовНе проверен

AST-aware chunking for source code RAG. Covers tree-sitter, LangChain Language splitter, LlamaIndex CodeSplitter, preserving function/class boundaries…

claude-dev-suiteот claude-dev-suite★ 51 звезда на GitHub

Об этом скилле

Code Chunking

Why AST-Based Chunking

Character splitters can split a function mid-body, separating a signature from its return statement. AST chunking respects function and class boundaries so each chunk is a self-contained semantic unit - dramatically better retrieval and generation quality for code.

tree-sitter (Python bindings)

from tree_sitter import Parser
from tree_sitter_languages import get_language

PY_LANG = get_language("python")
parser = Parser()
parser.set_language(PY_LANG)

source = open("app.py", "rb").read()
tree = parser.parse(source)

def walk(node, depth=0):
    if node.type in {"function_definition", "class_definition"}:
        start = node.start_byte
        end = node.end_byte
        name_node = node.child_by_field_name("name")
        name = source[name_node.start_byte:name_node.end_byte].decode() if name_node else "<anon>"
        yield {
            "type": node.type,
            "name": name,
            "start_line": node.start_point[0] + 1,
            "end_line": node.end_point[0] + 1,
            "text": source[start:end].decode(),
        }
    for child in node.children:
        yield from walk(child, depth + 1)

chunks = list(walk(tree.root_node))

Preserving Imports and Module Context

def extract_imports(tree, source: bytes) -> str:
    parts = []
    for child in tree.root_node.children:
        if child.type in {"import_statement", "import_from_statement"}:
            parts.append(source[child.start_byte:child.end_byte].decode())
    return "\n".join(parts)

imports = extract_imports(tree, source)
# Prepend imports to each chunk so the LLM has the full context
for c in chunks:
    c["text"] = f"{imports}\n\n{c['text']}" if imports else c["text"]

LangChain Language Splitter

from langchain_text_splitters import RecursiveCharacterTextSplitter, Language

splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.PYTHON,
    chunk_size=1500,
    chunk_overlap=200,
)
chunks = splitter.create_documents([source.decode()])

# Supported languages include: PYTHON, JS, TS, JAVA, GO, RUST, CPP, CSHARP,
# RUBY, PHP, SCALA, SWIFT, KOTLIN, MARKDOWN, LATEX, HTML, SOL

The language-aware splitter uses language-specific separators (e.g., \nclass , \ndef , \n\tdef ) which beats generic character splitting but still does not understand AST scope.

LlamaIndex CodeSplitter

from llama_index.core.node_parser import CodeSplitter
from llama_index.core import Document

splitter = CodeSplitter(
    language="python",           # tree-sitter language name
    chunk_lines=40,
    chunk_lines_overlap=10,
    max_chars=1500,
)

doc = Document(text=source.decode(), metadata={"path": "app.py"})
nodes = splitter.get_nodes_from_documents([doc])
for n in nodes:
    print(n.metadata, n.text[:80])

CodeSplitter wraps tree-sitter and respects function/class boundaries while staying under max_chars.

Docstring-Aware Chunking (keep docs with impl)

def chunk_with_docstring(tree, source: bytes) -> list[dict]:
    out = []
    for node in tree.root_node.children:
        if node.type != "function_definition":
            continue
        # Include decorators (previous siblings) + docstring (first body stmt)
        start = node.start_byte
        # Walk back for decorators
        prev = node.prev_sibling
        while prev and prev.type == "decorator":
            start = prev.start_byte
            prev = prev.prev_sibling
        out.append({
            "text": source[start:node.end_byte].decode(),
            "start_line": node.start_point[0] + 1,
        })
    return out

Tree-sitter naturally includes the docstring as the first statement of the function body, so extracting the whole function node preserves it.

Splitting by Scope (for very large classes)

def split_class(class_node, source: bytes, max_chars: int = 1500) -> list[str]:
    header_end = class_node.child_by_field_name("body").start_byte
    header = source[class_node.start_byte:header_end].decode()
    methods = [
        c for c in class_node.child_by_field_name("body").children
        if c.type == "function_definition"
    ]
    chunks, buf = [], header
    for m in methods:
        method_src = source[m.start_byte:m.end_byte].decode()
        if len(buf) + len(method_src) > max_chars:
            chunks.append(buf)
            buf = header + "\n    " + method_src
        else:
            buf += "\n\n    " + method_src
    if buf:
        chunks.append(buf)
    return chunks

Code Embeddings

Model Provider Dim Strength
voyage-code-3 Voyage AI 1024 Best code retrieval accuracy
voyage-code-2 Voyage AI 1536 Previous-gen, widely used
text-embedding-3-large OpenAI 3072 General, decent on code
jina-embeddings-v2-base-code Jina 768 Open source, runs locally
CodeBERT Microsoft 768 Classic, needs fine-tune
Полный SKILL.md на GitHub

Установить code-chunking в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Read
Grep
Glob
Write
Edit

FAQ

Что делает скилл code-chunking?

AST-aware chunking for source code RAG. Covers tree-sitter, LangChain Language splitter, LlamaIndex CodeSplitter, preserving function/class boundaries, keeping docstrings with implementations, and code-specific embeddings (voyage-code-2, CodeBERT). USE WHEN: user mentions "code chunking", "tree-sitter", "code splitter", "AST chunking", "code embedding", "voyage-code-2", "code RAG", "repo Q&A" DO NOT USE FOR: plain prose chunking - use `rag-patterns`; PDF extraction - use `pdf-extraction`

Как установить скилл code-chunking?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл code-chunking запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить code-chunking с