Skip to content

Commit

Permalink
fix(vscode-pv-handlebars-language-server): add better support for lay…
Browse files Browse the repository at this point in the history
…out option in the templates
  • Loading branch information
mbehzad committed Jun 25, 2024
1 parent e18d744 commit 9c5a8fb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export async function completionProvider(
if (/^\n*---[^---]*layout:\s*(\w|\d)*$/.test(text)) {
const isPageTemplate = filePath.includes("/frontend/src/pages");
const layouts = await getLayoutFiles(componentsRootPath);
const relevantLayouts = layouts?.[isPageTemplate ? "pages" : "lsg"];
const relevantLayouts = isPageTemplate ? layouts?.normal : {...layouts?.normal , ...layouts?.lsg};

if (relevantLayouts)
return Object.keys(relevantLayouts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export async function definitionProvider(
const componentsRootPath = filePath.includes("src/components/")
? `${filePath.split("/frontend/src/components")[0]}/frontend/src/components`
: filePath.includes("src/pages/")
? `${filePath.split("/frontend/src/pages")[0]}/frontend/src/components`
: `${filePath.split("/frontend/src/layouts")[0]}/frontend/src/components`;
? `${filePath.split("/frontend/src/pages")[0]}/frontend/src/components`
: `${filePath.split("/frontend/src/layouts")[0]}/frontend/src/components`;

// e.g. {{> partial
if (isPartial(textBefore)) {
Expand Down Expand Up @@ -149,12 +149,23 @@ export async function definitionProvider(
---
*/
else if (/^\n*---[^---]*layout:\s*(\w|\d)+$/.test(textBefore)) {
// templates inside /page/ directory use different layouts than the open under /components/
// templates inside /components/ directory in the older assemble/stylemark were rendered with the layout hbs files *and a second time* with the `lsgTemplatesSrc` layouts
// these option doesn't need to be shown for /pages/
const isPageTemplate = filePath.includes("/frontend/src/pages");
const layouts = await getLayoutFiles(componentsRootPath);
const layoutFilePath = layouts?.[isPageTemplate ? "pages" : "lsg"]?.[symbolName];
const allLayouts = await getLayoutFiles(componentsRootPath);
if (!allLayouts) return null;

const normalLayouts = allLayouts.normal;
const lsgLayouts = allLayouts.lsg;

const layouts = [];
if (normalLayouts) layouts.push(...Object.entries(normalLayouts));
if (lsgLayouts && !isPageTemplate) layouts.push(...Object.entries(lsgLayouts));
const results = [];

for (const [name, layoutFilePath] of layouts) {
if (name !== symbolName) continue;

if (layoutFilePath) {
const fileContent = await getFileContent(layoutFilePath);
// placeholder assemble-lite uses to place content in the layout
const BODY_PLACEHOLDER = "{% body%}";
Expand All @@ -164,14 +175,18 @@ export async function definitionProvider(
if (placeholderStart !== -1) {
const before = fileContent.slice(0, placeholderStart);
const lineNumber = (before.match(/\n/g) || []).length;
return Location.create(URI.file(layoutFilePath).toString(), Range.create(lineNumber, 0, lineNumber, 1000));
results.push(
Location.create(URI.file(layoutFilePath).toString(), Range.create(lineNumber, 0, lineNumber, 1000)),
);
}
// whole file
else {
return Location.create(URI.file(layoutFilePath).toString(), Range.create(0, 0, 0, 0));
results.push(Location.create(URI.file(layoutFilePath).toString(), Range.create(0, 0, 0, 0)));
}

}

return results;
}

return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import rgx from "./rgx";
interface PVConfig {
hbsHelperSrc?: string;
namespace?: string;
/**
* @deprecated
* was removed in pv-stylemark v4.
*/
lsgTemplatesSrc?: string;
cdTemplatesSrc?: string;
}
Expand Down Expand Up @@ -161,8 +165,13 @@ export async function getCustomHelperFiles(componentsRootPath: string): Promise<
}

interface LayoutFiles {
lsg?: { [name: string]: string };
pages?: { [name: string]: string };
// list of layouts only used to generate lsg_components
lsg?: {
// e.g. default: "/lsg/layouts/default.hbs"
[name: string]: string;
};
// layouts to generate pages/ and components/
normal?: { [name: string]: string };
}

// list of hbs files which are used in assemble-lite as layouts for lsg components or pages
Expand All @@ -172,13 +181,14 @@ export async function getLayoutFiles(componentsRootPath: string): Promise<Layout

if (pvConfig === null) return null;

const layouts: Array<{ name: "lsg" | "pages"; dir: string | undefined }> = [
const layouts: Array<{ name: "lsg" | "normal"; dir: string | undefined }> = [
// still provide backwards compatibility for projects not yet migrated to the new pv-stylemark
{
name: "lsg",
dir: pvConfig.lsgTemplatesSrc,
},
{
name: "pages",
name: "normal",
dir: pvConfig.cdTemplatesSrc,
},
];
Expand Down

0 comments on commit 9c5a8fb

Please sign in to comment.