-
-
Notifications
You must be signed in to change notification settings - Fork 117
feat(snapshot): add document symbols and folding support #764
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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,45 @@ | ||
| import * as vscode from 'vscode' | ||
| import { createSnapshotSymbol, pushToDocumentSymbol, type SnapshotEntryTool } from './tools' | ||
|
|
||
| export class SnapshotDocumentSymbolProvider implements vscode.DocumentSymbolProvider { | ||
| private latestUri: string | undefined = undefined | ||
| private latestVersion: number | undefined = undefined | ||
| latestDocumentSymbols: vscode.DocumentSymbol[] = [] | ||
| constructor(private snapshotEntryTool: SnapshotEntryTool) {} | ||
| provideDocumentSymbols( | ||
| document: vscode.TextDocument, | ||
| token: vscode.CancellationToken, | ||
| ): vscode.ProviderResult<vscode.DocumentSymbol[]> { | ||
| if (this.latestUri === document.uri.toString() && this.latestVersion === document.version) { | ||
| return this.latestDocumentSymbols | ||
| } | ||
| this.snapshotEntryTool.process(document, document.uri.toString(), document.version, token) | ||
| if (token.isCancellationRequested) return null // cancelled | ||
|
|
||
| this.latestUri = document.uri.toString() | ||
| this.latestVersion = document.version | ||
|
|
||
| const documentSymbols: vscode.DocumentSymbol[] = [] | ||
| forExportsSymbol: for (const entry of this.snapshotEntryTool.snapshotEntries) { | ||
| let currentLevel: vscode.DocumentSymbol[] = documentSymbols | ||
| let parent: vscode.DocumentSymbol[] | undefined | ||
|
|
||
| for (let i = 0; i < entry.breadcrumb.length; i++) { | ||
| const existingSymbol = currentLevel.at(-1) | ||
| if (!existingSymbol || existingSymbol.name !== entry.breadcrumb[i]) { | ||
| const newSymbol = createSnapshotSymbol(entry.breadcrumb[i], entry, i) | ||
| currentLevel.push(newSymbol) | ||
| i + 1 < entry.breadcrumb.length && pushToDocumentSymbol(newSymbol, entry, i + 1) | ||
| continue forExportsSymbol | ||
| } | ||
| parent = currentLevel | ||
| currentLevel = existingSymbol.children | ||
| } | ||
| // last level - all breadcrumbs matched, create duplicate leaf | ||
| ;(parent || documentSymbols).push( | ||
| createSnapshotSymbol(entry.breadcrumb.at(-1)!, entry, entry.breadcrumb.length - 1), | ||
| ) | ||
| } | ||
| return (this.latestDocumentSymbols = documentSymbols) | ||
| } | ||
| } |
This file contains hidden or 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,34 @@ | ||
| import * as vscode from 'vscode' | ||
| import { type SnapshotEntryTool } from './tools' | ||
|
|
||
| export class SnapshotFoldingRangeProvider implements vscode.FoldingRangeProvider { | ||
| private latestUri: string | undefined = undefined | ||
| private latestVersion: number | undefined = undefined | ||
| latestFoldingRanges: vscode.FoldingRange[] = [] | ||
| constructor(private snapshotEntryTool: SnapshotEntryTool) {} | ||
| provideFoldingRanges( | ||
| document: vscode.TextDocument, | ||
| _: vscode.FoldingContext, | ||
| token: vscode.CancellationToken, | ||
| ): vscode.ProviderResult<vscode.FoldingRange[]> { | ||
| if (this.latestUri === document.uri.toString() && this.latestVersion === document.version) { | ||
| return this.latestFoldingRanges | ||
| } | ||
| this.snapshotEntryTool.process(document, document.uri.toString(), document.version, token) | ||
| if (token.isCancellationRequested) return null // cancelled | ||
|
|
||
| this.latestUri = document.uri.toString() | ||
| this.latestVersion = document.version | ||
| const foldingRanges: vscode.FoldingRange[] = [] | ||
| for (const symbol of this.snapshotEntryTool.snapshotEntries) { | ||
| foldingRanges.push( | ||
| new vscode.FoldingRange( | ||
| document.positionAt(symbol.start).line, | ||
| document.positionAt(symbol.end).line, | ||
| vscode.FoldingRangeKind.Region, | ||
| ), | ||
| ) | ||
| } | ||
| return (this.latestFoldingRanges = foldingRanges) | ||
| } | ||
| } |
This file contains hidden or 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,101 @@ | ||
| import * as vscode from 'vscode' | ||
|
|
||
| const ExportSymbolRegex = /^exports\[`([^`]*)`\]/gm | ||
| const RangeEndRegex = /`;$/m | ||
|
|
||
| export interface SnapshotEntry { | ||
| name: string | ||
| breadcrumb: [...describeName: string[], itName: string] | ||
| start: number | ||
| end: number | ||
| fullRange: vscode.Range | ||
| keyRange: vscode.Range | ||
| } | ||
|
|
||
| export class SnapshotEntryTool { | ||
| private latestUri: string | undefined = undefined | ||
| private latestVersion: number | undefined = undefined | ||
| snapshotEntries: SnapshotEntry[] = [] | ||
| process( | ||
| document: vscode.TextDocument, | ||
| uri: string, | ||
| version: number, | ||
| token: vscode.CancellationToken, | ||
| ): void { | ||
| let changeUri = false | ||
| let changeVersion = false | ||
| if (this.latestUri !== uri) { | ||
| this.latestUri = uri | ||
| this.latestVersion = version | ||
| changeUri = true | ||
| changeVersion = true | ||
| } else if (this.latestVersion !== version) { | ||
| this.latestVersion = version | ||
| changeVersion = true | ||
| } | ||
|
|
||
| if (!changeUri && !changeVersion) { | ||
| return // cached | ||
| } else { | ||
| // reset snapshotEntries | ||
| this.snapshotEntries = [] | ||
| } | ||
| if (token.isCancellationRequested) return // cancelled | ||
| const text = document.getText() | ||
| const exportsSymbols = text.matchAll(ExportSymbolRegex) || [] | ||
|
|
||
| for (const match of exportsSymbols) { | ||
| const name = match[1] | ||
| const snapshotDataStart = match.index | ||
| const snapshotDataEnd = | ||
| snapshotDataStart + | ||
| // find the nearest closing delimiter | ||
| (text.slice(snapshotDataStart).match(RangeEndRegex)?.index ?? | ||
| // fallback to empty snapshot | ||
| 'exports[`'.length + name.length + '`]'.length + ' = `'.length + '""'.length) + | ||
| '`;'.length | ||
|
|
||
| this.snapshotEntries.push({ | ||
| name: name, | ||
| breadcrumb: name.split(' > ') as [...describeName: string[], itName: string], | ||
| start: snapshotDataStart, | ||
| end: snapshotDataEnd, | ||
| fullRange: new vscode.Range( | ||
| document.positionAt(snapshotDataStart), | ||
| document.positionAt(snapshotDataEnd), | ||
| ), | ||
| keyRange: new vscode.Range( | ||
| document.positionAt(snapshotDataStart + 'exports[`'.length), | ||
| document.positionAt(snapshotDataStart + 'exports[`'.length + name.length), | ||
| ), | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export function createSnapshotSymbol( | ||
| name: string, | ||
| entry: SnapshotEntry, | ||
| index: number, | ||
| ): vscode.DocumentSymbol { | ||
| const isLastRound = index === entry.breadcrumb.length - 1 | ||
| return new vscode.DocumentSymbol( | ||
| name, | ||
| isLastRound ? 'it' : 'describe', | ||
| vscode.SymbolKind.Function, | ||
| entry.fullRange, | ||
| entry.keyRange, | ||
| ) | ||
| } | ||
|
|
||
| export function pushToDocumentSymbol( | ||
| parentDocumentSymbol: vscode.DocumentSymbol, | ||
| entry: SnapshotEntry, | ||
| startIndex: number = 1, | ||
| ): void { | ||
| for (let i = startIndex; i < entry.breadcrumb.length; i++) { | ||
| const newDocumentSymbol = createSnapshotSymbol(entry.breadcrumb[i], entry, i) | ||
| parentDocumentSymbol.children.push(newDocumentSymbol) | ||
| parentDocumentSymbol = newDocumentSymbol | ||
| } | ||
| } | ||
This file contains hidden or 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,52 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| describe('fixture', () => { | ||
| describe('__fixtures__/file.spec.ts 1', () => { | ||
| it('snapshot', () => { | ||
| expect('').toMatchSnapshot() | ||
| }) | ||
| it('snapshot_1', () => { | ||
| expect('').toMatchSnapshot() | ||
| }) | ||
| }) | ||
|
|
||
| describe('__fixtures__/file.spec.ts 2', () => { | ||
| it('snapshot_1', () => { | ||
| expect('').toMatchSnapshot() | ||
| }) | ||
| it('snapshot_2', () => { | ||
| expect('').toMatchSnapshot() | ||
| }) | ||
| it('snapshot', () => { | ||
| expect('\nsome content\n').toMatchSnapshot() | ||
| }) | ||
| }) | ||
|
|
||
| // same name it | ||
| describe('__fixtures__/file.spec.ts 2', () => { | ||
| it('snapshot_1', () => { | ||
| expect('').toMatchSnapshot() | ||
| }) | ||
| it('snapshot_2', () => { | ||
| expect('').toMatchSnapshot() | ||
| }) | ||
| it('snapshot', () => { | ||
| expect('').toMatchSnapshot() | ||
| }) | ||
| }) | ||
| // same name expect | ||
| it('snapshot_2', () => { | ||
| expect('').toMatchSnapshot() | ||
| }) | ||
| }) | ||
|
|
||
| describe('fixture2', () => { | ||
| it('__fixtures__/file.spec.ts 4', () => { | ||
| expect('').toMatchSnapshot() | ||
| expect('').toMatchSnapshot() | ||
| }) | ||
| }) | ||
|
|
||
| it('fixture2', () => { | ||
| expect('').toMatchSnapshot() | ||
| }) |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.