Write to an output window pane to avoid opening two files
This commit is contained in:
Родитель
0f419cf1e8
Коммит
bd82a552e9
|
@ -7,7 +7,7 @@ All notable changes to the code converter will be documented here.
|
|||
### Visual studio extension
|
||||
* Works reliably in VS2017
|
||||
* Instead of copy to clipboard, opens result as Visual Studio window for quick viewing
|
||||
* Opens conversion summary window to explain what's happened
|
||||
* Writes summary to output window to explain what's happened
|
||||
* Project context used for all conversions
|
||||
|
||||
### VB -> C# specific
|
||||
|
|
|
@ -72,12 +72,12 @@ namespace CodeConverter.VsExtension
|
|||
|
||||
private void ShowFirstResultAndConversionSummary(List<string> files, List<string> errors)
|
||||
{
|
||||
if (files.Any()) {
|
||||
VisualStudioInteraction.OpenFile(new FileInfo(files.First()));
|
||||
files[0] = files[0] + " (opened in adjacent code window)";
|
||||
}
|
||||
VisualStudioInteraction.OutputWindow.ShowMessageToUser(GetConversionSummary(files, errors));
|
||||
|
||||
VisualStudioInteraction.NewTextWindow("Conversion result summary", GetConversionSummary(files, errors));
|
||||
if (files.Any()) {
|
||||
VisualStudioInteraction.OpenFile(new FileInfo(files.First())).SelectAll();
|
||||
files[0] = files[0] + " (opened in code window)";
|
||||
}
|
||||
}
|
||||
|
||||
private string GetConversionSummary(IReadOnlyCollection<string> files, IReadOnlyCollection<string> errors)
|
||||
|
@ -90,7 +90,7 @@ namespace CodeConverter.VsExtension
|
|||
var summaryOfSuccesses = "";
|
||||
if (files.Any()) {
|
||||
introSummary = "Code conversion completed";
|
||||
summaryOfSuccesses = "The following files have been written to disk (but are not part of the solution):"
|
||||
summaryOfSuccesses = "The following files have been written to disk:"
|
||||
+ Environment.NewLine + "* " + string.Join(Environment.NewLine + "* ", relativeFilePaths);
|
||||
}
|
||||
|
||||
|
@ -103,7 +103,7 @@ namespace CodeConverter.VsExtension
|
|||
+ Environment.NewLine
|
||||
+ string.Join(Environment.NewLine, errors);
|
||||
} else {
|
||||
WriteStatusBarText(introSummary);
|
||||
WriteStatusBarText(introSummary + " - see output window for details");
|
||||
return introSummary
|
||||
+ Environment.NewLine
|
||||
+ summaryOfSuccesses;
|
||||
|
|
|
@ -18,7 +18,7 @@ using Microsoft.VisualStudio.TextManager.Interop;
|
|||
|
||||
namespace CodeConverter.VsExtension
|
||||
{
|
||||
static class VisualStudioInteraction
|
||||
internal static class VisualStudioInteraction
|
||||
{
|
||||
public static VsDocument GetSingleSelectedItemOrDefault()
|
||||
{
|
||||
|
@ -76,24 +76,13 @@ namespace CodeConverter.VsExtension
|
|||
return Dte.ItemOperations.OpenFile(fileInfo.FullName, EnvDTE.Constants.vsViewKindTextView);
|
||||
}
|
||||
|
||||
public static Window NewTextWindow(string windowTitle, string windowContents)
|
||||
public static void SelectAll(this Window window)
|
||||
{
|
||||
var newTextWindow = Dte.ItemOperations.NewFile(Name: windowTitle, ViewKind: EnvDTE.Constants.vsViewKindTextView);
|
||||
newTextWindow.AppendLine(windowContents);
|
||||
return newTextWindow;
|
||||
}
|
||||
|
||||
private static void AppendLine(this Window newTextWindow, string textToAppend)
|
||||
{
|
||||
var textSelection = (TextSelection) newTextWindow.Document.Selection;
|
||||
textSelection.EndOfDocument();
|
||||
textSelection.Text = Environment.NewLine + textToAppend;
|
||||
textSelection.StartOfDocument();
|
||||
((TextSelection)window.Document.Selection).SelectAll();
|
||||
}
|
||||
|
||||
private static DTE2 Dte => Package.GetGlobalService(typeof(DTE)) as DTE2;
|
||||
|
||||
|
||||
public static IReadOnlyCollection<Project> GetSelectedProjects(string projectExtension)
|
||||
{
|
||||
var items = GetSelectedSolutionExplorerItems<Solution>()
|
||||
|
@ -144,5 +133,40 @@ namespace CodeConverter.VsExtension
|
|||
OLEMSGBUTTON.OLEMSGBUTTON_OK,
|
||||
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
|
||||
}
|
||||
|
||||
public static class OutputWindow
|
||||
{
|
||||
private const string PaneName = "Code Converter";
|
||||
private static readonly Guid PaneGuid = new Guid("44F575C6-36B5-4CDB-AAAE-E096E6A446BF");
|
||||
private static readonly Lazy<IVsOutputWindowPane> _outputPane = new Lazy<IVsOutputWindowPane>(CreateOutputPane);
|
||||
|
||||
private static IVsOutputWindow GetOutputWindow()
|
||||
{
|
||||
IServiceProvider serviceProvider = new ServiceProvider(Dte as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
|
||||
return serviceProvider.GetService(typeof(SVsOutputWindow)) as IVsOutputWindow;
|
||||
}
|
||||
private static IVsOutputWindowPane CreateOutputPane()
|
||||
{
|
||||
Guid generalPaneGuid = PaneGuid;
|
||||
IVsOutputWindowPane pane;
|
||||
var outputWindow = GetOutputWindow();
|
||||
outputWindow.GetPane(ref generalPaneGuid, out pane);
|
||||
|
||||
if (pane == null) {
|
||||
outputWindow.CreatePane(ref generalPaneGuid, PaneName, 1, 1);
|
||||
outputWindow.GetPane(ref generalPaneGuid, out pane);
|
||||
}
|
||||
|
||||
return pane;
|
||||
}
|
||||
|
||||
public static void ShowMessageToUser(string message)
|
||||
{
|
||||
Dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput).Visible = true;
|
||||
var ourOutputPane = _outputPane.Value;
|
||||
ourOutputPane.OutputString(message);
|
||||
ourOutputPane.Activate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче