@mhjuma/traverse 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/package.json +7 -4
- package/src/components/Explorer/Explorer.spec.tsx +11 -3
- package/src/components/Explorer/Explorer.tsx +11 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.spec.tsx +53 -17
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.tsx +78 -74
- package/src/components/ExplorerWorkspace/bootstrapKustoLanguage.spec.ts +150 -0
- package/src/components/ExplorerWorkspace/bootstrapKustoLanguage.ts +95 -0
- package/src/components/ExplorerWorkspace/kustoSchemaQueue.spec.ts +322 -0
- package/src/components/ExplorerWorkspace/kustoSchemaQueue.ts +188 -0
- package/src/components/ExplorerWorkspace/monacoLoader.spec.ts +82 -0
- package/src/components/ExplorerWorkspace/monacoLoader.ts +57 -0
- package/src/components/ExplorerWorkspace/monacoWorkers.spec.ts +81 -0
- package/src/components/ExplorerWorkspace/monacoWorkers.ts +90 -0
- package/src/index.ts +2 -0
- package/src/services/schema.spec.ts +107 -28
- package/src/services/schema.ts +66 -25
- package/src/test-setup.ts +3 -0
- package/vitest.config.ts +7 -0
- package/src/components/ExplorerWorkspace/kqlLanguage.spec.ts +0 -321
- package/src/components/ExplorerWorkspace/kqlLanguage.ts +0 -155
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
# @mhjuma/traverse
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 57f602c: Replace the hand-rolled KQL Monarch tokenizer and regex completion provider
|
|
8
|
+
with the official `@kusto/monaco-kusto` language plugin (Kusto.Language engine
|
|
9
|
+
running in a Web Worker). This gives semantic highlighting, full IntelliSense
|
|
10
|
+
(let-bindings, joins, function defs, plugin functions), hover, signature help,
|
|
11
|
+
diagnostics, and formatting.
|
|
12
|
+
|
|
13
|
+
Breaking changes for hosts:
|
|
14
|
+
|
|
15
|
+
- `monaco-editor` and `@monaco-editor/react` are now peer dependencies. Hosts
|
|
16
|
+
must install them directly; `monaco-editor` must be pinned to `0.52.x` for
|
|
17
|
+
compatibility with `@kusto/monaco-kusto@14.x`.
|
|
18
|
+
- A new `configureTraverseMonacoWorkers({ getEditorWorker, getKustoWorker })`
|
|
19
|
+
must be called once before mounting `<Explorer>` to wire up the Monaco and
|
|
20
|
+
Kusto language workers. See the Quick Start guide for the Vite recipe and
|
|
21
|
+
notes on Webpack/Next.js hosts.
|
|
22
|
+
|
|
23
|
+
Bundle impact: the Kusto language service ships ~6 MB of compiled engine code
|
|
24
|
+
in a dedicated worker chunk, loaded lazily only when an editor mounts.
|
|
25
|
+
|
|
26
|
+
Perceived-perf optimizations: the kusto language registers (Monarch tokens
|
|
27
|
+
provider + themes) on first `<Explorer>` render via an internal
|
|
28
|
+
`preloadKustoLanguage()` call, so first paint already shows colored keywords
|
|
29
|
+
and strings. Hosts can additionally pre-warm the kusto worker during
|
|
30
|
+
`requestIdleCallback` (see Quick Start) to overlap Bridge.NET evaluation with
|
|
31
|
+
auth/React init — semantic-token colors then arrive without a visible delay.
|
|
32
|
+
|
|
3
33
|
## 0.2.0
|
|
4
34
|
|
|
5
35
|
### Minor Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mhjuma/traverse",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -10,9 +10,8 @@
|
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@fluentui/react-components": "9.73.7",
|
|
12
12
|
"@fluentui/react-icons": "2.0.323",
|
|
13
|
-
"@monaco-
|
|
13
|
+
"@kusto/monaco-kusto": "14.1.0",
|
|
14
14
|
"@tanstack/react-virtual": "3.13.23",
|
|
15
|
-
"monaco-editor": "0.55.1",
|
|
16
15
|
"react": "19.2.5",
|
|
17
16
|
"react-dom": "19.2.5",
|
|
18
17
|
"vega": "6.2.0",
|
|
@@ -20,7 +19,9 @@
|
|
|
20
19
|
"vega-tooltip": "1.1.0"
|
|
21
20
|
},
|
|
22
21
|
"peerDependencies": {
|
|
23
|
-
"@azure/msal-browser": ">=3.0.0"
|
|
22
|
+
"@azure/msal-browser": ">=3.0.0",
|
|
23
|
+
"@monaco-editor/react": "4.7.0",
|
|
24
|
+
"monaco-editor": "0.52.2"
|
|
24
25
|
},
|
|
25
26
|
"peerDependenciesMeta": {
|
|
26
27
|
"@azure/msal-browser": {
|
|
@@ -29,6 +30,8 @@
|
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"@azure/msal-browser": "5.6.3",
|
|
33
|
+
"@monaco-editor/react": "4.7.0",
|
|
34
|
+
"monaco-editor": "0.52.2",
|
|
32
35
|
"@types/react": "19.2.14",
|
|
33
36
|
"@types/react-dom": "19.2.3",
|
|
34
37
|
"oxlint": "1.59.0",
|
|
@@ -6,6 +6,7 @@ import { Explorer } from './Explorer';
|
|
|
6
6
|
|
|
7
7
|
vi.mock('@monaco-editor/react', () => ({
|
|
8
8
|
Editor: () => React.createElement('div', { 'data-testid': 'monaco-editor' }),
|
|
9
|
+
loader: { config: vi.fn() },
|
|
9
10
|
}));
|
|
10
11
|
vi.mock('../../services/kusto', () => ({
|
|
11
12
|
queryKusto: vi.fn().mockResolvedValue({ columns: [], rows: [] }),
|
|
@@ -85,10 +86,17 @@ vi.mock('../../state/persistence', () => ({
|
|
|
85
86
|
validateAndCleanSnapshot: vi.fn(() => null),
|
|
86
87
|
extractSnapshot: vi.fn((s: { tabs: unknown[]; activeTabId: string }) => ({ version: 1, tabs: s.tabs, activeTabId: s.activeTabId, splitEnabled: false, splitDirection: 'vertical', splitTabId: null, focusedPane: 'primary' })),
|
|
87
88
|
}));
|
|
88
|
-
vi.mock('../ExplorerWorkspace/
|
|
89
|
-
|
|
90
|
-
setKqlSchemaResolver: vi.fn(),
|
|
89
|
+
vi.mock('../ExplorerWorkspace/bootstrapKustoLanguage', () => ({
|
|
90
|
+
bootstrapKustoLanguage: vi.fn().mockResolvedValue(undefined),
|
|
91
91
|
}));
|
|
92
|
+
vi.mock('../ExplorerWorkspace/kustoSchemaQueue', () => ({
|
|
93
|
+
applyKustoSchema: vi.fn(),
|
|
94
|
+
}));
|
|
95
|
+
// Stub `@kusto/monaco-kusto` — Vitest cannot resolve the real package
|
|
96
|
+
// against jsdom (engine + worker globals are not available). Keeping the
|
|
97
|
+
// stub minimal avoids unhandled-rejection noise from `kustoSchemaQueue`'s
|
|
98
|
+
// dynamic import path.
|
|
99
|
+
vi.mock('@kusto/monaco-kusto', () => ({ kustoDefaults: {}, getKustoWorker: () => Promise.resolve(() => Promise.resolve({})) }));
|
|
92
100
|
vi.mock('@fluentui/react-components', () => ({
|
|
93
101
|
Button: (p: Record<string, unknown>) => React.createElement('button', p, p['children'] as React.ReactNode),
|
|
94
102
|
Tooltip: (p: Record<string, unknown>) => React.createElement('div', null, p['children'] as React.ReactNode),
|
|
@@ -11,6 +11,7 @@ import type { ExplorerTabsSnapshot } from '../../state/persistence';
|
|
|
11
11
|
import { DEFAULT_QUERY, buildWellKnownClusters } from '../ExplorerWorkspace/ExplorerWorkspace.logic';
|
|
12
12
|
import type { WellKnownCluster } from '../ExplorerWorkspace/ExplorerWorkspace.logic';
|
|
13
13
|
import { ExplorerWorkspace } from '../ExplorerWorkspace/ExplorerWorkspace';
|
|
14
|
+
import { preloadKustoLanguage } from '../ExplorerWorkspace/monacoLoader';
|
|
14
15
|
|
|
15
16
|
export interface ExplorerProps {
|
|
16
17
|
className?: string;
|
|
@@ -35,6 +36,12 @@ export interface ExplorerProps {
|
|
|
35
36
|
const SNAPSHOT_SAVE_DEBOUNCE_MS = 300;
|
|
36
37
|
|
|
37
38
|
export function Explorer(props: ExplorerProps) {
|
|
39
|
+
// Trigger kusto language preload on first Explorer render. Editor-gated
|
|
40
|
+
// by design (here, not at module load) so consumers using only
|
|
41
|
+
// non-editor APIs from `@mhjuma/traverse` don't pay the kusto chunk cost.
|
|
42
|
+
// Idempotent — internally guarded so subsequent renders are no-ops.
|
|
43
|
+
preloadKustoLanguage();
|
|
44
|
+
|
|
38
45
|
const { initialQuery: providedInitialQuery } = props;
|
|
39
46
|
|
|
40
47
|
const { initialQuery, hasUrlQuery } = useMemo(() => {
|
|
@@ -100,6 +107,10 @@ export function Explorer(props: ExplorerProps) {
|
|
|
100
107
|
};
|
|
101
108
|
}, [hasUrlQuery]);
|
|
102
109
|
|
|
110
|
+
// The Kusto language is pre-bootstrapped in monacoLoader.ts at module
|
|
111
|
+
// load time so the editor's first paint already has Monarch tokens and
|
|
112
|
+
// themes registered. No idle-callback prefetch needed here.
|
|
113
|
+
|
|
103
114
|
return (
|
|
104
115
|
<KustoClientContext.Provider value={props.kustoClient}>
|
|
105
116
|
<ExplorerColorProvider value={props.colors}>
|
|
@@ -5,9 +5,10 @@ import { createRoot } from 'react-dom/client';
|
|
|
5
5
|
import { act } from 'react';
|
|
6
6
|
import { ExplorerWorkspace } from './ExplorerWorkspace';
|
|
7
7
|
|
|
8
|
-
import { loadSchema, getSchema } from '../../services/schema';
|
|
8
|
+
import { loadSchema, getSchema, getRawShowSchema } from '../../services/schema';
|
|
9
9
|
import { getHistory } from '../../services/queryHistory';
|
|
10
|
-
import {
|
|
10
|
+
import { bootstrapKustoLanguage } from './bootstrapKustoLanguage';
|
|
11
|
+
import { applyKustoSchema } from './kustoSchemaQueue';
|
|
11
12
|
import { saveActiveConnectionId } from './ExplorerWorkspace.logic';
|
|
12
13
|
|
|
13
14
|
vi.mock('../EditorToolbar/EditorToolbar', () => ({
|
|
@@ -53,8 +54,18 @@ vi.mock('@monaco-editor/react', () => ({
|
|
|
53
54
|
getValue: () => 'TestTable | take 10',
|
|
54
55
|
};
|
|
55
56
|
const mockMonaco = {
|
|
56
|
-
languages: {
|
|
57
|
-
|
|
57
|
+
languages: {
|
|
58
|
+
register: vi.fn(),
|
|
59
|
+
setMonarchTokensProvider: vi.fn(),
|
|
60
|
+
registerCompletionItemProvider: vi.fn(),
|
|
61
|
+
getLanguages: vi.fn(() => [{ id: 'kusto' }]),
|
|
62
|
+
},
|
|
63
|
+
editor: {
|
|
64
|
+
defineTheme: vi.fn(),
|
|
65
|
+
setTheme: vi.fn(),
|
|
66
|
+
getModels: vi.fn(() => []),
|
|
67
|
+
onDidCreateModel: vi.fn(() => ({ dispose: vi.fn() })),
|
|
68
|
+
},
|
|
58
69
|
KeyMod: { CtrlCmd: 2048, Shift: 1024, Alt: 512 },
|
|
59
70
|
KeyCode: { Enter: 3, KeyR: 48, KeyF: 36 },
|
|
60
71
|
};
|
|
@@ -114,6 +125,7 @@ vi.mock('../../services/queryHistory', () => ({
|
|
|
114
125
|
vi.mock('../../services/schema', () => ({
|
|
115
126
|
getSchema: vi.fn().mockReturnValue([]),
|
|
116
127
|
loadSchema: vi.fn().mockResolvedValue(undefined),
|
|
128
|
+
getRawShowSchema: vi.fn().mockReturnValue(null),
|
|
117
129
|
}));
|
|
118
130
|
vi.mock('./ExplorerWorkspace.logic', () => ({
|
|
119
131
|
formatKql: (s: string) => s,
|
|
@@ -154,9 +166,11 @@ vi.mock('./ExplorerWorkspace.logic', () => ({
|
|
|
154
166
|
vi.mock('./ExplorerWorkspace.styles', () => ({
|
|
155
167
|
useExplorerStyles: () => new Proxy({}, { get: (_, p) => String(p) }),
|
|
156
168
|
}));
|
|
157
|
-
vi.mock('./
|
|
158
|
-
|
|
159
|
-
|
|
169
|
+
vi.mock('./bootstrapKustoLanguage', () => ({
|
|
170
|
+
bootstrapKustoLanguage: vi.fn().mockResolvedValue(undefined),
|
|
171
|
+
}));
|
|
172
|
+
vi.mock('./kustoSchemaQueue', () => ({
|
|
173
|
+
applyKustoSchema: vi.fn(),
|
|
160
174
|
}));
|
|
161
175
|
|
|
162
176
|
|
|
@@ -378,13 +392,14 @@ describe('ExplorerWorkspace', () => {
|
|
|
378
392
|
expect(getHistory).toHaveBeenCalled();
|
|
379
393
|
});
|
|
380
394
|
|
|
381
|
-
// ---
|
|
382
|
-
it('
|
|
395
|
+
// --- No custom run-highlight CSS on mount ---
|
|
396
|
+
it('does not inject the legacy traverse-run-highlight style element', () => {
|
|
383
397
|
act(() => { root.render(React.createElement(ExplorerWorkspace, defaultProps)); });
|
|
384
398
|
|
|
399
|
+
// monaco-kusto's built-in KustoCommandHighlighter replaces our custom
|
|
400
|
+
// block decoration; the global style element must no longer be added.
|
|
385
401
|
const styleEl = document.getElementById('traverse-run-highlight-style');
|
|
386
|
-
expect(styleEl).
|
|
387
|
-
expect(styleEl?.textContent).toContain('traverse-run-highlight');
|
|
402
|
+
expect(styleEl).toBeFalsy();
|
|
388
403
|
});
|
|
389
404
|
|
|
390
405
|
// --- DragOver on primary editor ---
|
|
@@ -418,16 +433,37 @@ describe('ExplorerWorkspace', () => {
|
|
|
418
433
|
expect(toolbar?.getAttribute('data-show-cancel')).toBe('false');
|
|
419
434
|
});
|
|
420
435
|
|
|
421
|
-
// ---
|
|
422
|
-
it('
|
|
436
|
+
// --- applyKustoSchema called when schema available ---
|
|
437
|
+
it('pushes schema to the Kusto worker on mount when schema is cached', () => {
|
|
438
|
+
const fakeRaw = { Plugins: [], Databases: { DB: { Name: 'DB', Tables: {}, Functions: {} } } };
|
|
439
|
+
vi.mocked(getRawShowSchema).mockReturnValue(fakeRaw as unknown as ReturnType<typeof getRawShowSchema>);
|
|
440
|
+
act(() => { root.render(React.createElement(ExplorerWorkspace, defaultProps)); });
|
|
441
|
+
expect(applyKustoSchema).toHaveBeenCalled();
|
|
442
|
+
const call = vi.mocked(applyKustoSchema).mock.calls.at(-1)?.[0];
|
|
443
|
+
expect(call?.raw).toBe(fakeRaw);
|
|
444
|
+
expect(call?.clusterUri).toBe('https://test.kusto.windows.net');
|
|
445
|
+
expect(call?.dbName).toBe('DB');
|
|
446
|
+
expect(call?.monaco).toBeTruthy();
|
|
447
|
+
vi.mocked(getRawShowSchema).mockReturnValue(null);
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
// --- bootstrapKustoLanguage called on editor mount ---
|
|
451
|
+
it('bootstraps the Kusto language on editor mount', () => {
|
|
423
452
|
act(() => { root.render(React.createElement(ExplorerWorkspace, defaultProps)); });
|
|
424
|
-
expect(
|
|
453
|
+
expect(bootstrapKustoLanguage).toHaveBeenCalled();
|
|
454
|
+
// Bootstrap receives the same Monaco instance the editor was given —
|
|
455
|
+
// this is the contract the schema queue relies on to apply settings
|
|
456
|
+
// and themes against the right Monaco copy.
|
|
457
|
+
const bootstrapArg = vi.mocked(bootstrapKustoLanguage).mock.calls[0]?.[0];
|
|
458
|
+
expect(bootstrapArg).toBeTruthy();
|
|
425
459
|
});
|
|
426
460
|
|
|
427
|
-
// ---
|
|
428
|
-
it('
|
|
461
|
+
// --- no schema push when raw schema is not yet cached ---
|
|
462
|
+
it('does not push schema when getRawShowSchema returns null', () => {
|
|
463
|
+
vi.mocked(getRawShowSchema).mockReturnValue(null);
|
|
464
|
+
vi.mocked(applyKustoSchema).mockClear();
|
|
429
465
|
act(() => { root.render(React.createElement(ExplorerWorkspace, defaultProps)); });
|
|
430
|
-
expect(
|
|
466
|
+
expect(applyKustoSchema).not.toHaveBeenCalled();
|
|
431
467
|
});
|
|
432
468
|
|
|
433
469
|
// --- saveActiveConnectionId called ---
|
|
@@ -3,11 +3,10 @@ import type { Monaco } from '@monaco-editor/react';
|
|
|
3
3
|
import type { editor } from 'monaco-editor';
|
|
4
4
|
import React, { useState, useCallback, useRef, useEffect, useMemo } from 'react';
|
|
5
5
|
|
|
6
|
-
import { useExplorerColors } from '../../context/ExplorerColorContext';
|
|
7
6
|
import type { KustoTarget } from '../../services/kusto';
|
|
8
7
|
import { normalizeQuery, getHistory, saveHistoryEntry, recallResult } from '../../services/queryHistory';
|
|
9
8
|
import type { QueryHistoryEntry } from '../../services/queryHistory';
|
|
10
|
-
import { getSchema, loadSchema } from '../../services/schema';
|
|
9
|
+
import { getSchema, loadSchema, getRawShowSchema } from '../../services/schema';
|
|
11
10
|
import { EditorToolbar } from '../EditorToolbar/EditorToolbar';
|
|
12
11
|
import { ResultsPanel } from '../ResultsPanel/ResultsPanel';
|
|
13
12
|
import { SchemaSidebar } from '../SchemaSidebar/SchemaSidebar';
|
|
@@ -26,7 +25,8 @@ import {
|
|
|
26
25
|
import { useExplorerStyles } from './ExplorerWorkspace.styles';
|
|
27
26
|
import type { ExplorerProps } from '../Explorer/Explorer';
|
|
28
27
|
import { useKustoClient } from '../../context/KustoClientContext';
|
|
29
|
-
import {
|
|
28
|
+
import { bootstrapKustoLanguage } from './bootstrapKustoLanguage';
|
|
29
|
+
import { applyKustoSchema } from './kustoSchemaQueue';
|
|
30
30
|
|
|
31
31
|
|
|
32
32
|
const positionRelativeStyle = { position: 'relative' as const } as const;
|
|
@@ -38,12 +38,15 @@ const editorOptions = {
|
|
|
38
38
|
overviewRulerBorder: false, padding: { top: 4, bottom: 4 }, lineDecorationsWidth: 8,
|
|
39
39
|
glyphMargin: false, folding: false,
|
|
40
40
|
dropIntoEditor: { enabled: false },
|
|
41
|
+
// monaco-kusto exposes semantic-token-based highlighting (columns,
|
|
42
|
+
// functions, tables distinguished by AST role). Enable it explicitly —
|
|
43
|
+
// Monaco's default for standalone editors is false.
|
|
44
|
+
'semanticHighlighting.enabled': true,
|
|
41
45
|
} as const;
|
|
42
46
|
|
|
43
47
|
export function ExplorerWorkspace(props: ExplorerProps) {
|
|
44
48
|
const styles = useExplorerStyles();
|
|
45
49
|
const { isDark = false } = props;
|
|
46
|
-
const explorerColors = useExplorerColors();
|
|
47
50
|
const client = useKustoClient();
|
|
48
51
|
const state = useExplorerState();
|
|
49
52
|
const dispatch = useExplorerDispatch();
|
|
@@ -115,9 +118,8 @@ export function ExplorerWorkspace(props: ExplorerProps) {
|
|
|
115
118
|
);
|
|
116
119
|
|
|
117
120
|
useEffect(() => {
|
|
118
|
-
setKqlSchemaResolver(() => getSchema(focusedTarget));
|
|
119
121
|
saveActiveConnectionId(focusedConn.id);
|
|
120
|
-
}, [focusedConn.id
|
|
122
|
+
}, [focusedConn.id]);
|
|
121
123
|
|
|
122
124
|
// Setter shims — dispatch to reducer, keep same call signatures for existing callbacks
|
|
123
125
|
const setHasSelection = useCallback((v: boolean) => dispatch({ type: 'SET_HAS_SELECTION', hasSelection: v }), [dispatch]);
|
|
@@ -133,8 +135,17 @@ export function ExplorerWorkspace(props: ExplorerProps) {
|
|
|
133
135
|
// --- Editor refs (primary + secondary for split) ---
|
|
134
136
|
const primaryEditorRef = useRef<editor.IStandaloneCodeEditor | null>(null);
|
|
135
137
|
const secondaryEditorRef = useRef<editor.IStandaloneCodeEditor | null>(null);
|
|
136
|
-
|
|
137
|
-
|
|
138
|
+
// Monaco instance is captured at first editor mount. Stored in state (not
|
|
139
|
+
// a ref) so the schema-push effect re-runs when it becomes available.
|
|
140
|
+
const [monacoInstance, setMonacoInstance] = useState<Monaco | null>(null);
|
|
141
|
+
// Track when monaco-kusto's themed token colors (`kusto-light` /
|
|
142
|
+
// `kusto-dark`) have been registered. Before bootstrap resolves, those
|
|
143
|
+
// theme names don't exist yet — passing them as the Editor's `theme`
|
|
144
|
+
// prop on first mount makes Monaco silently fall back to the default
|
|
145
|
+
// `vs` (light) theme, which causes a brief white flash for dark-mode
|
|
146
|
+
// users on first paint. We render with the built-in `vs-dark` / `vs`
|
|
147
|
+
// theme until bootstrap completes, then swap to the kusto themes.
|
|
148
|
+
const [kustoThemeReady, setKustoThemeReady] = useState(false);
|
|
138
149
|
const runQueryRef = useRef<() => void>(() => {});
|
|
139
150
|
const recallQueryRef = useRef<() => void>(() => {});
|
|
140
151
|
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
@@ -142,6 +153,49 @@ export function ExplorerWorkspace(props: ExplorerProps) {
|
|
|
142
153
|
const historyKeysRef = useRef<Set<string>>(new Set());
|
|
143
154
|
const abortRef = useRef<AbortController | null>(null);
|
|
144
155
|
|
|
156
|
+
// Push the latest schema to the Kusto language worker whenever connection
|
|
157
|
+
// or schema changes. Debounce + identity-skip live in the queue.
|
|
158
|
+
useEffect(() => {
|
|
159
|
+
const monaco = monacoInstance;
|
|
160
|
+
if (!monaco) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const raw = getRawShowSchema(focusedTarget);
|
|
164
|
+
if (!raw) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
applyKustoSchema({
|
|
168
|
+
monaco,
|
|
169
|
+
raw,
|
|
170
|
+
clusterUri: focusedTarget.clusterUrl,
|
|
171
|
+
dbName: focusedTarget.database,
|
|
172
|
+
});
|
|
173
|
+
}, [monacoInstance, focusedTarget, schema]);
|
|
174
|
+
|
|
175
|
+
// Apply monaco-kusto's themed token colors once the language plugin has
|
|
176
|
+
// registered its themes (`kusto-light` / `kusto-dark`). Until bootstrap
|
|
177
|
+
// resolves we render with the built-in `vs` / `vs-dark` theme via the
|
|
178
|
+
// `effectiveTheme` prop below to avoid the white-flash described where
|
|
179
|
+
// `kustoThemeReady` is declared.
|
|
180
|
+
useEffect(() => {
|
|
181
|
+
const monaco = monacoInstance;
|
|
182
|
+
if (!monaco) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
let cancelled = false;
|
|
186
|
+
void bootstrapKustoLanguage(monaco).then(() => {
|
|
187
|
+
if (cancelled) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
setKustoThemeReady(true);
|
|
191
|
+
}).catch(() => { /* bootstrap errors surface elsewhere */ });
|
|
192
|
+
return () => { cancelled = true; };
|
|
193
|
+
}, [monacoInstance]);
|
|
194
|
+
|
|
195
|
+
const effectiveTheme = kustoThemeReady
|
|
196
|
+
? (isDark ? 'kusto-dark' : 'kusto-light')
|
|
197
|
+
: (isDark ? 'vs-dark' : 'vs');
|
|
198
|
+
|
|
145
199
|
// Convenience: the focused editor
|
|
146
200
|
const getFocusedEditor = useCallback(() => {
|
|
147
201
|
return focusedPane === 'primary' ? primaryEditorRef.current : secondaryEditorRef.current;
|
|
@@ -168,49 +222,6 @@ export function ExplorerWorkspace(props: ExplorerProps) {
|
|
|
168
222
|
setCanRecall(historyKeysRef.current.has(normalizeQuery(toRun.query)));
|
|
169
223
|
}, [setCanRecall]);
|
|
170
224
|
|
|
171
|
-
const updateStatementHighlight = useCallback((ed: editor.IStandaloneCodeEditor) => {
|
|
172
|
-
const decRef = ed === primaryEditorRef.current ? primaryDecorationsRef : secondaryDecorationsRef;
|
|
173
|
-
if (!decRef.current) {
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
const selection = ed.getSelection();
|
|
177
|
-
// When the user has an explicit selection, don't show the block highlight
|
|
178
|
-
if (selection && !selection.isEmpty()) {
|
|
179
|
-
decRef.current.set([]);
|
|
180
|
-
checkCanRecall(ed);
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
const model = ed.getModel();
|
|
184
|
-
if (!model) {
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
const cursorLine = ed.getPosition()?.lineNumber ?? 1;
|
|
188
|
-
const totalLines = model.getLineCount();
|
|
189
|
-
|
|
190
|
-
let startLine = cursorLine;
|
|
191
|
-
while (startLine > 1 && model.getLineContent(startLine - 1).trim() !== '') {
|
|
192
|
-
startLine--;
|
|
193
|
-
}
|
|
194
|
-
let endLine = cursorLine;
|
|
195
|
-
while (endLine < totalLines && model.getLineContent(endLine + 1).trim() !== '') {
|
|
196
|
-
endLine++;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
decRef.current.set([{
|
|
200
|
-
range: {
|
|
201
|
-
startLineNumber: startLine,
|
|
202
|
-
startColumn: 1,
|
|
203
|
-
endLineNumber: endLine,
|
|
204
|
-
endColumn: model.getLineMaxColumn(endLine),
|
|
205
|
-
},
|
|
206
|
-
options: {
|
|
207
|
-
isWholeLine: true,
|
|
208
|
-
className: 'traverse-run-highlight',
|
|
209
|
-
},
|
|
210
|
-
}]);
|
|
211
|
-
checkCanRecall(ed);
|
|
212
|
-
}, [checkCanRecall]);
|
|
213
|
-
|
|
214
225
|
const cancelQuery = useCallback(() => {
|
|
215
226
|
abortRef.current?.abort();
|
|
216
227
|
}, []);
|
|
@@ -317,10 +328,14 @@ export function ExplorerWorkspace(props: ExplorerProps) {
|
|
|
317
328
|
const createEditorMountHandler = useCallback((pane: 'primary' | 'secondary') => {
|
|
318
329
|
return (ed: editor.IStandaloneCodeEditor, monaco: Monaco) => {
|
|
319
330
|
const edRef = pane === 'primary' ? primaryEditorRef : secondaryEditorRef;
|
|
320
|
-
const decRef = pane === 'primary' ? primaryDecorationsRef : secondaryDecorationsRef;
|
|
321
331
|
edRef.current = ed;
|
|
322
|
-
|
|
323
|
-
|
|
332
|
+
setMonacoInstance(monaco);
|
|
333
|
+
// Kick off the @kusto/monaco-kusto bootstrap; fire-and-forget — the
|
|
334
|
+
// schema queue awaits this internally before applying schema.
|
|
335
|
+
// monaco-kusto also installs its own KustoCommandHighlighter, which
|
|
336
|
+
// visually marks the current command (delimited by blank lines) —
|
|
337
|
+
// that replaces our previous custom decoration.
|
|
338
|
+
void bootstrapKustoLanguage(monaco);
|
|
324
339
|
ed.addAction({
|
|
325
340
|
id: 'run-query',
|
|
326
341
|
label: 'Run Query',
|
|
@@ -340,35 +355,24 @@ export function ExplorerWorkspace(props: ExplorerProps) {
|
|
|
340
355
|
run: () => { formatQueryRef.current(); },
|
|
341
356
|
});
|
|
342
357
|
|
|
343
|
-
ed.onDidChangeCursorPosition(() =>
|
|
358
|
+
ed.onDidChangeCursorPosition(() => checkCanRecall(ed));
|
|
344
359
|
ed.onDidChangeCursorSelection((e) => {
|
|
345
360
|
setHasSelection(!e.selection.isEmpty());
|
|
346
|
-
|
|
361
|
+
checkCanRecall(ed);
|
|
347
362
|
});
|
|
348
|
-
ed.onDidChangeModelContent(() =>
|
|
363
|
+
ed.onDidChangeModelContent(() => checkCanRecall(ed));
|
|
349
364
|
ed.onDidFocusEditorWidget(() => setFocusedPane(pane));
|
|
350
365
|
|
|
351
|
-
|
|
366
|
+
checkCanRecall(ed);
|
|
352
367
|
if (pane === 'primary') {
|
|
353
368
|
ed.focus();
|
|
354
369
|
}
|
|
355
370
|
};
|
|
356
|
-
}, [
|
|
371
|
+
}, [checkCanRecall, setHasSelection, setFocusedPane]);
|
|
357
372
|
|
|
358
373
|
useEffect(() => { runQueryRef.current = runQuery; }, [runQuery]);
|
|
359
374
|
useEffect(() => { recallQueryRef.current = doRecall; }, [doRecall]);
|
|
360
375
|
|
|
361
|
-
// Inject global CSS for the run-highlight decoration (Monaco needs a real class)
|
|
362
|
-
useEffect(() => {
|
|
363
|
-
const id = 'traverse-run-highlight-style';
|
|
364
|
-
if (!document.getElementById(id)) {
|
|
365
|
-
const style = document.createElement('style');
|
|
366
|
-
style.id = id;
|
|
367
|
-
style.textContent = `.traverse-run-highlight { background-color: ${explorerColors.semantic.selectionBg}; transition: background-color 0.3s ease; }`;
|
|
368
|
-
document.head.appendChild(style);
|
|
369
|
-
}
|
|
370
|
-
}, [explorerColors.semantic.selectionBg]);
|
|
371
|
-
|
|
372
376
|
const insertText = useCallback((text: string) => {
|
|
373
377
|
const ed = getFocusedEditor();
|
|
374
378
|
if (!ed) {
|
|
@@ -595,8 +599,8 @@ export function ExplorerWorkspace(props: ExplorerProps) {
|
|
|
595
599
|
)}
|
|
596
600
|
<Editor
|
|
597
601
|
height="100%"
|
|
598
|
-
language="
|
|
599
|
-
theme={
|
|
602
|
+
language="kusto"
|
|
603
|
+
theme={effectiveTheme}
|
|
600
604
|
value={activeTab.kql}
|
|
601
605
|
onChange={handlePrimaryEditorChange}
|
|
602
606
|
onMount={handlePrimaryMount}
|
|
@@ -615,8 +619,8 @@ export function ExplorerWorkspace(props: ExplorerProps) {
|
|
|
615
619
|
onKeyDown={focusSecondaryPaneKeyDown}>
|
|
616
620
|
<Editor
|
|
617
621
|
height="100%"
|
|
618
|
-
language="
|
|
619
|
-
theme={
|
|
622
|
+
language="kusto"
|
|
623
|
+
theme={effectiveTheme}
|
|
620
624
|
value={splitTab.kql}
|
|
621
625
|
onChange={handleSecondaryEditorChange}
|
|
622
626
|
onMount={handleSecondaryMount}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import type * as Monaco from 'monaco-editor';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
bootstrapKustoLanguage,
|
|
6
|
+
__resetBootstrapForTests,
|
|
7
|
+
} from './bootstrapKustoLanguage';
|
|
8
|
+
|
|
9
|
+
const setLanguageSettings = vi.fn();
|
|
10
|
+
const setMaximumWorkerIdleTime = vi.fn();
|
|
11
|
+
const kustoDefaults = { setLanguageSettings, setMaximumWorkerIdleTime };
|
|
12
|
+
|
|
13
|
+
vi.mock('@kusto/monaco-kusto', () => ({
|
|
14
|
+
get kustoDefaults() {
|
|
15
|
+
return kustoDefaults;
|
|
16
|
+
},
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
function makeMonaco(languages: { id: string }[] = [{ id: 'kusto' }]): typeof Monaco {
|
|
20
|
+
return {
|
|
21
|
+
languages: {
|
|
22
|
+
getLanguages: vi.fn(() => languages),
|
|
23
|
+
},
|
|
24
|
+
} as unknown as typeof Monaco;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
__resetBootstrapForTests();
|
|
29
|
+
setLanguageSettings.mockReset();
|
|
30
|
+
setMaximumWorkerIdleTime.mockReset();
|
|
31
|
+
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
32
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
afterEach(() => {
|
|
36
|
+
vi.restoreAllMocks();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('bootstrapKustoLanguage', () => {
|
|
40
|
+
it('imports @kusto/monaco-kusto and applies default settings on first call', async () => {
|
|
41
|
+
const monaco = makeMonaco();
|
|
42
|
+
|
|
43
|
+
await bootstrapKustoLanguage(monaco);
|
|
44
|
+
|
|
45
|
+
expect(setLanguageSettings).toHaveBeenCalledTimes(1);
|
|
46
|
+
const settings = setLanguageSettings.mock.calls[0][0];
|
|
47
|
+
expect(settings.enableHover).toBe(true);
|
|
48
|
+
expect(settings.enableQueryWarnings).toBe(true);
|
|
49
|
+
expect(settings.enableQuerySuggestions).toBe(true);
|
|
50
|
+
expect(settings.includeControlCommands).toBe(true);
|
|
51
|
+
expect(settings.completionOptions).toEqual({ includeExtendedSyntax: false });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('is idempotent — repeated calls return the same promise without re-applying settings', async () => {
|
|
55
|
+
const monaco = makeMonaco();
|
|
56
|
+
|
|
57
|
+
const p1 = bootstrapKustoLanguage(monaco);
|
|
58
|
+
const p2 = bootstrapKustoLanguage(monaco);
|
|
59
|
+
|
|
60
|
+
expect(p1).toBe(p2);
|
|
61
|
+
await p1;
|
|
62
|
+
await p2;
|
|
63
|
+
expect(setLanguageSettings).toHaveBeenCalledTimes(1);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('merges caller-provided settings over defaults', async () => {
|
|
67
|
+
const monaco = makeMonaco();
|
|
68
|
+
|
|
69
|
+
await bootstrapKustoLanguage(monaco, {
|
|
70
|
+
languageSettings: {
|
|
71
|
+
enableHover: false,
|
|
72
|
+
disabledDiagnosticCodes: ['KS107'],
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const settings = setLanguageSettings.mock.calls[0][0];
|
|
77
|
+
expect(settings.enableHover).toBe(false);
|
|
78
|
+
expect(settings.enableQueryWarnings).toBe(true);
|
|
79
|
+
expect(settings.disabledDiagnosticCodes).toEqual(['KS107']);
|
|
80
|
+
expect(settings.completionOptions).toEqual({ includeExtendedSyntax: false });
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('falls back to default completionOptions when caller passes undefined', async () => {
|
|
84
|
+
const monaco = makeMonaco();
|
|
85
|
+
|
|
86
|
+
await bootstrapKustoLanguage(monaco, {
|
|
87
|
+
languageSettings: { completionOptions: undefined as never },
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const settings = setLanguageSettings.mock.calls[0][0];
|
|
91
|
+
expect(settings.completionOptions).toEqual({ includeExtendedSyntax: false });
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('uses caller-provided completionOptions when present', async () => {
|
|
95
|
+
const monaco = makeMonaco();
|
|
96
|
+
|
|
97
|
+
await bootstrapKustoLanguage(monaco, {
|
|
98
|
+
languageSettings: { completionOptions: { includeExtendedSyntax: true } },
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const settings = setLanguageSettings.mock.calls[0][0];
|
|
102
|
+
expect(settings.completionOptions).toEqual({ includeExtendedSyntax: true });
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('applies workerMaxIdleTimeMs when provided', async () => {
|
|
106
|
+
const monaco = makeMonaco();
|
|
107
|
+
|
|
108
|
+
await bootstrapKustoLanguage(monaco, { workerMaxIdleTimeMs: 5 * 60_000 });
|
|
109
|
+
|
|
110
|
+
expect(setMaximumWorkerIdleTime).toHaveBeenCalledWith(5 * 60_000);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('does not call setMaximumWorkerIdleTime when not provided', async () => {
|
|
114
|
+
const monaco = makeMonaco();
|
|
115
|
+
|
|
116
|
+
await bootstrapKustoLanguage(monaco);
|
|
117
|
+
|
|
118
|
+
expect(setMaximumWorkerIdleTime).not.toHaveBeenCalled();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('throws when the Monaco instance has no kusto language after import (two-Monaco)', async () => {
|
|
122
|
+
const monaco = makeMonaco([{ id: 'javascript' }]);
|
|
123
|
+
|
|
124
|
+
await expect(bootstrapKustoLanguage(monaco)).rejects.toThrow(/different Monaco instance/);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('clears the cached promise on failure so a subsequent call can retry', async () => {
|
|
128
|
+
const monaco = makeMonaco([{ id: 'javascript' }]);
|
|
129
|
+
|
|
130
|
+
await expect(bootstrapKustoLanguage(monaco)).rejects.toThrow();
|
|
131
|
+
|
|
132
|
+
// After failure, the next call should re-attempt (and re-invoke setLanguageSettings).
|
|
133
|
+
const monaco2 = makeMonaco();
|
|
134
|
+
await bootstrapKustoLanguage(monaco2);
|
|
135
|
+
|
|
136
|
+
expect(setLanguageSettings).toHaveBeenCalledTimes(2);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('throws a helpful error when @kusto/monaco-kusto did not expose kustoDefaults', async () => {
|
|
140
|
+
const monaco = makeMonaco();
|
|
141
|
+
const broken = kustoDefaults.setLanguageSettings;
|
|
142
|
+
kustoDefaults.setLanguageSettings = undefined as never;
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
await expect(bootstrapKustoLanguage(monaco)).rejects.toThrow(/did not expose kustoDefaults/);
|
|
146
|
+
} finally {
|
|
147
|
+
kustoDefaults.setLanguageSettings = broken;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
});
|