Implementation:TobikoData Sqlmesh SQLMeshDialect
| Knowledge Sources | |
|---|---|
| Domains | Web_UI, Code_Editor, SQL_Syntax, Autocomplete |
| Last Updated | 2026-02-07 20:00 GMT |
Overview
A CodeMirror language extension that provides SQL syntax highlighting and intelligent autocomplete for SQLMesh-specific keywords and model references.
Description
SQLMeshDialect is a sophisticated CodeMirror extension that extends standard SQL language support with SQLMesh-specific syntax and intelligent autocomplete functionality. It defines a custom SQL dialect that includes SQLMesh keywords (MODEL, kind, dialect, etc.), SQLMesh types (FULL, INCREMENTAL_BY_TIME_RANGE, etc.), and provides context-aware suggestions for model names, column names, and configuration keywords. The extension integrates with a SQLGlot worker for advanced parsing capabilities.
The autocomplete system is context-aware, providing different suggestions based on cursor position and surrounding code. When typing after a dot (.), it suggests column names for known models or model names for partial matches. Within MODEL definitions, it provides SQLMesh-specific keywords and configuration options. The extension also handles model and column name matching with support for both quoted and unquoted identifiers, making it flexible for different SQL naming conventions.
Usage
Use this extension to enhance the code editor with SQLMesh-aware syntax highlighting and autocomplete. It should be added to CodeMirror's extension array when configuring the editor for SQL files in a SQLMesh project.
Code Reference
Source Location
- Repository: TobikoData_Sqlmesh
- File: web/client/src/library/components/editor/extensions/SQLMeshDialect.ts
Signature
export type ExtensionSQLMeshDialect = (
models: Map<string, Model>,
allModelsNames: Array<{ label: string; type: string }>,
allColumnsNames: Array<{ label: string; type: string }>,
options?: { types: string; keywords: string },
dialects?: string[],
) => LanguageSupport
export const SQLMeshDialect: ExtensionSQLMeshDialect
export function SQLMeshDialectCleanUp(): void
export function getSQLMeshModelKeywords(
dialects?: string[]
): Map<string, Completion[]>
Import
import { SQLMeshDialect, SQLMeshDialectCleanUp } from '@components/editor/extensions/SQLMeshDialect'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| models | Map<string, Model> | Yes | Map of model names to model objects for column suggestions |
| allModelsNames | Array<{label, type}> | Yes | List of all model names for autocomplete |
| allColumnsNames | Array<{label, type}> | Yes | List of all column names for autocomplete |
| options | {types, keywords} | No | Additional SQL types and keywords to include |
| dialects | string[] | No | List of available SQL dialects for dialect suggestions |
Outputs
| Name | Type | Description |
|---|---|---|
| LanguageSupport | LanguageSupport | CodeMirror language support object with autocomplete |
SQLMesh Keywords
The extension recognizes the following SQLMesh-specific keywords:
const SQLMeshKeywords =
'path threshold jinja_query_begin number_of_rows jinja_end not_null ' +
'forall criteria length unique_values interval_unit unique_key ' +
'columns grain grains references metric tags audit model name kind ' +
'owner cron start storage_format time_column partitioned_by pre post ' +
'batch_size audits dialect'
SQLMesh Types
Model kind types recognized by the extension:
- FULL: Full refresh models
- INCREMENTAL_BY_TIME_RANGE: Time-based incremental models
- INCREMENTAL_BY_UNIQUE_KEY: Key-based incremental models
- VIEW: View models
- SEED: Seed data models
- EMBEDDED: Embedded models
Autocomplete Behavior
Dot Notation Completion
When typing after a dot, suggests columns or models:
// Example: model_name.column_name
// Provides column suggestions for the model
if (isNotNil(maybeModel)) {
options = maybeModel.columns.map(column => ({
label: column.name,
type: 'column',
}))
}
MODEL Block Completion
Inside MODEL() blocks, provides SQLMesh configuration keywords:
// Detects MODEL blocks and provides context-specific suggestions
const isInModel = matchModels
.filter(str => str.includes(word.text))
.some(([start, end]) => ctx.pos >= start && ctx.pos <= end)
if (isInModel) {
// Provide MODEL-specific keywords, kind options, or dialect options
suggestions = SQLMeshModelDictionary.get('keywords')
}
Kind and Dialect Completion
Provides specialized suggestions after 'kind' and 'dialect' keywords:
- After kind: Suggests FULL, INCREMENTAL_BY_TIME_RANGE, VIEW, SEED, EMBEDDED
- After dialect: Suggests available SQL dialects from the dialects array
Worker Integration
The extension registers a message handler with the SQLGlot worker:
const handler = function getTokensFromSQLGlot(e: MessageEvent): void {
if (e.data.topic === 'parse') {
// TODO: set parsed tree and use it to improve editor autocomplete
}
}
sqlglotWorker.addEventListener('message', handler)
Cleanup
Always call SQLMeshDialectCleanUp() when unmounting the editor to remove event listeners and prevent memory leaks.