Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New navigation mode and some small changes #22

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,6 @@ UpgradeLog*.htm

# Microsoft Fakes
FakesAssemblies/
*.ide
/.vs/slnx.sqlite
/.vs/VSWorkspaceState.json
31 changes: 30 additions & 1 deletion EasyMotion/EasyMotion.vsct
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,40 @@
<Buttons>
<Button guid="guidEasyMotionCmdSet" id="CmdEasyMotionNavigate" priority="0x0100" type="Button">
<Parent guid="guidEasyMotionCmdSet" id="MyMenuGroup" />
<Icon guid="guidImages" id="bmpPic1" />
<Strings>
<ButtonText>EasyMotionNavigate</ButtonText>
<CanonicalName>.EasyMotion.Navigate</CanonicalName>
<LocCanonicalName>.EasyMotion.Navigate</LocCanonicalName>
</Strings>
</Button>
<Button guid="guidEasyMotionCmdSet" id="CmdEasyMotionNavigateWord" priority="0x0100" type="Button">
<Parent guid="guidEasyMotionCmdSet" id="MyMenuGroup" />
<Strings>
<ButtonText>EasyMotionNavigateWord</ButtonText>
<CanonicalName>.EasyMotion.NavigateWord</CanonicalName>
<LocCanonicalName>.EasyMotion.NavigateWord</LocCanonicalName>
</Strings>
</Button>

<Button guid="guidEasyMotionCmdSet" id="CmdEasyMotionNavigateExtend" priority="0x0100" type="Button">
<Parent guid="guidEasyMotionCmdSet" id="MyMenuGroup" />
<Strings>
<ButtonText>EasyMotionNavigateExtend</ButtonText>
<CanonicalName>.EasyMotion.NavigateExtend</CanonicalName>
<LocCanonicalName>.EasyMotion.NavigateExtend</LocCanonicalName>
</Strings>
</Button>
<Button guid="guidEasyMotionCmdSet" id="CmdEasyMotionNavigateWordExtend" priority="0x0100" type="Button">
<Parent guid="guidEasyMotionCmdSet" id="MyMenuGroup" />
<Strings>
<ButtonText>EasyMotionNavigateWordExtend</ButtonText>
<CanonicalName>.EasyMotion.NavigateWordExtend</CanonicalName>
<LocCanonicalName>.EasyMotion.NavigateWordExtend</LocCanonicalName>
</Strings>
</Button>

</Buttons>


<Bitmaps>
<Bitmap guid="guidImages" href="Resources\Images.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows"/>
Expand All @@ -40,6 +66,9 @@
<GuidSymbol name="guidEasyMotionCmdSet" value="{907a344a-75bb-4df4-a3b3-9bc3299593b7}">
<IDSymbol name="MyMenuGroup" value="0x1020" />
<IDSymbol name="CmdEasyMotionNavigate" value="0x0100" />
<IDSymbol name="CmdEasyMotionNavigateWord" value="0x0101" />
<IDSymbol name="CmdEasyMotionNavigateExtend" value="0x0102" />
<IDSymbol name="CmdEasyMotionNavigateWordExtend" value="0x0103" />
</GuidSymbol>

<GuidSymbol name="guidImages" value="{a0c05e65-710e-45db-a639-008c57538b55}" >
Expand Down
37 changes: 31 additions & 6 deletions EasyMotion/EasyMotionPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,23 @@ protected override void Initialize()
_exportProvider = _componentModel.DefaultExportProvider;

// Add our command handlers for menu (commands must exist in the .vsct file)
OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if ( null != mcs )
{
// Create the command for the menu item.
CommandID menuCommandID = new CommandID(GuidList.guidEasyMotionCmdSet, (int)PkgCmdIDList.CmdEasyMotionNavigate);
MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID );
mcs.AddCommand( menuItem );
AddCommand(PkgCmdIDList.CmdEasyMotionNavigate, mcs);
AddCommand(PkgCmdIDList.CmdEasyMotionNavigateWord, mcs);
AddCommand(PkgCmdIDList.CmdEasyMotionNavigateExtend, mcs);
AddCommand(PkgCmdIDList.CmdEasyMotionNavigateWordExtend, mcs);
}
}

private void AddCommand(uint cmd, OleMenuCommandService mcs)
{
var menuCommandID = new CommandID(GuidList.guidEasyMotionCmdSet, (int)cmd);
var menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
mcs.AddCommand(menuItem);
}

