Skip to content

Commit

Permalink
! Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wmjordan committed Dec 20, 2018
1 parent f2f1e25 commit e99c890
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 114 deletions.
59 changes: 52 additions & 7 deletions Codist/Helpers/TextEditorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,49 @@ public static StyleBase GetStyle(string classificationType) {
}
}

public static void ExpandSelectionToLine(this IWpfTextView view) {
/// <summary>
/// Begins an edit operation to the <paramref name="view"/>.
/// </summary>
/// <typeparam name="TView">The type of the view.</typeparam>
/// <param name="view">The <see cref="ITextView"/> to be edited.</param>
/// <param name="action">The edit operation.</param>
/// <returns>Returns a new <see cref="ITextSnapshot"/> if <see cref="ITextEdit.HasEffectiveChanges"/> returns <see langword="true"/>, otherwise, returns <see langword="null"/>.</returns>
public static ITextSnapshot Edit<TView>(this TView view, Action<TView, ITextEdit> action)
where TView : ITextView {
using (var edit = view.TextSnapshot.TextBuffer.CreateEdit()) {
action(view, edit);
if (edit.HasEffectiveChanges) {
edit.Apply();
return edit.Snapshot;
}
return null;
}
}
/// <summary>
/// Performs edit operation to each selected spans in the <paramref name="view"/>.
/// </summary>
/// <typeparam name="TView">The type of the view.</typeparam>
/// <param name="view">The <see cref="ITextView"/> to be edited.</param>
/// <param name="action">The edit operation against each selected span.</param>
/// <returns>Returns a new <see cref="ITextSnapshot"/> if <see cref="ITextEdit.HasEffectiveChanges"/> returns <see langword="true"/>, otherwise, returns <see langword="null"/>.</returns>
public static ITextSnapshot EditSelection<TView>(this TView view, Action<TView, ITextEdit, SnapshotSpan> action)
where TView : ITextView {
using (var edit = view.TextSnapshot.TextBuffer.CreateEdit()) {
foreach (var item in view.Selection.SelectedSpans) {
action(view, edit, item);
}
if (edit.HasEffectiveChanges) {
edit.Apply();
return edit.Snapshot;
}
return null;
}
}

public static void ExpandSelectionToLine(this ITextView view) {
view.ExpandSelectionToLine(true);
}
public static void ExpandSelectionToLine(this IWpfTextView view, bool includeLineBreak) {
public static void ExpandSelectionToLine(this ITextView view, bool includeLineBreak) {
var start = view.TextSnapshot.GetLineFromPosition(view.Selection.Start.Position).Start;
var end = view.Selection.End.Position;
var endLine = view.TextSnapshot.GetLineFromPosition(end);
Expand All @@ -92,6 +131,12 @@ public static void ExpandSelectionToLine(this IWpfTextView view, bool includeLin
}
view.Selection.Select(new SnapshotSpan(start, end), false);
}
public static string GetFirstSelectionText(this ITextSelection selection) {
return selection.IsEmpty ? String.Empty : selection.SelectedSpans[0].GetText();
}
public static string GetFirstSelectionText(this ITextView view) {
return view.TryGetFirstSelectionSpan(out var span) ? span.GetText() : String.Empty;
}
public static bool TryGetFirstSelectionSpan(this ITextView view, out SnapshotSpan span) {
if (view.Selection.IsEmpty || view.Selection.SelectedSpans.Count < 1) {
span = new SnapshotSpan();
Expand Down Expand Up @@ -139,23 +184,23 @@ public static TokenType GetSelectedTokenType(this ITextView view) {
return t;
}

public static SnapshotPoint GetCaretPosition(this IWpfTextView textView) {
public static SnapshotPoint GetCaretPosition(this ITextView textView) {
return textView.Caret.Position.BufferPosition;
}

public static bool IsCaretInReadOnlyRegion(this IWpfTextView textView) {
public static bool IsCaretInReadOnlyRegion(this ITextView textView) {
return textView.TextBuffer.IsReadOnly(textView.Caret.Position.BufferPosition);
}

public static bool IsMultilineSelected(this IWpfTextView textView) {
public static bool IsMultilineSelected(this ITextView textView) {
var s = textView.Selection;
if (s.IsEmpty || s.SelectedSpans.Count < 1) {
return false;
}
return textView.GetTextViewLineContainingBufferPosition(s.Start.Position) != textView.GetTextViewLineContainingBufferPosition(s.End.Position);
}

public static void SelectNode(this IWpfTextView view, Microsoft.CodeAnalysis.SyntaxNode node, bool includeTrivia) {
public static void SelectNode(this ITextView view, Microsoft.CodeAnalysis.SyntaxNode node, bool includeTrivia) {
var span = includeTrivia ? node.FullSpan : node.Span;
if (view.TextSnapshot.Length > span.End) {
SnapshotSpan ss;
Expand All @@ -178,7 +223,7 @@ public static void SelectNode(this IWpfTextView view, Microsoft.CodeAnalysis.Syn
}
}

public static void SelectSpan(this IWpfTextView view, SnapshotSpan span) {
public static void SelectSpan(this ITextView view, SnapshotSpan span) {
view.ViewScroller.EnsureSpanVisible(span, EnsureSpanVisibleOptions.ShowStart);
view.Selection.Select(span, false);
view.Caret.MoveTo(span.End);
Expand Down
50 changes: 20 additions & 30 deletions Codist/SmartBars/CSharpSmartBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void AddContextualCommands(CancellationToken cancellationToken) {
}
}
if (isDesignMode == false) {
AddCommands(MyToolBar, KnownImageIds.BreakpointEnabled, "Debugger...\nLeft click: Toggle breakpoint\nRight click: Debugger menu...", ctx => TextEditorHelper.ExecuteEditorCommand("Debug.ToggleBreakpoint"), GetDebugCommands);
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);
}
Expand All @@ -178,30 +178,28 @@ void AddXmlDocCommands() {
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()) {
using (var edit = ctx.View.TextSnapshot.TextBuffer.CreateEdit()) {
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;
}
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 + "\"/>");
}
if (edit.HasEffectiveChanges) {
edit.Apply();
}
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);
Expand Down Expand Up @@ -461,14 +459,6 @@ void FindSymbolWithName(CommandContext ctx, MenuItem menuItem, ISymbol source) {
SortAndGroupSymbolByClass(menuItem, new List<ISymbol>(result));
}

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"))
};
}

List<CommandItem> GetExpandSelectionCommands(CommandContext ctx) {
var r = new List<CommandItem>();
var duplicate = ctx.RightClick;
Expand Down
10 changes: 1 addition & 9 deletions Codist/SmartBars/CppSmartBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,10 @@ protected override void AddCommands(CancellationToken cancellationToken) {
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);
AddCommands(MyToolBar, KnownImageIds.BreakpointEnabled, "Debugger...\nLeft click: Toggle breakpoint\nRight click: Debugger menu...", ctx => TextEditorHelper.ExecuteEditorCommand("Debug.ToggleBreakpoint"), ctx => DebugCommands);
}
}

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();
}
Expand Down
Loading

0 comments on commit e99c890

Please sign in to comment.