-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reference to snippet and blocks #677
Draft
aswamy
wants to merge
1
commit into
main
Choose a base branch
from
feature/reference-snippets-and-blocks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/theme-language-server-common/src/references/BaseReferencesProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { LiquidHtmlNode } from '@shopify/liquid-html-parser'; | ||
import { | ||
Location, | ||
ReferenceParams, | ||
} from 'vscode-languageserver-protocol'; | ||
|
||
export interface BaseReferencesProvider { | ||
references( | ||
node: LiquidHtmlNode, | ||
ancestors: LiquidHtmlNode[], | ||
params: ReferenceParams, | ||
): Promise<Location[] | undefined>; | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/theme-language-server-common/src/references/ReferencesProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Location, ReferenceParams } from "vscode-languageserver-protocol"; | ||
import { AugmentedLiquidSourceCode, DocumentManager } from "../documents"; | ||
import { BaseReferencesProvider } from "./BaseReferencesProvider"; | ||
import { findCurrentNode, SourceCodeType } from "@shopify/theme-check-common"; | ||
import { BlockReferencesProvider } from "./providers/BlockReferencesProvider"; | ||
import { SnippetReferencesProvider } from "./providers/SnippetReferencesProvider"; | ||
|
||
export type GetLiquidFiles = (rootUri: string) => Promise<AugmentedLiquidSourceCode[]>; | ||
|
||
export class ReferencesProvider { | ||
private providers: BaseReferencesProvider[]; | ||
|
||
constructor( | ||
private documentManager: DocumentManager, | ||
private getLiquidFiles: GetLiquidFiles, | ||
) { | ||
this.providers = [ | ||
new BlockReferencesProvider(this.documentManager, this.getLiquidFiles), | ||
new SnippetReferencesProvider(this.documentManager, this.getLiquidFiles), | ||
]; | ||
} | ||
|
||
async references(params: ReferenceParams): Promise<Location[] | undefined> { | ||
try { | ||
const document = this.documentManager.get(params.textDocument.uri) | ||
|
||
if (!document || document.type !== SourceCodeType.LiquidHtml || document.ast instanceof Error) { | ||
return; | ||
} | ||
|
||
const [currentNode, ancestors] = findCurrentNode(document.ast, document.textDocument.offsetAt(params.position)); | ||
|
||
const promises = this.providers.map((provider) => provider.references(currentNode, ancestors, params)); | ||
|
||
const locations = []; | ||
|
||
for(const promise of promises) { | ||
const result = await promise; | ||
if (result) { | ||
locations.push(...result); | ||
} | ||
} | ||
|
||
return locations; | ||
} catch (error) { | ||
console.error(error); | ||
return; | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
packages/theme-language-server-common/src/references/providers/BlockReferencesProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Location, ReferenceParams } from "vscode-languageserver-protocol"; | ||
import { DocumentManager } from "../../documents"; | ||
import { BaseReferencesProvider } from "../BaseReferencesProvider"; | ||
import { GetLiquidFiles } from "../ReferencesProvider"; | ||
import { LiquidHtmlNode, LiquidTag, NamedTags } from "@shopify/liquid-html-parser"; | ||
import { SourceCodeType, visit } from "@shopify/theme-check-common"; | ||
import { Range } from "vscode-json-languageservice"; | ||
|
||
export class BlockReferencesProvider implements BaseReferencesProvider { | ||
constructor(private documentManager: DocumentManager, private getLiquidFiles: GetLiquidFiles) {} | ||
|
||
async references(currentNode: LiquidHtmlNode, ancestors: LiquidHtmlNode[], params: ReferenceParams): Promise<Location[] | undefined> { | ||
const sources = await this.getLiquidFiles(params.textDocument.uri); | ||
const document = this.documentManager.get(params.textDocument.uri) | ||
|
||
if (!document || document.type !== SourceCodeType.LiquidHtml || document.ast instanceof Error) { | ||
return; | ||
} | ||
|
||
const parentNode = ancestors.at(-1); | ||
const grandparentNode = ancestors.at(-2); | ||
if (!parentNode || !grandparentNode) return; | ||
if ( | ||
currentNode.type !== 'String' || | ||
parentNode.type !== 'NamedArgument' || | ||
grandparentNode.type !== 'ContentForMarkup' | ||
) return; | ||
|
||
const referenceLocations = [] as Location[] | ||
|
||
for (const source of sources) { | ||
if (source.ast instanceof Error) continue; | ||
|
||
referenceLocations.push(...visit<SourceCodeType.LiquidHtml, Location>(source.ast, { | ||
LiquidTag(node: LiquidTag) { | ||
if (node.name === NamedTags.content_for) { | ||
if (typeof node.markup === 'string') return; | ||
|
||
const typeArg = node.markup.args.find((arg) => arg.name === 'type'); | ||
|
||
if (!typeArg || typeArg.value.type !== 'String') return; | ||
|
||
if (typeArg.value.value === currentNode.value) { | ||
return { | ||
uri: source.uri, | ||
range: Range.create( | ||
source.textDocument.positionAt(typeArg.value.position.start + 1), | ||
source.textDocument.positionAt(typeArg.value.position.end - 1), | ||
), | ||
}; | ||
} | ||
} | ||
} | ||
})); | ||
} | ||
|
||
// TODO: check if the block appears in any schema | ||
// Need building blocks to know where in the block it appears (i.e. position) | ||
|
||
return referenceLocations; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/theme-language-server-common/src/references/providers/SnippetReferencesProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Location, ReferenceParams } from "vscode-languageserver-protocol"; | ||
import { DocumentManager } from "../../documents"; | ||
import { BaseReferencesProvider } from "../BaseReferencesProvider"; | ||
import { GetLiquidFiles } from "../ReferencesProvider"; | ||
import { LiquidHtmlNode, LiquidTag, NamedTags, NodeTypes } from "@shopify/liquid-html-parser"; | ||
import { SourceCodeType, visit } from "@shopify/theme-check-common"; | ||
import { Range } from "vscode-json-languageservice"; | ||
|
||
export class SnippetReferencesProvider implements BaseReferencesProvider { | ||
constructor(private documentManager: DocumentManager, private getLiquidFiles: GetLiquidFiles) {} | ||
|
||
async references(currentNode: LiquidHtmlNode, ancestors: LiquidHtmlNode[], params: ReferenceParams): Promise<Location[] | undefined> { | ||
const sources = await this.getLiquidFiles(params.textDocument.uri); | ||
const document = this.documentManager.get(params.textDocument.uri) | ||
|
||
if (!document || document.type !== SourceCodeType.LiquidHtml || document.ast instanceof Error) { | ||
return; | ||
} | ||
|
||
const parentNode = ancestors.at(-1); | ||
if (!parentNode || parentNode.type !== 'RenderMarkup' || currentNode.type !== 'String') { | ||
return; | ||
} | ||
|
||
const referenceLocations = [] as Location[]; | ||
|
||
for (const source of sources) { | ||
if (source.ast instanceof Error) continue; | ||
|
||
referenceLocations.push(...visit<SourceCodeType.LiquidHtml, Location>(source.ast, { | ||
LiquidTag(node: LiquidTag) { | ||
if ((node.name === NamedTags.render || node.name === NamedTags.include) && typeof node.markup !== 'string') { | ||
const snippet = node.markup.snippet; | ||
if (snippet.type === NodeTypes.String && snippet.value === currentNode.value) { | ||
return { | ||
uri: source.uri, | ||
range: Range.create( | ||
source.textDocument.positionAt(snippet.position.start + 1), | ||
source.textDocument.positionAt(snippet.position.end - 1), | ||
), | ||
}; | ||
} | ||
} | ||
}, | ||
})) | ||
} | ||
|
||
return referenceLocations; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we keep a memoized ast for each file and invalidate it on file change? Reading every file when someone looks up references to the variable seems expensive.