Skip to content

Commit

Permalink
Cache liquidDoc definitions in DocumentManager
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmengo committed Jan 22, 2025
1 parent a167761 commit 9fa9b47
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/dry-donkeys-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/theme-language-server-common': minor
'@shopify/theme-check-common': minor
---

Cache liquidDoc fetch results
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import {
IsValidSchema,
memo,
Mode,
isError,
} from '@shopify/theme-check-common';
import { Connection } from 'vscode-languageserver';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { ClientCapabilities } from '../ClientCapabilities';
import { percent, Progress } from '../progress';
import { AugmentedSourceCode } from './types';
import { getSnippetDefinition } from '../liquidDoc';

export class DocumentManager {
/**
Expand Down Expand Up @@ -171,6 +173,12 @@ export class DocumentManager {
const mode = await this.getModeForUri!(uri);
return toSchema(mode, uri, sourceCode, this.isValidSchema);
}),
/** Lazy and only computed once per file version */
liquidDoc: memo(async (snippetName: string) => {
if (isError(sourceCode.ast)) return { name: snippetName };

return getSnippetDefinition(sourceCode.ast, snippetName);
}),
};
default:
return assertNever(sourceCode);
Expand Down
2 changes: 2 additions & 0 deletions packages/theme-language-server-common/src/documents/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AppBlockSchema,
} from '@shopify/theme-check-common';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { SnippetDefinition } from '../liquidDoc';

/** Util type to add the common `textDocument` property to the SourceCode. */
type _AugmentedSourceCode<SCT extends SourceCodeType = SourceCodeType> = SourceCode<SCT> & {
Expand All @@ -23,6 +24,7 @@ export type AugmentedJsonSourceCode = _AugmentedSourceCode<SourceCodeType.JSON>;
*/
export type AugmentedLiquidSourceCode = _AugmentedSourceCode<SourceCodeType.LiquidHtml> & {
getSchema: () => Promise<SectionSchema | ThemeBlockSchema | AppBlockSchema | undefined>;
liquidDoc: (snippetName: string) => Promise<SnippetDefinition>;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export function startServer(
return { name: snippetName };
}

return getSnippetDefinition(snippet.ast, snippetName);
return snippet.liquidDoc(snippetName);
};

const snippetFilter = ([uri]: FileTuple) => /\.liquid$/.test(uri) && /snippets/.test(uri);
Expand Down

0 comments on commit 9fa9b47

Please sign in to comment.