# Region-pattern refactor showcase

A flat `[env:STAGE_REGION]` matrix that the Norn extension can refactor into the templates + extends shape in one click.

## How to use

1. Open [.nornenv](./.nornenv) in VS Code.
2. A `$(sparkle) Refactor 4 envs into 2+2 templates` CodeLens appears at the top of the file.
3. Click it. A confirmation dialog summarises what will change:
   - 2 vars (`baseUrl`, `apiKey`) lifted to stage templates (`dev`, `prod`)
   - 2 vars (`dbHost`, `bucket`) lifted to region templates (`us`, `uk`)
   - 1 var (`failoverHost`) kept as leaf-specific on `prod_uk`
4. Confirm. The flat 4-env matrix is replaced with templates + extending envs:

   ```nornenv
   [template:dev]
       var baseUrl = https://dev.example.com
       var apiKey = dev-key-123

   [template:prod]
       var baseUrl = https://api.example.com
       secret apiKey = prod-key-789

   [template:us]
       var dbHost = db.us.example.com
       var bucket = data-us

   [template:uk]
       var dbHost = db.uk.example.com
       var bucket = data-uk

   [env:dev_us extends dev, us]

   [env:dev_uk extends dev, uk]

   [env:prod_us extends prod, us]

   [env:prod_uk extends prod, uk]
       var failoverHost = api-failover.uk.example.com
   ```

5. The `secret` keyword on `apiKey` is preserved when the var is lifted to a template.

## CLI

The same generator is available from the local CLI:

```bash
node ./dist/cli.js demos/nornenv-region-refactor/.nornenv --refactor-region-pattern
node ./dist/cli.js demos/nornenv-region-refactor/.nornenv --refactor-region-pattern --write
```

## When the refactor fires

The CodeLens appears only when **all** of these are true:
- At least 2 stages × 2 regions
- At least 3 populated cells
- All matrix envs are flat (no existing `extends` clause)

It does not touch envs that already use `extends`, envs with names that don't match the `STAGE_REGION` shape, or `connectionString` declarations (those are left in their leaves — refactor manually).

## Cmd+Z

Everything goes through a single `WorkspaceEdit`, so a single Cmd+Z undoes the whole refactor.
