This plugin integrates the powerful Tree-sitter parsing system into Acode, enabling sophisticated code analysis, syntax highlighting, and programmatic manipulation of code structures. Tree-sitter creates concrete syntax trees for source code that can be used for more advanced code intelligence features.
- Fast, Incremental Parsing: Efficient parsing that only re-parses changed portions of code
- Multi-Language Support: Install and use Tree-sitter grammars for various programming languages
- Programmatic API: Easily interface with Tree-sitter from your own plugins or scripts
- Language Management: Simple API to install, load, and uninstall language grammars
- Memory Efficient: Only loads language grammars when needed
This plugin provides an API only and does not affect the Acode user interface directly. It is intended to be used by other plugins or scripts that need advanced code parsing capabilities.
The main API accessible via acode.require('tree-sitter')
.
- isInitialized:
Boolean
- Indicates if Tree-sitter has been successfully initialized - parser:
Object
- Access to the underlying parser instances - config:
Object
- Current configuration object - TREE_SITTER_PATH:
String
- Path to Tree-sitter storage directory - CONFIG_PATH:
String
- Path to configuration file
Wait for Tree-sitter to initialize.
- Returns:
Promise<Boolean>
- Resolves when initialization is complete
Get a Tree-sitter language by identifier.
- Parameters:
lang
:String
- Language identifieroptions
:Object
- Optional parametersforceReload
:Boolean
- Force reload language even if cached
- Returns:
Promise<Language>
- Language instance
Create a new Tree-sitter parser for a specific language.
- Parameters:
lang
:String
- Language identifieroptions
:Object
- Optional parametersautoLoadGrammar
:Boolean
- Automatically load grammar if not loaded
- Returns:
Promise<Parser>
- Configured parser instance
Parse code with the specified language.
- Parameters:
lang
:String
- Language identifiercode
:String
- Code to parseoptions
:Object
- Optional parametersforceReload
:Boolean
- Force reload parser even if cached
- Returns:
Promise<Object>
- Syntax tree
Get list of available languages.
- Returns:
Promise<String[]>
- Array of language identifiers
Check if a language is available.
- Parameters:
lang
:String
- Language identifier
- Returns:
Promise<Boolean>
- True if language is available
Install a language.
- Parameters:
lang
:String
- Language identifier
- Returns:
Promise<Boolean>
- Installation success status
Uninstall a language.
- Parameters:
lang
:String
- Language identifier
- Returns:
Promise<Boolean>
- Success status
Clear languages and parsers.
The API extends EventEmitter and emits the following events:
- initialized: Emitted when Tree-sitter is fully initialized
- error: Emitted when an error occurs, with error object as parameter
- language-installed: Emitted when a language is installed, with language ID as parameter
- language-uninstalled: Emitted when a language is uninstalled, with language ID as parameter
Accessible via acode.require('@tree-sitter/language')
.
- name:
String
- Language identifier - config:
Object
- Language configuration - wasmUrl:
String|Object
- WASM URL or URLs - extensions:
Object
- Language extensions - isExtension:
Boolean
- Whether this language is an extension - grammar:
Object
- The compiled grammar (null if not loaded) - queries:
Object
- Queries for the language - isLoaded:
Boolean
- Whether the grammar has been loaded
Get a specific query by name.
- Parameters:
queryName
:String
- Name of the query (e.g., 'highlights', 'locals')
- Returns:
String|null
- Query content or null if not found
Load the grammar from WASM.
- Returns:
Promise<Object>
- Loaded grammar
Unload grammar to free memory.
- Returns:
Boolean
- Success status
// Get the Tree-sitter API
const treeSitter = acode.require('tree-sitter');
// Use Tree-sitter to parse JavaScript code
async function parseCode() {
// Make sure JavaScript language is installed
if (!(await treeSitter.isLanguageAvailable('javascript'))) {
await treeSitter.installLanguage('javascript');
}
// Parse some code
const code = 'function add(a, b) { return a + b; }';
const tree = await treeSitter.parse('javascript', code);
// Work with the syntax tree
console.log(tree.rootNode.toString());
}
// Get the Tree-sitter API
const treeSitter = acode.require('tree-sitter');
// Use Tree-sitter to find all function declarations in JavaScript code
async function findFunctions() {
const jsLang = await treeSitter.getLanguage('javascript');
const parser = await treeSitter.createParser('javascript');
const code = `
function add(a, b) { return a + b; }
const subtract = function(a, b) { return a - b; };
const multiply = (a, b) => a * b;
`;
const tree = parser.parse(code);
// Get the highlights query
const query = jsLang.getQuery('highlights');
if (query) {
const matches = query.matches(tree.rootNode);
console.log('Found function declarations:', matches);
}
}
// Get the Tree-sitter API
const treeSitter = acode.require('tree-sitter');
// Listen for events
treeSitter.on('language-installed', (lang) => {
console.log(`Language ${lang} was installed`);
});
treeSitter.on('error', (error) => {
console.error('Tree-sitter error:', error);
});
- Open Acode
- Go to Settings > Plugins
- Search for "TreeSitter"
- Install the plugin
- Tree-sitter - The incremental parsing system
- web-tree-sitter - Web bindings for Tree-sitter