/// <summary>
        /// Returns an object describing the quick info context for the specified text offset, if any.
        /// </summary>
        /// <param name="view">The <see cref="IEditorView"/> in which the offset is located.</param>
        /// <param name="offset">The text offset to examine.</param>
        /// <returns>
        /// An object describing the quick info context for the specified text offset, if any.
        /// A <see langword="null"/> value indicates that no context is available.
        /// </returns>
        /// <remarks>
        /// This method is called in response to keyboard events.
        /// </remarks>
        public override object GetContext(IEditorView view, int offset)
        {
            // Get the context factory service
            SimpleContextFactory contextFactory = view.SyntaxEditor.Document.Language.GetService <SimpleContextFactory>();

            if (contextFactory != null)
            {
                // Get a context
                return(contextFactory.CreateContext(new TextSnapshotOffset(view.CurrentSnapshot, offset), false));
            }
            return(null);
        }
예제 #2
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        // PUBLIC PROCEDURES
        /////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Requests that an <see cref="ICompletionSession"/> be opened for the specified <see cref="IEditorView"/>.
        /// </summary>
        /// <param name="view">The <see cref="IEditorView"/> that will host the session.</param>
        /// <param name="canCommitWithoutPopup">Whether the session can immediately commit if a single match is made when the session is opened, commonly known as "complete word" functionality.</param>
        /// <returns>
        /// <c>true</c> if a session was opened; otherwise, <c>false</c>.
        /// </returns>
        public override bool RequestSession(IEditorView view, bool canCommitWithoutPopup)
        {
            // Get the context factory service
            SimpleContextFactory contextFactory = view.SyntaxEditor.Document.Language.GetService <SimpleContextFactory>();

            if (contextFactory != null)
            {
                // Get a context
                SimpleContext context = contextFactory.CreateContext(view.Selection.EndSnapshotOffset, false);

                // Create a session
                CompletionSession session = new CompletionSession();
                session.CanCommitWithoutPopup = canCommitWithoutPopup;

                switch (context.Type)
                {
                case SimpleContextType.Default:
                    // Add items for keywords
                    session.Items.Add(new CompletionItem("function", new CommonImageSourceProvider(CommonImageKind.Keyword),
                                                         new PlainTextContentProvider("Declares a function.")));
                    break;

                case SimpleContextType.FunctionDeclarationBlock:
                case SimpleContextType.FunctionReference: {
                    // Add items for keywords
                    session.Items.Add(new CompletionItem("var", new CommonImageSourceProvider(CommonImageKind.Keyword),
                                                         new PlainTextContentProvider("Declares a variable.")));
                    session.Items.Add(new CompletionItem("return", new CommonImageSourceProvider(CommonImageKind.Keyword),
                                                         new PlainTextContentProvider("Returns a value.")));

                    // Add items (one for each function name)
                    ILLParseData parseData = view.SyntaxEditor.Document.ParseData as ILLParseData;
                    if (parseData != null)
                    {
                        CompilationUnit compilationUnit = parseData.Ast as CompilationUnit;
                        if ((compilationUnit != null) && (compilationUnit.HasMembers))
                        {
                            // Loop through the AST nodes
                            foreach (FunctionDeclaration functionAstNode in compilationUnit.Members)
                            {
                                session.Items.Add(new CompletionItem(functionAstNode.Name, new CommonImageSourceProvider(CommonImageKind.MethodPublic),
                                                                     new FunctionContentProvider(view.HighlightingStyleRegistry, functionAstNode, false, view.DefaultBackgroundColor)));
                            }
                        }
                    }
                    break;
                }
                }

                if (session.Items.Count > 0)
                {
                    // Ensure the caret is visible
                    view.Scroller.ScrollToCaret();

                    // Ensure the items are sorted and open the session
                    session.SortItems();
                    session.Open(view);
                    return(true);
                }
            }
            return(false);
        }