From 248d9e09c1224b329f6bd06b082ee4133e0b4776 Mon Sep 17 00:00:00 2001 From: WMJ Date: Mon, 17 Dec 2018 20:26:50 +0800 Subject: [PATCH] ! Navigated to symbol declaration identifier instead of the start of node span when items in Navi Bar was clicked --- Codist/Helpers/CodeAnalysisHelper.cs | 21 +++++++++++++ Codist/NaviBar/CSharpBar.cs | 45 ++++++++-------------------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/Codist/Helpers/CodeAnalysisHelper.cs b/Codist/Helpers/CodeAnalysisHelper.cs index 2037c1d5..a514aaab 100644 --- a/Codist/Helpers/CodeAnalysisHelper.cs +++ b/Codist/Helpers/CodeAnalysisHelper.cs @@ -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; diff --git a/Codist/NaviBar/CSharpBar.cs b/Codist/NaviBar/CSharpBar.cs index 0aabf00f..6c508a44 100644 --- a/Codist/NaviBar/CSharpBar.cs +++ b/Codist/NaviBar/CSharpBar.cs @@ -265,24 +265,11 @@ void FindInProject(string text) { } async Task FindDeclarationsAsync(string symbolName, CancellationToken token) { - var result = new SortedSet(Comparer.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)); } } @@ -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 @@ -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() { @@ -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