Skip to content

Code

Detect languages, count lines and functions, and extract symbols, imports and comments from source code

Connector software
Free and open source
Service credentials
Not needed
Use with an agent

Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.

npx skills add Query-farm/vgi-skills --skill orchard

Copy this request into your coding agent to get started.

Use https://query.farm/products/orchard/connectors/code/index.md to help me query Code with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.

List the function symbols and their line spans in a Rust source string.

Example output · captured
Connector release
v0.5.0
Engine
2.0.1
VGI protocol
3.4.0
SELECT name, start_line, end_line FROM code.main.symbols('fn a() {}
fn b() {}
', 'rust') WHERE kind = 'function' ORDER BY start_line
Result
name start_line end_line
a 1 1
b 2 2

Quick start

Choose where to run Code, then follow the steps below.

The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.

1. Install the vgi extension

Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.

setup.sql
SET allow_extension_repositories = 'allowed';

CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
    WITH PREFIX 'https://duckdb-extensions.query.farm';

INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;

2. Attach the connector

Attach the release directly

Downloads the latest release for your platform and caches it locally.

ATTACH 'code' AS code (
  TYPE vgi,
  LOCATION 'github-auto://Query-farm/vgi-code'
);

3. Run a query

query.sql
SELECT name, start_line, end_line FROM code.main.symbols('fn a() {}
fn b() {}
', 'rust') WHERE kind = 'function' ORDER BY start_line
More example queries →
Technical details
Verified connector release
v0.5.0
Engine
2.0.1
VGI protocol
3.4.0
Verified on
December 1, 2026
Release notes on GitHub
Pin a version for production

When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.

Attach the release directly

Uses the verified release each time you attach the connector.

ATTACH 'code' AS code (
  TYPE vgi,
  LOCATION 'github-auto://Query-farm/[email protected]'
);

Overview

Source-code structure for DuckDB: tree-sitter symbols, imports, comments, strings, line counts and arbitrary tree-sitter queries.

Catalog

Everything code exposes once attached — read straight from the worker's released build.

code 1 schema
main 1 table · 10 functions

Source-code structure functions: language detection, line/function counts, import/comment/string extraction, tree-sitter queries and symbol listing.

Schema examples (9)
Examples (9)

Detect a file's language from its extension.

SELECT code.main.language_of('src/main.rs');

Count the lines of code (excluding blank and comment lines) in a Rust source string.

SELECT code.main.loc('fn a() {}
// note
fn b() {}
', 'rust');

Count the function definitions in a Python source string.

SELECT code.main.count_functions('def a(): pass
def b(): pass
', 'python');

Extract each Python import statement, one per row.

SELECT UNNEST(code.main.extract_imports('import os
import sys
', 'python')) AS import;
5 more examples

List the function symbols and their line spans in a Rust source string.

SELECT name, start_line, end_line FROM code.main.symbols('fn a() {}
fn b() {}
', 'rust') WHERE kind = 'function' ORDER BY start_line;
Result
name start_line end_line
a 1 1
b 2 2

List every language id the worker can parse.

SELECT language FROM code.main.supported_languages ORDER BY language;

Per-file code metrics over a table of (path, source) rows: detect each file's language from its path with language_of and pass it to loc, count_functions and extract_imports.

SELECT f.path, code.main.language_of(f.path) AS lang, code.main.loc(f.src, code.main.language_of(f.path)) AS code_lines, code.main.count_functions(f.src, code.main.language_of(f.path)) AS functions, len(code.main.extract_imports(f.src, code.main.language_of(f.path))) AS imports FROM (VALUES ('src/lib.rs', 'use std::fmt;
// helper
fn a() {}
fn b() {}
'), ('app/main.py', 'import os

def run():
    pass
')) AS f(path, src) ORDER BY f.path;

Build a call graph: join the function spans from symbols with the call sites captured by ts_nodes to list which function calls which.

SELECT s.name AS caller, c.text AS callee, c.start_line FROM code.main.symbols('fn main() {
    parse();
    run();
}
fn run() {
    log();
}
', 'rust') AS s JOIN code.main.ts_nodes('fn main() {
    parse();
    run();
}
fn run() {
    log();
}
', 'rust', '(call_expression function: (identifier) @callee)') AS c ON c.start_line BETWEEN s.start_line AND s.end_line WHERE s.kind = 'function' ORDER BY c.start_line;

Per-function metrics: capture each Rust function's source with ts_nodes, then read its name with ts_query and measure it with loc and extract_comments.

SELECT code.main.ts_query(n.text, 'rust', '(function_item name: (identifier) @n)')[1] AS function_name, code.main.loc(n.text, 'rust') AS code_lines, len(code.main.extract_comments(n.text, 'rust')) AS comments FROM code.main.ts_nodes('fn small() {}
fn add(a: i32, b: i32) -> i32 {
    // sum the inputs
    a + b
}
', 'rust', '(function_item) @f') AS n ORDER BY n.seq;
Tables
supported_languages 12 examples1 col

The language ids this worker can parse, one per row (the accepted language argument values for the other functions).

Columns
languagenot null VARCHAR
A language id this worker can parse (the accepted `language` argument value for the other functions), e.g. `rust`, `python`, `go`.
ConstraintsPRIMARY KEY (language)
Examples (12)

List every language id the worker can parse.

SELECT language FROM code.main.supported_languages ORDER BY language;

Check whether a language is supported.

SELECT count(*) > 0 AS supported
FROM code.main.supported_languages
WHERE language = 'rust';
SELECT language FROM code.main.supported_languages() ORDER BY language;
9 more examples
SELECT code.main.language_of('src/main.rs') AS lang;
SELECT code.main.count_lines('fn a() {}
fn b() {}
') AS lines;
SELECT code.main.loc('fn a() {}
// note
fn b() {}
', 'rust') AS loc;
SELECT code.main.count_functions('def a(): pass
def b(): pass
', 'python') AS fns;
SELECT code.main.extract_imports('import os
import sys
', 'python') AS imports;
SELECT kind, name, start_line FROM code.main.symbols('fn alpha() {}
fn beta() {}
', 'rust') ORDER BY start_line;
SELECT capture, text FROM code.main.ts_nodes('fn alpha() {}
', 'rust', '(function_item name: (identifier) @n)') ORDER BY seq;
SELECT code.main.extract_comments('// header
fn a() {}
', 'rust') AS comments;
SELECT s.name AS caller, c.text AS callee, c.start_line FROM code.main.symbols('fn main() {
    parse();
    run();
}
fn run() {
    log();
}
', 'rust') AS s JOIN code.main.ts_nodes('fn main() {
    parse();
    run();
}
fn run() {
    log();
}
', 'rust', '(call_expression function: (identifier) @callee)') AS c ON c.start_line BETWEEN s.start_line AND s.end_line WHERE s.kind = 'function' ORDER BY c.start_line;
Link
Scalar functions
count_functions (source VARCHAR, language VARCHAR) 1 example

Number of function/method definitions for the given language

Arguments
source VARCHAR
The source code to analyze, parsed with tree-sitter using the grammar selected by `language`.
language VARCHAR
The language id selecting the parser grammar, e.g. 'rust', 'python', 'go'; must be one of supported_languages().
ReturnsINTEGER
Example

Count function/method definitions in the source (→ 2).

SELECT code.main.count_functions('def a(): pass
def b(): pass
', 'python');
Link
count_lines (source VARCHAR) 1 example

Total number of physical lines in the source (NULL → NULL)

Arguments
source VARCHAR
The source code to count lines in; counted verbatim with no parsing.
ReturnsINTEGER
Example

Count the physical lines in a source string (→ 2).

SELECT code.main.count_lines('fn a() {}
fn b() {}
');
Link
extract_comments (source VARCHAR, language VARCHAR) 1 example

Comment texts in the source, as a VARCHAR[]

Arguments
source VARCHAR
The source code to extract from, parsed with tree-sitter using the grammar selected by `language`.
language VARCHAR
The language id selecting the parser grammar, e.g. 'rust', 'python', 'go'; must be one of supported_languages().
ReturnsVARCHAR[]
Example

Collect the comment texts from Rust source as a VARCHAR[].

SELECT code.main.extract_comments('// header
fn a() {}
', 'rust');
Link
extract_imports (source VARCHAR, language VARCHAR) 1 example

Import / use / require statements in the source, as a VARCHAR[]

Arguments
source VARCHAR
The source code to extract from, parsed with tree-sitter using the grammar selected by `language`.
language VARCHAR
The language id selecting the parser grammar, e.g. 'rust', 'python', 'go'; must be one of supported_languages().
ReturnsVARCHAR[]
Example

Extract each import statement from Python source, one per row.

SELECT UNNEST(code.main.extract_imports('import os
import sys
', 'python'));
Link
extract_strings (source VARCHAR, language VARCHAR) 1 example

String-literal texts in the source, as a VARCHAR[]

Arguments
source VARCHAR
The source code to extract from, parsed with tree-sitter using the grammar selected by `language`.
language VARCHAR
The language id selecting the parser grammar, e.g. 'rust', 'python', 'go'; must be one of supported_languages().
ReturnsVARCHAR[]
Example

Collect the string-literal texts from Rust source as a VARCHAR[].

SELECT code.main.extract_strings('let s = "hello";
', 'rust');
Link
language_of (filename VARCHAR) 2 examples

Infer the source language from a filename's extension (NULL if unknown)

Arguments
filename VARCHAR
The file name or path whose language to infer; only the extension is used (e.g. 'src/main.rs' or 'app.py').
ReturnsVARCHAR
Examples (2)

Detect the source language of a file from its extension (→ 'rust').

SELECT code.main.language_of('src/main.rs');

Per-file code metrics over a table of (path, source) rows: detect each file's language from its path with language_of and pass it to loc, count_functions and extract_imports.

SELECT f.path, code.main.language_of(f.path) AS lang, code.main.loc(f.src, code.main.language_of(f.path)) AS code_lines, code.main.count_functions(f.src, code.main.language_of(f.path)) AS functions, len(code.main.extract_imports(f.src, code.main.language_of(f.path))) AS imports FROM (VALUES ('src/lib.rs', 'use std::fmt;
// helper
fn a() {}
fn b() {}
'), ('app/main.py', 'import os

def run():
    pass
')) AS f(path, src) ORDER BY f.path;
Link
loc (source VARCHAR, language VARCHAR) 1 example

Lines of code: non-blank, non-comment lines for the given language

Arguments
source VARCHAR
The source code to analyze, parsed with tree-sitter using the grammar selected by `language`.
language VARCHAR
The language id selecting the parser grammar, e.g. 'rust', 'python', 'go'; must be one of supported_languages().
ReturnsINTEGER
Example

Count lines of code, excluding blank and comment lines (→ 2).

SELECT code.main.loc('fn a() {}
// note
fn b() {}
', 'rust');
Link
ts_query (source VARCHAR, language VARCHAR, query VARCHAR) 1 example

Run a tree-sitter query over the source and return the captured node texts (VARCHAR[])

Arguments
source VARCHAR
The source code to query, parsed with tree-sitter using the grammar selected by `language`.
language VARCHAR
The language id selecting the parser grammar, e.g. 'rust', 'python', 'go'; must be one of supported_languages().
query VARCHAR
A tree-sitter S-expression query whose captured nodes' texts are returned, e.g. '(function_item name: (identifier) @n)'.
ReturnsVARCHAR[]
Example

Run a tree-sitter query and return captured node texts (here, the two function names).

SELECT code.main.ts_query('fn alpha() {}
fn beta() {}
', 'rust', '(function_item name: (identifier) @n)');
Link
Table functions
symbols (source VARCHAR, language VARCHAR) 3 examples

Structural symbols (functions, classes, methods, structs, enums, …) of a source doc

Arguments
source VARCHAR
The source document to outline, parsed with tree-sitter using the grammar selected by `language` (a bind-time constant).
language VARCHAR
The language id selecting the parser grammar, e.g. 'rust', 'python', 'go'; must be one of supported_languages() (a bind-time constant).
Returns columns
kind VARCHAR
Symbol kind: one of function, method, class, struct, enum, interface, or trait.
name VARCHAR
The symbol's identifier (NULL when the definition is anonymous).
start_line INTEGER
1-based line where the definition starts.
end_line INTEGER
1-based line where the definition ends.
Examples (3)

List the function symbols and their line spans in a Rust source string.

SELECT name, start_line, end_line FROM code.main.symbols('fn a() {}
fn b() {}
', 'rust') WHERE kind = 'function' ORDER BY start_line;
Result
name start_line end_line
a 1 1
b 2 2

Count the defined symbols by kind in a Python source string.

SELECT kind, count(*) AS n FROM code.main.symbols('class C:
    def m(self): pass
def f(): pass
', 'python') GROUP BY kind ORDER BY kind;

Build a call graph: join the function spans from symbols with the call sites captured by ts_nodes to list which function calls which.

SELECT s.name AS caller, c.text AS callee, c.start_line FROM code.main.symbols('fn main() {
    parse();
    run();
}
fn run() {
    log();
}
', 'rust') AS s JOIN code.main.ts_nodes('fn main() {
    parse();
    run();
}
fn run() {
    log();
}
', 'rust', '(call_expression function: (identifier) @callee)') AS c ON c.start_line BETWEEN s.start_line AND s.end_line WHERE s.kind = 'function' ORDER BY c.start_line;
Link
ts_nodes (source VARCHAR, language VARCHAR, query VARCHAR) 3 examples

Tree-sitter query matches over a source doc as (seq, capture, text, start_line, end_line) rows

Arguments
source VARCHAR
The source document to query, parsed with tree-sitter using the grammar selected by `language` (a bind-time constant).
language VARCHAR
The language id selecting the parser grammar, e.g. 'rust', 'python', 'go'; must be one of supported_languages() (a bind-time constant).
query VARCHAR
A tree-sitter S-expression query; each capture becomes a row, e.g. '(function_item) @f' (a bind-time constant).
Returns columns
seq BIGINT
0-based capture index in document order.
capture VARCHAR
The query capture name (the @name in the tree-sitter query).
text VARCHAR
The captured node's source text.
start_line INTEGER
1-based line where the captured node starts.
end_line INTEGER
1-based line where the captured node ends.
Examples (3)

Return each tree-sitter capture's name and text in document order.

SELECT capture, text FROM code.main.ts_nodes('fn alpha() {}
fn beta() {}
', 'rust', '(function_item name: (identifier) @n)') ORDER BY seq;

Return just the captured node texts and their line spans.

SELECT text, start_line FROM code.main.ts_nodes('fn alpha() {}
fn beta() {}
', 'rust', '(function_item name: (identifier) @n)') ORDER BY seq;

Per-function metrics: capture each Rust function's source with ts_nodes, then read its name with ts_query and measure it with loc and extract_comments.

SELECT code.main.ts_query(n.text, 'rust', '(function_item name: (identifier) @n)')[1] AS function_name, code.main.loc(n.text, 'rust') AS code_lines, len(code.main.extract_comments(n.text, 'rust')) AS comments FROM code.main.ts_nodes('fn small() {}
fn add(a: i32, b: i32) -> i32 {
    // sum the inputs
    a + b
}
', 'rust', '(function_item) @f') AS n ORDER BY n.seq;
Link

Try these queries

Detect a file's language from its extension.

SELECT code.main.language_of('src/main.rs')

Uses language_of

Count the lines of code (excluding blank and comment lines) in a Rust source string.

SELECT code.main.loc('fn a() {}
// note
fn b() {}
', 'rust')

Uses loc

Count the function definitions in a Python source string.

SELECT code.main.count_functions('def a(): pass
def b(): pass
', 'python')

Uses count_functions

Extract each Python import statement, one per row.

SELECT UNNEST(code.main.extract_imports('import os
import sys
', 'python')) AS import

Uses extract_imports

List every language id the worker can parse.

SELECT language FROM code.main.supported_languages ORDER BY language

Uses supported_languages

Build a call graph: join the function spans from symbols with the call sites captured by ts_nodes to list which function calls which.

SELECT s.name AS caller, c.text AS callee, c.start_line FROM code.main.symbols('fn main() {
    parse();
    run();
}
fn run() {
    log();
}
', 'rust') AS s JOIN code.main.ts_nodes('fn main() {
    parse();
    run();
}
fn run() {
    log();
}
', 'rust', '(call_expression function: (identifier) @callee)') AS c ON c.start_line BETWEEN s.start_line AND s.end_line WHERE s.kind = 'function' ORDER BY c.start_line

Uses symbolsts_nodes

20 more examples in the Catalog, each listed under the table or function it uses.

Questions it can answer

Plain-English questions from the worker's agent test tasks. An AI agent with this catalog attached should answer each one with SQL.

  • Using this worker, how many function definitions are in this Rust source string: 'fn a() {} fn b() {} fn c() {}' ? Return the single integer count.
  • Using this worker, list the names of the top-level definitions in this Rust source string: 'fn a() {} struct S {}' . Return just the names, one per row.
  • Using this worker, which programming languages can it parse? Return the language ids, one per row.

Code is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.