Skip to content

Commit 778fc14

Browse files
committed
Add boilerplate for AML preview
1 parent 58347fb commit 778fc14

File tree

3 files changed

+71
-15
lines changed

3 files changed

+71
-15
lines changed

extensions/vscode-aml/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ This Visual Studio Code extension provides language support for [AML](https://az
1616

1717
## Roadmap
1818

19-
- Add parsing errors and auto-complete
19+
- Add parsing errors
20+
- auto-complete
21+
- quick-fixes (code actions)
22+
- Go-to-definition for relations (cf https://microsoft.github.io/monaco-editor/typedoc/interfaces/languages.DocumentSymbolProvider.html)
23+
- hover info
2024
- Add diagram preview & Open in Azimutt
2125
- Add AML support in Markdown
2226
- Connect to a database
@@ -35,7 +39,7 @@ Here are some interesting VS Code documentation you may find helpful:
3539

3640
- [VS Code extension get started](https://code.visualstudio.com/api/get-started/your-first-extension).
3741
- [Language Configuration Guide](https://code.visualstudio.com/api/language-extensions/language-configuration-guide)
38-
- [Syntax Highlight Guide](https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide)
42+
- [Syntax Highlight Guide](https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide) & [Semantic Highlight Guide](https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide)
3943
- [Snippet Guide](https://code.visualstudio.com/api/language-extensions/snippet-guide)
4044

4145
Tips:

extensions/vscode-aml/package.json

+22-9
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,47 @@
6060
],
6161
"commands": [
6262
{
63-
"command": "vscode-aml.fromJson",
63+
"command": "aml.fromJson",
6464
"category": "AML",
6565
"title": "convert JSON to AML"
6666
},
6767
{
68-
"command": "vscode-aml.fromSQL",
68+
"command": "aml.fromSQL",
6969
"category": "AML",
7070
"title": "convert SQL to AML"
7171
},
7272
{
73-
"command": "vscode-aml.convert",
73+
"command": "aml.convert",
7474
"category": "AML",
7575
"title": "convert AML to"
76+
},
77+
{
78+
"command": "aml.preview",
79+
"category": "AML",
80+
"title": "preview AML",
81+
"icon": "$(open-preview)"
7682
}
7783
],
7884
"menus": {
7985
"commandPalette": [
8086
{
81-
"command": "vscode-aml.fromJson",
82-
"when": "editorLangId == json"
87+
"when": "editorLangId == json",
88+
"command": "aml.fromJson"
8389
},
8490
{
85-
"command": "vscode-aml.fromSQL",
86-
"when": "editorLangId == sql"
91+
"when": "editorLangId == sql",
92+
"command": "aml.fromSQL"
8793
},
8894
{
89-
"command": "vscode-aml.convert",
90-
"when": "editorLangId == aml"
95+
"when": "editorLangId == aml",
96+
"command": "aml.convert"
97+
}
98+
],
99+
"editor/title": [
100+
{
101+
"when": "editorLangId == aml",
102+
"command": "aml.preview",
103+
"group": "navigation"
91104
}
92105
]
93106
}

extensions/vscode-aml/src/web/extension.ts

+43-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import vscode, {TextDocument, TextEditor, TextEditorEdit} from "vscode";
1+
import vscode, {TextDocument, TextEditor, TextEditorEdit, ViewColumn, WebviewPanel} from "vscode";
22
import {ParserError, ParserErrorLevel} from "@azimutt/models";
33
import {generateSql, parseSql} from "@azimutt/parser-sql";
44
import {
@@ -14,10 +14,26 @@ import {
1414
} from "@azimutt/aml";
1515

1616
export function activate(context: vscode.ExtensionContext) {
17+
let previewPanel: WebviewPanel | undefined = undefined
1718
context.subscriptions.push(
18-
vscode.commands.registerTextEditorCommand('vscode-aml.fromJson', (editor: TextEditor, edit: TextEditorEdit) => convertJson(editor, edit)),
19-
vscode.commands.registerTextEditorCommand('vscode-aml.fromSQL', (editor: TextEditor, edit: TextEditorEdit) => convertSql(editor, edit)),
20-
vscode.commands.registerTextEditorCommand('vscode-aml.convert', (editor: TextEditor, edit: TextEditorEdit) => convertAml(editor, edit))
19+
vscode.commands.registerTextEditorCommand('aml.fromJson', (editor: TextEditor, edit: TextEditorEdit) => convertJson(editor, edit)),
20+
vscode.commands.registerTextEditorCommand('aml.fromSQL', (editor: TextEditor, edit: TextEditorEdit) => convertSql(editor, edit)),
21+
vscode.commands.registerTextEditorCommand('aml.convert', (editor: TextEditor, edit: TextEditorEdit) => convertAml(editor, edit)),
22+
vscode.commands.registerTextEditorCommand('aml.preview', (editor: TextEditor, edit: TextEditorEdit) => {
23+
vscode.window.showInformationMessage('aml.preview called')
24+
if (editor.document.languageId !== 'aml') {
25+
vscode.window.showErrorMessage('Needs AML file to preview it.')
26+
return
27+
}
28+
const viewColumn = editor.viewColumn ? editor.viewColumn + 1 : ViewColumn.Two
29+
if (!previewPanel) {
30+
previewPanel = vscode.window.createWebviewPanel('aml-preview', 'Preview AML', {viewColumn, preserveFocus: true}, {localResourceRoots: []})
31+
previewPanel.onDidDispose(() => previewPanel = undefined, null, context.subscriptions)
32+
}
33+
updateAmlPreview(editor.document, previewPanel, viewColumn)
34+
// TODO: update preview when editor text changes or when editor changes to another aml (with debounce)
35+
// vscode.window.onDidChangeActiveTextEditor((editor: TextEditor) => {})
36+
})
2137
)
2238
}
2339

@@ -101,6 +117,28 @@ async function convertAml(editor: TextEditor, edit: TextEditorEdit): Promise<voi
101117
}
102118
}
103119

120+
function updateAmlPreview(doc: TextDocument, panel: WebviewPanel, col: ViewColumn) {
121+
panel.title = 'Preview ' + doc.fileName
122+
panel.webview.html = buildAmlPreview(doc.getText())
123+
if (panel.viewColumn !== col) {
124+
panel.reveal(col)
125+
}
126+
}
127+
128+
function buildAmlPreview(aml: string): string {
129+
return `<!DOCTYPE html>
130+
<html lang="en">
131+
<head>
132+
<meta charset="UTF-8">
133+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
134+
<title>AML preview</title>
135+
</head>
136+
<body>
137+
<pre>${aml}</pre>
138+
</body>
139+
</html>`;
140+
}
141+
104142
async function openFile(lang: string, content: string): Promise<TextDocument> {
105143
const doc: TextDocument = await vscode.workspace.openTextDocument({language: lang, content: content})
106144
await vscode.window.showTextDocument(doc)
@@ -124,5 +162,6 @@ function formatErrorLevel(level: ParserErrorLevel): string {
124162
case 'warning': return '[WARN]'
125163
case 'info': return '[INFO]'
126164
case 'hint': return '[HINT]'
165+
default: return '[ERR] '
127166
}
128167
}

0 commit comments

Comments
 (0)