Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ async function handleSessionStream(
formatLogEvent(logEvent)
)
if (formattedLogEvents.length !== 0) {
//Determine should scroll before adding new lines to doc because adding large
//amount of new lines can push bottom of file out of view before scrolling.
const editorsToScroll = getTextEditorsToScroll(document)
await updateTextDocumentWithNewLogEvents(formattedLogEvents, document, session.maxLines)
editorsToScroll.forEach(scrollTextEditorToBottom)
}
}
}
Expand All @@ -99,6 +103,24 @@ function formatLogEvent(logEvent: LiveTailSessionLogEvent): string {
return line
}

//Auto scroll visible LiveTail session editors if the end-of-file is in view.
//This allows for newly added log events to stay in view.
function getTextEditorsToScroll(document: vscode.TextDocument): vscode.TextEditor[] {
return vscode.window.visibleTextEditors.filter((editor) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone has a two visible editors, one on the left and one on the right like:

____________________________________________________
|                      |                           |
| first editor         |       second editor       |
|                      |                           |
____________________________________________________

does that mean both of them will scroll?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! they'd both be considered visible and able to be scrolled if at EOF. If one isn't at EOF, and the other is, then only one would be scrolled.

const isEditorForSession = editor.document === document
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is only used once, should you just inline this on line 111

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could do that!

The reason I wrote this as a function was not so much about reusability, but more about readability. The code going into handleSessionStream is growing in size and complexity. I worry that as we do more per stream update that it will become less clear what "actions" we take. Having this defined as a function makes it easier to see that per stream update that we need to 'getTextEditorsToScroll`, then update the document, then scroll it. While abstracting away those implementations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I thought that make sense. I was more just commenting on doing:

if (editor.document !== document) {
...
}

vs

const isEditorForSession = editor.document === document
if (!isEditorForSession) {

}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh haha I misunderstood 😅 I'll change this

if (!isEditorForSession) {
return false
}
return editor.visibleRanges[0].contains(new vscode.Position(document.lineCount - 1, 0))
})
}

function scrollTextEditorToBottom(editor: vscode.TextEditor) {
const topPosition = new vscode.Position(Math.max(editor.document.lineCount - 2, 0), 0)
const bottomPosition = new vscode.Position(Math.max(editor.document.lineCount - 2, 0), 0)
editor.revealRange(new vscode.Range(topPosition, bottomPosition), vscode.TextEditorRevealType.Default)
}

async function updateTextDocumentWithNewLogEvents(
formattedLogEvents: string[],
document: vscode.TextDocument,
Expand Down
Loading