private void MenuItemCallback(object sender, EventArgs e)
{
ITextView textView;
Expand All @@ -81,7 +88,25 @@ private void MenuItemCallback(object sender, EventArgs e)
}
else
{
easyMotionUtil.ChangeToLookingForChar();
var command = sender as MenuCommand;
Debug.Assert(command != null);
var mode = EasyMotionSearchMode.Char;
switch (command.CommandID.ID)
{
case (int)PkgCmdIDList.CmdEasyMotionNavigate:
mode = EasyMotionSearchMode.Char;
break;
case (int)PkgCmdIDList.CmdEasyMotionNavigateWord:
mode = EasyMotionSearchMode.Word;
break;
case (int)PkgCmdIDList.CmdEasyMotionNavigateExtend:
mode = EasyMotionSearchMode.CharExtend;
break;
case (int)PkgCmdIDList.CmdEasyMotionNavigateWordExtend:
mode = EasyMotionSearchMode.WordExtend;
break;
}
easyMotionUtil.ChangeToLookingForChar(mode);
}
}

Expand Down
31 changes: 30 additions & 1 deletion EasyMotion/IEasyMotionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,31 @@

namespace EasyMotion
{
internal enum EasyMotionSearchMode
{
/// <summary>
/// Developer is looking for characters anywhere
/// </summary>
Char,

/// <summary>
/// Developer is looking for words starting with the typed character
/// </summary>
Word,

/// <summary>
/// Same as Char + extending the selection
/// </summary>
CharExtend,

/// <summary>
/// Same as Word + extending the selection
/// </summary>
WordExtend
}



internal enum EasyMotionState
{
/// <summary>
Expand Down Expand Up @@ -37,6 +62,8 @@ internal interface IEasyMotionUtil

EasyMotionState State { get; }

EasyMotionSearchMode SearchMode { get; }

/// <summary>
/// During the LookingForDecision state this will be the character which
/// the user has decided to make an easy motion for
Expand All @@ -47,11 +74,13 @@ internal interface IEasyMotionUtil

void ChangeToDisabled();

void ChangeToLookingForChar();
void ChangeToLookingForChar(EasyMotionSearchMode searchMode);

void ChangeToLookingForDecision(char target);

void ChangeToLookingCharNotFound();

bool IsInWordMode { get; }
}

internal interface IEasyMotionUtilProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Text.Editor;
using System.Windows;
using Microsoft.VisualStudio.Text.Operations;

namespace EasyMotion.Implementation.Adornment
{
Expand All @@ -24,16 +25,21 @@ internal sealed class EasyMotionAdornmentController : IEasyMotionNavigator
private readonly IWpfTextView _wpfTextView;
private readonly IEditorFormatMap _editorFormatMap;
private readonly IClassificationFormatMap _classificationFormatMap;
private readonly ITextSearchService _TextSerachService;
private readonly IEditorOperations _editorOperations;
private readonly Dictionary<string, SnapshotPoint> _navigateMap = new Dictionary<string, SnapshotPoint>();
private readonly object _tag = new object();
private IAdornmentLayer _adornmentLayer;

internal EasyMotionAdornmentController(IEasyMotionUtil easyMotionUtil, IWpfTextView wpfTextview, IEditorFormatMap editorFormatMap, IClassificationFormatMap classificationFormatMap)
internal EasyMotionAdornmentController(IEasyMotionUtil easyMotionUtil, IWpfTextView wpfTextview, IEditorFormatMap editorFormatMap, IClassificationFormatMap classificationFormatMap
, ITextSearchService textSerachService, IEditorOperations editorOperations)
{
_easyMotionUtil = easyMotionUtil;
_wpfTextView = wpfTextview;
_editorFormatMap = editorFormatMap;
_classificationFormatMap = classificationFormatMap;
_TextSerachService = textSerachService;
_editorOperations = editorOperations;
}

internal void SetAdornmentLayer(IAdornmentLayer adornmentLayer)
Expand Down Expand Up @@ -102,18 +108,33 @@ private void AddAdornments()
var endPoint = textViewLines.LastVisibleLine.End;
var snapshot = startPoint.Snapshot;
int navigateIndex = 0;
for (int i = startPoint.Position; i < endPoint.Position; i++)

if (_easyMotionUtil.IsInWordMode && !char.IsLetterOrDigit(_easyMotionUtil.TargetChar))
{
var point = new SnapshotPoint(snapshot, i);
_easyMotionUtil.ChangeToLookingCharNotFound();
return;
}
var toSearch = _easyMotionUtil.TargetChar.ToString();
var data = new FindData()
{
SearchString = _easyMotionUtil.IsInWordMode ? @"\b" + toSearch : toSearch,
TextSnapshotToSearch = snapshot,
FindOptions = _easyMotionUtil.IsInWordMode ? FindOptions.UseRegularExpressions : FindOptions.None
};

if (Char.ToLower(point.GetChar()) == Char.ToLower(_easyMotionUtil.TargetChar) && navigateIndex < NavigationKeys.Length)
var startindex = startPoint.Position;
while (navigateIndex < NavigationKeys.Length)
{
var res = _TextSerachService.FindNext(startindex, false, data);
if (!res.HasValue || res.Value.Start.Position > endPoint.Position)
{
string key = NavigationKeys[navigateIndex];
navigateIndex++;
AddNavigateToPoint(textViewLines, point, key);
break;
}
var key = NavigationKeys[navigateIndex];
AddNavigateToPoint(textViewLines, res.Value.Start, key);
startindex = res.Value.Start.Position + 1;
navigateIndex++;
}

if (navigateIndex == 0)
{
_easyMotionUtil.ChangeToLookingCharNotFound();
Expand Down Expand Up @@ -156,7 +177,14 @@ public bool NavigateTo(string key)
return false;
}

_wpfTextView.Caret.MoveTo(point);
if (_easyMotionUtil.SearchMode == EasyMotionSearchMode.CharExtend || _easyMotionUtil.SearchMode == EasyMotionSearchMode.WordExtend)
{
_editorOperations.ExtendSelection(point.Position);
}
else
{
_wpfTextView.Caret.MoveTo(point);
}
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.VisualStudio.Text.Operations;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
Expand All @@ -25,6 +26,8 @@ internal sealed class EasyMotionAdornmentFactory : IWpfTextViewCreationListener,
private readonly IEasyMotionUtilProvider _easyMotionUtilProvider;
private readonly IEditorFormatMapService _editorFormatMapService;
private readonly IClassificationFormatMapService _classificationFormatMapService;
private readonly ITextSearchService _textSerachService;
private readonly IEditorOperationsFactoryService _editorOperationsFactory;

#pragma warning disable 169
[Export(typeof(AdornmentLayerDefinition))]
Expand All @@ -34,11 +37,14 @@ internal sealed class EasyMotionAdornmentFactory : IWpfTextViewCreationListener,
#pragma warning restore 169

[ImportingConstructor]
internal EasyMotionAdornmentFactory(IEasyMotionUtilProvider easyMotionUtilProvider, IEditorFormatMapService editorFormatMapService, IClassificationFormatMapService classificationFormatMapService)
internal EasyMotionAdornmentFactory(IEasyMotionUtilProvider easyMotionUtilProvider, IEditorFormatMapService editorFormatMapService
, IClassificationFormatMapService classificationFormatMapService, ITextSearchService textSearchService, IEditorOperationsFactoryService editorOperations)
{
_easyMotionUtilProvider = easyMotionUtilProvider;
_editorFormatMapService = editorFormatMapService;
_classificationFormatMapService = classificationFormatMapService;
_textSerachService = textSearchService;
_editorOperationsFactory = editorOperations;
}

private EasyMotionAdornmentController GetOrCreate(IWpfTextView wpfTextView)
Expand All @@ -50,7 +56,7 @@ private EasyMotionAdornmentController GetOrCreate(IWpfTextView wpfTextView)
var easyMotionUtil = _easyMotionUtilProvider.GetEasyMotionUtil(wpfTextView);
var editorFormatMap = _editorFormatMapService.GetEditorFormatMap(wpfTextView);
var classificationFormatMap = _classificationFormatMapService.GetClassificationFormatMap(wpfTextView);
return new EasyMotionAdornmentController(easyMotionUtil, wpfTextView, editorFormatMap, classificationFormatMap);
return new EasyMotionAdornmentController(easyMotionUtil, wpfTextView, editorFormatMap, classificationFormatMap, _textSerachService, _editorOperationsFactory.GetEditorOperations(wpfTextView));
});
}

