<commit_before>﻿// Copyright (C) Pash Contributors. License: GPL/BSD. See https://github.com/Pash-Project/Pash/
using System;
using System.Management.Automation;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Management.Automation.Internal;

namespace Microsoft.PowerShell.Commands
{
    [Cmdlet("Get", "Member")]
    public class GetMemberCommand : PSCmdlet
    {
        private HybridDictionary _membersCollection;

        [Parameter(ValueFromPipeline = true)]
        public PSObject InputObject { get; set; }

        [Alias(new string[] { "Type" }), Parameter]
        public PSMemberTypes MemberType { get; set; }

        [Parameter(Position = 0)]
        public string[] Name { get; set; }

        [Parameter]
        public SwitchParameter Static { get; set; }

        public GetMemberCommand()
        {
            MemberType = PSMemberTypes.All;
            _membersCollection = new HybridDictionary();
        }

        protected override void ProcessRecord()
        {
            // MUST: implement the InputObject binding in the pipe
            if ((InputObject == null) || (InputObject == AutomationNull.Value))
                return;

            string fullName;

            // TODO: deal with Static

            if (InputObject.TypeNames.Count != 0)
            {
                fullName = this.InputObject.TypeNames[0];
            }
            else
            {
                fullName = "<null>";
            }

            if (!_membersCollection.Contains(fullName))
            {
                _membersCollection.Add(fullName, "");

                foreach (string name in Name)
                {
                    ReadOnlyPSMemberInfoCollection<PSMemberInfo> infos;

                    // TODO: deal with Static members
                    infos = InputObject.Members.Match(name, this.MemberType);

                    List<MemberDefinition> members = new List<MemberDefinition>();
                    foreach (PSMemberInfo info in infos)
                    {
                        members.Add(new MemberDefinition(fullName, info.Name, info.MemberType, info.ToString()));
                    }

                    members.Sort((def1, def2) =>
                    {
                        int diff =
                            string.Compare(def1.MemberType.ToString(), def2.MemberType.ToString(),
                                           StringComparison.CurrentCultureIgnoreCase);
                        if (diff != 0)
                        {
                            return diff;
                        }
                        return
                            string.Compare(def1.Name, def2.Name,
                                           StringComparison.CurrentCultureIgnoreCase);
                    });

                    foreach (MemberDefinition definition in members)
                    {
                        WriteObject(definition);
                    }
                }
            }
        }

        protected override void EndProcessing()
        {
            if (_membersCollection.Count == 0)
            {
                // TODO: WriteError
                throw new Exception("No object specified");
            }
        }
    }
}<commit_msg>Refactoring and fixing of Get-Member cmdlet<commit_after>﻿// Copyright (C) Pash Contributors. License: GPL/BSD. See https://github.com/Pash-Project/Pash/
using System;
using System.Management.Automation;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Management.Automation.Internal;

namespace Microsoft.PowerShell.Commands
{
    [Cmdlet("Get", "Member")]
    public class GetMemberCommand : PSCmdlet
    {
        private HashSet<string> _displayedTypes;

        [Parameter(ValueFromPipeline = true)]
        public PSObject InputObject { get; set; }

        [Alias(new string[] { "Type" }), Parameter]
        public PSMemberTypes MemberType { get; set; }

        [Parameter(Position = 0)]
        public string[] Name { get; set; }

        [Parameter]
        public SwitchParameter Static { get; set; }

        public GetMemberCommand()
        {
            MemberType = PSMemberTypes.All;
            _displayedTypes = new HashSet<string>();
        }

        protected override void ProcessRecord()
        {
            if (InputObject == null || InputObject.BaseObject == null)
            {
                return;
            }

            string fullName = InputObject.TypeNames.Count != 0 ? this.InputObject.TypeNames[0] : "<null>";

            if (_displayedTypes.Contains(fullName))
            {
                return;
            }

            _displayedTypes.Add(fullName);

            if (Name == null)
            {
                Name = new string[] { "*" }; // select all members
            }

            foreach (string name in Name)
            {
                ReadOnlyPSMemberInfoCollection<PSMemberInfo> infos;

                if (Static.IsPresent)
                {
                    infos = InputObject.StaticMembers.Match(name, this.MemberType);
                }
                else
                {
                    infos = InputObject.Members.Match(name, this.MemberType);
                }

                List<MemberDefinition> members = new List<MemberDefinition>();
                foreach (PSMemberInfo info in infos)
                {
                    members.Add(new MemberDefinition(fullName, info.Name, info.MemberType, info.ToString()));
                }

                members.Sort((def1, def2) =>
                {
                    int diff = string.Compare(def1.MemberType.ToString(), def2.MemberType.ToString(),
                                       StringComparison.CurrentCultureIgnoreCase);
                    if (diff != 0)
                    {
                        return diff;
                    }
                    return string.Compare(def1.Name, def2.Name, StringComparison.CurrentCultureIgnoreCase);
                });

                foreach (MemberDefinition definition in members)
                {
                    WriteObject(definition);
                }
            }
        }

        protected override void EndProcessing()
        {
            if (_displayedTypes.Count == 0)
            {
                WriteError(new ErrorRecord(new InvalidOperationException("No input object specified"),
                                           "NoObjectInGetMember", ErrorCategory.CloseError, null));
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08834299664327129, 'fraction_numerical': 0.0033567287152883735, 'mean_word_length': 1.3940832724616508, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4229564', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1458, 'n_words': 334}