This repository was archived by the owner on Apr 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 133
Symbol indexer #521
Merged
Merged
Symbol indexer #521
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e89df57
first working indexer prototype
jakebailey b5497d4
rework indexer, add VS-specific fields, full walker testing
jakebailey eba0c1f
basic indexer testing
jakebailey 9b3b79e
drop async, case insensitive match
jakebailey 2a8fe55
remove null check in Flatten
jakebailey 2e50be3
check symbol ordering strictly, process RHS of assignments before lef…
jakebailey 4e26002
inline zip, name tuple elements
jakebailey f8e1e5b
support comprehensions
jakebailey ba622fb
handle nested comprehensions
jakebailey c46ea7e
ignore __future__ imports
jakebailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Python Tools for Visual Studio | ||
// Copyright(c) Microsoft Corporation | ||
// All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the License); you may not use | ||
// this file except in compliance with the License. You may obtain a copy of the | ||
// License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS | ||
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY | ||
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
// MERCHANTABLITY OR NON-INFRINGEMENT. | ||
// | ||
// See the Apache Version 2.0 License for specific language governing | ||
// permissions and limitations under the License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.PythonTools.Analysis.Indexing { | ||
internal interface ISymbolIndex { | ||
IEnumerable<HierarchicalSymbol> HierarchicalDocumentSymbols(Uri uri); | ||
|
||
IEnumerable<FlatSymbol> WorkspaceSymbols(string query); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Python Tools for Visual Studio | ||
// Copyright(c) Microsoft Corporation | ||
// All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the License); you may not use | ||
// this file except in compliance with the License. You may obtain a copy of the | ||
// License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS | ||
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY | ||
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
// MERCHANTABLITY OR NON-INFRINGEMENT. | ||
// | ||
// See the Apache Version 2.0 License for specific language governing | ||
// permissions and limitations under the License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.PythonTools.Analysis.Indexing { | ||
internal static class SymbolExtensions { | ||
public static IEnumerable<FlatSymbol> Flatten(this IEnumerable<HierarchicalSymbol> docSyms, Uri uri, string parent = null, int? depthLimit = null) { | ||
foreach (var sym in docSyms ?? Enumerable.Empty<HierarchicalSymbol>()) { | ||
yield return new FlatSymbol(sym.Name, sym.Kind, uri, sym.SelectionRange, parent); | ||
|
||
if (depthLimit != null) { | ||
if (depthLimit < 1) { | ||
yield break; | ||
} | ||
depthLimit--; | ||
} | ||
|
||
foreach (var si in sym.Children.Flatten(uri, sym.Name, depthLimit)) { | ||
yield return si; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Python Tools for Visual Studio | ||
// Copyright(c) Microsoft Corporation | ||
// All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the License); you may not use | ||
// this file except in compliance with the License. You may obtain a copy of the | ||
// License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS | ||
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY | ||
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
// MERCHANTABLITY OR NON-INFRINGEMENT. | ||
// | ||
// See the Apache Version 2.0 License for specific language governing | ||
// permissions and limitations under the License. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.PythonTools.Analysis.Infrastructure; | ||
using Microsoft.PythonTools.Parsing.Ast; | ||
|
||
namespace Microsoft.PythonTools.Analysis.Indexing { | ||
internal class SymbolIndex : ISymbolIndex { | ||
private readonly ConcurrentDictionary<Uri, IReadOnlyList<HierarchicalSymbol>> _index = new ConcurrentDictionary<Uri, IReadOnlyList<HierarchicalSymbol>>(); | ||
|
||
public SymbolIndex() { } | ||
|
||
public IEnumerable<HierarchicalSymbol> HierarchicalDocumentSymbols(Uri uri) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you can easily return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's true, though my intention was that a future PR allows this data to be persisted in a local database, where not needing to collect the entire result beforehand. Though, I guess it's a bit pointless when the |
||
return _index.TryGetValue(uri, out var list) ? list : Enumerable.Empty<HierarchicalSymbol>(); | ||
} | ||
|
||
public IEnumerable<FlatSymbol> WorkspaceSymbols(string query) { | ||
foreach (var kvp in _index) { | ||
foreach (var found in WorkspaceSymbolsQuery(query, kvp.Key, kvp.Value)) { | ||
yield return found; | ||
} | ||
} | ||
} | ||
|
||
private IEnumerable<FlatSymbol> WorkspaceSymbolsQuery(string query, Uri uri, IEnumerable<HierarchicalSymbol> symbols) { | ||
// Some semblance of a BFS. | ||
var ll = new LinkedList<(HierarchicalSymbol, string)>(symbols.Select(s => (s, (string)null))); | ||
jakebailey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
while (ll.First != null) { | ||
var (sym, parent) = ll.First.Value; | ||
ll.RemoveFirst(); | ||
|
||
if (sym.Name.ContainsOrdinal(query, ignoreCase: true)) { | ||
yield return new FlatSymbol(sym.Name, sym.Kind, uri, sym.SelectionRange, parent); | ||
} | ||
|
||
foreach (var child in sym.Children.MaybeEnumerate()) { | ||
ll.AddLast((child, sym.Name)); | ||
} | ||
} | ||
} | ||
|
||
public void UpdateParseTree(Uri uri, PythonAst ast) { | ||
var walker = new SymbolIndexWalker(ast); | ||
ast.Walk(walker); | ||
_index[uri] = walker.Symbols; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.