Skip to content

Commit

Permalink
Add "On section rename" handling
Browse files Browse the repository at this point in the history
When a `sections/*.liquid` file is renamed, we'll update automatically update references in the following locations:

- `templates/*.json` files that were using the old name
- `sections/*.json` files that were using the old name
- Static section calls of the form `{% section 'old-name' %}`

Fixes #546
  • Loading branch information
charlespwd committed Jan 14, 2025
1 parent b31e0f8 commit 9c93229
Show file tree
Hide file tree
Showing 8 changed files with 619 additions and 55 deletions.
12 changes: 12 additions & 0 deletions .changeset/sixty-jobs-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@shopify/theme-language-server-common': minor
'theme-check-vscode': minor
---

Add "On section rename" handling

When `sections/*.liquid` files are renamed, we will update all references to these files in their previous locations. This includes:

- `templates/*.json` files that referenced the old file name
- `sections/*.json` files that referenced the old file name
- Static section calls formatted as `{% section 'old-name' %}`
1 change: 1 addition & 0 deletions packages/theme-check-common/src/types/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { ThemeBlock } from './theme-block';
export { Section } from './section';
export { Setting } from './setting';
export { Preset } from './preset';
export { Template } from './template';
34 changes: 34 additions & 0 deletions packages/theme-check-common/src/types/schemas/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Setting } from './setting';

export namespace Template {
export interface Template {
layout?: string | false;
wrapper?: string;
sections: Record<string, Template.Section>;
order: string[];
}

export interface Section {
type: string;
settings?: Setting.Values;
disabled?: boolean;
blocks?: Record<string, Template.Block>;
block_order?: string[];
}

export interface Block {
type: string;
settings?: Setting.Values;
disabled?: boolean;
blocks?: Record<string, Template.Block>;
block_order?: string[];
static?: boolean;
}

export interface SectionGroup {
type: string;
name: string;
sections: Record<string, Template.Section>;
order: string[];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BaseRenameHandler } from './BaseRenameHandler';
import { AssetRenameHandler } from './handlers/AssetRenameHandler';
import { BlockRenameHandler } from './handlers/BlockRenameHandler';
import { SnippetRenameHandler } from './handlers/SnippetRenameHandler';
import { SectionRenameHandler } from './handlers/SectionRenameHandler';

/**
* The RenameHandler is responsible for handling workspace/didRenameFiles notifications.
Expand All @@ -27,6 +28,7 @@ export class RenameHandler {
new SnippetRenameHandler(documentManager, connection, capabilities, findThemeRootURI),
new AssetRenameHandler(documentManager, connection, capabilities, findThemeRootURI),
new BlockRenameHandler(documentManager, connection, capabilities, findThemeRootURI),
new SectionRenameHandler(documentManager, connection, capabilities, findThemeRootURI),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
path,
Preset,
Section,
Setting,
SourceCodeType,
Template,
ThemeBlock,
visit,
} from '@shopify/theme-check-common';
Expand All @@ -35,42 +35,10 @@ import {
} from '../../documents';
import { blockName, isBlock, isSection, isSectionGroup, isTemplate } from '../../utils/uri';
import { BaseRenameHandler } from '../BaseRenameHandler';
import { isValidSectionGroup, isValidTemplate } from './utils';

type DocumentChange = TextDocumentEdit;

export namespace Template {
export interface Template {
layout?: string | false;
wrapper?: string;
sections: Record<string, Template.Section>;
order: string[];
}

export interface Section {
type: string;
settings?: Setting.Values;
disabled?: boolean;
blocks?: Record<string, Template.Block>;
block_order?: string[];
}

export interface Block {
type: string;
settings?: Setting.Values;
disabled?: boolean;
blocks?: Record<string, Template.Block>;
block_order?: string[];
static?: boolean;
}

export interface SectionGroup {
type: string;
name: string;
sections: Record<string, Template.Section>;
order: string[];
}
}

const annotationId = 'renameBlock';

/**
Expand Down Expand Up @@ -392,27 +360,6 @@ function isLocalBlock(blockDef: ThemeBlock.Block | Section.Block): blockDef is S
return 'name' in blockDef && typeof blockDef.name === 'string';
}

// this is very very optimistic...
function isValidTemplate(parsed: unknown): parsed is Template.Template {
return (
typeof parsed === 'object' &&
parsed !== null &&
'sections' in parsed &&
'order' in parsed &&
Array.isArray((parsed as Template.Template).order)
);
}

function isValidSectionGroup(parsed: unknown): parsed is Template.SectionGroup {
return (
typeof parsed === 'object' &&
parsed !== null &&
'sections' in parsed &&
'order' in parsed &&
Array.isArray((parsed as Template.SectionGroup).order)
);
}

function getBlocksEditsFactory(
oldBlockName: string,
newBlockName: string,
Expand Down
Loading

0 comments on commit 9c93229

Please sign in to comment.