Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the algorithm used to calculate semantic diff #2126

Merged
merged 11 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from 8 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
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ public override SemanticTokensOrSemanticTokensEdits GetSemanticTokensEdits(Razor
}
var newTokens = ConvertSemanticRangesToSemanticTokens(semanticRanges, codeDocument);

var semanticEdits = SyntaxTokenToSemanticTokensEditHelper.ConvertSyntaxTokensToSemanticEdits(newTokens, previousResults);
if (previousResults is null)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can remove this if if you move line 59 above line 50

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What if I convert it to a conditional expression instead? previousResultId == null isn't the only way to get previousResults == null, it also happens if the previousResult was evicted from the cache (most likely scenario for that is if someone was working on a different document for a while).

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah. Didn't realize _semanticTokensCache.Get(previousResultId); could return null. In case you touch it in the future, making that a TryGet would be more inline with how other cache APIs work.

{
return newTokens;
}

var semanticEdits = SemanticTokensEditsDiffer.ComputeSemanticTokensEdits(newTokens, previousResults);

return semanticEdits;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright(c).NET Foundation.All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.LanguageServer.Semantic.Models;
using System.Linq;
Copy link
Contributor

Choose a reason for hiding this comment

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

Sort


namespace Microsoft.AspNetCore.Razor.LanguageServer.Semantic.Services
{
internal class SemanticTokensEditsDiffer : TextDiffer
{
private SemanticTokensEditsDiffer(uint[] oldArray, uint[] newArray)
{
if (oldArray is null)
{
throw new ArgumentNullException();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
throw new ArgumentNullException();
throw new ArgumentNullException(nameof(oldArray));

Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto below

}
if (newArray is null)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: newline

{
throw new ArgumentNullException();
}

OldArray = oldArray;
NewArray = newArray;
}

private uint[] OldArray { get; }
private uint[] NewArray { get; }

protected override int OldTextLength => OldArray.Length;
protected override int NewTextLength => NewArray.Length;

protected override bool ContentEquals(int oldTextIndex, int newTextIndex)
{
return OldArray[oldTextIndex] == NewArray[newTextIndex];
}

public static SemanticTokensOrSemanticTokensEdits ComputeSemanticTokensEdits(
SemanticTokens newTokens,
IReadOnlyList<uint> previousResults)
{
var differ = new SemanticTokensEditsDiffer(previousResults.ToArray(), newTokens.Data);
var diffs = differ.ComputeDiff();
var edits = differ.ProcessEdits(diffs);
var result = new SemanticTokensEditCollection
{
ResultId = newTokens.ResultId,
Edits = edits,
};

return result;
}

private IReadOnlyList<SemanticTokensEdit> ProcessEdits(IReadOnlyList<DiffEdit> diffs)
{
var results = new List<SemanticTokensEdit>();
foreach (var diff in diffs)
{
var current = results.Any() ? results.Last() : null;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd avoid using Linq when possible because this is a hot path,

Suggested change
var current = results.Any() ? results.Last() : null;
var current = results.Count > 0 ? results[results.Count - 1] : null;

switch (diff.Operation)
{
case DiffEdit.Type.Delete:
if (current != null &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Possibly break out the logic in each case into a separate method? If you prefer it this way, that's fine too :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That wouldn't be my preference, the function fits on my screen in one go, is accomplishing a single task, and has interaction with the loop ("current" or results.Add). I will admit it's pushing it though, if it got much larger I'd probably agree with you.

current.Start + current.DeleteCount == diff.Position)
{
current.DeleteCount += 1;
}
else
{
results.Add(new SemanticTokensEdit
{
Start = diff.Position,
Data = new uint[] { },
DeleteCount = 1,
});
}
break;
case DiffEdit.Type.Insert:
if (current != null &&
current.Data.Count() > 0 &&
current.Start == diff.Position)
{
current.Data = current.Data.Append(NewArray[diff.NewTextPosition.Value]);
}
else
{
results.Add(new SemanticTokensEdit
{
Start = diff.Position,
Data = new uint[] { NewArray[diff.NewTextPosition.Value] },
DeleteCount = 0,
});
}
break;
}
}

return results;
}
}
}

This file was deleted.

This file was deleted.

Loading