コード例 #1
0
        public IEnumerable <string> GetFilesystemExpansions(string cmdStart, string replacableEnd)
        {
            replacableEnd = StripQuotes(replacableEnd);

            var    p         = new System.Management.Path(replacableEnd).NormalizeSlashes().ResolveTilde();
            var    startPath = new System.Management.Path(".");
            string lookFor   = replacableEnd;

            if (p.ToString().Contains(p.CorrectSlash))
            {
                // we already deal with a path
                var escapedSlash = p.CorrectSlash;
                if (escapedSlash.Equals(@"\"))
                {
                    escapedSlash = @"\\";
                }
                if (!p.HasDrive() && !Regex.IsMatch(p, @"^\.+" + escapedSlash))
                {
                    p = new System.Management.Path(".").Combine(p);
                }
                if (p.EndsWithSlash())
                {
                    startPath = p;
                    lookFor   = "";
                }
                else
                {
                    startPath = p.GetParentPath(null);
                    lookFor   = p.GetChildNameOrSelfIfNoChild();
                }
            }
            var dirinfo = new DirectoryInfo(startPath);

            if (!dirinfo.Exists)
            {
                return(Enumerable.Empty <string>());
            }
            var  expansions  = new List <string>();
            var  pattern     = new WildcardPattern(lookFor + "*");
            bool allowHidden = lookFor.Length > 0;

            // add directories
            expansions.AddRange(
                from subdir in dirinfo.GetDirectories()
                where pattern.IsMatch(subdir.Name) &&
                (allowHidden || (subdir.Attributes & FileAttributes.Hidden) == 0)
                orderby subdir.Name ascending
                select QuoteIfNecessary(startPath.Combine(subdir.Name).AppendSlashAtEnd())
                );
            // add files
            expansions.AddRange(
                from file in dirinfo.GetFiles()
                where pattern.IsMatch(file.Name) &&
                (allowHidden || (file.Attributes & FileAttributes.Hidden) == 0)
                orderby file.Name ascending
                select QuoteIfNecessary(startPath.Combine(file.Name))
                );
            return(expansions);
        }
コード例 #2
0
ファイル: TabExpansionProvider.cs プロジェクト: mauve/Pash
        public IEnumerable<string> GetFilesystemExpansions(string cmdStart, string replacableEnd)
        {
            replacableEnd = StripQuotes(replacableEnd);

            var p = new System.Management.Path(replacableEnd).NormalizeSlashes().ResolveTilde();
            var startPath = new System.Management.Path(".");
            string lookFor = replacableEnd;
            if (p.ToString().Contains(p.CorrectSlash))
            {
                // we already deal with a path
                var escapedSlash = p.CorrectSlash;
                if (escapedSlash.Equals(@"\"))
                {
                    escapedSlash = @"\\";
                }
                if (!p.HasDrive() && !Regex.IsMatch(p, @"^\.+" + escapedSlash))
                {
                    p = new System.Management.Path(".").Combine(p);
                }
                if (p.EndsWithSlash())
                {
                    startPath = p;
                    lookFor = "";
                }
                else
                {
                    startPath = p.GetParentPath(null);
                    lookFor = p.GetChildNameOrSelfIfNoChild();
                }
            }
            var dirinfo = new DirectoryInfo(startPath);
            if (!dirinfo.Exists)
            {
                return Enumerable.Empty<string>();
            }
            var expansions = new List<string>();
            var pattern = new WildcardPattern(lookFor + "*");
            bool allowHidden = lookFor.Length > 0;

            // add directories
            expansions.AddRange(
                from subdir in dirinfo.GetDirectories()
                where pattern.IsMatch(subdir.Name) &&
                      (allowHidden || (subdir.Attributes & FileAttributes.Hidden) == 0)
                orderby subdir.Name ascending
                select QuoteIfNecessary(startPath.Combine(subdir.Name).AppendSlashAtEnd())
            );
            // add files
            expansions.AddRange(
                from file in dirinfo.GetFiles()
                where pattern.IsMatch(file.Name) &&
                (allowHidden || (file.Attributes & FileAttributes.Hidden) == 0)
                orderby file.Name ascending
                select QuoteIfNecessary(startPath.Combine(file.Name))
            );
            return expansions;
        }