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); } }
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 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"); }); } }
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); }
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); } }
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)))) ); }
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 AddRenameCommand(SyntaxNode node) { if (_Symbol.ContainingAssembly.GetSourceType() != AssemblySource.Metadata) { AddCommand(MyToolBar, IconIds.Rename, R.CMD_RenameSymbol, ctx => { ctx.KeepToolBar(false); TextEditorHelper.ExecuteEditorCommand("Refactor.Rename"); }); } }
void AddCutCommand() { AddCommand(ToolBar, IconIds.Cut, R.CMD_CutSelectedText, ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } TextEditorHelper.ExecuteEditorCommand("Edit.Cut"); }); }
void AddCopyCommand() { AddCommand(ToolBar, KnownImageIds.Copy, "Copy selected text\nRight click: Copy line", ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } TextEditorHelper.ExecuteEditorCommand("Edit.Copy"); }); }
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(); }); } }
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"); }); }
void AddDuplicateCommand() { AddCommand(ToolBar, IconIds.Duplicate, R.CMD_DuplicateSelection, ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } ctx.KeepToolBar(true); TextEditorHelper.ExecuteEditorCommand("Edit.Duplicate"); }); }
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(); } }
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"); } }
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(); } } }
CommandItem[] GetDebugCommands(CommandContext ctx) { return(new CommandItem[] { new CommandItem("Toggle breakpoint", KnownImageIds.BreakpointEnabled, c => c.ToolTip = "Toggle breakpoint", c => TextEditorHelper.ExecuteEditorCommand("Debug.ToggleBreakpoint")), new CommandItem("Delete breakpoints", KnownImageIds.DeleteBreakpoint, c => c.ToolTip = "Delete all breakpoints", c => TextEditorHelper.ExecuteEditorCommand("Debug.DeleteAllBreakpoints")) }); }
void AddXmlDocCommands() { AddCommand(MyToolBar, IconIds.TagCode, R.CMD_TagXmlDocC, ctx => { WrapWith(ctx, "<c>", "</c>", true); }); AddCommand(MyToolBar, IconIds.TagXmlDocSee, R.CMD_TagXmlDocSee, 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) => { ctx.KeepToolBar(true); string t = null; foreach (var item in view.Selection.SelectedSpans) { t = item.GetText(); var d = _Context.GetNode(item.Start, false, false).GetAncestorOrSelfDeclaration(); if (d != null) { if (((d as BaseMethodDeclarationSyntax)?.ParameterList ?? (d as DelegateDeclarationSyntax)?.ParameterList) ?.Parameters.Any(p => p.Identifier.Text == t) == true) { edit.Replace(item, "<paramref name=\"" + t + "\"/>"); continue; } if (d.FindTypeParameter(t) != null) { edit.Replace(item, "<typeparamref name=\"" + t + "\"/>"); continue; } } edit.Replace(item, (SyntaxFacts.GetKeywordKind(t) != SyntaxKind.None ? "<see langword=\"" : "<see cref=\"") + t + "\"/>"); } if (t != null && Keyboard.Modifiers.MatchFlags(ModifierKeys.Control | ModifierKeys.Shift) && FindNext(ctx, t) == false) { ctx.HideToolBar(); } }); }); AddCommand(MyToolBar, IconIds.TagXmlDocPara, R.CMD_TagXmlDocPara, ctx => { WrapWith(ctx, "<para>", "</para>", false); }); AddCommand(MyToolBar, IconIds.TagBold, R.CMD_TagXmlDocB, ctx => { WrapWith(ctx, "<b>", "</b>", true); }); AddCommand(MyToolBar, IconIds.TagItalic, R.CMD_TagXmlDocI, ctx => { WrapWith(ctx, "<i>", "</i>", true); }); AddCommand(MyToolBar, IconIds.TagUnderline, R.CMD_TagXmlDocU, ctx => { WrapWith(ctx, "<u>", "</u>", true); }); AddCommand(MyToolBar, IconIds.Comment, R.CMD_CommentSelection, ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } TextEditorHelper.ExecuteEditorCommand("Edit.CommentSelection"); }); }
async Task <IEnumerable <CommandItem> > GetReferenceCommandsAsync(CommandContext ctx) { var r = new List <CommandItem>(); var symbol = await SymbolFinder.FindSymbolAtPositionAsync(_Context.Document, View.GetCaretPosition(), ctx.CancellationToken); if (symbol == null) { return(r); } symbol = symbol.GetAliasTarget(); switch (symbol.Kind) { case SymbolKind.Method: case SymbolKind.Property: case SymbolKind.Event: r.Add(CreateCommandMenu("Find Callers...", KnownImageIds.ShowCallerGraph, symbol, "No caller was found", FindCallers)); if (symbol.MayHaveOverride()) { r.Add(CreateCommandMenu("Find Overrides...", KnownImageIds.OverloadBehavior, symbol, "No override was found", FindOverrides)); } var st = symbol.ContainingType as INamedTypeSymbol; if (st != null && st.TypeKind == TypeKind.Interface) { r.Add(CreateCommandMenu("Find Implementations...", KnownImageIds.ImplementInterface, symbol, "No implementation was found", FindImplementations)); } if (symbol.Kind != SymbolKind.Event) { CreateCommandsForReturnTypeCommand(symbol, r); } if (symbol.Kind == SymbolKind.Method && (symbol as IMethodSymbol).MethodKind == MethodKind.Constructor) { goto case SymbolKind.NamedType; } //r.Add(CreateCommandMenu("Find similar...", KnownImageIds.DropShadow, symbol, "No similar symbol was found", FindSimilarSymbols)); break; case SymbolKind.Field: case SymbolKind.Local: case SymbolKind.Parameter: CreateCommandsForReturnTypeCommand(symbol, r); break; case SymbolKind.NamedType: var t = symbol as INamedTypeSymbol; if (symbol.Kind == SymbolKind.Method) // from case SymbolKind.Method { t = symbol.ContainingType as INamedTypeSymbol; } else { if (t.TypeKind == TypeKind.Class || t.TypeKind == TypeKind.Struct) { var ctor = _Context.NodeIncludeTrivia.GetObjectCreationNode(); if (ctor != null) { var s = _Context.SemanticModel.GetSymbolOrFirstCandidate(ctor); if (s != null) { r.Add(CreateCommandMenu("Find Callers...", KnownImageIds.ShowCallerGraph, s, "No caller was found", FindCallers)); } } else if (t.InstanceConstructors.Length > 0) { r.Add(CreateCommandMenu("Find Constructor Callers...", KnownImageIds.ShowCallerGraph, t, "No caller was found", FindCallers)); } } r.Add(CreateCommandMenu("Find Members...", KnownImageIds.ListMembers, t, "No member was found", FindMembers)); if (t.IsStatic == false) { r.Add(CreateCommandMenu("Find Extensions...", KnownImageIds.ListMembers, t, "No extension method was found", FindExtensionMethods)); } } if (t.IsStatic || t.SpecialType != SpecialType.None) { break; } r.Add(CreateCommandMenu("Find Instance Producer...", KnownImageIds.NewItem, t, "No instance creator was found", FindInstanceProducer)); r.Add(CreateCommandMenu("Find Instance as Parameter...", KnownImageIds.Parameter, t, "No instance as parameter was found", FindInstanceAsParameter)); if (t.IsSealed == false) { if (t.TypeKind == TypeKind.Class) { r.Add(CreateCommandMenu("Find Derived Classes...", KnownImageIds.NewClass, t, "No derived class was found", FindDerivedClasses)); } else if (t.TypeKind == TypeKind.Interface) { r.Add(CreateCommandMenu("Find Implementations...", KnownImageIds.ImplementInterface, symbol, "No implementation was found", FindImplementations)); } } break; case SymbolKind.Namespace: r.Add(CreateCommandMenu("Find Members...", KnownImageIds.ListMembers, symbol, "No member was found", FindMembers)); break; } //r.Add(CreateCommandMenu("Find references...", KnownImageIds.ReferencedDimension, symbol, "No reference found", FindReferences)); r.Add(CreateCommandMenu("Find Symbol Named " + symbol.Name + "...", KnownImageIds.FindSymbol, symbol, "No symbol was found", FindSymbolWithName)); r.Add(new CommandItem(KnownImageIds.ReferencedDimension, "Find All References", _ => TextEditorHelper.ExecuteEditorCommand("Edit.FindAllReferences"))); r.Add(new CommandItem(KnownImageIds.ListMembers, "Go to Member", _ => TextEditorHelper.ExecuteEditorCommand("Edit.GoToMember"))); r.Add(new CommandItem(KnownImageIds.Type, "Go to Type", _ => TextEditorHelper.ExecuteEditorCommand("Edit.GoToType"))); r.Add(new CommandItem(KnownImageIds.FindSymbol, "Go to Symbol", _ => TextEditorHelper.ExecuteEditorCommand("Edit.GoToSymbol"))); return(r); }
void AddContextualCommands(CancellationToken cancellationToken) { // anti-pattern for a small margin of performance bool isDesignMode = CodistPackage.DebuggerStatus == DebuggerStatus.Design; var isReadOnly = _Context.View.IsCaretInReadOnlyRegion(); var node = _Context.NodeIncludeTrivia; if (isDesignMode && isReadOnly == false && node is XmlTextSyntax) { AddXmlDocCommands(); return; } var trivia = _Context.GetNodeTrivia(); if (trivia.RawKind == 0) { var token = _Context.Token; if (token.Span.Contains(View.Selection, true) && token.Kind() == SyntaxKind.IdentifierToken && (node.IsDeclaration() || node is TypeSyntax || node is ParameterSyntax || node.IsKind(SyntaxKind.VariableDeclarator))) { // selection is within a symbol _Symbol = ThreadHelper.JoinableTaskFactory.Run(() => _Context.GetSymbolAsync(cancellationToken)); if (_Symbol != null) { if (node is IdentifierNameSyntax) { AddEditorCommand(MyToolBar, KnownImageIds.GoToDefinition, "Edit.GoToDefinition", "Go to definition\nRight click: Peek definition", "Edit.PeekDefinition"); } AddCommands(MyToolBar, KnownImageIds.ReferencedDimension, "Analyze symbol...", GetReferenceCommandsAsync); if (Classifiers.SymbolMarkManager.CanBookmark(_Symbol)) { AddCommands(MyToolBar, KnownImageIds.FlagGroup, "Mark symbol...", null, GetMarkerCommands); } if (isDesignMode && isReadOnly == false) { AddRefactorCommands(node); } } } else if (token.RawKind >= (int)SyntaxKind.NumericLiteralToken && token.RawKind <= (int)SyntaxKind.StringLiteralToken) { AddEditorCommand(MyToolBar, KnownImageIds.ReferencedDimension, "Edit.FindAllReferences", "Find all references"); } else if (isReadOnly == false && (token.IsKind(SyntaxKind.TrueKeyword) || token.IsKind(SyntaxKind.FalseKeyword))) { AddCommand(MyToolBar, KnownImageIds.ToggleButton, "Toggle value", ctx => { Replace(ctx, v => v == "true" ? "false" : "true", true); }); } else if (node.IsRegionalDirective()) { AddDirectiveCommands(); } if (isDesignMode && isReadOnly == false) { if (node.IsKind(SyntaxKind.VariableDeclarator)) { if (node?.Parent?.Parent is MemberDeclarationSyntax) { AddCommand(MyToolBar, KnownImageIds.AddComment, "Insert comment", ctx => { TextEditorHelper.ExecuteEditorCommand("Edit.InsertComment"); ctx.View.Selection.Clear(); }); } } else if (node.IsDeclaration()) { if (node is TypeDeclarationSyntax || node is MemberDeclarationSyntax || node is ParameterListSyntax) { AddCommand(MyToolBar, KnownImageIds.AddComment, "Insert comment", ctx => { TextEditorHelper.ExecuteEditorCommand("Edit.InsertComment"); ctx.View.Selection.Clear(); }); } } else if (IsInvertableOperation(node.Kind())) { AddCommand(MyToolBar, KnownImageIds.Operator, "Invert operator", InvertOperator); } else { AddEditorCommand(MyToolBar, KnownImageIds.ExtractMethod, "Refactor.ExtractMethod", "Extract Method"); } } } if (CodistPackage.DebuggerStatus != DebuggerStatus.Running && isReadOnly == false) { AddCommentCommands(); } if (isDesignMode == false) { AddCommands(MyToolBar, KnownImageIds.BreakpointEnabled, "Debugger...\nLeft click: Toggle breakpoint\nRight click: Debugger menu...", ctx => TextEditorHelper.ExecuteEditorCommand("Debug.ToggleBreakpoint"), ctx => DebugCommands); } AddCommands(MyToolBar, KnownImageIds.SelectFrame, "Expand selection...\nRight click: Duplicate...\nCtrl click item: Copy\nShift click item: Exclude whitespaces and comments", null, GetExpandSelectionCommands); }
void AddContextualCommands(CancellationToken cancellationToken) { // anti-pattern for a small margin of performance bool isDesignMode = CodistPackage.DebuggerStatus == DebuggerStatus.Design; var isReadOnly = _Context.View.IsCaretInReadOnlyRegion(); var node = _Context.NodeIncludeTrivia; if (isDesignMode && isReadOnly == false && (node is XmlTextSyntax)) { AddXmlDocCommands(); return; } var trivia = _Context.GetNodeTrivia(); var nodeKind = node.Kind(); if (trivia.RawKind == 0) { var token = _Context.Token; if (token.Span.Contains(View.Selection, true) && token.Kind() == SyntaxKind.IdentifierToken) { if (nodeKind.IsDeclaration() || node is TypeSyntax || node is ParameterSyntax || nodeKind == SyntaxKind.VariableDeclarator || nodeKind == SyntaxKind.ForEachStatement || nodeKind == SyntaxKind.SingleVariableDesignation) { // selection is within a symbol _Symbol = SyncHelper.RunSync(() => _Context.GetSymbolAsync(cancellationToken)); if (_Symbol != null) { AddSymbolCommands(isReadOnly, node); } } else if (nodeKind == SyntaxKind.TypeParameter) { _Symbol = SyncHelper.RunSync(() => _Context.GetSymbolAsync(cancellationToken)); if (_Symbol != null && isReadOnly == false) { AddRenameCommand(node); } } } else if (token.RawKind >= (int)SyntaxKind.NumericLiteralToken && token.RawKind <= (int)SyntaxKind.StringLiteralToken) { AddEditorCommand(MyToolBar, IconIds.FindReference, "Edit.FindAllReferences", R.CMD_FindAllReferences); } else if (nodeKind.IsRegionalDirective()) { AddDirectiveCommands(); } if (isReadOnly == false) { if (token.IsKind(SyntaxKind.TrueKeyword) || token.IsKind(SyntaxKind.FalseKeyword)) { AddCommand(MyToolBar, IconIds.ToggleValue, R.CMD_ToggleValue, ctx => Replace(ctx, v => v == "true" ? "false" : "true", true)); } else if (token.IsKind(SyntaxKind.ExplicitKeyword) || token.IsKind(SyntaxKind.ImplicitKeyword)) { AddCommand(MyToolBar, IconIds.ToggleValue, R.CMD_ToggleOperator, ctx => Replace(ctx, v => v == "implicit" ? "explicit" : "implicit", true)); } if (nodeKind == SyntaxKind.VariableDeclarator) { if (node?.Parent?.Parent is MemberDeclarationSyntax) { AddCommand(MyToolBar, IconIds.AddXmlDoc, R.CMD_InsertComment, ctx => { TextEditorHelper.ExecuteEditorCommand("Edit.InsertComment"); ctx.View.Selection.Clear(); }); } } else if (nodeKind.IsDeclaration()) { if (node is TypeDeclarationSyntax || node is MemberDeclarationSyntax || node is ParameterListSyntax) { AddCommand(MyToolBar, IconIds.AddXmlDoc, R.CMD_InsertComment, ctx => { TextEditorHelper.ExecuteEditorCommand("Edit.InsertComment"); ctx.View.Selection.Clear(); }); } } else if (IsInvertableOperation(nodeKind)) { AddCommand(MyToolBar, IconIds.InvertOperator, R.CMD_InvertOperator, InvertOperator); } else if (isDesignMode && nodeKind != SyntaxKind.TypeParameter) { AddEditorCommand(MyToolBar, IconIds.ExtractMethod, "Refactor.ExtractMethod", R.CMD_ExtractMethod); } } } if (CodistPackage.DebuggerStatus != DebuggerStatus.Running && isReadOnly == false) { AddCommentCommands(); } if (isDesignMode == false) { AddCommands(MyToolBar, IconIds.ToggleBreakpoint, R.CMD_Debugger, ctx => TextEditorHelper.ExecuteEditorCommand("Debug.ToggleBreakpoint"), ctx => DebugCommands); } //AddCommands(MyToolBar, KnownImageIds.SelectFrame, "Expand selection...\nRight click: Duplicate...\nCtrl click item: Copy\nShift click item: Exclude whitespaces and comments", null, GetExpandSelectionCommands); }
List <CommandItem> GetReferenceCommands(CommandContext ctx) { var r = new List <CommandItem>(); var symbol = SymbolFinder.FindSymbolAtPositionAsync(_Document, View.Caret.Position.BufferPosition).Result; if (symbol == null) { return(r); } symbol = symbol.GetAliasTarget(); switch (symbol.Kind) { case SymbolKind.Method: //if ((symbol as IMethodSymbol).MethodKind == MethodKind.Constructor) { // r.Add(CreateCommandMenu("Find members...", KnownImageIds.ListMembers, symbol.ContainingType, "No member was found", FindMembers)); //} //goto case SymbolKind.Property; case SymbolKind.Property: case SymbolKind.Event: r.Add(CreateCommandMenu("Find callers...", KnownImageIds.ShowCallerGraph, symbol, "No caller was found", FindCallers)); if (symbol.MayHaveOverride()) { r.Add(CreateCommandMenu("Find overrides...", KnownImageIds.OverloadBehavior, symbol, "No override was found", FindOverrides)); } var st = symbol.ContainingType as INamedTypeSymbol; if (st != null && st.TypeKind == TypeKind.Interface) { r.Add(CreateCommandMenu("Find implementations...", KnownImageIds.ImplementInterface, symbol, "No implementation was found", FindImplementations)); } if (symbol.Kind != SymbolKind.Event) { CreateFindMemberForReturnTypeCommand(symbol, r); } //r.Add(CreateCommandMenu("Find similar...", KnownImageIds.DropShadow, symbol, "No similar symbol was found", FindSimilarSymbols)); break; case SymbolKind.Field: case SymbolKind.Local: case SymbolKind.Parameter: CreateFindMemberForReturnTypeCommand(symbol, r); break; case SymbolKind.NamedType: var t = symbol as INamedTypeSymbol; if (t.TypeKind == TypeKind.Class || t.TypeKind == TypeKind.Struct) { var ctor = _Node.GetObjectCreationNode(); if (ctor != null) { var s = _SemanticModel.GetSymbolOrFirstCandidate(ctor); if (s != null) { r.Add(CreateCommandMenu("Find callers...", KnownImageIds.ShowCallerGraph, s, "No caller was found", FindCallers)); } } else if (t.InstanceConstructors.Length > 0) { r.Add(CreateCommandMenu("Find constructor callers...", KnownImageIds.ShowCallerGraph, t, "No caller was found", FindCallers)); } } r.Add(CreateCommandMenu("Find members...", KnownImageIds.ListMembers, t, "No member was found", FindMembers)); if (t.IsStatic || t.SpecialType != SpecialType.None) { break; } r.Add(CreateCommandMenu("Find instance producer...", KnownImageIds.NewItem, t, "No instance creator was found", FindInstanceProducer)); r.Add(CreateCommandMenu("Find instance as parameter...", KnownImageIds.Parameter, t, "No instance as parameter was found", FindInstanceAsParameter)); if (t.IsSealed) { break; } if (t.TypeKind == TypeKind.Class) { r.Add(CreateCommandMenu("Find derived classes...", KnownImageIds.NewClass, symbol, "No derived class was found", FindDerivedClasses)); } else if (t.TypeKind == TypeKind.Interface) { r.Add(CreateCommandMenu("Find implementations...", KnownImageIds.ImplementInterface, symbol, "No implementation was found", FindImplementations)); } break; case SymbolKind.Namespace: r.Add(CreateCommandMenu("Find members...", KnownImageIds.ListMembers, symbol, "No member was found", FindMembers)); break; } //r.Add(CreateCommandMenu("Find references...", KnownImageIds.ReferencedDimension, symbol, "No reference found", FindReferences)); r.Add(new CommandItem("Find all references", KnownImageIds.ReferencedDimension, null, _ => TextEditorHelper.ExecuteEditorCommand("Edit.FindAllReferences"))); return(r); }
protected virtual void AddCommands() { var readOnly = View.IsCaretInReadOnlyRegion(); if (readOnly == false) { AddCommand(ToolBar, KnownImageIds.Cut, "Cut selected text\nRight click: Cut line", ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } TextEditorHelper.ExecuteEditorCommand("Edit.Cut"); }); } AddCommand(ToolBar, KnownImageIds.Copy, "Copy selected text\nRight click: Copy line", ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } TextEditorHelper.ExecuteEditorCommand("Edit.Copy"); }); if (readOnly == false) { if (Clipboard.ContainsText()) { AddCommand(ToolBar, KnownImageIds.Paste, "Paste text from clipboard\nRight click: Paste over line\nCtrl click: Paste and select next", ctx => ExecuteAndFind(ctx, "Edit.Paste")); } AddCommand(ToolBar, KnownImageIds.CopyItem, "Duplicate selection\nRight click: Duplicate line", ctx => { if (ctx.RightClick) { ctx.View.ExpandSelectionToLine(); } TextEditorHelper.ExecuteEditorCommand("Edit.Duplicate"); ctx.KeepToolbar(); }); AddCommand(ToolBar, KnownImageIds.Cancel, "Delete selected text\nRight click: Delete line\nCtrl click: Delete and select next", ctx => ExecuteAndFind(ctx, "Edit.Delete")); switch (View.GetSelectedTokenType()) { case TokenType.None: AddEditorCommand(ToolBar, KnownImageIds.FormatSelection, "Edit.FormatSelection", "Format selected text\nRight click: Format document", "Edit.FormatDocument"); break; case TokenType.Digit: AddCommand(ToolBar, KnownImageIds.Counter, "Increment number", ctx => { var span = ctx.View.Selection.SelectedSpans[0]; var t = span.GetText(); long l; if (long.TryParse(t, out l)) { using (var ed = ctx.View.TextBuffer.CreateEdit()) { t = (++l).ToString(System.Globalization.CultureInfo.InvariantCulture); if (ed.Replace(span.Span, t)) { ed.Apply(); ctx.View.Selection.Select(new Microsoft.VisualStudio.Text.SnapshotSpan(ctx.View.TextSnapshot, span.Start, t.Length), false); ctx.KeepToolbar(); } } } }); break; } //var selection = View.Selection; //if (View.Selection.Mode == TextSelectionMode.Stream && View.TextViewLines.GetTextViewLineContainingBufferPosition(selection.Start.Position) != View.TextViewLines.GetTextViewLineContainingBufferPosition(selection.End.Position)) { // AddCommand(ToolBar, KnownImageIds.Join, "Join lines", ctx => { // var span = View.Selection.SelectedSpans[0]; // var t = span.GetText(); // View.TextBuffer.Replace(span, System.Text.RegularExpressions.Regex.Replace(t, @"[ \t]*\r?\n[ \t]*", " ")); // }); //} } if (CodistPackage.DebuggerStatus != DebuggerStatus.Design) { AddEditorCommand(ToolBar, KnownImageIds.ToolTip, "Edit.QuickInfo", "Show quick info"); } AddEditorCommand(ToolBar, KnownImageIds.FindNext, "Edit.FindNextSelected", "Find next selected text\nRight click: Find previous selected", "Edit.FindPreviousSelected"); //AddEditorCommand(ToolBar, "Edit.Capitalize", KnownImageIds.ASerif, "Capitalize"); }
async Task <IEnumerable <CommandItem> > GetReferenceCommandsAsync(CommandContext ctx) { var r = new List <CommandItem>(); var symbol = await SymbolFinder.FindSymbolAtPositionAsync(_Context.Document, View.GetCaretPosition(), ctx.CancellationToken); if (symbol == null) { return(r); } symbol = symbol.GetAliasTarget(); switch (symbol.Kind) { case SymbolKind.Method: case SymbolKind.Property: case SymbolKind.Event: r.Add(new CommandItem(KnownImageIds.ShowCallerGraph, "Find Callers...", s => FindCallers(s, symbol))); if (symbol.MayHaveOverride()) { r.Add(new CommandItem(KnownImageIds.OverloadBehavior, "Find Overrides...", c => FindOverrides(c, symbol))); } var st = symbol.ContainingType; if (st != null && st.TypeKind == TypeKind.Interface) { r.Add(new CommandItem(KnownImageIds.ImplementInterface, "Find Implementations...", c => FindImplementations(c, symbol))); } if (symbol.Kind != SymbolKind.Event) { CreateCommandsForReturnTypeCommand(symbol, r); } if (symbol.Kind == SymbolKind.Method && (symbol as IMethodSymbol).MethodKind == MethodKind.Constructor && st.SpecialType == SpecialType.None) { CreateInstanceCommandsForType(st, r); } break; case SymbolKind.Field: case SymbolKind.Local: case SymbolKind.Parameter: CreateCommandsForReturnTypeCommand(symbol, r); break; case SymbolKind.NamedType: CreateCommandForNamedType(symbol as INamedTypeSymbol, r); break; case SymbolKind.Namespace: r.Add(new CommandItem(KnownImageIds.ListMembers, "Find Members...", s => FindMembers(symbol))); break; } if (_Context.Node.IsDeclaration() && symbol.Kind != SymbolKind.Namespace) { r.Add(new CommandItem(KnownImageIds.ShowReferencedElements, "Find Referenced Symbols...", c => FindReferencedSymbols(c, symbol))); } //r.Add(CreateCommandMenu("Find references...", KnownImageIds.ReferencedDimension, symbol, "No reference found", FindReferences)); r.Add(new CommandItem(KnownImageIds.ReferencedDimension, "Find All References", _ => TextEditorHelper.ExecuteEditorCommand("Edit.FindAllReferences"))); r.Add(new CommandItem(KnownImageIds.FindSymbol, "Find Symbol with Name " + symbol.Name + "...", c => FindSymbolWithName(c, symbol))); r.Add(new CommandItem(KnownImageIds.ListMembers, "Go to Member", _ => TextEditorHelper.ExecuteEditorCommand("Edit.GoToMember"))); r.Add(new CommandItem(KnownImageIds.Type, "Go to Type", _ => TextEditorHelper.ExecuteEditorCommand("Edit.GoToType"))); r.Add(new CommandItem(KnownImageIds.FindSymbol, "Go to Symbol", _ => TextEditorHelper.ExecuteEditorCommand("Edit.GoToSymbol"))); return(r); }
void AddContextualCommands() { if (_Node == null) { return; } // anti-pattern for a small margin of performance bool isDesignMode = CodistPackage.DebuggerStatus == DebuggerStatus.Design; if (isDesignMode && _Node is XmlTextSyntax) { AddCommand(KnownImageIds.MarkupTag, "Tag XML Doc with <c>", edit => { foreach (var item in View.Selection.SelectedSpans) { edit.Replace(item, "<c>" + item.GetText() + "</c>"); } }); AddCommand(KnownImageIds.GoToNext, "Tag XML Doc with <see>", edit => { foreach (var item in View.Selection.SelectedSpans) { var t = item.GetText(); edit.Replace(item, (SyntaxFacts.GetKeywordKind(t) != SyntaxKind.None ? "<see langword=\"" : "<see cref=\"") + t + "\"/>"); } }); AddCommand(KnownImageIds.ParagraphHardReturn, "Tag XML Doc with <para>", edit => { foreach (var item in View.Selection.SelectedSpans) { edit.Replace(item, "<para>" + item.GetText() + "</para>"); } }); } else if (_Trivia.RawKind == 0) { if (_Token.Span.Contains(View.Selection, true) && _Token.Kind() == SyntaxKind.IdentifierToken && (_Node.IsDeclaration() || _Node is TypeSyntax || _Node is ParameterSyntax || _Node.IsKind(SyntaxKind.VariableDeclarator))) { // selection is within a symbol _Symbol = SymbolFinder.FindSymbolAtPositionAsync(_Document, View.Selection.Start.Position).Result; if (_Symbol != null) { if (_Node is IdentifierNameSyntax) { AddEditorCommand(MyToolBar, KnownImageIds.GoToDefinition, "Edit.GoToDefinition", "Go to definition"); } AddCommands(MyToolBar, KnownImageIds.ReferencedDimension, "Analyze references...", GetReferenceCommands); if (Classifiers.SymbolMarkManager.CanBookmark(_Symbol)) { AddCommands(MyToolBar, KnownImageIds.FlagGroup, "Mark symbol...", GetMarkerCommands); } if (isDesignMode) { AddCommand(MyToolBar, KnownImageIds.Rename, "Rename symbol", ctx => { TextEditorHelper.ExecuteEditorCommand("Refactor.Rename"); ctx.KeepToolbarOnClick = true; }); if (_Node is ParameterSyntax && _Node.Parent is ParameterListSyntax) { AddEditorCommand(MyToolBar, KnownImageIds.ReorderParameters, "Refactor.ReorderParameters", "Reorder parameters"); } } } } else if (_Token.RawKind >= (int)SyntaxKind.NumericLiteralToken && _Token.RawKind <= (int)SyntaxKind.StringLiteralToken) { AddEditorCommand(MyToolBar, KnownImageIds.ReferencedDimension, "Edit.FindAllReferences", "Find all references"); } if (isDesignMode) { if (_Node.IsKind(SyntaxKind.VariableDeclarator)) { if (_Node?.Parent?.Parent is MemberDeclarationSyntax) { AddCommand(MyToolBar, KnownImageIds.AddComment, "Insert comment", ctx => { TextEditorHelper.ExecuteEditorCommand("Edit.InsertComment"); ctx.View.Selection.Clear(); }); } } else if (_Node.IsDeclaration()) { if (_Node is TypeDeclarationSyntax || _Node is MemberDeclarationSyntax || _Node is ParameterListSyntax) { AddCommand(MyToolBar, KnownImageIds.AddComment, "Insert comment", ctx => { TextEditorHelper.ExecuteEditorCommand("Edit.InsertComment"); ctx.View.Selection.Clear(); }); } } else { AddEditorCommand(MyToolBar, KnownImageIds.ExtractMethod, "Refactor.ExtractMethod", "Extract Method"); } } } if (CodistPackage.DebuggerStatus != DebuggerStatus.Running) { 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"); }); } } if (isDesignMode == false) { AddCommands(MyToolBar, KnownImageIds.BreakpointEnabled, "Debugger...", GetDebugCommands); } AddCommands(MyToolBar, KnownImageIds.SelectFrame, "Expand selection...\nRight click: Duplicate...\nCtrl click item: Copy\nShift click item: Exclude whitespaces and comments", GetExpandSelectionCommands); }