Skip to content

feat: template ranges + two-way template/source navigation - #1624

Merged
megha-narayanan merged 4 commits into
aws:feat/cdk-lspfrom
megha-narayanan:feat/explorer-range-mapping
Jun 16, 2026
Merged

feat: template ranges + two-way template/source navigation#1624
megha-narayanan merged 4 commits into
aws:feat/cdk-lspfrom
megha-narayanan:feat/explorer-range-mapping

Conversation

@megha-narayanan

@megha-narayanan megha-narayanan commented Jun 12, 2026

Copy link
Copy Markdown
Contributor
  • Computes character ranges for CloudFormation resource blocks in synthesized templates and uses them for navigation in both directions:
  • CodeLens "go to" now selects the whole resource block (was a zero-width cursor).
  • New go-to-definition from a synthesized template back to the construct's source.

A note on the JSON parser dependency Computing character ranges needs a position-aware JSON parser, since JSON.parse discards offsets. The natural choice is jsonc-parser (what the VS Code JSON language service uses). We could not use it here: its UMD entry loads internal modules through a parameter-shadowed require("./impl/...") that esbuild (used by node-backpack to bundle the CLI) cannot statically trace, so the bundled cdk/cdk-assets binaries fail with Cannot find module './impl/format'. Known, still-open: microsoft/node-jsonc-parser#57, evanw/esbuild#1619. Its ESM build bundles fine, but the esbuild mainFields workaround isn't exposed by node-backpack, and importing the ESM build directly breaks our CommonJS tests; marking it external isn't appropriate for a self-contained CLI. So we use json-source-map, a single-file CommonJS module that bundles cleanly.

Checklist

  • This change contains a major version upgrade for a dependency and I confirm all breaking changes are addressed
    • Release notes for the new version:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

Adds jsonc-parser@3.2.0 as a runtime dep, in preparation for computing
character ranges of template resource/property blocks (PR-A). Dedupes to
the jsonc-parser already resolved in the lockfile; no new version added.
…te ranges

Compute character-accurate ranges of CloudFormation resource blocks in
synthesized templates and use them for navigation in both directions.

cloud-assembly-api:
- resolveResourceRange(text, logicalId): the character range of a resource's
  value block, via jsonc-parser. A position-aware parse is used instead of a
  line scan because real templates contain literal braces and escaped quotes
  inside string values (for example Fn::Sub placeholders) that defeat naive
  brace matching.
- resolveLogicalIdAtOffset(text, offset): the inverse, mapping a position in a
  template back to the enclosing resource's logical id.

cdk-explorer LSP:
- resourceTarget now returns the real resource block range (previously a
  zero-width cursor at the logical-id key), so the CodeLens "go to" selects the
  whole block.
- onDefinition: go-to-definition from a synthesized template back to the
  construct's source, keyed by (templateFile, logicalId) since logical ids are
  only unique within a template.
- offset and position conversions extracted to lib/lsp/positions.ts.

The jsonc-parser runtime dependency is added in the preceding commit.
@github-actions github-actions Bot added the p2 label Jun 12, 2026
@aws-cdk-automation
aws-cdk-automation requested a review from a team June 12, 2026 20:06
@megha-narayanan
megha-narayanan marked this pull request as ready for review June 12, 2026 20:35
cloud-assembly-api now depends on jsonc-parser, so the bundled CLIs that
include it (aws-cdk, cdk-assets, integ-runner) must attribute it. Regenerated
via node-backpack so the build's self-mutation check passes.
…for template ranges

