src/vs/editor/common/cursor/cursorMoveCommands.ts/CursorMoveCommands/findPositionInViewportIfOutside
class CursorMoveCommands:	public static findPositionInViewportIfOutside(viewModel: IViewModel, cursor: CursorState, visibleViewRange: Range, inSelectionMode: boolean): PartialCursorState {
		const viewLineNumber = cursor.viewState.position.lineNumber;

		if (visibleViewRange.startLineNumber <= viewLineNumber && viewLineNumber <= visibleViewRange.endLineNumber - 1) {
			// Nothing to do, cursor is in viewport
			return new CursorState(cursor.modelState, cursor.viewState);

		} else {
			let newViewLineNumber: number;
			if (viewLineNumber > visibleViewRange.endLineNumber - 1) {
				newViewLineNumber = visibleViewRange.endLineNumber - 1;
			} else if (viewLineNumber < visibleViewRange.startLineNumber) {
				newViewLineNumber = visibleViewRange.startLineNumber;
			} else {
				newViewLineNumber = viewLineNumber;
			}
			const position = MoveOperations.vertical(viewModel.cursorConfig, viewModel, viewLineNumber, cursor.viewState.position.column, cursor.viewState.leftoverVisibleColumns, newViewLineNumber, false);
			return CursorState.fromViewState(cursor.viewState.move(inSelectionMode, position.lineNumber, position.column, position.leftoverVisibleColumns));
		}
	}