Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Symbol indexer #521

Merged
merged 10 commits into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
26 changes: 26 additions & 0 deletions src/Analysis/Engine/Impl/Indexing/ISymbolIndex.cs
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);
}
}
40 changes: 40 additions & 0 deletions src/Analysis/Engine/Impl/Indexing/SymbolExtensions.cs
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;
}
}
}
}
}
66 changes: 66 additions & 0 deletions src/Analysis/Engine/Impl/Indexing/SymbolIndex.cs
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you can easily return IReadOnlyList<HierarchicalSymbol> here.

Copy link
Member Author

Choose a reason for hiding this comment

The 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 HierarchicalSymbol itself contains an IList<HierarchicalSymbol>.

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)));

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;
}
}
}
Loading