From f2f1e2588c4f4ade856717bc846e6b8d3d686c9f Mon Sep 17 00:00:00 2001 From: WMJ Date: Thu, 20 Dec 2018 21:38:58 +0800 Subject: [PATCH] + Added smart bar buttons for C/C++ code file type --- Codist/Codist.csproj | 1 + Codist/Constants.cs | 1 + Codist/SmartBars/CppSmartBar.cs | 57 +++++++++++++++++++ .../SmartBarTextViewCreationListener.cs | 3 + 4 files changed, 62 insertions(+) create mode 100644 Codist/SmartBars/CppSmartBar.cs diff --git a/Codist/Codist.csproj b/Codist/Codist.csproj index 8d53254a..30015e2a 100644 --- a/Codist/Codist.csproj +++ b/Codist/Codist.csproj @@ -214,6 +214,7 @@ + diff --git a/Codist/Constants.cs b/Codist/Constants.cs index e2405ea2..c8fc12cf 100644 --- a/Codist/Constants.cs +++ b/Codist/Constants.cs @@ -14,6 +14,7 @@ static partial class Constants public static class CodeTypes { + public const string CPlusPlus = "C/C++"; public const string Code = nameof(Code); public const string CSharp = nameof(CSharp); public const string Text = nameof(Text); diff --git a/Codist/SmartBars/CppSmartBar.cs b/Codist/SmartBars/CppSmartBar.cs new file mode 100644 index 00000000..ab6a43e2 --- /dev/null +++ b/Codist/SmartBars/CppSmartBar.cs @@ -0,0 +1,57 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Threading; +using System.Windows.Controls; +using Microsoft.VisualStudio.Imaging; +using Microsoft.VisualStudio.Text.Editor; +using Microsoft.VisualStudio.Text.Operations; + +namespace Codist.SmartBars +{ + sealed class CppSmartBar : SmartBar + { + readonly ITextStructureNavigator _TextStructureNavigator; + public CppSmartBar(IWpfTextView textView, ITextSearchService2 textSearchService) : base(textView, textSearchService) { + _TextStructureNavigator = ServicesHelper.Instance.TextStructureNavigator.GetTextStructureNavigator(textView.TextBuffer); + } + + ToolBar MyToolBar => ToolBar2; + + 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"), GetDebugCommands); + } + } + + CommandItem[] GetDebugCommands(CommandContext ctx) { + return new CommandItem[] { + new CommandItem(KnownImageIds.Watch, "Add Watch", c => TextEditorHelper.ExecuteEditorCommand("Debug.AddWatch")), + new CommandItem(KnownImageIds.Watch, "Add Parallel Watch", c => TextEditorHelper.ExecuteEditorCommand("Debug.AddParallelWatch")), + new CommandItem(KnownImageIds.DeleteBreakpoint, "Delete All Breakpoints", c => TextEditorHelper.ExecuteEditorCommand("Debug.DeleteAllBreakpoints")) + }; + } + + string GetCurrentWord(ITextView view) { + return _TextStructureNavigator.GetExtentOfWord(view.Selection.Start.Position).Span.GetText(); + } + } +} diff --git a/Codist/SmartBars/SmartBarTextViewCreationListener.cs b/Codist/SmartBars/SmartBarTextViewCreationListener.cs index 161674f1..419b3ba3 100644 --- a/Codist/SmartBars/SmartBarTextViewCreationListener.cs +++ b/Codist/SmartBars/SmartBarTextViewCreationListener.cs @@ -62,6 +62,9 @@ public void TextViewCreated(IWpfTextView textView) { ) { new OutputSmartBar(textView, _TextSearchService); } + else if (contentType.IsOfType(Constants.CodeTypes.CPlusPlus)) { + new CppSmartBar(textView, _TextSearchService); + } else { new SmartBar(textView, _TextSearchService); }