看看光标是一个方法,类或命名空间块内 [英] Find out if cursor is inside a method, class or namespace block

查看:195
本文介绍了看看光标是一个方法,类或命名空间块内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Visual Studio插件可识别插入符当前是一个方法,类或命名空间块中,也就是说,如果插入符移动,插件应该可以记下插入符当前状态里面某某等等元素。

I would like to create a Visual Studio addin which can identify if the caret currently is inside a method, class or namespace block, i.e. if the caret moves, the addin should be able to note down the status that the caret is currently inside so-and-so element.

如果这可以扩展到大括号括起来,如任何C#块性能,这将是极好的。

If this can be extended to any C# block enclosed in curly braces, e.g. properties, that would be excellent.

虽然我已经检查了类似的问题,请让我知道,如果这是一个重复的问题,这样我就可以相应地对它进行标记。如果其他VS外接程序相关的问题显然是有益的在这里,请让我知道这一点。

Although I have checked for similar questions, please let me know if this is a repeat question so I can mark it accordingly. If other VS Addin-related questions are obviously helpful here, please let me know that too.

基本上,我想知道我们可以用什么方法和如何能。完成

Basically, I would like to know what techniques we can use and how this can be accomplished.

编辑:我想简单的答案上手:

I guess the short answer to get started is:


  • 的Visual Studio 2010 SP1 SDK

  • 可能的使用可扩展性项目模板编辑文本装饰品或一些其他类似的模板。

  • Get Visual Studio 2010 SDK SP1
  • Probably use the "Extensibility" project template "Editor Text Adornment" or some other such template.

推荐答案

事实上,我认为你需要做的第一件事就是判断有多少在你的类方法或属性,什么是他们的位置。你以后获取这些信息。你需要做的下一步是判断什么是当前光标所在位置了。然后,你可以做你获得了这些方法的信息进行比较。 。到现在为止,你可以得到哪种方法,你的光标这是一个大概的解决方案,我觉得

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获得的方法和属性的位置?

您可以使用的 NRefacotry CSParser 做到这一点(我用NRefactory完成我的要求)

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

2.How得到光标位置?

有一个在IVsTextView名为GetCaretPos的方法。您可以通过TextManager得到ActiveTextView。然后,你可以用GetCaretPos的方法。这里有一些代码可以帮助你。

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;
        }
    }

这篇关于看看光标是一个方法,类或命名空间块内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