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

Provide token-based highlight if references are not available #2025

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
c6a9c22
Remove stale reference
Sep 30, 2019
1360827
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 1, 2019
ccaaa02
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 4, 2019
da40dcc
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 4, 2019
c348ac3
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 5, 2019
53bc044
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 7, 2019
484a92a
Merge branch 'master' of https://github.com/MikhailArkhipov/python-la…
Oct 7, 2019
e6df3aa
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 8, 2019
1d289d8
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 8, 2019
126f355
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 12, 2019
7e715f3
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 25, 2019
32923a5
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 31, 2019
1b72f4b
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 2, 2019
f74d5b6
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 7, 2019
0b28fa4
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 12, 2019
6109ac7
Don't suppress LHS diagnostics on augmented assign
Nov 12, 2019
bcfc3b7
Revert "Don't suppress LHS diagnostics on augmented assign"
Nov 12, 2019
dc286b8
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 13, 2019
0aab4f5
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 16, 2019
b953ce7
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 18, 2019
aef887d
Merge branch 'master' of https://github.com/microsoft/python-language…
Dec 10, 2019
b97b641
Merge branch 'master' of https://github.com/microsoft/python-language…
Dec 11, 2019
c16646d
Escape [ and ]
Dec 13, 2019
f3e08d5
Merge branch 'master' of https://github.com/MikhailArkhipov/python-la…
Dec 13, 2019
5700642
PR feedback
Dec 13, 2019
1d091db
Merge branch 'master' of https://github.com/microsoft/python-language…
Dec 13, 2019
b8c3615
Merge branch 'master' of https://github.com/microsoft/python-language…
Dec 16, 2019
6a88069
Merge branch 'master' of https://github.com/microsoft/python-language…
Jan 28, 2020
078c975
Merge branch 'master' of https://github.com/microsoft/python-language…
Mar 12, 2020
ac07704
Merge master
Mar 18, 2020
40be62b
Merge branch 'master' of https://github.com/microsoft/python-language…
Mar 19, 2020
b384853
Merge branch 'master' of https://github.com/microsoft/python-language…
Mar 20, 2020
a0632ef
Merge branch 'master' of https://github.com/microsoft/python-language…
Mar 26, 2020
c9b72bd
Merge branch 'master' of https://github.com/microsoft/python-language…
Mar 26, 2020
1e8291b
Merge branch 'master' of https://github.com/microsoft/python-language…
Apr 17, 2020
d4beb86
Merge branch 'master' of https://github.com/microsoft/python-language…
May 9, 2020
d5afe97
Hightlight of unknown items
May 1, 2020
f155711
Small fix
May 9, 2020
e4da882
Tests, token cache
May 10, 2020
415a986
Cache tests
May 10, 2020
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
105 changes: 98 additions & 7 deletions src/LanguageServer/Impl/Sources/DocumentHighlightSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Python.Analysis;
using Microsoft.Python.Analysis.Documents;
using Microsoft.Python.Analysis.Modules;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Core;
using Microsoft.Python.Core.IO;
using Microsoft.Python.Core.Text;
using Microsoft.Python.LanguageServer.Documents;
using Microsoft.Python.LanguageServer.Protocol;
using Microsoft.Python.Parsing;

