Building Language Extensions
Language extensions add support for programming languages in Glass. They can provide syntax highlighting, language servers, Tree-sitter grammars, and language-specific features.Language Components
A complete language extension typically includes:- Language metadata - Configuration and file associations
- Grammar - Tree-sitter parser for syntax analysis
- Queries - Tree-sitter queries for highlighting and structure
- Language server - LSP server for IDE features
Defining a Language
name = "My Language"
grammar = "my-language"
path_suffixes = ["myl", "mylang"]
line_comments = ["# "]
block_comment = { start = "/*", end = "*/" }
tab_size = 4
hard_tabs = false
name - Human-readable name shown in the language selectorgrammar - Name of the Tree-sitter grammar (registered separately)path_suffixes - File extensions (without the dot)line_comments - Prefixes for line commentsblock_comment - Start and end delimiters for block commentstab_size - Default indentation sizehard_tabs - Use tabs instead of spaces[grammars.my-language]
repository = "https://github.com/username/tree-sitter-my-language"
rev = "58b7cac8fc14c92b0677c542610d8738c373fa81"
Language Server Integration
use zed_extension_api::{self as zed, LanguageServerId, Result};
struct MyExtension {
cached_binary_path: Option<String>,
}
impl zed::Extension for MyExtension {
fn new() -> Self {
Self {
cached_binary_path: None,
}
}
fn language_server_command(
&mut self,
language_server_id: &LanguageServerId,
worktree: &zed::Worktree,
) -> Result<zed::Command> {
// Check if binary exists in PATH
if let Some(path) = worktree.which("my-language-server") {
return Ok(zed::Command {
command: path,
args: vec!["--stdio".to_string()],
env: Default::default(),
});
}
// Download or install the language server
let server_path = self.install_language_server(language_server_id)?;
Ok(zed::Command {
command: server_path,
args: vec!["--stdio".to_string()],
env: Default::default(),
})
}
}
fn install_language_server(
&mut self,
language_server_id: &LanguageServerId,
) -> Result<String> {
if let Some(path) = &self.cached_binary_path {
if fs::metadata(path).is_ok() {
return Ok(path.clone());
}
}
zed::set_language_server_installation_status(
language_server_id,
&zed::LanguageServerInstallationStatus::CheckingForUpdate,
);
let release = zed::latest_github_release(
"owner/repo",
zed::GithubReleaseOptions {
require_assets: true,
pre_release: false,
},
)?;
let (platform, arch) = zed::current_platform();
let asset_name = format!(
"my-language-server-{}-{}.tar.gz",
platform_name(platform),
arch_name(arch)
);
let asset = release
.assets
.iter()
.find(|asset| asset.name == asset_name)
.ok_or_else(|| format!("No asset found: {}", asset_name))?;
zed::set_language_server_installation_status(
language_server_id,
&zed::LanguageServerInstallationStatus::Downloading,
);
let version_dir = format!("my-language-server-{}", release.version);
zed::download_file(
&asset.download_url,
&version_dir,
zed::DownloadedFileType::GzipTar,
)?;
let binary_path = format!("{}/my-language-server", version_dir);
self.cached_binary_path = Some(binary_path.clone());
zed::set_language_server_installation_status(
language_server_id,
&zed::LanguageServerInstallationStatus::None,
);
Ok(binary_path)
}
Language Server Configuration
Provide initialization options and workspace configuration:Tree-sitter Queries
Syntax Highlighting
Createlanguages/my-language/highlights.scm:
Bracket Matching
Createlanguages/my-language/brackets.scm:
Code Outline
Createlanguages/my-language/outline.scm:
Multi-Language Support
If your language server supports multiple languages, map them usinglanguage_ids: