private IEnumerable<IPackageInfo> GetPackages(RCompletionContext context) {
            if (context.IsInNameSpace()) {
                return GetSpecificPackage(context);
            }

            return GetAllFilePackages(context);
        }
        public IReadOnlyCollection<RCompletion> GetEntries(RCompletionContext context) {
            List<RCompletion> completions = new List<RCompletion>();
            ImageSource functionGlyph = GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupMethod, StandardGlyphItem.GlyphItemPublic);
            ImageSource constantGlyph = GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupConstant, StandardGlyphItem.GlyphItemPublic);

            // TODO: this is different in the console window where 
            // packages may have been loaded from the command line. 
            // We need an extensibility point here.
            IEnumerable<IPackageInfo> packages = GetPackages(context);

            // Get list of functions in the package
            foreach (IPackageInfo pkg in packages) {
                Debug.Assert(pkg != null);

                IEnumerable<INamedItemInfo> functions = pkg.Functions;
                if (functions != null) {
                    foreach (INamedItemInfo function in functions) {
                        ImageSource glyph = function.ItemType == NamedItemType.Constant ? constantGlyph : functionGlyph;

                        var completion = new RCompletion(function.Name, CompletionUtilities.BacktickName(function.Name), function.Description, glyph);
                        completions.Add(completion);
                    }
                }
            }

            return completions;
        }
        public IReadOnlyCollection<RCompletion> GetEntries(RCompletionContext context) {
            List<RCompletion> completions = new List<RCompletion>();
            ImageSource functionGlyph = GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupMethod, StandardGlyphItem.GlyphItemPublic);
            ImageSource variableGlyph = GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupVariable, StandardGlyphItem.GlyphItemPublic);
            Selector selector = Selector.Dollar;

            string variableName = RCompletionContext.GetVariableName(context.Session.TextView, context.TextBuffer.CurrentSnapshot);
            if (variableName.IndexOfAny(new char[] { '$', '@' }) < 0) {
                variableName = string.Empty;
                selector = Selector.None;
            } else if (variableName.EndsWith("@", StringComparison.Ordinal)) {
                selector = Selector.At;
            }

            VariablesProvider.Initialize();
            int memberCount = VariablesProvider.GetMemberCount(variableName);
            IReadOnlyCollection<INamedItemInfo> members = VariablesProvider.GetMembers(variableName, 200);
            var filteredList = FilterList(members, selector);

            // Get list of functions in the package
            foreach (INamedItemInfo v in filteredList) {
                Debug.Assert(v != null);

                if (v.Name.Length > 0 && v.Name[0] != '[') {
                    ImageSource glyph = v.ItemType == NamedItemType.Variable ? variableGlyph : functionGlyph;
                    var completion = new RCompletion(v.Name, CompletionUtilities.BacktickName(v.Name), v.Description, glyph);
                    completions.Add(completion);
                }
            }

            return completions;
        }
        public IReadOnlyCollection<RCompletion> GetEntries(RCompletionContext context) {
            List<RCompletion> completions = new List<RCompletion>();
            ImageSource glyph = GlyphService.GetGlyph(StandardGlyphGroup.GlyphLibrary, StandardGlyphItem.GlyphItemPublic);

            IEnumerable<IPackageInfo> packages = PackageIndex.Packages;
            foreach (var packageInfo in packages) {
                completions.Add(new RCompletion(packageInfo.Name, packageInfo.Name, packageInfo.Description, glyph));
            }

            return completions;
        }
        public IReadOnlyCollection<RCompletion> GetEntries(RCompletionContext context) {
            List<RCompletion> completions = new List<RCompletion>();
            var infoSource = SnippetInformationSource?.InformationSource;

            if (!context.IsInNameSpace() && infoSource != null) {
                ImageSource snippetGlyph = GlyphService.GetGlyph(StandardGlyphGroup.GlyphCSharpExpansion, StandardGlyphItem.GlyphItemPublic);
                foreach (string name in infoSource.SnippetNames) {
                    completions.Add(new RCompletion(name, name, string.Empty, snippetGlyph));
                }
            }
            return completions;
        }
        public IReadOnlyCollection<RCompletion> GetEntries(RCompletionContext context) {
            List<RCompletion> completions = new List<RCompletion>();
            ImageSource functionGlyph = GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupValueType, StandardGlyphItem.GlyphItemPublic);

            // Safety checks
            FunctionCall funcCall = context.AstRoot.GetNodeOfTypeFromPosition<FunctionCall>(context.Position);
            if (funcCall == null || funcCall.OpenBrace == null || funcCall.Arguments == null) {
                return completions;
            }

            if (context.Position < funcCall.OpenBrace.End || context.Position >= funcCall.SignatureEnd) {
                return completions;
            }

            // Retrieve parameter positions from the current text buffer snapshot
            ParameterInfo parametersInfo = SignatureHelp.GetParametersInfoFromBuffer(context.AstRoot, context.TextBuffer.CurrentSnapshot, context.Position);
            if (parametersInfo == null) {
                return completions;
            }

            // Get collection of function signatures from documentation (parsed RD file)
            IFunctionInfo functionInfo = FunctionIndex.GetFunctionInfo(parametersInfo.FunctionName, o => { }, context.Session.TextView);
            if (functionInfo == null) {
                return completions;
            }

            // Collect parameter names from all signatures
            IEnumerable<KeyValuePair<string, IArgumentInfo>> arguments = new Dictionary<string, IArgumentInfo>();
            foreach (ISignatureInfo signature in functionInfo.Signatures) {
                var args = signature.Arguments.ToDictionary(x => x.Name);
                arguments = arguments.Union(args);
            }

            // Add names of arguments that  are not yet specified to the completion
            // list with '=' sign so user can tell them from function names.
            IEnumerable<string> declaredArguments = funcCall.Arguments.Where(x => x is NamedArgument).Select(x => ((NamedArgument)x).Name);
            IEnumerable<KeyValuePair<string, IArgumentInfo>> possibleArguments = arguments.Where(x => x.Key != "..." && !declaredArguments.Contains(x.Key));

            foreach (KeyValuePair<string, IArgumentInfo> arg in possibleArguments) {
                string displayText = arg.Key + " =";
                string insertionText = arg.Key + " = ";
                completions.Add(new RCompletion(displayText, insertionText, arg.Value.Description, functionGlyph));
            }

            return completions;
        }
        public IReadOnlyCollection<RCompletion> GetEntries(RCompletionContext context) {
            List<RCompletion> completions = new List<RCompletion>();

            if (!context.IsInNameSpace()) {
                ImageSource keyWordGlyph = GlyphService.GetGlyph(StandardGlyphGroup.GlyphKeyword, StandardGlyphItem.GlyphItemPublic);

                foreach (string keyword in Keywords.KeywordList) {
                    completions.Add(new RCompletion(keyword, keyword, string.Empty, keyWordGlyph));
                }

                ImageSource buildInGlyth = GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupIntrinsic, StandardGlyphItem.GlyphItemPublic);
                foreach (string s in Builtins.BuiltinList) {
                    completions.Add(new RCompletion(s, s, string.Empty, buildInGlyth));
                }
            }

            return completions;
        }
        /// <summary>
        /// Retrieves name of the package in 'package::' statement
        /// so intellisense can show list of functions available
        /// in the specific package.
        /// </summary>
        private IEnumerable<IPackageInfo> GetSpecificPackage(RCompletionContext context) {
            List<IPackageInfo> packages = new List<IPackageInfo>();
            ITextSnapshot snapshot = context.TextBuffer.CurrentSnapshot;
            int colons = 0;

            for (int i = context.Position - 1; i >= 0; i--, colons++) {
                char ch = snapshot[i];
                if (ch != ':') {
                    break;
                }
            }

            if (colons > 1 && colons < 4) {
                string packageName = string.Empty;
                int start = 0;
                int end = context.Position - colons;

                for (int i = end - 1; i >= 0; i--) {
                    char ch = snapshot[i];
                    if (char.IsWhiteSpace(ch)) {
                        start = i + 1;
                        break;
                    }
                }

                if (start < end) {
                    packageName = snapshot.GetText(Span.FromBounds(start, end));
                    if (packageName.Length > 0) {
                        if (colons == 3)
                            context.InternalFunctions = true;

                        IPackageInfo package = PackageIndex.GetPackageByName(packageName);
                        if (package != null) {
                            packages.Add(package);
                        }
                    }
                }
            }

            return packages;
        }
        public IReadOnlyCollection<RCompletion> GetEntries(RCompletionContext context) {
            List<RCompletion> completions = new List<RCompletion>();

            if (!context.IsInNameSpace()) {
                var infoSource = SnippetInformationSource?.InformationSource;
                ImageSource keyWordGlyph = GlyphService.GetGlyph(StandardGlyphGroup.GlyphKeyword, StandardGlyphItem.GlyphItemPublic);

                foreach (string keyword in Keywords.KeywordList) {
                    bool? isSnippet = infoSource?.IsSnippet(keyword);
                    if (!isSnippet.HasValue || !isSnippet.Value) {
                        completions.Add(new RCompletion(keyword, keyword, string.Empty, keyWordGlyph));
                    }
                }

                ImageSource buildInGlyph = GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupIntrinsic, StandardGlyphItem.GlyphItemPublic);
                foreach (string s in Builtins.BuiltinList) {
                    completions.Add(new RCompletion(s, s, string.Empty, buildInGlyph));
                }
            }

            return completions;
        }
        public IReadOnlyCollection<RCompletion> GetEntries(RCompletionContext context) {
            List<RCompletion> completions = new List<RCompletion>();
            ImageSource folderGlyph = GlyphService.GetGlyph(StandardGlyphGroup.GlyphClosedFolder, StandardGlyphItem.GlyphItemPublic);
            string currentDir = RToolsSettings.Current.WorkingDirectory;
            string directory = null;

            try {
                string dir = Path.Combine(currentDir, _directory);
                if (Directory.Exists(dir)) {
                    directory = dir;
                }
            } catch (IOException) { } catch (UnauthorizedAccessException) { } catch (ArgumentException) { }

            if (directory != null) {
                try {
                    foreach (string dir in Directory.GetDirectories(directory)) {
                        DirectoryInfo di = new DirectoryInfo(dir);
                        if (!di.Attributes.HasFlag(FileAttributes.Hidden) && !di.Attributes.HasFlag(FileAttributes.System)) {
                            string dirName = Path.GetFileName(dir);
                            completions.Add(new RCompletion(dirName, dirName + "/", string.Empty, folderGlyph));
                        }
                    }

                    foreach (string file in Directory.GetFiles(directory)) {
                        FileInfo di = new FileInfo(file);
                        if (!di.Attributes.HasFlag(FileAttributes.Hidden) && !di.Attributes.HasFlag(FileAttributes.System)) {
                            ImageSource fileGlyph = ImagesProvider?.GetFileIcon(file);
                            string fileName = Path.GetFileName(file);
                            completions.Add(new RCompletion(fileName, fileName, string.Empty, fileGlyph));
                        }
                    }
                } catch (IOException) { } catch (UnauthorizedAccessException) { }
            }

            return completions;
        }
        /// <summary>
        /// Retrieves list of packages declared in the file via 'library' statements
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private IEnumerable<IPackageInfo> GetAllFilePackages(RCompletionContext context) {
            List<IPackageInfo> packages = new List<IPackageInfo>();
            LoadedPackagesProvider?.Initialize();

            IEnumerable <string> loadedPackages = LoadedPackagesProvider?.GetPackageNames() ?? Enumerable.Empty<string>();
            IEnumerable<string> filePackageNames = context.AstRoot.GetFilePackageNames();
            IEnumerable<string> allPackageNames = Enumerable.Union(_preloadPackages, Enumerable.Union(filePackageNames, loadedPackages));

            foreach (string packageName in allPackageNames) {
                IPackageInfo p = PackageIndex.GetPackageByName(packageName);
                // May be null if user mistyped package name in the library()
                // statement or package is not installed.
                if (p != null) {
                    packages.Add(p);
                }
            }

            return packages;
        }