private async Task ConvertDocumentAsync(string documentPath, Span selected, CancellationToken cancellationToken) { if (documentPath == null || !CodeConversion.IsCSFileName(documentPath)) { return; } try { await _codeConversion.ConvertDocumentAsync <CSToVBConversion>(documentPath, selected, cancellationToken); } catch (Exception ex) { await _package.ShowExceptionAsync(ex); } }
private async Task ConvertDocumentAsync(string documentPath, Span selected, CancellationToken cancellationToken) { if (documentPath == null || !CodeConversion.IsVBFileName(documentPath)) { return; } try { await _codeConversion.ConvertDocumentAsync <VBToCSConversion>(documentPath, selected, cancellationToken); } catch (Exception ex) { await VisualStudioInteraction.ShowExceptionAsync(ex); } }
/// <summary> /// Initializes a new instance of the <see cref="ConvertVBToCSCommand" /> 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="codeConversion"></param> /// <param name="commandService"></param> /// <remarks>Must be called on the UI thread due to VS 2017's implementation of AddCommand which calls GetService</remarks> private ConvertVBToCSCommand(CodeConverterPackage package, CodeConversion codeConversion, OleMenuCommandService commandService) { ThreadHelper.ThrowIfNotOnUIThread(); _package = package ?? throw new ArgumentNullException(nameof(package)); _codeConversion = codeConversion; if (commandService != null) { // Command in main menu var menuCommandId = new CommandID(CommandSet, MainMenuCommandId); var menuItem = package.CreateCommand(CodeEditorMenuItemCallbackAsync, menuCommandId); menuItem.BeforeQueryStatus += MainEditMenuItem_BeforeQueryStatusAsync; commandService.AddCommand(menuItem); // Command in code editor's context menu var ctxMenuCommandId = new CommandID(CommandSet, CtxMenuCommandId); var ctxMenuItem = package.CreateCommand(CodeEditorMenuItemCallbackAsync, ctxMenuCommandId); ctxMenuItem.BeforeQueryStatus += CodeEditorMenuItem_BeforeQueryStatusAsync; commandService.AddCommand(ctxMenuItem); // Command in project item context menu var projectItemCtxMenuCommandId = new CommandID(CommandSet, ProjectItemCtxMenuCommandId); var projectItemCtxMenuItem = package.CreateCommand(ProjectItemMenuItemCallbackAsync, projectItemCtxMenuCommandId); projectItemCtxMenuItem.BeforeQueryStatus += ProjectItemMenuItem_BeforeQueryStatusAsync; commandService.AddCommand(projectItemCtxMenuItem); // Command in project context menu var projectCtxMenuCommandId = new CommandID(CommandSet, ProjectCtxMenuCommandId); var projectCtxMenuItem = package.CreateCommand(SolutionOrProjectMenuItemCallbackAsync, projectCtxMenuCommandId); projectCtxMenuItem.BeforeQueryStatus += SolutionOrProjectMenuItem_BeforeQueryStatusAsync; commandService.AddCommand(projectCtxMenuItem); // Command in project context menu var solutionCtxMenuCommandId = new CommandID(CommandSet, SolutionCtxMenuCommandId); var solutionCtxMenuItem = package.CreateCommand(SolutionOrProjectMenuItemCallbackAsync, solutionCtxMenuCommandId); solutionCtxMenuItem.BeforeQueryStatus += SolutionOrProjectMenuItem_BeforeQueryStatusAsync; commandService.AddCommand(solutionCtxMenuItem); var nodeItemCtxMenuCommandId = new CommandID(CommandSet, NodeItemCtxMenuCommandId); var nodeItemCtxMenuItem = package.CreateCommand(ProjectItemMenuItemCallbackAsync, nodeItemCtxMenuCommandId); nodeItemCtxMenuItem.BeforeQueryStatus += ProjectItemMenuItem_BeforeQueryStatusAsync; commandService.AddCommand(nodeItemCtxMenuItem); } }
/// <summary> /// Initialization of the package; this method is called right after the package is sited, so this is the place /// where you can put all the initialization code that rely on services provided by VisualStudio. /// </summary> protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress) { var oleMenuCommandService = await this.GetServiceAsync <IMenuCommandService, OleMenuCommandService>(); var componentModel = await this.GetServiceAsync <SComponentModel, IComponentModel>(); await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); var visualStudioWorkspace = componentModel.GetService <VisualStudioWorkspace>(); var codeConversion = await CodeConversion.CreateAsync(this, visualStudioWorkspace, this.GetDialogPageAsync <ConverterOptionsPage>); ConvertCSToVBCommand.Initialize(this, oleMenuCommandService, codeConversion); ConvertVBToCSCommand.Initialize(this, oleMenuCommandService, codeConversion); VisualStudioInteraction.Initialize(PackageCancellation); await TaskScheduler.Default; await base.InitializeAsync(cancellationToken, progress); }
private async Task ProjectItemMenuItem_BeforeQueryStatusAsync(object sender, EventArgs e) { if (sender is OleMenuCommand menuItem) { menuItem.Visible = false; menuItem.Enabled = false; string itemPath = await VisualStudioInteraction.GetSingleSelectedItemPathOrDefaultAsync(); if (itemPath == null || !CodeConversion.IsCSFileName(itemPath)) { return; } menuItem.Visible = true; menuItem.Enabled = true; } }
/// <remarks> /// Must be called from UI thread /// </remarks> public static void Initialize(CodeConverterPackage package, OleMenuCommandService menuCommandService, CodeConversion codeConversion) { ThreadHelper.ThrowIfNotOnUIThread(); Instance = new PasteAsVB(package, codeConversion, menuCommandService); }