|
| 1 | +import * as vscode from 'vscode' |
| 2 | + |
| 3 | +const ExportSymbolRegex = /^exports\[`([^`]*)`\]/gm |
| 4 | +const RangeEndRegex = /`;$/m |
| 5 | + |
| 6 | +export interface SnapshotEntry { |
| 7 | + name: string |
| 8 | + breadcrumb: [...describeName: string[], itName: string] |
| 9 | + start: number |
| 10 | + end: number |
| 11 | + fullRange: vscode.Range |
| 12 | + keyRange: vscode.Range |
| 13 | +} |
| 14 | + |
| 15 | +export class SnapshotEntryTool { |
| 16 | + private latestUri: string | undefined = undefined |
| 17 | + private latestVersion: number | undefined = undefined |
| 18 | + snapshotEntries: SnapshotEntry[] = [] |
| 19 | + process( |
| 20 | + document: vscode.TextDocument, |
| 21 | + uri: string, |
| 22 | + version: number, |
| 23 | + token: vscode.CancellationToken, |
| 24 | + ): void { |
| 25 | + let changeUri = false |
| 26 | + let changeVersion = false |
| 27 | + if (this.latestUri !== uri) { |
| 28 | + this.latestUri = uri |
| 29 | + this.latestVersion = version |
| 30 | + changeUri = true |
| 31 | + changeVersion = true |
| 32 | + } else if (this.latestVersion !== version) { |
| 33 | + this.latestVersion = version |
| 34 | + changeVersion = true |
| 35 | + } |
| 36 | + |
| 37 | + if (!changeUri && !changeVersion) { |
| 38 | + return // cached |
| 39 | + } else { |
| 40 | + // reset snapshotEntries |
| 41 | + this.snapshotEntries = [] |
| 42 | + } |
| 43 | + if (token.isCancellationRequested) return // cancelled |
| 44 | + const text = document.getText() |
| 45 | + const exportsSymbols = text.matchAll(ExportSymbolRegex) || [] |
| 46 | + |
| 47 | + for (const match of exportsSymbols) { |
| 48 | + const name = match[1] |
| 49 | + const snapshotDataStart = match.index |
| 50 | + const snapshotDataEnd = |
| 51 | + snapshotDataStart + |
| 52 | + // find the nearest closing delimiter |
| 53 | + (text.slice(snapshotDataStart).match(RangeEndRegex)?.index ?? |
| 54 | + // fallback to empty snapshot |
| 55 | + 'exports[`'.length + name.length + '`]'.length + ' = `'.length + '""'.length) + |
| 56 | + '`;'.length |
| 57 | + |
| 58 | + this.snapshotEntries.push({ |
| 59 | + name: name, |
| 60 | + breadcrumb: name.split(' > ') as [...describeName: string[], itName: string], |
| 61 | + start: snapshotDataStart, |
| 62 | + end: snapshotDataEnd, |
| 63 | + fullRange: new vscode.Range( |
| 64 | + document.positionAt(snapshotDataStart), |
| 65 | + document.positionAt(snapshotDataEnd), |
| 66 | + ), |
| 67 | + keyRange: new vscode.Range( |
| 68 | + document.positionAt(snapshotDataStart + 'exports[`'.length), |
| 69 | + document.positionAt(snapshotDataStart + 'exports[`'.length + name.length), |
| 70 | + ), |
| 71 | + }) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export function createSnapshotSymbol( |
| 77 | + name: string, |
| 78 | + entry: SnapshotEntry, |
| 79 | + index: number, |
| 80 | +): vscode.DocumentSymbol { |
| 81 | + const isLastRound = index === entry.breadcrumb.length - 1 |
| 82 | + return new vscode.DocumentSymbol( |
| 83 | + name, |
| 84 | + isLastRound ? 'it' : 'describe', |
| 85 | + vscode.SymbolKind.Function, |
| 86 | + entry.fullRange, |
| 87 | + entry.keyRange, |
| 88 | + ) |
| 89 | +} |
| 90 | + |
| 91 | +export function pushToDocumentSymbol( |
| 92 | + parentDocumentSymbol: vscode.DocumentSymbol, |
| 93 | + entry: SnapshotEntry, |
| 94 | + startIndex: number = 1, |
| 95 | +): void { |
| 96 | + for (let i = startIndex; i < entry.breadcrumb.length; i++) { |
| 97 | + const newDocumentSymbol = createSnapshotSymbol(entry.breadcrumb[i], entry, i) |
| 98 | + parentDocumentSymbol.children.push(newDocumentSymbol) |
| 99 | + parentDocumentSymbol = newDocumentSymbol |
| 100 | + } |
| 101 | +} |
0 commit comments