/// <summary> /// Shows the tool window when the menu item is clicked. /// </summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event args.</param> internal static void Execute(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); if (Config.Instance.Features.MatchFlags(Features.SyntaxHighlight) == false) { CodistPackage.ShowMessageBox(R.T_SyntaxHighlightDisabled, R.CMD_ConfigureSyntaxHighlight, true); return; } if (_Window == null || _Window.IsVisible == false) { var v = TextEditorHelper.GetActiveWpfDocumentView(); if (v == null) { CodistPackage.ShowMessageBox(R.T_CustomizeSyntaxHighlightNote, R.CMD_ConfigureSyntaxHighlight, true); return; } CreateWindow(v); } _Window.Show(); //// Get the instance number 0 of this tool window. This window is single instance so this instance //// is actually the only one. //// The last flag is set to true so that if the tool window does not exists it will be created. //var window = CodistPackage.Instance.FindToolWindow(typeof(SyntaxCustomizerWindow), 0, true); //if ((null == window) || (null == window.Frame)) { // throw new NotSupportedException("Cannot create SyntaxCustomizerWindow"); //} //var windowFrame = (IVsWindowFrame)window.Frame; //Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show()); }
static void Execute(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); var doc = CodistPackage.DTE.ActiveDocument; if (doc == null) { return; } var docWindow = TextEditorHelper.GetActiveWpfDocumentView(); if (docWindow == null) { return; } using (var f = new System.Windows.Forms.SaveFileDialog { Filter = "PNG images (*.png)|*.png", AddExtension = true, Title = "Please specify the location of the screenshot file", FileName = System.IO.Path.GetFileNameWithoutExtension(doc.Name) + ".png" }) { if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK) { try { var g = docWindow.VisualElement.GetParent <System.Windows.Controls.Grid>(); WpfHelper.ScreenShot(g, f.FileName, (int)g.ActualWidth, (int)g.ActualHeight); } catch (Exception ex) { CodistPackage.ShowErrorMessageBox("Failed to save screenshot for " + doc.Name + "\n" + ex.Message, null, true); } } } }
static void Execute(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); var doc = CodistPackage.DTE.ActiveDocument; if (doc == null) { return; } var docWindow = TextEditorHelper.GetActiveWpfDocumentView(); if (docWindow == null) { return; } using (var f = new System.Windows.Forms.SaveFileDialog { Filter = R.T_PngFileFilter, AddExtension = true, Title = R.T_SpecifyScreenshotLocation, FileName = System.IO.Path.GetFileNameWithoutExtension(doc.Name) + ".png" }) { if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK) { try { var g = docWindow.VisualElement.GetParent <System.Windows.Controls.Grid>(); WpfHelper.ScreenShot(g, f.FileName, (int)g.ActualWidth, (int)g.ActualHeight); } catch (Exception ex) { CodistPackage.ShowMessageBox(R.T_FailedToSaveScreenshot.Replace("<NAME>", doc.Name) + Environment.NewLine + ex.Message, null, true); } } } }
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread static void Execute(object sender, EventArgs e) { var item = GetSelectedProjectItem(); if (item == null) { return; } const int YesButton = 6; if (item.Saved == false && YesButton == VsShellUtilities.ShowMessageBox(CodistPackage.Instance, item.Name + " is not saved.\nDiscard its changes?", "Increment Version", OLEMSGICON.OLEMSGICON_QUERY, OLEMSGBUTTON.OLEMSGBUTTON_YESNO, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_SECOND)) { return; } string message; bool error = IncrementVersion(item, out message) == false; CodistPackage.ShowMessageBox(message, "Increment Version", error); }
static void Execute(object sender, EventArgs e) { var item = GetSelectedProjectItem(); if (item == null) { return; } const int YesButton = 6; if (item.Saved == false && YesButton == VsShellUtilities.ShowMessageBox(CodistPackage.Instance, item.Name + " is not saved.\nDiscard its changes?", "Increment Version", OLEMSGICON.OLEMSGICON_QUERY, OLEMSGBUTTON.OLEMSGBUTTON_YESNO, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_SECOND)) { return; } string message; bool error = false; const string Namespace = "http://schemas.microsoft.com/developer/vsx-schema/2011"; try { var fileName = item.FileNames[0]; var doc = XDocument.Load(fileName); var v = doc.Root.Element(XName.Get("Metadata", Namespace))?.Element(XName.Get("Identity", Namespace))?.Attribute("Version"); if (v != null && Version.TryParse(v.Value, out var ver)) { v.Value = new Version(Math.Max(ver.Major, 1), Math.Max(ver.Minor, 0), Math.Max(ver.Build, 0), Math.Max(ver.Revision, 0) + 1).ToString(); doc.Save(fileName); message = "Incremented version to " + v.Value; } else { message = "Version not found or verion number invalid in file " + fileName; error = true; } } catch (Exception ex) { message = ex.Message; error = true; } CodistPackage.ShowErrorMessageBox(message, "Increment Version", error); }
protected override void AddCommands(CancellationToken cancellationToken) { base.AddCommands(cancellationToken); string t = TryGetPath(View); if (t == null) { return; } if (File.Exists(t)) { AddCommand(MyToolBar, IconIds.Open, R.CMD_OpenOrExecuteFile, ctx => { TryRun(TryGetPath(View)); }); AddCommand(MyToolBar, IconIds.OpenFolder, R.CMD_OpenFolder, ctx => { var s = TryGetPath(View); if (s != null) { Process.Start("Explorer.exe", "/select,\"" + s + "\""); } }); if (IsFileTypeRegisteredInVS(t)) { AddCommand(MyToolBar, IconIds.OpenWithVisualStudio, R.CMD_OpenWithVS, ctx => { var s = TryGetPath(View); if (s != null && IsFileTypeRegisteredInVS(s)) { CodistPackage.DTE.OpenFile(s, 1, 1); } }); } } else if (Directory.Exists(t)) { AddCommand(MyToolBar, IconIds.OpenFolder, R.CMD_OpenFolder, ctx => { TryRun(TryGetPath(View)); }); } string TryGetPath(ITextView view) { if (view.TryGetFirstSelectionSpan(out var span) && span.Length < 255) { var text = view.TextSnapshot.GetText(span).TrimStart(); try { if (Path.IsPathRooted(text)) { return(text.Replace(@"\\", @"\")); } } catch (ArgumentException) { // ignore } } return(null); } void TryRun(string path) { if (path == null) { return; } try { Process.Start(path); } catch (System.ComponentModel.Win32Exception ex) { CodistPackage.ShowErrorMessageBox(ex.Message, null, true); } catch (FileNotFoundException) { // ignore } } bool IsFileTypeRegisteredInVS(string fileName) { try { return(ServicesHelper.Instance.FileExtensionRegistry.GetContentTypeForExtension(Path.GetExtension(fileName)).TypeName != "UNKNOWN"); } catch (ArgumentException) { return(false); } } }
protected override void AddCommands(CancellationToken cancellationToken) { const int PathIsFile = 1, PathIsDirectory = 2; base.AddCommands(cancellationToken); int p; string t; try { t = TryGetPathFromView(View, out p); } catch (Exception ex) { CodistPackage.ShowMessageBox(ex.Message, null, true); return; } switch (p) { case PathIsFile: AddCommand(MyToolBar, IconIds.Open, R.CMD_OpenOrExecuteFile, ctx => { TryRun(t); }); AddCommand(MyToolBar, IconIds.OpenFolder, R.CMD_OpenFolder, ctx => { Process.Start(new ProcessStartInfo("Explorer.exe", "/select,\"" + t + "\"") { WorkingDirectory = Environment.SystemDirectory }); }); switch (Path.GetExtension(t).ToLowerInvariant()) { case ".exe": case ".bat": case ".cmd": case ".py": case ".js": case ".vbs": case ".com": case ".wsf": AddCommand(MyToolBar, IconIds.OpenWithCmd, R.CMD_OpenFileInCmd, ctx => { Process.Start(new ProcessStartInfo(Environment.SystemDirectory + "\\cmd.exe", "/K \"" + t + "\"") { WorkingDirectory = Path.GetDirectoryName(t) }); }); break; } if (IsFileTypeRegisteredInVS(t)) { AddCommand(MyToolBar, IconIds.OpenWithVisualStudio, R.CMD_OpenWithVS, ctx => { TextEditorHelper.OpenFile(t); }); } return; case PathIsDirectory: AddCommand(MyToolBar, IconIds.OpenFolder, R.CMD_OpenFolder, ctx => { TryRun(t); }); AddCommand(MyToolBar, IconIds.OpenWithCmd, R.CMD_OpenFolderWithCmd, ctx => { Process.Start(new ProcessStartInfo(Environment.SystemDirectory + "\\cmd.exe") { WorkingDirectory = t }); }); return; } string TryGetPathFromView(ITextView view, out int pathKind) { if (view.TryGetFirstSelectionSpan(out var span) && span.Length < 255) { var path = TryGetPath(view.TextSnapshot.GetText(span).TrimStart(), out pathKind); if (path != null) { return(path); } // assume the selection is the file name, try get its full path var ts = view.TextSnapshot; var line = ts.GetLineFromPosition(span.Start); var eol = line.End.Position; var e = span.End < eol ? span.End : eol; // find start of path int ss = span.Start - line.Start > 255 ? span.Start - 255 : line.Start; var text = ts.GetText(ss, eol - ss); int s = span.Start - ss; if (s >= text.Length) { return(null); } s = text.LastIndexOf(Path.VolumeSeparatorChar, s, s); if (s < 1 || Char.IsLetterOrDigit(text[--s]) == false || s > 1 && Char.IsLetterOrDigit(text[s - 1])) { return(null); } s += ss; if (s > e) { e = eol; } // detect end of path char c; int i; for (i = e; i < eol; i++) { switch (c = ts[i]) { case '.': case '_': case '$': continue; } if (Char.IsLetterOrDigit(c)) { continue; } break; } e = i; return(TryGetPath(ts.GetText(s, e - s), out pathKind)); } pathKind = 0; return(null); } string TryGetPath(string path, out int pathKind) { if (path.Length == 0) { pathKind = 0; return(null); } path = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); try { if (Path.IsPathRooted(path)) { path = path.Replace(@"\\", @"\"); if (File.Exists(path)) { pathKind = PathIsFile; return(path); } if (Directory.Exists(path)) { pathKind = PathIsDirectory; return(path); } } } catch (ArgumentException) { // ignore } pathKind = 0; return(null); } void TryRun(string path) { if (path == null) { return; } try { Process.Start(path); } catch (System.ComponentModel.Win32Exception ex) { CodistPackage.ShowMessageBox(ex.Message, null, true); } catch (FileNotFoundException) { // ignore } } bool IsFileTypeRegisteredInVS(string fileName) { try { return(ServicesHelper.Instance.FileExtensionRegistry.GetContentTypeForExtension(Path.GetExtension(fileName)).TypeName != "UNKNOWN"); } catch (ArgumentException) { return(false); } } }