Find out if cursor is inside a method, class or namespace block

℡╲_俬逩灬. 提交于 2019-12-04 22:54:40

Actually, I think the first thing you need to do is to judge how many methods or properties in your class and what's the position of them. After you obtain those information. The next step you need to do is to judge what's the current cursor position now. Then you can do a compare with those methods information you obtained. Until now, you can get which method that your cursor in. This is a ballpark solution that I find.

Let's talk about some technical details:

1.How to get the positions of methods and properties?

you can use NRefacotry or CSParser to do this (I use NRefactory to finish my requirement)

2.How to get cursor position?

there is a method named "GetCaretPos" in IVsTextView. You can get ActiveTextView through TextManager. Then you could use "GetCaretPos" method. Here are some codes may help you.

    public static IVsTextManager TextManager
    {
        get
        {
            if (textManager == null)
            {
                Object obj = Package.GetGlobalService(typeof(SVsTextManager));
                if (obj == null)
                {
                    throw new ArgumentException("get textmanager failed in VSTextView");
                }
                textManager = obj as IVsTextManager;
            }
            return textManager;
        }
    }


    public static IVsTextView ActiveTextView
    {
        get
        {
            IVsTextView activeView = null;
            if (TextManager != null)
            {
                TextManager.GetActiveView(1, activeTextBuffer, out activeView);
            }
            return activeView;
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!