using EnvDTE; using Microsoft; using Microsoft.VisualStudio; using Microsoft.VisualStudio.ComponentModelHost; using Microsoft.VisualStudio.Editor; using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.TextManager.Interop; using System; using System.ComponentModel.Design; using System.Globalization; using System.Threading; using System.Threading.Tasks; using Task = System.Threading.Tasks.Task;
///<summary> /// Command menu group (command set GUID). ///</summary> publicstaticreadonly Guid CommandSet = new Guid( "d7d69c46-e99c-4a3c-95b8-9ac3a1e45289" );
///<summary> /// VS Package that provides this command, not null. ///</summary> privatereadonly AsyncPackage package;
///<summary> /// Initializes a new instance of the <see cref="CommandGoToFile"/> class. /// Adds our command handlers for menu (commands must exist in the command table file) ///</summary> ///<param name="package">Owner package, not null.</param> ///<param name="commandService">Command service to add command to, not null.</param> privateCommandGoToFile(AsyncPackage package, OleMenuCommandService commandService) { this.package = package ?? thrownew ArgumentNullException( nameof( package ) ); commandService = commandService ?? thrownew ArgumentNullException( nameof( commandService ) );
var menuCommandID = new CommandID( CommandSet, CommandId ); var menuItem = new MenuCommand( this.Execute, menuCommandID ); commandService.AddCommand( menuItem ); }
///<summary> /// Gets the instance of the command. ///</summary> publicstatic CommandGoToFile Instance { get; privateset; }
///<summary> /// Gets the service provider from the owner package. ///</summary> private IServiceProvider ServiceProvider { get { returnthis.package; } }
///<summary> /// Initializes the singleton instance of the command. ///</summary> ///<param name="package">Owner package, not null.</param> publicstaticasync Task InitializeAsync(AsyncPackage package) { // Switch to the main thread - the call to AddCommand in CommandGoToFile's constructor requires // the UI thread. await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync( package.DisposalToken ); dte = (DTE)await package.GetServiceAsync(typeof(DTE)); OleMenuCommandService commandService = await package.GetServiceAsync( typeof( IMenuCommandService ) ) as OleMenuCommandService; Assumes.Present(dte); Instance = new CommandGoToFile( package, commandService ); }
///<summary> /// This function is the callback used to execute the command when the menu item is clicked. /// See the constructor to see how the menu item is associated with this function using /// OleMenuCommandService service and MenuCommand class. ///</summary> ///<param name="sender">Event sender.</param> ///<param name="e">Event args.</param> privatevoidExecute(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread( ); Exec( ); }