void AddCommentCommands() { var token = _Context.Token; var triviaList = token.HasLeadingTrivia ? token.LeadingTrivia : token.HasTrailingTrivia ? token.TrailingTrivia : default; var lineComment = new SyntaxTrivia(); if (triviaList.Equals(SyntaxTriviaList.Empty) == false && triviaList.FullSpan.Contains(View.Selection.Start.Position)) { lineComment = triviaList.FirstOrDefault(i => i.IsLineComment()); } if (lineComment.RawKind != 0) { AddEditorCommand(MyToolBar, KnownImageIds.UncommentCode, "Edit.UncommentSelection", "Uncomment selection"); } else { AddCommand(MyToolBar, KnownImageIds.CommentCode, "Comment selection\nRight click: Comment line", ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } TextEditorHelper.ExecuteEditorCommand("Edit.CommentSelection"); }); } }
void AddCommentCommands() { AddCommand(MyToolBar, IconIds.Comment, R.CMD_CommentSelection, ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } TextEditorHelper.ExecuteEditorCommand("Edit.CommentSelection"); }); if (View.TryGetFirstSelectionSpan(out var ss) && ss.Length < 0x2000) { foreach (var t in _Context.Compilation.DescendantTrivia(ss.ToTextSpan())) { if (t.IsKind(SyntaxKind.SingleLineCommentTrivia)) { AddEditorCommand(MyToolBar, IconIds.Uncomment, "Edit.UncommentSelection", R.CMD_UncommentSelection); return; } } } var token = _Context.Token; var triviaList = token.HasLeadingTrivia ? token.LeadingTrivia : token.HasTrailingTrivia ? token.TrailingTrivia : default; var lineComment = new SyntaxTrivia(); if (triviaList.Equals(SyntaxTriviaList.Empty) == false && triviaList.FullSpan.Contains(View.Selection.Start.Position)) { lineComment = triviaList.FirstOrDefault(i => i.IsLineComment()); } if (lineComment.RawKind != 0) { AddEditorCommand(MyToolBar, IconIds.Uncomment, "Edit.UncommentSelection", R.CMD_UncommentSelection); } }
public static void Initialize() { Command.CodeWindowScreenshot.Register(Execute, (s, args) => { ThreadHelper.ThrowIfNotOnUIThread(); ((OleMenuCommand)s).Visible = TextEditorHelper.GetActiveWpfDocumentView() != null; }); }
void OnChanged(object sender, TextContentChangedEventArgs e) { if (TextEditorHelper.AnyTextChanges(e.Before.Version, e.After.Version)) { ScanBuffer(e.After); } }
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); } } } }
/// <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()); }
List <CommandItem> GetExpandSelectionCommands(CommandContext ctx) { var r = new List <CommandItem>(); var duplicate = ctx.RightClick; var node = _Context.NodeIncludeTrivia; while (node != null) { if (node.FullSpan.Contains(ctx.View.Selection, false)) { var nodeKind = node.Kind(); if ((nodeKind.IsSyntaxBlock() || nodeKind.IsDeclaration() || nodeKind == SyntaxKind.VariableDeclarator) && nodeKind != SyntaxKind.VariableDeclaration) { var n = node; r.Add(new CommandItem(CodeAnalysisHelper.GetImageId(n), (duplicate ? "Duplicate " : "Select ") + nodeKind.GetSyntaxBrief() + " " + n.GetDeclarationSignature(), ctx2 => { ctx2.View.SelectNode(n, Keyboard.Modifiers == ModifierKeys.Shift ^ Config.Instance.SmartBarOptions.MatchFlags(SmartBarOptions.ExpansionIncludeTrivia) || n.Span.Contains(ctx2.View.Selection, false) == false); if (Keyboard.Modifiers == ModifierKeys.Control) { TextEditorHelper.ExecuteEditorCommand("Edit.Copy"); } if (duplicate) { TextEditorHelper.ExecuteEditorCommand("Edit.Duplicate"); } })); } } node = node.Parent; } r.Add(new CommandItem(IconIds.SelectAll, R.CMD_SelectAll, ctrl => ctrl.ToolTip = R.CMDT_SelectAll, ctx2 => TextEditorHelper.ExecuteEditorCommand("Edit.SelectAll"))); return(r); }
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); } } } }
protected override void AddCommands(CancellationToken cancellationToken) { base.AddCommands(cancellationToken); AddCommand(MyToolBar, KnownImageIds.GoToDefinition, "Go to definition", ctx => { TextEditorHelper.ExecuteEditorCommand("Edit.GoToDefinition", GetCurrentWord(ctx.View)); }); AddCommand(MyToolBar, KnownImageIds.GoToDeclaration, "Go to declaration", ctx => { TextEditorHelper.ExecuteEditorCommand("Edit.GoToDeclaration", GetCurrentWord(ctx.View)); }); var mode = CodistPackage.DebuggerStatus; if (mode != DebuggerStatus.Running) { //AddEditorCommand(MyToolBar, KnownImageIds.IntellisenseLightBulb, "EditorContextMenus.CodeWindow.QuickActionsForPosition", "Quick actions for position"); AddCommand(MyToolBar, KnownImageIds.CommentCode, "Comment selection\nRight click: Comment line", ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } TextEditorHelper.ExecuteEditorCommand("Edit.CommentSelection"); }); AddEditorCommand(MyToolBar, KnownImageIds.UncommentCode, "Edit.UncommentSelection", "Uncomment selection"); } else if (mode != DebuggerStatus.Design) { AddCommands(MyToolBar, KnownImageIds.BreakpointEnabled, "Debugger...\nLeft click: Toggle breakpoint\nRight click: Debugger menu...", ctx => TextEditorHelper.ExecuteEditorCommand("Debug.ToggleBreakpoint"), ctx => DebugCommands); } }
protected override void AddCommands(CancellationToken cancellationToken) { base.AddCommands(cancellationToken); AddCommand(MyToolBar, IconIds.GoToDefinition, R.CMD_GoToDefinition, ctx => { TextEditorHelper.ExecuteEditorCommand("Edit.GoToDefinition", GetCurrentWord(ctx.View)); }); AddCommand(MyToolBar, IconIds.GoToDeclaration, R.CMD_GoToDeclaration, ctx => { TextEditorHelper.ExecuteEditorCommand("Edit.GoToDeclaration", GetCurrentWord(ctx.View)); }); AddCommand(MyToolBar, IconIds.FindReference, R.CMD_FindAllReferences, ctx => { TextEditorHelper.ExecuteEditorCommand("Edit.FindAllReferences"); }); var mode = CodistPackage.DebuggerStatus; if (mode != DebuggerStatus.Running) { //AddEditorCommand(MyToolBar, KnownImageIds.IntellisenseLightBulb, "EditorContextMenus.CodeWindow.QuickActionsForPosition", "Quick actions for position"); AddCommand(MyToolBar, IconIds.Comment, R.CMD_CommentSelection, ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } TextEditorHelper.ExecuteEditorCommand("Edit.CommentSelection"); }); AddEditorCommand(MyToolBar, IconIds.Uncomment, "Edit.UncommentSelection", R.CMD_UncommentSelection); } else if (mode != DebuggerStatus.Design) { AddCommands(MyToolBar, IconIds.ToggleBreakpoint, R.CMD_Debugger, ctx => TextEditorHelper.ExecuteEditorCommand("Debug.ToggleBreakpoint"), ctx => DebugCommands); } }
void AddFindAndReplaceCommands() { AddCommands(ToolBar, IconIds.FindNext, R.CMD_FindReplace, ctx => { ThreadHelper.ThrowIfNotOnUIThread(); string t = ctx.View.GetFirstSelectionText(); if (t.Length == 0) { return; } ctx.KeepToolBar(false); if (Keyboard.Modifiers == ModifierKeys.Alt) { TextEditorHelper.ExecuteEditorCommand("Edit.InsertNextMatchingCaret"); return; } var r = ctx.TextSearchService.Find(ctx.View.Selection.StreamSelectionSpan.End.Position, t, Keyboard.Modifiers == ModifierKeys.Control ? FindOptions.MatchCase | FindOptions.Wrap : Keyboard.Modifiers == ModifierKeys.Shift ? FindOptions.Wrap | FindOptions.WholeWord : FindOptions.None); if (r.HasValue) { ctx.View.SelectSpan(r.Value); ctx.KeepToolBar(true); } else { ctx.HideToolBar(); } }, ctx => __FindAndReplaceCommands.Concat( Config.Instance.SearchEngines.ConvertAll(s => new CommandItem(IconIds.SearchWebSite, R.CMD_SearchWith.Replace("<NAME>", s.Name), c => SearchSelection(s.Pattern, c)))) ); }
public static void Initialize() { Command.GetContentType.Register(Execute, (s, args) => { ThreadHelper.ThrowIfNotOnUIThread(); ((OleMenuCommand)s).Visible = Config.Instance.DeveloperOptions.MatchFlags(DeveloperOptions.ShowDocumentContentType) && TextEditorHelper.GetActiveWpfDocumentView() != null; }); }
void AddXmlDocCommands() { AddCommand(MyToolBar, KnownImageIds.MarkupTag, "Tag XML Doc with <c>", ctx => { SurroundWith(ctx, "<c>", "</c>", true); }); AddCommand(MyToolBar, KnownImageIds.GoToNext, "Tag XML Doc with <see> or <paramref>", ctx => { // updates the semantic model before executing the command, // for it could be modified by external editor commands or duplicated document windows if (UpdateSemanticModel() == false) { return; } ctx.View.Edit((view, edit) => { foreach (var item in view.Selection.SelectedSpans) { var t = item.GetText(); var d = _Context.GetNode(item.Start, false, false).GetAncestorOrSelfDeclaration(); if (d != null) { var mp = (d as BaseMethodDeclarationSyntax).FindParameter(t); if (mp != null) { edit.Replace(item, "<paramref name=\"" + t + "\"/>"); continue; } var tp = d.FindTypeParameter(t); if (tp != null) { edit.Replace(item, "<typeparamref name=\"" + t + "\"/>"); continue; } } edit.Replace(item, (SyntaxFacts.GetKeywordKind(t) != SyntaxKind.None ? "<see langword=\"" : "<see cref=\"") + t + "\"/>"); } }); }); AddCommand(MyToolBar, KnownImageIds.ParagraphHardReturn, "Tag XML Doc with <para>", ctx => { SurroundWith(ctx, "<para>", "</para>", false); }); AddCommand(MyToolBar, KnownImageIds.Bold, "Tag XML Doc with HTML <b>", ctx => { SurroundWith(ctx, "<b>", "</b>", true); }); AddCommand(MyToolBar, KnownImageIds.Italic, "Tag XML Doc with HTML <i>", ctx => { SurroundWith(ctx, "<i>", "</i>", true); }); AddCommand(MyToolBar, KnownImageIds.Underline, "Tag XML Doc with HTML <u>", ctx => { SurroundWith(ctx, "<u>", "</u>", true); }); AddCommand(MyToolBar, KnownImageIds.CommentCode, "Comment selection\nRight click: Comment line", ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } TextEditorHelper.ExecuteEditorCommand("Edit.CommentSelection"); }); }
void AddCopyCommand() { AddCommand(ToolBar, KnownImageIds.Copy, "Copy selected text\nRight click: Copy line", ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } TextEditorHelper.ExecuteEditorCommand("Edit.Copy"); }); }
void AddCutCommand() { AddCommand(ToolBar, IconIds.Cut, R.CMD_CutSelectedText, ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } TextEditorHelper.ExecuteEditorCommand("Edit.Cut"); }); }
void AddRenameCommand(SyntaxNode node) { if (_Symbol.ContainingAssembly.GetSourceType() != AssemblySource.Metadata) { AddCommand(MyToolBar, IconIds.Rename, R.CMD_RenameSymbol, ctx => { ctx.KeepToolBar(false); TextEditorHelper.ExecuteEditorCommand("Refactor.Rename"); }); } }
public static void Initialize(AsyncPackage package) { var menuItem = new OleMenuCommand(Execute, new CommandID(CommandSet, CommandId)); menuItem.BeforeQueryStatus += (s, args) => { ThreadHelper.ThrowIfNotOnUIThread(); var c = s as OleMenuCommand; c.Enabled = TextEditorHelper.GetActiveWpfDocumentView() != null; }; CodistPackage.MenuService.AddCommand(menuItem); }
void AddDuplicateCommand() { AddCommand(ToolBar, IconIds.Duplicate, R.CMD_DuplicateSelection, ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } ctx.KeepToolBar(true); TextEditorHelper.ExecuteEditorCommand("Edit.Duplicate"); }); }
void AddDuplicateCommand() { AddCommand(ToolBar, KnownImageIds.CopyItem, "Duplicate selection\nRight click: Duplicate line", ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } ctx.KeepToolBar(true); TextEditorHelper.ExecuteEditorCommand("Edit.Duplicate"); }); }
protected void AddEditorCommand(ToolBar toolBar, int imageId, string command, string tooltip, string command2) { ThreadHelper.ThrowIfNotOnUIThread(); if (CodistPackage.DTE.Commands.Item(command).IsAvailable) { AddCommand(toolBar, imageId, tooltip, (ctx) => { TextEditorHelper.ExecuteEditorCommand(ctx.RightClick ? command2 : command); //View.Selection.Clear(); }); } }
async void OnChanged(object sender, TextContentChangedEventArgs e) { try { if (TextEditorHelper.AnyTextChanges(e.Before.Version, e.After.Version)) { await ScanBufferAsync(e.After); } } catch (OperationCanceledException) { // ignores cancellation } }
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; } var t = docWindow.TextBuffer.ContentType; using (var b = ReusableStringBuilder.AcquireDefault(100)) { var sb = b.Resource; var d = docWindow.TextBuffer.GetTextDocument(); sb.Append(R.T_ContentTypeOfDocument); if (d != null) { sb.Append(System.IO.Path.GetFileName(d.FilePath)); } sb.AppendLine(); sb.AppendLine(); var h = new HashSet <IContentType>(); ShowContentType(t, sb, h, 0); System.Windows.Forms.MessageBox.Show(sb.ToString(), nameof(Codist)); } void ShowContentType(IContentType type, StringBuilder sb, HashSet <IContentType> h, int indent) { sb.Append(' ', indent) .Append(type.DisplayName); if (type.DisplayName != type.TypeName) { sb.Append('(') .Append(type.TypeName) .Append(')'); } sb.AppendLine(); foreach (var bt in type.BaseTypes) { if (h.Add(bt)) { ShowContentType(bt, sb, h, indent + 2); } } } }
static void ExecuteAndFind(CommandContext ctx, string command, string text) { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(false); } ctx.KeepToolBar(false); TextEditorHelper.ExecuteEditorCommand(command); if (Keyboard.Modifiers == ModifierKeys.Control && FindNext(ctx, text) == false) { ctx.HideToolBar(); } }
SnapshotSpan WrapWith(string prefix, string suffix, bool selectModified) { string s = View.GetFirstSelectionText(); var firstModified = View.WrapWith(prefix, suffix); if (s != null && Keyboard.Modifiers.MatchFlags(ModifierKeys.Control | ModifierKeys.Shift) && View.FindNext(_TextSearch, s, TextEditorHelper.GetFindOptionsFromKeyboardModifiers()) == false) { // } else if (selectModified) { View.SelectSpan(firstModified); } return(firstModified); }
void AddRefactorCommands(SyntaxNode node) { if (_Symbol.ContainingAssembly.GetSourceType() != AssemblySource.Metadata) { AddCommand(MyToolBar, KnownImageIds.Rename, "Rename symbol", ctx => { ctx.KeepToolBar(false); TextEditorHelper.ExecuteEditorCommand("Refactor.Rename"); }); } if (node is ParameterSyntax && node.Parent is ParameterListSyntax) { AddEditorCommand(MyToolBar, KnownImageIds.ReorderParameters, "Refactor.ReorderParameters", "Reorder parameters"); } if (node.IsKind(SyntaxKind.ClassDeclaration) || node.IsKind(SyntaxKind.StructDeclaration)) { AddEditorCommand(MyToolBar, KnownImageIds.ExtractInterface, "Refactor.ExtractInterface", "Extract interface"); } }
static void RefreshWindow(object sender, EventArgs e) { if (_Window != null && _Window.IsClosing == false && _Window.IsVisible) { var b = _Window.RestoreBounds; var s = _Window.WindowState; _Window.Close(); if (_Window.IsClosing == false) { CreateWindow(TextEditorHelper.GetActiveWpfDocumentView()); _Window.Top = b.Top; _Window.Left = b.Left; _Window.Width = b.Width; _Window.Height = b.Height; _Window.WindowState = s; } _Window.Show(); } }
List <CommandItem> GetMarkerCommands(CommandContext arg) { var r = new List <CommandItem>(3); var symbol = _Symbol; if (symbol.Kind == SymbolKind.Method) { var ctor = symbol as IMethodSymbol; if (ctor != null && ctor.MethodKind == MethodKind.Constructor) { symbol = ctor.ContainingType; } } r.Add(new CommandItem(IconIds.MarkSymbol, R.CMD_Mark.Replace("<NAME>", symbol.Name), AddHighlightMenuItems, null)); if (Taggers.SymbolMarkManager.Contains(symbol)) { r.Add(new CommandItem(IconIds.UnmarkSymbol, R.CMD_Unmark.Replace("<NAME>", symbol.Name), ctx => { UpdateSemanticModel(); if (_Symbol != null && Taggers.SymbolMarkManager.Remove(_Symbol)) { Config.Instance.FireConfigChangedEvent(Features.SyntaxHighlight); return; } })); } else if (Taggers.SymbolMarkManager.HasBookmark) { r.Add(CreateCommandMenu(IconIds.UnmarkSymbol, R.CMD_UnmarkSymbol, symbol, "No symbol marked", (ctx, m, s) => { foreach (var item in Taggers.SymbolMarkManager.MarkedSymbols) { m.Items.Add(new CommandMenuItem(this, new CommandItem(item.ImageId, item.DisplayString, _ => { Taggers.SymbolMarkManager.Remove(item); Config.Instance.FireConfigChangedEvent(Features.SyntaxHighlight); }))); } })); } r.Add(new CommandItem(IconIds.ToggleBreakpoint, R.CMD_ToggleBreakpoint, _ => TextEditorHelper.ExecuteEditorCommand("Debug.ToggleBreakpoint"))); r.Add(new CommandItem(IconIds.ToggleBookmark, R.CMD_ToggleBookmark, _ => TextEditorHelper.ExecuteEditorCommand("Edit.ToggleBookmark"))); return(r); }
static void ExecuteAndFind(CommandContext ctx, string command) { ThreadHelper.ThrowIfNotOnUIThread(); if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(false); } string t = null; if (Keyboard.Modifiers == ModifierKeys.Control && ctx.View.Selection.IsEmpty == false) { t = ctx.View.TextSnapshot.GetText(ctx.View.Selection.SelectedSpans[0]); } TextEditorHelper.ExecuteEditorCommand(command); if (t != null) { var p = (CodistPackage.DTE.ActiveDocument.Object() as EnvDTE.TextDocument).Selection; if (p != null && p.FindText(t, 0)) { ctx.KeepToolbar(); } } }
void DecorateClassificationTypes() { if (_ClassificationFormatMap.IsInBatchUpdate) { return; } _ClassificationFormatMap.BeginBatchUpdate(); var defaultSize = _ClassificationFormatMap.DefaultTextProperties.FontRenderingEmSize; foreach (var item in _ClassificationFormatMap.CurrentPriorityOrder) { StyleBase style; TextFormattingRunProperties textFormatting; if (item == null || (style = TextEditorHelper.GetStyle(item.Classification)) == null || (textFormatting = TextEditorHelper.GetBackupFormatting(item.Classification)) == null) { continue; } _ClassificationFormatMap.SetTextProperties(item, SetProperties(textFormatting, style, defaultSize)); } _ClassificationFormatMap.EndBatchUpdate(); Debug.WriteLine("Decorated"); }
protected static bool FindNext(CommandContext ctx, string t) { return(ctx.View.FindNext(ctx.TextSearchService, t, TextEditorHelper.GetFindOptionsFromKeyboardModifiers())); }