Created
October 31, 2018 10:41
-
-
Save hexagit/f19b8f7bdcc2515c0ea9786862036171 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel.Composition; | |
using System.Runtime.InteropServices; | |
using Microsoft.VisualStudio; | |
using Microsoft.VisualStudio.Editor; | |
using Microsoft.VisualStudio.OLE.Interop; | |
using Microsoft.VisualStudio.Text.Editor; | |
using Microsoft.VisualStudio.TextManager.Interop; | |
using Microsoft.VisualStudio.Utilities; | |
/* | |
* 必要な参照 | |
* System.ComponentModel.Composition | |
* Microsoft.VisualStudio.Editor | |
* Microsoft.VisualStudio.Shell.15.0 | |
* Microsoft.VisualStudio.OLE.Interop | |
* Microsoft.VisualStudio.Text.UI | |
* Microsoft.VisualStudio.Text.UI.Wpf | |
* Microsoft.VisualStudio.TextManager.Interop | |
* Microsoft.VisualStudio.CoreUtility | |
*/ | |
namespace VSIXTest3 | |
{ | |
[Export(typeof(IVsTextViewCreationListener))] | |
[ContentType("text")] | |
[TextViewRole(PredefinedTextViewRoles.Editable)] | |
internal sealed class CommandHandlerProvider : IVsTextViewCreationListener | |
{ | |
//! ビューの作成時に呼ばれる | |
public void VsTextViewCreated(IVsTextView textViewAdapter) | |
{ | |
ITextView view = _adapterService.GetWpfTextView(textViewAdapter); | |
if( view == null ) return; | |
//! フック用のクラス作成処理を登録 | |
view.Properties.GetOrCreateSingletonProperty(() => new CommandHandler(textViewAdapter)); | |
} | |
[Import] | |
internal IVsEditorAdaptersFactoryService _adapterService = null; | |
} | |
internal sealed class CommandHandler : IOleCommandTarget | |
{ | |
//! コンストラクタ | |
internal CommandHandler(IVsTextView adapter) | |
{ | |
adapter.AddCommandFilter(this, out _nextCmdHdl); | |
} | |
public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText) | |
{ | |
return _nextCmdHdl.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText); | |
} | |
public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) | |
{ | |
//! コマンドのグループGUIDによって、nCmdIDの解釈が変わる | |
if( pguidCmdGroup == VSConstants.VSStd2K ) { | |
//! 文字入力が行われた場合、入力された文字を判別して、'a'なら無視 | |
if( nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR ) { | |
var typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn); | |
if( typedChar == 'a' ) return VSConstants.S_OK; | |
} | |
} | |
//! コマンド入力が行われた場合、「カット」は無視 | |
if( pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97 ) { | |
if( nCmdID == (uint)VSConstants.VSStd97CmdID.Cut ) { | |
return VSConstants.S_OK; | |
} | |
} | |
//! 次のハンドラに処理を任せる | |
return _nextCmdHdl.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut); | |
} | |
private IOleCommandTarget _nextCmdHdl = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment