Ejemplo n.º 1
0
        static IWpfTextViewHost GetCurrentVBViewHost(IServiceProvider serviceProvider)
        {
            IWpfTextViewHost viewHost = VisualStudioInteraction.GetCurrentViewHost(serviceProvider);

            if (viewHost == null)
            {
                return(null);
            }

            ITextDocument textDocument = viewHost.GetTextDocument();

            if ((textDocument == null) || !IsVBFileName(textDocument.FilePath))
            {
                return(null);
            }

            return(viewHost);
        }
        public static void PerformVBToCSConversion(IServiceProvider serviceProvider, string inputCode)
        {
            string convertedText = null;

            try
            {
                var result = TryConvertingVBToCSCode(inputCode);
                if (!result.Success)
                {
                    var newLines = Environment.NewLine + Environment.NewLine;
                    VsShellUtilities.ShowMessageBox(
                        serviceProvider,
                        $"Selected VB code seems to have errors or to be incomplete:{newLines}{result.GetExceptionsAsString()}",
                        VBToCSConversionTitle,
                        OLEMSGICON.OLEMSGICON_WARNING,
                        OLEMSGBUTTON.OLEMSGBUTTON_OK,
                        OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                    return;
                }

                convertedText = result.ConvertedCode;
            }
            catch (Exception ex)
            {
                VisualStudioInteraction.ShowException(serviceProvider, VBToCSConversionTitle, ex);
                return;
            }

            // Direct output for debugging
            //string message = convertedText;
            //VsShellUtilities.ShowMessageBox(
            //    serviceProvider,
            //    message,
            //    VBToCSConversionTitle,
            //    OLEMSGICON.OLEMSGICON_INFO,
            //    OLEMSGBUTTON.OLEMSGBUTTON_OK,
            //    OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);

            WriteStatusBarText(serviceProvider, "Copied converted C# code to clipboard.");

            Clipboard.SetText(convertedText);
        }
Ejemplo n.º 3
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.GetSingleSelectedItemPath();
                var    fileInfo = new FileInfo(itemPath);
                if (!CodeConversion.IsCSFileName(fileInfo.Name))
                {
                    return;
                }

                menuItem.Visible = true;
                menuItem.Enabled = true;
            }
        }
Ejemplo n.º 4
0
        async void ProjectItemMenuItemCallback(object sender, EventArgs e)
        {
            string itemPath = VisualStudioInteraction.GetSingleSelectedItemPath();
            var    fileInfo = new FileInfo(itemPath);

            if (!CodeConversion.IsVBFileName(fileInfo.Name))
            {
                return;
            }

            try {
                using (StreamReader reader = new StreamReader(itemPath)) {
                    string csCode = await reader.ReadToEndAsync();

                    CodeConversion.PerformVBToCSConversion(ServiceProvider, csCode);
                }
            } catch (Exception ex) {
                VisualStudioInteraction.ShowException(ServiceProvider, CodeConversion.VBToCSConversionTitle, ex);
            }
        }
Ejemplo n.º 5
0
        public static void PerformCSToVBConversion(IServiceProvider serviceProvider, string inputCode)
        {
            string convertedText = null;

            try
            {
                if (!TryConvertingCSToVBCode(inputCode, out convertedText))
                {
                    VsShellUtilities.ShowMessageBox(
                        serviceProvider,
                        "Selected C# code seems to have errors or to be incomplete.",
                        CSToVBConversionTitle,
                        OLEMSGICON.OLEMSGICON_WARNING,
                        OLEMSGBUTTON.OLEMSGBUTTON_OK,
                        OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                    return;
                }
            }
            catch (Exception ex)
            {
                VisualStudioInteraction.ShowException(serviceProvider, CSToVBConversionTitle, ex);
                return;
            }

            // Direct output for debugging
            //string message = convertedText;
            //VsShellUtilities.ShowMessageBox(
            //    serviceProvider,
            //    message,
            //    CSToVBConversionTitle,
            //    OLEMSGICON.OLEMSGICON_INFO,
            //    OLEMSGBUTTON.OLEMSGBUTTON_OK,
            //    OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);

            WriteStatusBarText(serviceProvider, "Copied converted VB code to clipboard.");

            Clipboard.SetText(convertedText);
        }