Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs (C#)

Code before:
            if (runspace == null)
            {
                // No runspace, just return no results.
                return CommandCompletion.EmptyCompletionResult;
            }

            var helper = new PowerShellExecutionHelper(PowerShell.Create(RunspaceMode.CurrentRunspace));
            var executionContext = helper.CurrentPowerShell.Runspace.ExecutionContext;
            return CompleteFilename(new CompletionContext { WordToComplete = fileName, Helper = helper, ExecutionContext = executionContext });
        }

        internal static IEnumerable<CompletionResult> CompleteFilename(CompletionContext context)
        {
            return CompleteFilename(context, containerOnly: false, extension: null);
        }

        [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly")]
        internal static IEnumerable<CompletionResult> CompleteFilename(CompletionContext context, bool containerOnly, HashSet<string> extension)
        {
            var wordToComplete = context.WordToComplete;
            var quote = HandleDoubleAndSingleQuote(ref wordToComplete);
            var results = new List<CompletionResult>();

            // Matches file shares with and without the provider name and with either slash direction.
            // Avoids matching Windows device paths like \\.\CDROM0 and \\?\Volume{b8f3fc1c-5cd6-4553-91e2-d6814c4cd375}\
            var shareMatch = Regex.Match(
                wordToComplete,
                @"(^Microsoft\.PowerShell\.Core\\FileSystem::|^FileSystem::|^)(?:\\\\|//)(?![.|?])([^\\/]+)(?:\\|/)([^\\/]*)$",
                RegexOptions.IgnoreCase);
            if (shareMatch.Success)
            {
                // Only match share names, no filenames.
                var provider = shareMatch.Groups[1].Value;
                var server = shareMatch.Groups[2].Value;
                var sharePattern = WildcardPattern.Get(shareMatch.Groups[3].Value + "*", WildcardOptions.IgnoreCase);
                var ignoreHidden = context.GetOption("IgnoreHiddenShares", @default: false);
                var shares = GetFileShares(server, ignoreHidden);
                foreach (var share in shares)
                {
                    if (sharePattern.IsMatch(share))
                    {
                        string sharePath = $"\\\\{server}\\{share}";
                        string completionText;
                        if (quote == string.Empty)
                        {
                            completionText = share.Contains(' ')
                                ? $"'{provider}{sharePath}'"
                                : $"{provider}{sharePath}";
                        }
                        else
                        {

Review comment (refactor):
It looks like the regex is serving two purposes:

1. is the path a UNC path
2. get the server name

It seems like it may be better to use [PathIsUnc()](https://github.com/PowerShell/PowerShell/blob/744127608efdb07b800407c78b2a58434943e42b/src/System.Management.Automation/engine/Utils.cs#L1233) internal API to determine if it's UNC.  Then if it is, use string methods to extract the server.  There doesn't seem to be a need to use regex here at all.

Code after:
            if (prev == null)
            {
                return new ArgumentLocation() { Argument = null, IsPositional = true, Position = 0 };
            }

            switch (prev.ParameterArgumentType)
            {
                case AstParameterArgumentType.AstPair:
                case AstParameterArgumentType.Switch:
                    if (!prev.ParameterSpecified)
                        return new ArgumentLocation() { Argument = null, IsPositional = true, Position = position };

                    return prev.Parameter.Extent.Text.EndsWith(':')
                        ? new ArgumentLocation() { Argument = prev, IsPositional = false, Position = -1 }

                        : new ArgumentLocation() { Argument = null, IsPositional = true, Position = position };
                case AstParameterArgumentType.Fake:
                    return new ArgumentLocation() { Argument = prev, IsPositional = false, Position = -1 };
                default:
                    Diagnostics.Assert(false, "parsed arguments should not contain AstArray and PipeObject");
                    return null;
            }
        }

        /// <summary>
        /// Find the location where 'tab' is typed based on the expressionAst.
        /// </summary>
        /// <param name="parsedArguments"></param>
        /// <param name="expAst"></param>
        /// <returns></returns>
        private static ArgumentLocation FindTargetArgumentLocation(Collection<AstParameterArgumentPair> parsedArguments, ExpressionAst expAst)
        {
            Diagnostics.Assert(expAst != null, "Caller needs to make sure expAst is not null");
            int position = 0;
            foreach (AstParameterArgumentPair pair in parsedArguments)
            {
                switch (pair.ParameterArgumentType)
                {
                    case AstParameterArgumentType.AstPair:
                        {
                            AstPair arg = (AstPair)pair;
                            if (arg.ArgumentIsCommandParameterAst)
                                continue;

                            if (arg.ParameterContainsArgument && arg.Argument == expAst)
                            {
                                return new ArgumentLocation() { IsPositional = false, Position = -1, Argument = arg };
                            }

                            if (arg.Argument.GetHashCode() == expAst.GetHashCode())
                            {