-1

selected.Contains即使用户在调用该方法之前选择了一些代码,也会引发空指针异常。

this.package = package;        

string selected;
selected = (string)this.ServiceProvider.GetService(typeof(TextSelection));

if (selected.Contains("for")) 
{
    MessageBox.Show("user "  + "selected" + selected);
}
4

1 回答 1

1

我会带你到那里的。

 private IVsEditorAdaptersFactoryService GetEditorAdaptersFactoryService()
 {
        IComponentModel componentModel =(IComponentModel)GetService(typeof(SComponentModel));
        return componentModel.GetService<IVsEditorAdaptersFactoryService>();
 }   
private Microsoft.VisualStudio.Text.Editor.IWpfTextView GetTextView()
 {
        IVsTextManager textManager = (IVsTextManager)GetService(typeof(SVsTextManager));
        if (textManager == null)
            return null;
        IVsTextView textView = null;
        textManager.GetActiveView(1, null, out textView);
        if (textView == null)
            return null;
        return GetEditorAdaptersFactoryService().GetWpfTextView(textView);
 }
public void SomeFUnction()
{
    Microsoft.VisualStudio.Text.Editor.IWpfTextView textView = GetTextView();
    if (textView != null)
    {
                    SnapshotPoint caretPosition = textView.Caret.Position.BufferPosition;
    }
}

现在你有了插入符号的位置来弄清楚那里有什么。类似 textView.GetTextElementSpan(caretPosition).GetText()

于 2016-06-14T19:46:19.833 回答