Monaco SQL Languages

English | 简体中文
This project is based on the SQL language project of Monaco Editor, which was forked from the monaco-languages.The difference is that Monaco SQL Languages supports various SQL languages and the corresponding advanced features for the Big Data field.
Feature highlights
- Code Highlighting
- Syntax Validation
- Code Completion
- Built-in SQL Snippets
Powered By dt-sql-parser
Online Preview
https://dtstack.github.io/monaco-sql-languages/
Powered By molecule.
Supported SQL Languages
- MySQL
- Flink
- Spark
- Hive
- Trino (Presto)
- PostgreSQL
- Impala
- GenericSQL
Installing
npm install monaco-sql-languages
Tips: Monaco SQL Languages is only guaranteed to work stably on [email protected] for now.
Integrating
Usage
-
Import language contributions
Tips: If integrated via MonacoEditorWebpackPlugin, it will help us to import contribution files automatically. Otherwise, you need to import the contribution files manually.
import 'monaco-sql-languages/esm/languages/mysql/mysql.contribution';
import 'monaco-sql-languages/esm/languages/flink/flink.contribution';
import 'monaco-sql-languages/esm/languages/spark/spark.contribution';
import 'monaco-sql-languages/esm/languages/hive/hive.contribution';
import 'monaco-sql-languages/esm/languages/trino/trino.contribution';
import 'monaco-sql-languages/esm/languages/pgsql/pgsql.contribution';
import 'monaco-sql-languages/esm/languages/impala/impala.contribution';
import 'monaco-sql-languages/esm/languages/generic/generic.contribution';
// Or you can import all language contributions at once.
// import 'monaco-sql-languages/esm/all.contributions';
-
Setup language features
You can setup language features via setupLanguageFeatures. For example, setup code completion feature of flinkSQL language.
import { LanguageIdEnum, setupLanguageFeatures } from 'monaco-sql-languages';
setupLanguageFeatures(LanguageIdEnum.FLINK, {
completionItems: {
enable: true,
triggerCharacters: [' ', '.'],
// Automatically trigger the next completion after a completion item is accepted
continuousCompletion: true,
completionService: //... ,
}
});
By default, Monaco SQL Languages only provides keyword autocompletion and built-in SQL snippets, and you can customize your completionItem list via completionService.
Set continuousCompletion to true to enable continuous completion: after a completion item is accepted, the suggest widget is triggered again automatically whenever the insertion moves the caret into a new syntactic context, so the next set of suggestions is offered without re-typing. Concretely:
- keyword items without an explicit
insertText are inserted with a trailing space (e.g. SELECT ), so the next suggestions (columns, *, …) are offered immediately;
- when the parser reports that the current position is within a
function syntactic context, keyword items are inserted as-is (without a trailing space) and do not re-trigger the suggest widget, so you can type the function’s parentheses yourself;
- items whose
insertText ends with a trigger character re-trigger completion — e.g. a custom completionService can emit database items with insertText: 'my_database.', so that tables inside the database are suggested right after the database name is accepted;
- items that define their own
command are left untouched, and plain entity items (tables, columns, …) do not re-trigger, since accepting them does not change the completion context.
import { languages } from 'monaco-editor/esm/vs/editor/editor.api';
import {
setupLanguageFeatures,
LanguageIdEnum,
CompletionService,
ICompletionItem,
EntityContextType
} from 'monaco-sql-languages';
const completionService: CompletionService = function (
model,
position,
completionContext,
suggestions, // syntax context info at caretPosition
entities, // tables, columns in the syntax context of the editor text
snippets // SQL snippets
) {
return new Promise((resolve, reject) => {
if (!suggestions) {
return Promise.resolve([]);
}
const { keywords, syntax } = suggestions;
const keywordsCompletionItems: ICompletionItem[] = keywords.map((kw) => ({
label: kw,
kind: languages.CompletionItemKind.Keyword,
detail: 'keyword',
sortText: '2' + kw
}));
let syntaxCompletionItems: ICompletionItem[] = [];
syntax.forEach((item) => {
if (item.syntaxContextType === EntityContextType.DATABASE) {
const databaseCompletions: ICompletionItem[] = []; // some completions about databaseName
syntaxCompletionItems = [...syntaxCompletionItems, ...databaseCompletions];
}
if (item.syntaxContextType === EntityContextType.TABLE) {
const tableCompletions: ICompletionItem[] = []; // some completions about tableName
syntaxCompletionItems = [...syntaxCompletionItems, ...tableCompletions];
}
});
resolve([...syntaxCompletionItems, ...keywordsCompletionItems]);
});
};
setupLanguageFeatures(LanguageIdEnum.FLINK, {
completionItems: {
enable: true,
completionService: //... ,
}
});
-
Create the Monaco Editor instance and specify the language you need
import { LanguageIdEnum } from 'monaco-sql-languages';
monaco.editor.create(document.getElementById('container'), {
value: 'select * from tb_test',
language: LanguageIdEnum.FLINK // languageId
});
-
SQL Formatting (optional)
Formatting uses sql-formatter as an optional peer. The main package entry does not require it: completion and diagnostics work without installing sql-formatter.
Install the peer only if you enable formatting or import the format API. Import the format entry once (side effect) before setupLanguageFeatures(..., { format: { enable: true } }). Enabling format without that import throws. If the format entry is imported later, already-loaded languages are re-registered automatically.
npm install sql-formatter
format.enable must be true to register the editor Format action (passing only fallback does nothing). Priority: built-in sql-formatter → fallback (unavailable or throw) → original code.
import { KeyCode, KeyMod } from 'monaco-editor';
import 'monaco-sql-languages/format';
import { LanguageIdEnum, setupLanguageFeatures } from 'monaco-sql-languages';
setupLanguageFeatures(LanguageIdEnum.MYSQL, {
format: {
enable: true, // required for the context-menu / shortcut action
tabWidth: 4,
fallback: (code, languageId) => myLegacyFormat(code, languageId),
// optional; default is Ctrl/Cmd+Alt+F
keybindings: [KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KeyF]
}
});
Toolbar or other UI should import the format entry (this does require sql-formatter at build time):
import { formatEditorSQL, formatSQL } from 'monaco-sql-languages/format';
await formatEditorSQL(editor, { tabWidth: 4, fallback: myLegacyFormat });
const text = await formatSQL(code, LanguageIdEnum.MYSQL, { tabWidth: 4 });
Versions: Use the sql-formatter version you install. Peer requires >=10.7.2 (covers hive / trino and tabWidth). Modern browsers may use v15+; Chrome 75: pin [email protected].
Editor shortcut defaults to Ctrl/Cmd+Alt+F (context menu Format). Unchanged results apply no edit.
Add a gutter run icon before each SQL statement. Call createRunStatementButton after creating the editor and dispose the controller when the editor is destroyed.
import {
LanguageIdEnum,
createRunStatementButton,
getStatementRangesByLanguage
} from 'monaco-sql-languages';
const controller = createRunStatementButton({
editor,
languageId: LanguageIdEnum.FLINK,
glyphMarginHoverMessage: 'Run this statement',
preprocessCode: (code) => code,
onRun: ({ statement, editor, model }) => {
console.log(statement.text);
}
});
// controller.refresh(); // recompute icons manually when needed
// controller.dispose(); // on editor unmount
Notes
- Pass
languageId explicitly so the built-in splitter uses the correct parser.
- Custom
getStatementRanges only needs accurate startLineNumber and text; executableLineNumber is normalized automatically.
- Icons are hidden and clicks are ignored when the editor is read-only.
- Use
getStatementRangesByLanguage(code, languageId, preprocessCode) if you need the built-in splitter outside the button.
preprocessCode must keep the same line mapping as the editor model (no insert/delete that shifts lines); decorations are applied to the original model.
- By default, blank or semicolon-only fragments (e.g.
;;;) do not show a run icon.
executableLineNumber detection skips leading comments but does not parse string literals; a leading string that contains /* or -- may be misidentified (rare in practice).
- When multiple statements share the same
executableLineNumber, a console warning is emitted and the last one wins for gutter clicks.
- Call
controller.dispose() when the editor is destroyed (one controller per editor instance).
SQL Snippets
We provide some built-in SQL snippets for each SQL language, which helps us to write SQL quickly.
How to customize SQL snippets?
When setting language features, you can customize SQL snippets via snippets configuration. When snippets is passed in as an empty array, the built-in SQL snippets are disabled.
import { snippets, CompletionSnippetOption } from 'monaco-sql-languages/esm/main.js';
const customSnippets: CompletionSnippetOption[] = [
{
label: 'INSERT',
prefix: 'insert',
// Will join the line with `\n`
body: [
'INSERT INTO ${1:table_name}',
'SELECT ${3:column1}, ${4:column2}',
'FROM ${2:source_table}',
'WHERE ${5:conditions};\n$6'
],
description: "This is an 'insert into select' snippet"
}
];
Snippets syntax can refer to vscode-snippet.
But it is different from vscode code snippets, we only provide snippets completions at the beginning of the SQL statement.
Note: If you provide a custom completionService method, you need to manually return the snippets as completions, as shown in the following example:
const completionService: CompletionService = async function (
model,
position,
completionContext,
suggestions,
entities,
snippets
) {
const { keywords } = suggestions;
const keywordsCompletionItems: ICompletionItem[] = keywords.map((kw) => ({
label: kw,
kind: languages.CompletionItemKind.Keyword,
detail: 'keyword',
sortText: '2' + kw
}));
const snippetCompletionItems: ICompletionItem[] =
snippets?.map((item) => ({
label: item.label || item.prefix,
kind: languages.CompletionItemKind.Snippet,
filterText: item.prefix,
insertText: item.insertText,
insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet,
sortText: '3' + item.prefix,
detail: item.description !== undefined ? item.description : 'SQL Snippet',
documentation: item.insertText
})) || [];
return [...keywordsCompletionItems, ...snippetCompletionItems];
};
Other Notes:
When in code snippet context, you can use Tab key to move to the next input position, but the keywords completions is also triggered by Tab key, which will cause a shortcut key conflict. So Monaco-Editor stipulates that when in code snippet context, it will not trigger completion.
If you want to still support intelligent completion in code snippet context, you can set the Monaco-Editor configuration item suggest.snippetsPreventQuickSuggestions to false to achieve it.
editor.create(editorElement, {
suggest: {
snippetsPreventQuickSuggestions: false
}
})

Monaco Theme
Monaco SQL Languages plan to support more themes in the future.
Monaco SQL Languages provides built-in Monaco Theme that is named vsPlusTheme. vsPlusTheme inspired by vscode default plus colorTheme and it contains three styles of themes inside:
darkTheme: Inherited from monaco built-in theme vs-dark;
lightTheme: Inherited from monaco built-in theme vs;
hcBlackTheme: Inherited from monaco built-in theme hc-black;
Use Monaco SQL Languages built-in vsPlusTheme
import { vsPlusTheme } from 'monaco-sql-languages';
import { editor } from 'monaco-editor';
// import themeData and defineTheme, you can customize the theme name, e.g. sql-dark
editor.defineTheme('sql-dark', vsPlusTheme.darkThemeData);
editor.defineTheme('sql-light', vsPlusTheme.lightThemeData);
editor.defineTheme('sql-hc', vsPlusTheme.hcBlackThemeData);
// specify the theme you have defined
editor.create(null as any, {
theme: 'sql-dark',
language: 'flinksql'
});
Customize your own Monaco theme
import { TokenClassConsts, postfixTokenClass } from 'monaco-sql-languages';
// Customize the various tokens style
const myThemeData: editor.IStandaloneThemeData = {
base: 'vs-dark',
inherit: true,
rules: [
{ token: postfixTokenClass(TokenClassConsts.COMMENT), foreground: '6a9955' },
{ token: postfixTokenClass(TokenClassConsts.IDENTIFIER), foreground: '9cdcfe' },
{ token: postfixTokenClass(TokenClassConsts.KEYWORD), foreground: '569cd6' },
{ token: postfixTokenClass(TokenClassConsts.NUMBER), foreground: 'b5cea8' },
{ token: postfixTokenClass(TokenClassConsts.STRING), foreground: 'ce9178' },
{ token: postfixTokenClass(TokenClassConsts.TYPE), foreground: '4ec9b0' }
],
colors: {}
};
// Define the monaco theme
editor.defineTheme('my-theme', myThemeData);
postfixTokenClass is not required in most cases, but since Monaco SQL Languages has tokenPostfix: 'sql' internally set for all SQL languages, in some cases your custom style may not work if you don’t use postfixTokenClassClass to handle TokenClassConsts.*.
Dev: cheat sheet
Code of Conduct
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
License
MIT
评论