Expand Down
11 changes: 10 additions & 1 deletion EasyMotion/Implementation/EasyMotionUtil/EasyMotionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ internal sealed class EasyMotionUtil : IEasyMotionUtil
{
private readonly ITextView _textView;
private EasyMotionState _state;
private EasyMotionSearchMode _searchMode;
private char _targetChar;

public EasyMotionState State
{
get { return _state; }
}

public EasyMotionSearchMode SearchMode
{
get { return _searchMode; }
}

public char TargetChar
{
get { return _targetChar; }
Expand All @@ -28,6 +34,8 @@ public ITextView TextView
get { return _textView; }
}

public bool IsInWordMode => SearchMode == EasyMotionSearchMode.Word || SearchMode == EasyMotionSearchMode.WordExtend;

public event EventHandler StateChanged;

internal EasyMotionUtil(ITextView textView)
Expand All @@ -43,9 +51,10 @@ public void ChangeToDisabled()
RaiseStateChanged();
}

public void ChangeToLookingForChar()
public void ChangeToLookingForChar(EasyMotionSearchMode mode)
{
_state = EasyMotionState.LookingForChar;
_searchMode = mode;
_targetChar = (char)0;
RaiseStateChanged();
}
Expand Down
8 changes: 5 additions & 3 deletions EasyMotion/PkgCmdID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ namespace EasyMotion
{
static class PkgCmdIDList
{
public const uint CmdEasyMotionNavigate = 0x100;
public const uint CmdEasyMotionNavigate = 0x100;
public const uint CmdEasyMotionNavigateWord = 0x101;
public const uint CmdEasyMotionNavigateExtend = 0x102;
public const uint CmdEasyMotionNavigateWordExtend = 0x103;


};
};
}