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 has integrated with various SQL languages for the Big Data field, such as FlinkSQL, SparkSQL, HiveSQL, and others.
In addition, Monaco SQL Languages provides SQL syntax validation and CodeCompletion feature for these languages via dt-sql-parser.
Online Preview: https://dtstack.github.io/monaco-sql-languages/
Supported SQL Languages
- MySQL
- FlinkSQL
- SparkSQL
- HiveSQL
- TrinoSQL (PrestoSQL)
- PostgreSQL
- PL/SQL
Supported CodeCompletion SQL Languages
SQL Type | Language Id | Code-Completion |
---|
MySQL | mysql | WIP |
Flink SQL | flinksql | ✅ |
Spark SQL | sparksql | ✅ |
Hive SQL | hivesql | ✅ |
Trino SQL | trinosql | ✅ |
PostgreSQL | pgsql | WIP |
PL/SQL | plsql | WIP |
Monaco SQL Languages plan to support more types of SQL Languages in the future. If you need some SQL Languages that are not currently supported, you can contact us at github.
Installing
npm install monaco-sql-languages
Tips: Your version of Monaco Editor should be 0.31.0, Monaco SQL Languages is only guaranteed to work stably on monaco-editor@0.31.0
for now.
Integrating
Using the Monaco Editor Webpack Plugin
-
Install Monaco Editor Webpack Plugin
npm install monaco-editor-webpack-plugin
-
Apply Monaco Editor Webpack Plugin in webpack config
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
const path = require('path');
const monacoWebpackPlugin = new MonacoWebpackPlugin({
features: [],
languages: [],
customLanguages: [
{
label: 'mysql',
entry: 'monaco-sql-languages/out/esm/mysql/mysql.contribution',
worker: {
id: 'monaco-sql-languages/out/esm/mysql/mySQLWorker',
entry: 'monaco-sql-languages/out/esm/mysql/mysql.worker'
}
},
{
label: 'flinksql',
entry: 'monaco-sql-languages/out/esm/flinksql/flinksql.contribution',
worker: {
id: 'monaco-sql-languages/out/esm/flinksql/flinkSQLWorker',
entry: 'monaco-sql-languages/out/esm/flinksql/flinksql.worker'
}
},
{
label: 'sparksql',
entry: 'monaco-sql-languages/out/esm/sparksql/sparksql.contribution',
worker: {
id: 'monaco-sql-languages/out/esm/sparksql/sparkSQLWorker',
entry: 'monaco-sql-languages/out/esm/sparksql/sparksql.worker'
}
},
{
label: 'hivesql',
entry: 'monaco-sql-languages/out/esm/hivesql/hivesql.contribution',
worker: {
id: 'monaco-sql-languages/out/esm/hivesql/hiveSQLWorker',
entry: 'monaco-sql-languages/out/esm/hivesql/hivesql.worker'
}
},
{
label: 'trinosql',
entry: 'monaco-sql-languages/out/esm/trinosql/trinosql.contribution',
worker: {
id: 'monaco-sql-languages/out/esm/trinosql/TrinoSQLWorker',
entry: 'monaco-sql-languages/out/esm/trinosql/trinosql.worker'
}
},
{
label: 'pgsql',
entry: 'monaco-sql-languages/out/esm/pgsql/pgsql.contribution',
worker: {
id: 'monaco-sql-languages/out/esm/pgsql/PgSQLWorker',
entry: 'monaco-sql-languages/out/esm/pgsql/pgsql.worker'
}
},
{
label: 'plsql',
entry: 'monaco-sql-languages/out/esm/plsql/plsql.contribution',
worker: {
id: 'monaco-sql-languages/out/esm/plsql/plSQLWorker',
entry: 'monaco-sql-languages/out/esm/plsql/plsql.worker'
}
}
]
});
module.exports = {
entry: './index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app.js'
},
module: {},
plugins: [monacoEditorPlugin]
};
More options of Monaco Editor Webpack Plugin, see here.
Using Plain Webpack
Output worker files via webpack entries.
entry: {
'mysql.worker': 'monaco-sql-languages/out/esm/mysql/mysql.worker.js',
'flinksql.worker': 'monaco-sql-languages/out/esm/flinksql/flinksql.worker.js',
'sparksql.worker': 'monaco-sql-languages/out/esm/sparksql/sparksql.worker.js',
'hivesql.worker': 'monaco-sql-languages/out/esm/hivesql/hivesql.worker.js',
'trinosql.worker': 'monaco-sql-languages/out/esm/trinosql/trinosql.worker.js',
'pgsql.worker': 'monaco-sql-languages/out/esm/pgsql/pgsql.worker.js',
'plsql.worker': 'monaco-sql-languages/out/esm/plsql/plsql.worker.js',
'editor.worker': 'monaco-editor/esm/vs/editor/editor.worker.js',
},
Define the global variable MonacoEnvironment
and specify the path of the worker file
window.MonacoEnvironment = {
getWorkerUrl: function (moduleId, label) {
switch (label) {
case 'mysql': {
return './mysql.worker.js';
}
case 'sparksql': {
return './sparksql.worker.js';
}
case 'flinksql': {
return './flinksql.worker.js';
}
case 'hivesql': {
return './hivesql.worker.js';
}
case 'trinosql': {
return './trinosql.worker.js';
}
case 'pgsql': {
return './pgsql.worker.js';
}
case 'plsql': {
return './plsql.worker.js';
}
default: {
return './editor.worker.js';
}
}
}
};
Using Vite
Vite example see https://github.com/DTStack/monaco-sql-languages/blob/main/website/src/languageWorker.ts
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/out/esm/mysql/mysql.contribution';
import 'monaco-sql-languages/out/esm/flinksql/flinksql.contribution';
import 'monaco-sql-languages/out/esm/sparksql/sparksql.contribution';
import 'monaco-sql-languages/out/esm/hivesql/hivesql.contribution';
import 'monaco-sql-languages/out/esm/trinosql/trinosql.contribution';
import 'monaco-sql-languages/out/esm/plsql/plsql.contribution';
import 'monaco-sql-languages/out/esm/pgsql/pgsql.contribution';
import 'monaco-sql-languages/out/esm/sql/sql.contribution';
-
Setup language features
You can setup language features via setupLanguageFeatures
. For example, disable code completion feature of flinkSQL language.
import {
setupLanguageFeatures,
LanguageIdEnum,
} from 'monaco-sql-languages';
setupLanguageFeatures({
languageId: LanguageIdEnum.FLINK,
completionItems: false
})
By default, MonacoSQLLanguages only provides keyword autocompletion, and you can customize your completionItem list via completionService
.
import { languages } from 'monaco-editor/esm/vs/editor/editor.api';
import {
setupLanguageFeatures,
LanguageIdEnum,
CompletionService,
ICompletionItem,
SyntaxContextType
} from 'monaco-sql-languages';
const completionService: CompletionService = function (
model,
position,
completionContext,
suggestions
) {
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 === SyntaxContextType.DATABASE) {
const databaseCompletions: ICompletionItem[] = [];
syntaxCompletionItems = [...syntaxCompletionItems, ...databaseCompletions];
}
if (item.syntaxContextType === SyntaxContextType.TABLE) {
const tableCompletions: ICompletionItem[] = [];
syntaxCompletionItems = [...syntaxCompletionItems, ...tableCompletions];
}
});
resolve([...syntaxCompletionItems, ...keywordsCompletionItems]);
});
};
setupLanguageFeatures({
languageId: LanguageIdEnum.FLINK,
completionService: completionService,
})
-
Create the Monaco Editor instance and specify the language you need
monaco.editor.create(document.getElementById('container'), {
value: 'select * from tb_test',
language: 'flinksql'
});
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';
editor.defineTheme('sql-dark', vsPlusTheme.darkThemeData);
editor.defineTheme('sql-light', vsPlusTheme.lightThemeData);
editor.defineTheme('sql-hc', vsPlusTheme.hcBlackThemeData);
editor.create(null as any, {
theme: 'sql-dark',
language: 'flinksql'
});
Customize your own Monaco theme
import { TokenClassConsts, postfixTokenClass } from 'monaco-sql-languages';
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: {}
};
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 opencode@microsoft.com with any additional questions or comments.
License
MIT