jsonc-parser cannot be bundled into the CLI. Its UMD entry loads its internal
modules through a parameter-shadowed require("./impl/..."), which esbuild (via
node-backpack) cannot statically trace, so the packaged cdk and cdk-assets
binaries fail their build-time sanity check with "Cannot find module
'./impl/format'". This is a known, still-open limitation
(microsoft/node-jsonc-parser#57, evanw/esbuild#1619), and node-backpack does not
expose the esbuild mainFields workaround.

Switch resolveResourceRange and resolveLogicalIdAtOffset to json-source-map, a
single-file CommonJS module that bundles cleanly and gives the same byte offsets.
The range contract and the JSON.parse(slice) oracle tests are unchanged.
json-source-map is a strict parser, so the one test that exercised lenient input
now asserts undefined.

Regenerates THIRD_PARTY_LICENSES for aws-cdk, cdk-assets, and integ-runner.
@rix0rrr rix0rrr self-assigned this Jun 16, 2026
@megha-narayanan
megha-narayanan merged commit e5fb0c3 into aws:feat/cdk-lsp Jun 16, 2026
38 checks passed
@megha-narayanan
megha-narayanan deleted the feat/explorer-range-mapping branch June 16, 2026 17:23
megha-narayanan added a commit that referenced this pull request Jul 9, 2026
A full cdk explore UI over the synthesized assembly. Builds on #1624.

Features

- Three panes: construct tree, source, template, with syntax
highlighting for all CDK source languages (TypeScript, JavaScript,
Python, Java, C#, Go)
- Template pane: JSON/YAML toggle
- Linked navigation by double-click: tree → source + template, template
→ source, and source → template
- Violations panel grouped by rule (click an occurrence to jump to its
construct), inline diagnostic squiggles in the source pane, and severity
coloring in the tree (inherited up to ancestors)
- Resizable split panes and an "Open" file picker for either pane


Design decisions

- Custom line renderer over PrismJS tokens, not Prism's HTML. syntax.ts
calls Prism.tokenize and flattens the token tree into per-line token
arrays (Prism tokens can straddle newlines). CodeViewer then renders
each line itself, which is what lets one component compose four things
per line: syntax colors, the nav-highlight band, scroll-to-line, and
column-accurate diagnostic squiggles. Prism's string output can't be
sliced per line or overlaid with diagnostics.
- PrismJS grammars, not a full editor/highlighter. This bundles into the
shipped CLI via esbuild, so Monaco-scale dependencies are probably too
big. Prism core plus JSON, YAML, and the CDK source languages keeps it
small, and one CodeViewer serves both panes.
- Navigate by logical ID, not line numbers. JSON and YAML render at
different lines, so navigation carries only the logical ID and the
template viewer resolves the highlight line in whichever format is on
screen.
- Source → template is nearest-preceding, ties to the top-most
construct. Synthesized children share their parent's single creation
line (every subnet/NAT under a new ec2.Vpc(...)), so a click there
resolves to the authored parent, not a child.

<img width="2564" height="1306" alt="Screenshot 2026-07-06 at 12 08
28 PM"
src="https://github.com/user-attachments/assets/2d2ac1c7-16a6-4279-94ed-c91c90eb3837"
/>


### Checklist
- [ ] This change contains a major version upgrade for a dependency and
I confirm all breaking changes are addressed
  - Release notes for the new version:

---
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license
fossamagna pushed a commit to fossamagna/aws-cdk-cli that referenced this pull request Jul 17, 2026
Merges the`feat/cdk-lsp' branch into `main`. The change is additive and
introduces no behavior change to existing CLI commands.

- New `@aws-cdk/cdk-explorer` package containing the Language Server
under `lib/lsp` (server, diagnostics, CodeLens, template locator,
position mapping).
- Extends `@aws-cdk/cloud-assembly-api` with two parsing modules
consumed by the server: `construct-tree.ts` (builds the construct tree
from a cloud assembly) and `template-ranges.ts` (resolves a logical ID
or property to its byte range in the template).

Capabilities (folds in aws#1559, aws#1593, aws#1592, aws#1617, aws#1624, aws#1630, aws#1631,
aws#1662, aws#1634, aws#1674):

- Diagnostics: surfaces synth errors and policy-validation violations in
the editor, mapped back to the source.
- Surfaces CFN resources and adds CodeLens navigation from a construct
to its template resource.
- Navigation between construct source and the synthesized template in
both directions.
- Live refresh: diagnostics and CodeLens update when `cdk.out` changes.
- Reads are constrained to the project directory, and template reads run
off the LSP event loop.

This PR is the server and parsing foundation. It does not add a shipped
CLI command or the web explorer.

### Checklist
- [ ] This change contains a major version upgrade for a dependency and
I confirm all breaking changes are addressed
  - Release notes for the new version:

---
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license

---------

Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Otavio Macedo <288203+otaviomacedo@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants