Skip to content

Commit a27c8e5

Browse files
feat(cdk-explorer): syntax highlighting for all CDK languages
Adds Java, C#, and Go grammars alongside TypeScript, JavaScript, Python, JSON, and YAML, and extracts a shared Language type. detectLanguage maps each source extension.
1 parent 6285314 commit a27c8e5

8 files changed

Lines changed: 811 additions & 818 deletions

File tree

.projenrc.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,8 @@ const cdkExplorer = configureProject(
17361736
'supertest@^6',
17371737
'@types/supertest@^6',
17381738
'@types/convert-source-map@^2',
1739+
'prismjs@^1',
1740+
'@types/prismjs@^1',
17391741
'yaml@^2',
17401742
],
17411743
tsconfig: {

packages/@aws-cdk/cdk-explorer/.projen/deps.json

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

packages/@aws-cdk/cdk-explorer/frontend/App.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { buildSourceAnchorIndex, findConstructAtLine } from '../lib/web/source-n
99
import { api, type DirEntry, type TemplateResponse, type TreeResponse, type ViolationsResponse } from './api';
1010
import { CodeViewer, type Diagnostic } from './components/CodeViewer';
1111
import { ConstructTree } from './components/ConstructTree';
12+
import type { Language } from './syntax';
1213
import { TemplateViewer } from './components/TemplateViewer';
1314
import { ViolationsPanel } from './components/ViolationsPanel';
1415
import type { NavigateHandler } from './nav-types';
@@ -520,10 +521,15 @@ const RESIZER_BUTTON_VERTICAL: React.CSSProperties = {
520521
borderRadius: '7px',
521522
};
522523

523-
function detectLanguage(file: string | undefined): 'typescript' | 'json' | 'yaml' {
524+
function detectLanguage(file: string | undefined): Language {
524525
if (!file) return 'typescript';
525526
if (file.endsWith('.json')) return 'json';
526527
if (file.endsWith('.yaml') || file.endsWith('.yml')) return 'yaml';
528+
if (file.endsWith('.py')) return 'python';
529+
if (file.endsWith('.java')) return 'java';
530+
if (file.endsWith('.cs')) return 'csharp';
531+
if (file.endsWith('.go')) return 'go';
532+
if (file.endsWith('.js') || file.endsWith('.jsx') || file.endsWith('.mjs')) return 'javascript';
527533
return 'typescript';
528534
}
529535

packages/@aws-cdk/cdk-explorer/frontend/components/CodeViewer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { tokenizeLines, type Token } from '../syntax';
2+
import { tokenizeLines, type Language, type Token } from '../syntax';
33

44
export interface Diagnostic {
55
readonly startLine: number;
@@ -11,7 +11,7 @@ export interface Diagnostic {
1111

1212
export interface CodeViewerProps {
1313
readonly content: string;
14-
readonly language: 'typescript' | 'json' | 'yaml';
14+
readonly language: Language;
1515
readonly highlightStart?: number;
1616
readonly highlightEnd?: number;
1717
readonly highlightColor?: string;

packages/@aws-cdk/cdk-explorer/frontend/syntax.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
/**
2-
* PrismJS-based syntax tokenizer. Only imports the three grammars we need
3-
* (~12KB minified total). Returns structured tokens per line for our custom
4-
* line renderer (which handles highlighting, scroll, diagnostics).
2+
* PrismJS-based syntax tokenizer covering the template formats (JSON, YAML) and
3+
* every CDK source language (TypeScript, JavaScript, Python, Java, C#, Go).
4+
* Returns structured tokens per line for our custom line renderer (which handles
5+
* highlighting, scroll, diagnostics).
56
*/
67
import Prism from 'prismjs/components/prism-core';
78
import 'prismjs/components/prism-json';
89
import 'prismjs/components/prism-yaml';
910
import 'prismjs/components/prism-clike';
1011
import 'prismjs/components/prism-javascript';
1112
import 'prismjs/components/prism-typescript';
13+
import 'prismjs/components/prism-python';
14+
import 'prismjs/components/prism-java';
15+
import 'prismjs/components/prism-csharp';
16+
import 'prismjs/components/prism-go';
17+
18+
/** Languages the viewer can highlight: template formats plus CDK source languages. */
19+
export type Language = 'json' | 'yaml' | 'typescript' | 'javascript' | 'python' | 'java' | 'csharp' | 'go';
1220

1321
export interface Token {
1422
readonly type: string | undefined;
1523
readonly content: string;
1624
}
1725

18-
const GRAMMAR_MAP: Record<string, Prism.Grammar> = {
26+
const GRAMMAR_MAP: Record<Language, Prism.Grammar> = {
1927
json: Prism.languages.json,
2028
yaml: Prism.languages.yaml,
2129
typescript: Prism.languages.typescript,
30+
javascript: Prism.languages.javascript,
31+
python: Prism.languages.python,
32+
java: Prism.languages.java,
33+
csharp: Prism.languages.csharp,
34+
go: Prism.languages.go,
2235
};
2336

24-
export function tokenizeLines(code: string, language: 'json' | 'yaml' | 'typescript'): Token[][] {
37+
export function tokenizeLines(code: string, language: Language): Token[][] {
2538
const grammar = GRAMMAR_MAP[language];
2639
if (!grammar) {
2740
return code.split('\n').map((line) => [{ type: undefined, content: line }]);

packages/@aws-cdk/cdk-explorer/tsconfig.dev.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/cdk-explorer/tsconfig.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)