VS2015 C++ 插件开发-代码处理-格式化

来源:互联网 发布:it读音 编辑:程序博客网 时间:2024/06/05 19:21

// Command handler called when the user selects the "My Command" command.void OnMyCommand(CommandHandler* /*pSender*/, DWORD /*flags*/, VARIANT* /*pIn*/, VARIANT* /*pOut*/){CComPtr<IServiceProvider> spServiceProvider;this->GetSite(&spServiceProvider);if(spServiceProvider){CComPtr<IVsTextManager> spTextMgr;spServiceProvider->QueryService<IVsTextManager>(SID_SVsTextManager, &spTextMgr);if(spTextMgr){CComPtr<IVsTextView> spTextView;spTextMgr->GetActiveView(TRUE, NULL, &spTextView);if(spTextView){TextSpan txSpan;HRESULT hr1 = spTextView->GetSelectionSpan(&txSpan);if(hr1 == S_OK){CComBSTR bstrText;HRESULT hr2 = spTextView->GetSelectedText(&bstrText);if(hr2 == S_OK && bstrText){CString strChanged = FormatCode(CString(bstrText));if(strChanged.GetLength()){CComPtr < IVsTextLines> spTextLines;spTextView->GetBuffer(&spTextLines);if(spTextLines){TextSpan txSpan2;HRESULT hr3 = spTextLines->ReplaceLines(txSpan.iStartLine, txSpan.iStartIndex, txSpan.iEndLine, txSpan.iEndIndex, strChanged, strChanged.GetLength(), &txSpan2);if(hr3 == S_OK){spTextView->SetSelection(txSpan2.iStartLine, txSpan2.iStartIndex, txSpan2.iEndLine, txSpan2.iEndIndex);}}}}}}}}}

经过摸索,C++开发一个代码格式化插件的方法如下

(1)选择新建项目

(2)C++ / 扩展 /VSIX 包

(3)创建菜单命令工具

(4)编写代码处理选中文字,如下:





原创粉丝点击