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

// 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)编写代码处理选中文字,如下:





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值