(host: SlashCommandHost, count: number)
| 78 | } |
| 79 | |
| 80 | async function undoByCount(host: SlashCommandHost, count: number): Promise<boolean> { |
| 81 | const session = host.session; |
| 82 | if (session === undefined) { |
| 83 | host.showError(NO_ACTIVE_SESSION_MESSAGE); |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | const entries = host.state.transcriptEntries; |
| 88 | const lastUserIndex = findUndoAnchorEntryIndex(entries, count); |
| 89 | if (lastUserIndex === undefined) { |
| 90 | showUndoLimitStatus(host, 'Nothing to undo.'); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | try { |
| 95 | await session.undoHistory(count); |
| 96 | } catch (error) { |
| 97 | const limit = undoLimitFromError(error); |
| 98 | if (limit !== undefined) { |
| 99 | showUndoLimitStatus(host, formatUndoLimitMessage(limit.requestedCount, limit)); |
| 100 | return false; |
| 101 | } |
| 102 | const message = formatErrorMessage(error); |
| 103 | host.showError(`Failed to undo: ${message}`); |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | const children = host.state.transcriptContainer.children; |
| 108 | const lastUserComponentIndex = findUndoAnchorComponentIndex(children, count); |
| 109 | if (lastUserComponentIndex !== undefined) { |
| 110 | removeUndoContextComponents(children, lastUserComponentIndex); |
| 111 | host.state.transcriptContainer.invalidate(); |
| 112 | } |
| 113 | |
| 114 | const preservedEntries = entries.slice(lastUserIndex).filter( |
| 115 | (entry) => !isUndoContextEntry(entry), |
| 116 | ); |
| 117 | entries.splice(lastUserIndex, entries.length - lastUserIndex, ...preservedEntries); |
| 118 | |
| 119 | if (entries.length === 0) { |
| 120 | renderWelcome(host); |
| 121 | } |
| 122 | |
| 123 | host.state.ui.requestRender(); |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | async function showUndoSelector(host: SlashCommandHost): Promise<void> { |
| 128 | if (host.session === undefined) { |
no test coverage detected