Partager via


Supporting the Autos Window (Managed Package Framework)

When the debugger is active, the Autos window can display expressions such as variables and parameters that are taken from the local scope where the program being debugged is paused (either due to a breakpoint or an exception). The expressions can include variables, local or global, and parameters that have been changed in the local scope. The Autos window can also include objects that are instantiations of a class, structure, or some other type. Anything that an expression evaluator can evaluate can potentially be shown in the Autos window.

The managed package framework (MPF) does not provide direct support for the Autos window. However, if you override one method in the LanguageService class, you can return a list of expressions to be presented in the Autos window.

Implementing Support for the Autos Window

All you need to do to support the Autos window is to implement the GetProximityExpressions method in the LanguageService class. The GetProximityExpressions method is given a location in the source file and it is up to your implementation to decide what expressions, from the context of the location, should appear in the Autos window. Your implementation of the GetProximityExpressions returns a list of strings where each string represents a single expression. In addition, a return value of S_OK indicates the list contains expressions. A return value of S_FALSE indicates there are no expressions to show.

The actual expressions returned are the names of the variables or parameters that appear at that location in the code. These names are passed to the expression evaluator to obtain values and types that are then displayed in the Autos window.

Example

The following example shows an implementation of the GetProximityExpressions method in a derivation of the LanguageService class that obtains a list of expressions from the ParseSource method parser using the parse reason Autos. The list of expressions is bundled into an instantiation of a class called MyVsEnumBSTR that implements the IVsEnumBSTR interface and this list object is returned.

Note that the GetAutoExpressionsCount and GetAutoExpression methods are custom methods on the MyAuthoringSink object and were added to support this example. They represent one way in which expressions added to the MyAuthoringSink object by the parser (by calling the AutoExpression method) can be accessed outside the parser.

using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.TextManager.Interop;

namespace MyLanguagePackage
{
    public class MyLanguageService : LanguageService
    {
        public override int GetProximityExpressions(IVsTextBuffer buffer,
                                                    int line,
                                                    int col,
                                                    int cLines,
                                                    out IVsEnumBSTR ppEnum)
        {
            int retval = VSConstants.E_NOTIMPL;
            ppEnum = null;
            if (buffer != null)
            {
                IVsTextLines textLines = buffer as IVsTextLines;
                if (textLines != null)
                {
                    Source src = this.GetSource(textLines);
                    if (src != null)
                    {
                        TokenInfo tokenInfo = new TokenInfo();
                        string text = src.GetText();
                        ParseRequest req = CreateParseRequest(src,
                                                              line,
                                                              col,
                                                              tokenInfo,
                                                              text,
                                                              src.GetFilePath(),
                                                              ParseReason.Autos,
                                                              null);
                        req.Scope = this.ParseSource(req);
                        MyAuthoringSink sink = req.Sink as MyAuthoringSink;

                        retval = VSConstants.S_FALSE;
                        int spanCount = sink.GetAutoExpressionsCount();
                        if (spanCount > 0) {
                            MyVsEnumBSTR bstrList = new MyVsEnumBSTR();
                            for (int i = 0; i < spanCount; i++)
                            {
                                TextSpan span;
                                sink.GetAutoExpression(i, out span);
                                string expression = src.GetText(span.iStartLine,
                                                                span.iStartIndex,
                                                                span.iEndLine,
                                                                span.iEndIndex);
                                bstrList.AddString(expression);
                            }
                            ppEnum = bstrList;
                            retval = VSConstants.S_OK;
                        }
                    }
                }
            }
            return retval;
        }
    }
}

See Also

Concepts

Language Service Features (Managed Package Framework)