<commit_before>using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace DotVVM.Framework.Controls
{
    public class GridViewDataSet<T> : IGridViewDataSet
    {
        public string SortExpression { get; set; }

        public bool SortDescending { get; set; }

        public int PageIndex { get; set; }

        public int PageSize { get; set; }

        public int TotalItemsCount { get; set; }

        public IList<T> Items { get; set; }

        public string PrimaryKeyPropertyName { get; set; }

        public object EditRowId { get; set; }

        public IList<int> NearPageIndexes
        {
            get { return Enumerable.Range(0, PagesCount).Where(n => Math.Abs(n - PageIndex) <= 5).ToList(); }
        }

        public int PagesCount
        {
            get
            {
                if (TotalItemsCount == 0 || PageSize == 0) return 1;
                return (int)Math.Ceiling((double)TotalItemsCount / PageSize);
            }
        }

        public bool IsFirstPage
        {
            get { return PageIndex == 0; }
        }

        public bool IsLastPage
        {
            get { return PageIndex == PagesCount - 1; }
        }

        IList IGridViewDataSet.Items
        {
            get { return (IList)Items; }
        }

        public GridViewDataSet()
        {
            Items = new List<T>();
        }

        public void GoToFirstPage()
        {
            PageIndex = 0;
        }

        public void GoToPreviousPage()
        {
            if (!IsFirstPage)
            {
                PageIndex--;
            }
        }

        public void GoToLastPage()
        {
            PageIndex = PagesCount - 1;
        }

        public void GoToNextPage()
        {
            if (!IsLastPage)
            {
                PageIndex++;
            }
        }

        public void GoToPage(int index)
        {
            PageIndex = index;
        }

        public void Reset()
        {
            PageIndex = 0;
        }

        public void SetSortExpression(string expression)
        {
            if (SortExpression == expression)
            {
                SortDescending = !SortDescending;
                GoToFirstPage();
            }
            else
            {
                SortExpression = expression;
                SortDescending = false;
                GoToFirstPage();
            }
        }

        public void LoadFromQueryable(IQueryable<T> queryable)
        {
            TotalItemsCount = queryable.Count();

            if (!string.IsNullOrEmpty(SortExpression))
            {
                queryable = ApplySortExpression(queryable);
            }

            if (PageSize > 0)
            {
                queryable = queryable.Skip(PageSize * PageIndex).Take(PageSize);
            }

            Items = queryable.ToList();
        }

        public IQueryable<T> ApplySortExpression(IQueryable<T> queryable)
        {
            var type = typeof(T);
            var property = type.GetTypeInfo().GetProperty(SortExpression);

            if (property == null)
            {
                throw new Exception($"Could not sort by property '{SortExpression}', since it does not exists.");
            }

            var parameter = Expression.Parameter(type, "p");
            var propertyAccess = Expression.MakeMemberAccess(parameter, property);
            var orderBy = Expression.Lambda(propertyAccess, parameter);

            var result = Expression.Call(typeof(Queryable),
                GetSortingMethodName(),
                new[] { type, property.PropertyType },
                queryable.Expression,
                Expression.Quote(orderBy));

            return queryable.Provider.CreateQuery<T>(result);
        }

        private string GetSortingMethodName()
        {
            return SortDescending ? "OrderByDescending" : "OrderBy";<commit_msg>Methods on GridViewDataSet made virtual to allow extensibility<commit_after>using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace DotVVM.Framework.Controls
{
    public class GridViewDataSet<T> : IGridViewDataSet
    {
        public string SortExpression { get; set; }

        public bool SortDescending { get; set; }

        public int PageIndex { get; set; }

        public int PageSize { get; set; }

        public int TotalItemsCount { get; set; }

        public IList<T> Items { get; set; }

        public string PrimaryKeyPropertyName { get; set; }

        public object EditRowId { get; set; }

        public virtual IList<int> NearPageIndexes
        {
            get { return Enumerable.Range(0, PagesCount).Where(n => Math.Abs(n - PageIndex) <= 5).ToList(); }
        }

        public int PagesCount
        {
            get
            {
                if (TotalItemsCount == 0 || PageSize == 0) return 1;
                return (int)Math.Ceiling((double)TotalItemsCount / PageSize);
            }
        }

        public bool IsFirstPage
        {
            get { return PageIndex == 0; }
        }

        public bool IsLastPage
        {
            get { return PageIndex == PagesCount - 1; }
        }

        IList IGridViewDataSet.Items
        {
            get { return (IList)Items; }
        }

        public GridViewDataSet()
        {
            Items = new List<T>();
        }

        public void GoToFirstPage()
        {
            PageIndex = 0;
        }

        public void GoToPreviousPage()
        {
            if (!IsFirstPage)
            {
                PageIndex--;
            }
        }

        public void GoToLastPage()
        {
            PageIndex = PagesCount - 1;
        }

        public void GoToNextPage()
        {
            if (!IsLastPage)
            {
                PageIndex++;
            }
        }

        public void GoToPage(int index)
        {
            PageIndex = index;
        }

        public void Reset()
        {
            PageIndex = 0;
        }

        public virtual void SetSortExpression(string expression)
        {
            if (SortExpression == expression)
            {
                SortDescending = !SortDescending;
                GoToFirstPage();
            }
            else
            {
                SortExpression = expression;
                SortDescending = false;
                GoToFirstPage();
            }
        }

        public virtual void LoadFromQueryable(IQueryable<T> queryable)
        {
            TotalItemsCount = queryable.Count();

            if (!string.IsNullOrEmpty(SortExpression))
            {
                queryable = ApplySortExpression(queryable);
            }

            if (PageSize > 0)
            {
                queryable = queryable.Skip(PageSize * PageIndex).Take(PageSize);
            }

            Items = queryable.ToList();
        }

        public virtual IQueryable<T> ApplySortExpression(IQueryable<T> queryable)
        {
            var type = typeof(T);
            var property = type.GetTypeInfo().GetProperty(SortExpression);

            if (property == null)
            {
                throw new Exception($"Could not sort by property '{SortExpression}', since it does not exists.");
            }

            var parameter = Expression.Parameter(type, "p");
            var propertyAccess = Expression.MakeMemberAccess(parameter, property);
            var orderBy = Expression.Lambda(propertyAccess, parameter);

            var result = Expression.Call(typeof(Queryable),
                GetSortingMethodName(),
                new[] { type, property.PropertyType },
                queryable.Expression,
                Expression.Quote(orderBy));

            return queryable.Provider.CreateQuery<T>(result);
        }

        private string GetSortingMethodName()
        {
            return SortDescending ? "OrderByDescending" : "OrderBy";
{'fraction_non_alphanumeric': 0.08708596948269445, 'fraction_numerical': 0.0027291899268080885, 'mean_word_length': 1.4639364303178484, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4659075', 'n_tokens_mistral': 2097, 'n_tokens_neox': 1978, 'n_words': 472}