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 VisualStudioInteraction.ShowExceptionAsync(ServiceProvider, CodeConversion.ConverterTitle, ex);
            }
        }
        private async Task ConvertDocumentAsync(string documentPath, Span selected)
        {
            if (documentPath == null || !CodeConversion.IsVBFileName(documentPath))
            {
                return;
            }

            try {
                await _codeConversion.PerformDocumentConversionAsync <VBToCSConversion>(documentPath, selected);
            } catch (Exception ex) {
                await VisualStudioInteraction.ShowExceptionAsync(ServiceProvider, CodeConversion.ConverterTitle, ex);
            }
        }
Exemple #3
0
        private async Task ProjectItemMenuItem_BeforeQueryStatusAsync(object sender, EventArgs e)
        {
            if (sender is OleMenuCommand menuItem)
            {
                menuItem.Visible = false;
                menuItem.Enabled = false;

                string itemPath = (await VisualStudioInteraction.GetSingleSelectedItemOrDefaultAsync())?.ItemPath;
                if (itemPath == null || !CodeConversion.IsCSFileName(itemPath))
                {
                    return;
                }

                menuItem.Visible = true;
                menuItem.Enabled = true;
            }
        }
        /// <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();

            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);

            await TaskScheduler.Default;
            await base.InitializeAsync(cancellationToken, progress);
        }
Exemple #5
0
        void ProjectItemMenuItem_BeforeQueryStatus(object sender, EventArgs e)
        {
            var menuItem = sender as OleMenuCommand;

            if (menuItem != null)
            {
                menuItem.Visible = false;
                menuItem.Enabled = false;

                string itemPath = VisualStudioInteraction.GetSingleSelectedItemOrDefault()?.ItemPath;
                if (itemPath == null || !CodeConversion.IsVBFileName(itemPath))
                {
                    return;
                }

                menuItem.Visible = true;
                menuItem.Enabled = true;
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ConvertCSToVBCommand"/> 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>
        ConvertCSToVBCommand(REConverterPackage package, CodeConversion codeConversion, OleMenuCommandService commandService)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            this._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);
            }
        }
        /// <summary>
        /// Initializes the singleton instance of the command.
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        public static async Task InitializeAsync(REConverterPackage package)
        {
            CodeConversion codeConversion = await CodeConversion.CreateAsync(package, package.VsWorkspace, package.GetOptionsAsync);

            Instance = new ConvertCSToVBCommand(package, codeConversion, await package.GetServiceAsync <IMenuCommandService, OleMenuCommandService>());
        }
 /// <remarks>
 /// Must be called from UI thread
 /// </remarks>
 public static void Initialize(REConverterPackage package, OleMenuCommandService menuCommandService, CodeConversion codeConversion)
 {
     ThreadHelper.ThrowIfNotOnUIThread();
     Instance = new ConvertCSToVBCommand(package, codeConversion, menuCommandService);
 }