Skip to content

Commit b6df8a7

Browse files
author
zhaoge
committed
Merge branch 'main' into next
2 parents 6fa4312 + 7231989 commit b6df8a7

23 files changed

Lines changed: 1003 additions & 32 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/node_modules/
44
/out/
55
/esm/
6+
/esm-dev/
67
/release/
78
.history
89
/docs/

README-zh_CN.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,47 @@ npm install monaco-sql-languages
162162
});
163163
```
164164

165+
4. **SQL 格式化(可选)**
166+
167+
格式化依赖 [`sql-formatter`](https://github.com/sql-formatter-org/sql-formatter) 作为**可选 peer**。主入口**不要求**安装该包:只用补全/诊断时不必安装 `sql-formatter`。
168+
169+
仅在开启格式化,或 import 格式化 API 时再安装。应用里须 **import 一次** format 入口(副作用),再调用 `setupLanguageFeatures(..., { format: { enable: true } })`。未 import 就开启 format**抛错**;若之后再 import format,已加载的语言会自动补偿注册。
170+
171+
```bash
172+
npm install sql-formatter
173+
```
174+
175+
必须设置 `format.enable: true` 才会注册编辑器 Format 菜单/快捷键(只传 `fallback` 无效)。**优先级:** 内置 sql-formatter`fallback`(不可用或抛错)→ 原文。
176+
177+
```typescript
178+
import { KeyCode, KeyMod } from 'monaco-editor';
179+
import 'monaco-sql-languages/format';
180+
import { LanguageIdEnum, setupLanguageFeatures } from 'monaco-sql-languages';
181+
182+
setupLanguageFeatures(LanguageIdEnum.MYSQL, {
183+
format: {
184+
enable: true, // 注册右键 / 快捷键所必需
185+
tabWidth: 4,
186+
fallback: (code, languageId) => myLegacyFormat(code, languageId),
187+
// 可选;默认 Ctrl/Cmd+Alt+F
188+
keybindings: [KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KeyF]
189+
}
190+
});
191+
```
192+
193+
工具栏等外部 UI 请从 format 入口引入(**构建期需要**已安装 `sql-formatter`):
194+
195+
```typescript
196+
import { formatEditorSQL, formatSQL } from 'monaco-sql-languages/format';
197+
198+
await formatEditorSQL(editor, { tabWidth: 4, fallback: myLegacyFormat });
199+
const text = await formatSQL(code, LanguageIdEnum.MYSQL, { tabWidth: 4 });
200+
```
201+
202+
> **版本:** 使用项目中安装的 sql-formatterpeer 要求 **`>=10.7.2`**(覆盖 `hive` / `trino``tabWidth`)。现代浏览器用 v15+ 亦可;Chrome 75pin `sql-formatter@10.7.2`
203+
204+
默认快捷键:**Ctrl/Cmd+Alt+F**(右键 **Format**)。结果与原文相同则不写 edits
205+
165206
<br/>
166207

167208
## 运行语句按钮

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,47 @@ npm install monaco-sql-languages
162162
});
163163
```
164164

