-
Notifications
You must be signed in to change notification settings - Fork 199
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
Changes from 8 commits
3a71582
af436d4
f3108dc
898219a
3b0a132
c92117a
90ee0dc
948398c
441610e
741578a
2ba084c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||||||
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. 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(); | ||||||
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.
Suggested change
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. Ditto below |
||||||
} | ||||||
if (newArray is null) | ||||||
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. 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; | ||||||
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. I'd avoid using Linq when possible because this is a hot path,
Suggested change
|
||||||
switch (diff.Operation) | ||||||
{ | ||||||
case DiffEdit.Type.Delete: | ||||||
if (current != null && | ||||||
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. Possibly break out the logic in each case into a separate method? If you prefer it this way, that's fine too :) 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 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.
There was a problem hiding this comment.
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 50There was a problem hiding this comment.
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 getpreviousResults == null
, it also happens if thepreviousResult
was evicted from the cache (most likely scenario for that is if someone was working on a different document for a while).There was a problem hiding this comment.
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 aTryGet
would be more inline with how other cache APIs work.