Skip to content

Commit

Permalink
feat: Option to set double-click to switch to the editor #139
Browse files Browse the repository at this point in the history
  • Loading branch information
sboulema committed Sep 18, 2022
1 parent 5235692 commit 7bdbcb2
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 9 deletions.
13 changes: 11 additions & 2 deletions CodeNav.Shared/Controls/ItemDataTemplate.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
</ResourceDictionary.MergedDictionaries>

<DataTemplate DataType="{x:Type models:CodeItem}">
<Button Command="{Binding Path=ClickItemCommand}" CommandParameter="{Binding StartLinePosition}"
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
ContextMenu="{StaticResource ItemContextMenu}"
BorderThickness="0"
HorizontalContentAlignment="Stretch"
Expand All @@ -19,6 +18,16 @@
Margin="-3"
Opacity="{Binding Opacity}">

<Button.InputBindings>
<MouseBinding
Gesture="LeftClick"
Command="{Binding Path=ClickItemCommand}"
CommandParameter="{Binding StartLinePosition}" />
<MouseBinding
Gesture="LeftDoubleClick"
Command="{Binding Path=EditLineCommand}" />
</Button.InputBindings>

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
Expand Down
34 changes: 34 additions & 0 deletions CodeNav.Shared/Helpers/DocumentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;
using Task = System.Threading.Tasks.Task;

namespace CodeNav.Helpers
Expand Down Expand Up @@ -128,6 +129,39 @@ public static async Task ScrollToLine(LinePosition? linePosition, string filePat
}
}

public static async Task EditLine(LinePosition? linePosition, string filePath = "")
{
if (linePosition == null)
{
return;
}

try
{
var documentView = await VS.Documents.GetActiveDocumentViewAsync();

if (documentView?.FilePath != filePath)
{
documentView = await VS.Documents.OpenInPreviewTabAsync(filePath);
}

if (documentView?.TextBuffer == null ||
documentView?.TextView == null)
{
return;
}

var line = documentView.TextView.TextSnapshot.GetLineFromLineNumber(linePosition.Value.Line);
var point = new SnapshotPoint(line.Snapshot, line.Start.Position + linePosition.Value.Character);

documentView.TextView.Caret.MoveTo(point);
}
catch (Exception)
{
// Ignore
}
}

public static async Task SelectLines(TextSpan textSpan)
{
try
Expand Down
6 changes: 6 additions & 0 deletions CodeNav.Shared/Mappers/BaseMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public static LinePosition GetStartLinePosition(SyntaxToken identifier) =>
public static int GetStartLine(SyntaxToken identifier) =>
GetStartLinePosition(identifier).Line + 1;

public static LinePosition GetStartLinePosition(SyntaxNode source, SyntaxTokenList modifiers) =>
source.SyntaxTree.GetLineSpan(modifiers.Span).StartLinePosition;

public static int GetStartLine(SyntaxNode source, SyntaxTokenList modifiers) =>
GetStartLinePosition(source, modifiers).Line + 1;

private static CodeItemAccessEnum MapAccess(SyntaxTokenList modifiers, SyntaxNode source)
{
if (modifiers.Any(m => m.RawKind == (int)SyntaxKind.SealedKeyword ||
Expand Down
4 changes: 2 additions & 2 deletions CodeNav.Shared/Mappers/MethodMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public static class MethodMapper
item.Id = IdMapper.MapId(item.FullName, parameterList);
item.Kind = kind;
item.Moniker = IconMapper.MapMoniker(item.Kind, item.Access);
item.StartLine = BaseMapper.GetStartLine(identifier);
item.StartLinePosition = BaseMapper.GetStartLinePosition(identifier);
item.StartLine = BaseMapper.GetStartLine(node, modifiers);
item.StartLinePosition = BaseMapper.GetStartLinePosition(node, modifiers);

if (TriviaSummaryMapper.HasSummary(node) && SettingsHelper.UseXMLComments)
{
Expand Down
8 changes: 7 additions & 1 deletion CodeNav.Shared/Models/CodeItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using System.Runtime.Serialization;
using Task = System.Threading.Tasks.Task;
using Microsoft.VisualStudio.Shell;
using System.Threading;

namespace CodeNav.Models
{
Expand Down Expand Up @@ -230,6 +229,13 @@ private async Task ClickItemAsync()
public ICommand SelectInCodeCommand => new DelegateCommand(SelectInCode);
public void SelectInCode(object args) => DocumentHelper.SelectLines(Span).FireAndForget();

public ICommand EditLineCommand => new DelegateCommand(EditLine);
public void EditLine(object args)
{
IsDoubleClicked = true;
DocumentHelper.EditLine(StartLinePosition, FilePath).FireAndForget();
}

public ICommand CopyNameCommand => new DelegateCommand(CopyName);
public void CopyName(object args) => Clipboard.SetText(Name);

Expand Down
7 changes: 3 additions & 4 deletions CodeNav.Shared/Styles/PlusMinusExpanderStyles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,13 @@
BorderThickness="0"
Margin="4,0,0,0">
<Button.InputBindings>

<MouseBinding
Gesture="LeftDoubleClick"
Command="{Binding Path=ToggleIsExpandedCommand}" />
<MouseBinding
Gesture="LeftClick"
Command="{Binding Path=ClickItemCommand}"
CommandParameter="{Binding StartLinePosition}" />
<MouseBinding
Gesture="LeftDoubleClick"
Command="{Binding Path=ToggleIsExpandedCommand}" />
</Button.InputBindings>
</Button>
</Border>
Expand Down

0 comments on commit 7bdbcb2

Please sign in to comment.