165+
4. **SQL Formatting (optional)**
166+
167+
Formatting uses [`sql-formatter`](https://github.com/sql-formatter-org/sql-formatter) as an **optional peer**. The main package entry does **not** require it: completion and diagnostics work without installing `sql-formatter`.
168+
169+
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.
170+
171+
```bash
172+
npm install sql-formatter
173+
```
174+
175+
`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.
176+
177+
```typescript
178+
import { KeyCode, KeyMod } from 'monaco-editor';
179+
import 'monaco-sql-languages/format';
180+
import { LanguageIdEnum, setupLanguageFeatures } from 'monaco-sql-languages';
181+
182+
setupLanguageFeatures(LanguageIdEnum.MYSQL, {
183+
format: {
184+
enable: true, // required for the context-menu / shortcut action
185+
tabWidth: 4,
186+
fallback: (code, languageId) => myLegacyFormat(code, languageId),
187+
// optional; default is Ctrl/Cmd+Alt+F
188+
keybindings: [KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KeyF]
189+
}
190+
});
191+
```
192+
193+
Toolbar or other UI should import the format entry (this **does** require `sql-formatter` at build time):
194+
195+
```typescript
196+
import { formatEditorSQL, formatSQL } from 'monaco-sql-languages/format';
197+
198+
await formatEditorSQL(editor, { tabWidth: 4, fallback: myLegacyFormat });
199+
const text = await formatSQL(code, LanguageIdEnum.MYSQL, { tabWidth: 4 });
200+
```
201+
202+
> **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 `sql-formatter@10.7.2`.
203+
204+
Editor shortcut defaults to **Ctrl/Cmd+Alt+F** (context menu **Format**). Unchanged results apply no edit.
205+
165206
<br/>
166207

167208
## Run Statement Button

package.json

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "monaco-sql-languages",
3-
"version": "1.2.0",
3+
"version": "1.2.1-beta.2",
44
"description": "SQL languages for the Monaco Editor, based on monaco-languages.",
55
"scripts": {
66
"prepublishOnly": "npm run build",
77
"build": "rm -rf ./esm && tsc -p ./tsconfig.esm.json",
8+
"build:dev": "rm -rf ./esm-dev && tsc -p ./tsconfig.esm.json --outDir esm-dev",
89
"build-amd": "rm -rf ./out && tsc -p ./tsconfig.amd.json",
910
"watch-esm": "tsc -p ./tsconfig.esm.json --watch",
1011
"test": "npm run build-amd && mocha ./test/all.js",
11-
"dev": "node --max_old_space_size=4092 & cd website && npm run dev",
12+
"dev": "npm run build:dev && node --max_old_space_size=4092 & cd website && npm run dev",
1213
"prod": "rm -rf ./docs && node --max_old_space_size=4092 & cd website && npm run build",
1314
"deploy": "npm run build && npm run prod && gh-pages -d docs -r git@github.com:DTStack/monaco-sql-languages.git",
1415
"format": "prettier --write .",
@@ -21,6 +22,17 @@
2122
"main": "esm/main.js",
2223
"module": "esm/main.js",
2324
"types": "esm/main.d.ts",
25+
"exports": {
26+
".": {
27+
"types": "./esm/main.d.ts",
28+
"default": "./esm/main.js"
29+
},
30+
"./format": {
31+
"types": "./esm/format.entry.d.ts",
32+
"default": "./esm/format.entry.js"
33+
},
34+
"./esm/*": "./esm/*"
35+
},
2436
"files": [
2537
"esm"
2638
],
@@ -58,6 +70,7 @@
5870
"mocha": "^11.7.4",
5971
"monaco-editor": "0.54.0",
6072
"prettier": "^3.3.3",
73+
"sql-formatter": "^15.8.2",
6174
"pretty-quick": "^4.0.0",
6275
"requirejs": "^2.3.6",
6376
"simple-git-hooks": "^2.11.1",
@@ -68,7 +81,13 @@
6881
"pre-commit": "npx pretty-quick --staged"
6982
},
7083
"peerDependencies": {
71-
"monaco-editor": ">=0.37.1"
84+
"monaco-editor": ">=0.37.1",
85+
"sql-formatter": ">=10.7.2"
86+
},
87+
"peerDependenciesMeta": {
88+
"sql-formatter": {
89+
"optional": true
90+
}
7291
},
7392
"engines": {
7493
"node": ">=18"

pnpm-lock.yaml

Lines changed: 55 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/format.entry.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { registerFormatAction } from './formatEditor';
2+
import { setFormatActionRegistrar } from './formatBridge';
3+
4+
/**
5+
* Optional format API entry (`monaco-sql-languages/format`).
6+
* Importing this module pulls in sql-formatter and registers the built-in Format action
7+
* used when `format.enable` is true.
8+
*/
9+
setFormatActionRegistrar(registerFormatAction);
10+
11+
export { formatSQL } from './format';
12+
export { registerFormatAction };
13+
export { formatEditorSQL, type FormatTargetEditor, type TextRange } from './formatEditor';
14+
export type { FormatFallback } from './formatTypes';
15+
export type { FormatSQLOptions } from './monaco.contribution';

0 commit comments

Comments
 (0)