Skip to content

Commit 6ac28ef

Browse files
keeganirbykaranA-aws
authored andcommitted
feat(cwl): Support autoscrolling live tail session's visible editors (aws#5857)
## Problem New log events in a LiveTail session are added to the end of a TextDocument. Session updates can contain multiple LogEvents, leading to the text document growing quickly past the user's view. requiring them to constantly be scrolling the document themselves. ## Solution To maintain parity with CWL console experience (and the classic `tail -f` experience), we want to auto scroll the customer's LiveTail session's visible editors to the bottom. This will only happen IF the end of file is already in view. Meaning, if a User scrolls UP (causing the end of file to not be in view), auto scrolling will not enable. However, if they scroll back down to EOF themselves, it will re-enable. Simply, if the end of file is in view, when new logEvents are added to the document, the editor will be autoscrolled back to the end of the file.
1 parent f1e4268 commit 6ac28ef

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

packages/core/src/awsService/cloudWatchLogs/commands/tailLogGroup.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ async function handleSessionStream(
7777
formatLogEvent(logEvent)
7878
)
7979
if (formattedLogEvents.length !== 0) {
80+
//Determine should scroll before adding new lines to doc because adding large
81+
//amount of new lines can push bottom of file out of view before scrolling.
82+
const editorsToScroll = getTextEditorsToScroll(document)
8083
await updateTextDocumentWithNewLogEvents(formattedLogEvents, document, session.maxLines)
84+
editorsToScroll.forEach(scrollTextEditorToBottom)
8185
}
8286
}
8387
}
@@ -99,6 +103,22 @@ function formatLogEvent(logEvent: LiveTailSessionLogEvent): string {
99103
return line
100104
}
101105

106+
//Auto scroll visible LiveTail session editors if the end-of-file is in view.
107+
//This allows for newly added log events to stay in view.
108+
function getTextEditorsToScroll(document: vscode.TextDocument): vscode.TextEditor[] {
109+
return vscode.window.visibleTextEditors.filter((editor) => {
110+
if (editor.document !== document) {
111+
return false
112+
}
113+
return editor.visibleRanges[0].contains(new vscode.Position(document.lineCount - 1, 0))
114+
})
115+
}
116+
117+
function scrollTextEditorToBottom(editor: vscode.TextEditor) {
118+
const position = new vscode.Position(Math.max(editor.document.lineCount - 2, 0), 0)
119+
editor.revealRange(new vscode.Range(position, position), vscode.TextEditorRevealType.Default)
120+
}
121+
102122
async function updateTextDocumentWithNewLogEvents(
103123
formattedLogEvents: string[],
104124
document: vscode.TextDocument,

0 commit comments

Comments
 (0)