Skip to content

Commit

Permalink
Merge pull request #340 from workgroupengineering/features/Intellisen…
Browse files Browse the repository at this point in the history
…se/Prediction_1

feat: Intellisense Enanched Prediction 1
  • Loading branch information
maxkatz6 authored Jun 15, 2023
2 parents 9e2e8a5 + 3ae6a0e commit 0b7a126
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
17 changes: 13 additions & 4 deletions AvaloniaVS.Shared/IntelliSense/XamlCompletion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ public XamlCompletion(Completion completion)
completion.Description,
GetImage(completion.Kind),
completion.Kind.ToString(),
suffix: string.IsNullOrWhiteSpace(completion.Suffix) ? string.Empty : $"({completion.Suffix})")
suffix: string.IsNullOrWhiteSpace(completion.Suffix) ? string.Empty : $"({completion.Suffix})")
{
if (completion.RecommendedCursorOffset.HasValue)
{
CursorOffset = completion.InsertText.Length - completion.RecommendedCursorOffset.Value;
}

TriggerCompletion = completion.TriggerCompletionAfterInsert;
Kind = completion.Kind;
DeleteTextOffset = completion.DeleteTextOffset;
if (completion.Priority < 255)
{
this.AttributeIcons = new Microsoft.VisualStudio.Language.Intellisense.CompletionIcon2[]
{
new (KnownMonikers.OverlayProtected,"",""),
};
}
}

public int? DeleteTextOffset { get; }
Expand All @@ -41,7 +48,7 @@ public override string InsertionText
{
if (HasFlag(Kind, CompletionKind.Name) && !string.IsNullOrEmpty(Suffix))
{
return $"{Suffix.Substring(1,Suffix.Length-2)}#{base.InsertionText}";
return $"{Suffix.Substring(1, Suffix.Length - 2)}#{base.InsertionText}";
}
return base.InsertionText;
}
Expand All @@ -53,6 +60,8 @@ public override string InsertionText

public CompletionKind Kind { get; }

public bool TriggerCompletion { get; }

public static IEnumerable<XamlCompletion> Create(
IEnumerable<Completion> source)
{
Expand Down Expand Up @@ -100,7 +109,7 @@ private static void LoadImages()
ThreadHelper.ThrowIfNotOnUIThread();

var capacity = Enum.GetValues(typeof(CompletionKind)).Cast<int>().Max() + 1;

s_images = new ImageMoniker[capacity];
s_images[(int)CompletionKind.Property] = KnownMonikers.Property;
s_images[(int)CompletionKind.Event] = KnownMonikers.Event;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ private bool HandleSessionCompletion(char c)
TriggerCompletion();
}
}
else if (state != XmlParser.ParserState.StartElement)
else if (state != XmlParser.ParserState.StartElement || selected.TriggerCompletion)
{
TriggerCompletion();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,23 @@ public record Completion(string DisplayText,
CompletionKind Kind,
int? RecommendedCursorOffset = null,
string? Suffix = null,
int? DeleteTextOffset = null
int? DeleteTextOffset = null,
byte Priority = 255
)
{
public override string ToString() => DisplayText;

public Completion(string insertText, CompletionKind kind, string? suffix = default) :
this(insertText, insertText, insertText, kind, Suffix: suffix)
public Completion(string insertText, CompletionKind kind, string? suffix = default, byte priority = 255) :
this(insertText, insertText, insertText, kind, Suffix: suffix, Priority: priority)
{

}

public Completion(string displayText, string insertText, CompletionKind kind, string? suffix = default) :
this(displayText, insertText, displayText, kind)
public Completion(string displayText, string insertText, CompletionKind kind, string? suffix = default, byte priority = 255) :
this(displayText, insertText, displayText, kind, Priority: priority)
{

}

public bool TriggerCompletionAfterInsert { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private static Dictionary<string, string> GetNamespaceAliases(string xml)
var name = closingState.GetParentTagName(0);
if (name == null)
return null;
completions.Add(new Completion("/" + name + ">", CompletionKind.Class));
completions.Add(new Completion("/" + name + ">", CompletionKind.Class, priority: 0));
}
else if (tagName.Contains('.'))
{
Expand All @@ -260,7 +260,23 @@ private static Dictionary<string, string> GetNamespaceAliases(string xml)
}
else
{
completions.AddRange(_helper.FilterTypes(tagName).Select(kvp =>
if (state.GetParentTagName(1) is string parentTag)
{
if (!state.IsInClosingTag)
{
completions.Add(new Completion("/" + parentTag + ">", CompletionKind.Class, priority: 0));
}
if (parentTag.IndexOf('.') == -1)
{
completions.Add(new Completion(parentTag, $"{parentTag}.", CompletionKind.Class, priority: 1)
{
TriggerCompletionAfterInsert = true,
});
}
}

completions.AddRange(_helper.FilterTypes(tagName)
.Select(kvp =>
{
if (kvp.Value.IsMarkupExtension)
{
Expand Down Expand Up @@ -327,7 +343,7 @@ private static Dictionary<string, string> GetNamespaceAliases(string xml)

if (targetType.IsAvaloniaObjectType)
{
if (string.IsNullOrEmpty(attributeName) || "xmlns".StartsWith(attributeName,StringComparison.OrdinalIgnoreCase))
if (string.IsNullOrEmpty(attributeName) || "xmlns".StartsWith(attributeName, StringComparison.OrdinalIgnoreCase))
{
completions.Add(new("xmlns:", CompletionKind.Class));
}
Expand Down Expand Up @@ -508,7 +524,8 @@ private static List<Completion> SortCompletions(List<Completion> completions)
// Group the completions based on Kind, and sort the completions for each group
return completions
.GroupBy(i => i.Kind, (kind, compl) =>
(Kind: kind, Completions: compl.OrderBy(j => j.DisplayText)))
(Kind: kind, Completions: compl
.OrderBy(j => j.Priority).ThenBy(j => j.DisplayText)))
.OrderBy(i => GetCompletionPriority(i.Kind))
.SelectMany(i => i.Completions)
.ToList();
Expand Down
6 changes: 3 additions & 3 deletions tests/CompletionEngineTests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ public void Completions_Should_Be_Sorted()
{
var compl = GetCompletionsFor("<DataTemplate");

Assert.Equal(2, compl.Completions.Count);
Assert.Equal("DataTemplate", compl.Completions[0].DisplayText);
Assert.Equal("DataTemplates", compl.Completions[1].DisplayText);
Assert.Equal(4, compl.Completions.Count);
Assert.Equal("DataTemplate", compl.Completions[2].DisplayText);
Assert.Equal("DataTemplates", compl.Completions[3].DisplayText);
}

[Fact]
Expand Down

0 comments on commit 0b7a126

Please sign in to comment.