namespace Microsoft.Python.LanguageServer.Sources {
internal sealed class DocumentHighlightSource {
private const int DocumentHighlightAnalysisTimeout = 10000;
private const int DocumentHighlightAnalysisTimeout = 1000;
private static TokenCache _tokenCache = new TokenCache(10, TimeSpan.FromMinutes(30));
private readonly IServiceContainer _services;

public DocumentHighlightSource(IServiceContainer services) {
Expand All @@ -48,17 +48,108 @@ public async Task<DocumentHighlight[]> DocumentHighlightAsync(Uri uri, SourceLoc

var definition = definitionSource.FindDefinition(analysis, location, out var definingMember);
if (definition == null || definingMember == null) {
return Array.Empty<DocumentHighlight>();
return FromTokens(analysis, location);
}

var rootDefinition = definingMember.GetRootDefinition();

var result = rootDefinition.References
.Where(r => r.DocumentUri.Equals(uri))
.Select((r, i) => new DocumentHighlight { kind = (i == 0) ? DocumentHighlightKind.Write : DocumentHighlightKind.Read, range = r.Span })
.Select((r, i) => new DocumentHighlight {
kind = i == 0 ? DocumentHighlightKind.Write : DocumentHighlightKind.Read, range = r.Span
})
.ToArray();

return result;
}

private static DocumentHighlight[] FromTokens(IDocumentAnalysis analysis, SourceLocation location) {
var position = analysis.Ast.LocationToIndex(location);
var content = analysis.Document.Content;

var tokens = _tokenCache.GetTokens(analysis.Document.Content, analysis.Document.Interpreter.LanguageVersion);
var t = tokens.FirstOrDefault(x => x.SourceSpan.Start.Index <= position && position < x.SourceSpan.End.Index);
if (t.Category != TokenCategory.None) {
var length = t.SourceSpan.End.Index - t.SourceSpan.Start.Index;
return tokens
.Where(x =>
x.SourceSpan.End.Index - x.SourceSpan.Start.Index == length &&
string.Compare(content, x.SourceSpan.Start.Index, content, t.SourceSpan.Start.Index, length) == 0)
.Select(s => new DocumentHighlight {
kind = DocumentHighlightKind.Text,
range = s.SourceSpan
}).ToArray();
}

return Array.Empty<DocumentHighlight>();
}
}


internal class TokenCache {
internal class Entry {
public DateTime AccessTime;
public WeakReference<IReadOnlyList<TokenInfo>> Tokens;
}

private readonly Dictionary<long, Entry> _cache = new Dictionary<long, Entry>();
private readonly int _maxEntries;
private readonly TimeSpan _expiration;

public TokenCache(int maxEntries, TimeSpan expiration) {
_maxEntries = maxEntries;
_expiration = expiration;
}

public IReadOnlyList<TokenInfo> GetTokens(string content, PythonLanguageVersion languageVersion) {
IReadOnlyList<TokenInfo> tokens;

var hash = GetHash(content);
if (_cache.TryGetValue(hash, out var entry)) {
if (entry.Tokens.TryGetTarget(out tokens)) {
entry.AccessTime = DateTime.Now;
return tokens;
}
}

var tokenizer = new Tokenizer(languageVersion);
using (var sr = new StringReader(content)) {
tokenizer.Initialize(null, sr, SourceLocation.MinValue);
tokens = tokenizer.ReadTokens(content.Length);
_cache[hash] = new Entry {
AccessTime = DateTime.Now,
Tokens = new WeakReference<IReadOnlyList<TokenInfo>>(tokens)
};
}

var byTime = _cache.OrderByDescending(kvp => (DateTime.Now - kvp.Value.AccessTime).TotalSeconds).ToArray();

var expired = byTime.TakeWhile(kvp => DateTime.Now - kvp.Value.AccessTime > _expiration).ToArray();
foreach (var e in expired) {
_cache.Remove(e.Key);
}

if (_cache.Count > _maxEntries) {
var (key, _) = byTime.FirstOrDefault();
if (key != default) {
_cache.Remove(key);
}
}

return tokens;
}

// For tests
internal IEnumerable<(DateTime, IReadOnlyList<TokenInfo>)> Entries
=> _cache.Values.Select(kvp => {
kvp.Tokens.TryGetTarget(out var t);
return (kvp.AccessTime, t);
});

internal static long GetHash(string content) {
using (var sha = SHA1.Create()) {
return BitConverter.ToInt64(sha.ComputeHash(Encoding.UTF32.GetBytes(content)));
}
}
}
}
83 changes: 76 additions & 7 deletions src/LanguageServer/Test/DocumentHighlightTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
// permissions and limitations under the License.

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Python.Analysis.Analyzer;
using Microsoft.Python.Analysis.Documents;
using Microsoft.Python.Core.Text;
using Microsoft.Python.LanguageServer.Protocol;
using Microsoft.Python.LanguageServer.Sources;
using Microsoft.Python.LanguageServer.Tests.FluentAssertions;
using Microsoft.Python.Parsing.Tests;
using Microsoft.Python.Parsing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestUtilities;

Expand Down Expand Up @@ -76,10 +75,80 @@ def func(x):

[TestMethod, Priority(0)]
public async Task HighlightEmptyDocument() {
await GetAnalysisAsync(string.Empty);
var analysis = await GetAnalysisAsync(string.Empty);
var dhs = new DocumentHighlightSource(Services);
var references = await dhs.DocumentHighlightAsync(null, new SourceLocation(1, 1));
references.Should().BeEmpty();
var highlights = await dhs.DocumentHighlightAsync(analysis.Document.Uri, new SourceLocation(1, 1));
highlights.Should().BeEmpty();
}

[TestMethod, Priority(0)]
public async Task HighlightNonReference() {
const string code = @"
x = y = 0
assert x == 1
assert y != 3
";
var analysis = await GetAnalysisAsync(code);
var dhs = new DocumentHighlightSource(Services);
var highlights = await dhs.DocumentHighlightAsync(analysis.Document.Uri, new SourceLocation(3, 5));

highlights.Should().HaveCount(2);
highlights[0].range.Should().Be(2, 0, 2, 6);
highlights[0].kind.Should().Be(DocumentHighlightKind.Text);
highlights[1].range.Should().Be(3, 0, 3, 6);
highlights[1].kind.Should().Be(DocumentHighlightKind.Text);
}

[TestMethod, Priority(0)]
public async Task HighlightUndefined() {
const string code = @"
assert x == 1
assert x != 3
";
var analysis = await GetAnalysisAsync(code);
var dhs = new DocumentHighlightSource(Services);
var highlights = await dhs.DocumentHighlightAsync(analysis.Document.Uri, new SourceLocation(2, 8));

highlights.Should().HaveCount(2);
highlights[0].range.Should().Be(1, 7, 1, 8);
highlights[0].kind.Should().Be(DocumentHighlightKind.Text);
highlights[1].range.Should().Be(2, 7, 2, 8);
highlights[1].kind.Should().Be(DocumentHighlightKind.Text);
}

[TestMethod, Priority(0)]
public void TokenCacheEntriesLimit() {
var c = new TokenCache(3, TimeSpan.FromMinutes(1));
c.GetTokens("1", PythonLanguageVersion.V38);
c.GetTokens("12", PythonLanguageVersion.V38);
c.GetTokens("123", PythonLanguageVersion.V38);
c.GetTokens("1234", PythonLanguageVersion.V38);

(DateTime AccessTime, IReadOnlyList<TokenInfo> Tokens)[] e = c.Entries.ToArray();

e.Should().HaveCount(3);
var byTime = e.OrderBy(x => x.AccessTime).ToArray();
(byTime[0].Tokens[0].SourceSpan.End.Column - byTime[0].Tokens[0].SourceSpan.Start.Column).Should().Be(2);
(byTime[1].Tokens[0].SourceSpan.End.Column - byTime[1].Tokens[0].SourceSpan.Start.Column).Should().Be(3);
(byTime[2].Tokens[0].SourceSpan.End.Column - byTime[2].Tokens[0].SourceSpan.Start.Column).Should().Be(4);
}

[TestMethod, Priority(0)]
public async Task TokenCacheExpiration() {
var c = new TokenCache(5, TimeSpan.FromMilliseconds(50));
c.GetTokens("1", PythonLanguageVersion.V38);
await Task.Delay(10);
c.GetTokens("12", PythonLanguageVersion.V38);
await Task.Delay(10);
c.GetTokens("123", PythonLanguageVersion.V38);
await Task.Delay(40);
c.GetTokens("1234", PythonLanguageVersion.V38);

(DateTime AccessTime, IReadOnlyList<TokenInfo> Tokens)[] e = c.Entries.ToArray();
e.Should().HaveCount(2);
var byTime = e.OrderBy(x => x.AccessTime).ToArray();
(byTime[0].Tokens[0].SourceSpan.End.Column - byTime[1].Tokens[0].SourceSpan.Start.Column).Should().Be(3);
(byTime[1].Tokens[0].SourceSpan.End.Column - byTime[1].Tokens[0].SourceSpan.Start.Column).Should().Be(4);
}
}
}