Skip to content

Commit

Permalink
! Navigated to symbol declaration identifier instead of the start of …
Browse files Browse the repository at this point in the history
…node span when items in Navi Bar was clicked
  • Loading branch information
wmjordan committed Dec 17, 2018
1 parent 234cd9b commit 248d9e0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
21 changes: 21 additions & 0 deletions Codist/Helpers/CodeAnalysisHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,27 @@ public static IdentifierNameSyntax GetFirstIdentifier(this SyntaxNode node) {
public static IdentifierNameSyntax GetLastIdentifier(this SyntaxNode node) {
return node.DescendantNodes().LastOrDefault(i => i.IsKind(SyntaxKind.IdentifierName)) as IdentifierNameSyntax;
}
public static SyntaxToken GetIdentifierToken(this SyntaxNode node) {
switch (node.Kind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.StructDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.EnumDeclaration:
return (node as BaseTypeDeclarationSyntax).Identifier;
case SyntaxKind.DelegateDeclaration: return (node as DelegateDeclarationSyntax).Identifier;
case SyntaxKind.MethodDeclaration: return (node as MethodDeclarationSyntax).Identifier;
case SyntaxKind.OperatorDeclaration: return (node as OperatorDeclarationSyntax).OperatorToken;
case SyntaxKind.ConversionOperatorDeclaration: return (node as ConversionOperatorDeclarationSyntax).Type.GetFirstToken();
case SyntaxKind.ConstructorDeclaration: return (node as ConstructorDeclarationSyntax).Identifier;
case SyntaxKind.DestructorDeclaration: return (node as DestructorDeclarationSyntax).Identifier;
case SyntaxKind.PropertyDeclaration: return (node as PropertyDeclarationSyntax).Identifier;
case SyntaxKind.IndexerDeclaration: return (node as IndexerDeclarationSyntax).ThisKeyword;
case SyntaxKind.EventDeclaration: return (node as EventDeclarationSyntax).Identifier;
case SyntaxKind.EnumMemberDeclaration: return (node as EnumMemberDeclarationSyntax).Identifier;
case SyntaxKind.VariableDeclarator: return (node as VariableDeclaratorSyntax).Identifier;
}
return node.GetFirstToken();
}
public static string GetName(this NameSyntax name) {
if (name == null) {
return null;
Expand Down
45 changes: 13 additions & 32 deletions Codist/NaviBar/CSharpBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,24 +265,11 @@ void FindInProject(string text) {
}

async Task FindDeclarationsAsync(string symbolName, CancellationToken token) {
var result = new SortedSet<ISymbol>(Comparer<ISymbol>.Create((x, y) => {
var l = x.Name.Length - y.Name.Length;
return l != 0 ? l : x.GetHashCode() - y.GetHashCode();
}));
int maxNameLength = 0;
foreach (var symbol in await Microsoft.CodeAnalysis.FindSymbols.SymbolFinder.FindSourceDeclarationsAsync(_Bar._SemanticContext.Document.Project, name => name.IndexOf(symbolName, StringComparison.OrdinalIgnoreCase) != -1, token)) {
if (result.Count < 50) {
result.Add(symbol);
}
else {
maxNameLength = result.Max.Name.Length;
if (symbol.Name.Length < maxNameLength) {
result.Remove(result.Max);
result.Add(symbol);
}
}
}
var result = await _Bar._SemanticContext.Document.Project.FindDeclarationsAsync(symbolName, 50, false, false, SymbolFilter.All, token);
foreach (var item in result) {
if (token.IsCancellationRequested) {
break;
}
Items.Add(new SymbolItem(item, _Bar._SemanticContext));
}
}
Expand All @@ -292,26 +279,14 @@ sealed class MemberFinderBox : ThemedTextBox

public MemberFinderBox(ItemCollection items) {
_Items = items;
PreviewKeyUp += ControlMenuSelection;
IsVisibleChanged += (s, args) => {
var b = s as TextBox;
if (b.IsVisible) {
b.Focus();
b.SelectAll();
}
};
}

void ControlMenuSelection(object sender, KeyEventArgs e) {
if (e.Key == Key.Enter && _Items.Count > 1) {
foreach (var item in _Items) {
var nav = item as NaviItem;
if (nav != null) {
nav.RaiseEvent(new RoutedEventArgs(ClickEvent));
return;
}
}
}
}
}
}
sealed class NaviItem : ThemedMenuItem, IMemberFilterable
Expand Down Expand Up @@ -549,7 +524,13 @@ async Task RefreshItemsAsync(ItemCollection items, SyntaxNode node, Cancellation

#region Helper methods
public void GoToLocation() {
_Bar._SemanticContext.RelocateDeclarationNode(Node)?.GetLocation().GoToSource();
var node = _Bar._SemanticContext.RelocateDeclarationNode(Node);
if (node != null) {
node.GetIdentifierToken().GetLocation().GoToSource();
}
else {
CodistPackage.ShowErrorMessageBox("Syntax node was not found", nameof(Codist), true);
}
}

void SelectOrGoToSource() {
Expand All @@ -562,7 +543,7 @@ void SelectOrGoToSource(SyntaxNode node) {
_Bar._View.SelectNode(node, Keyboard.Modifiers != ModifierKeys.Control);
}
else {
node.GetLocation().GoToSource();
node.GetIdentifierToken().GetLocation().GoToSource();
}
}
#endregion
Expand Down

0 comments on commit 248d9e0

Please sign in to comment.