Skip to content

Commit

Permalink
Merge pull request #323 from workgroupengineering/features/xmlns_auto…
Browse files Browse the repository at this point in the history
…complete

feat: improved xmlns autocomplete
  • Loading branch information
maxkatz6 authored Jun 9, 2023
2 parents 6809360 + 6cbd827 commit 8d3834d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ private bool HandleSessionCompletion(char c)
_textView.Caret.MoveTo(newCursorPos);
}

// special-cased avoid TriggerCompletion
if (selected.InsertionText == "xmlns:")
{
return true;
}

// Ideally, we should only parse the current line of text, where the parser State would return
// 'None' if you're spreading control attributes out across multiple lines
// BUT, Selectors can span multiple lines (aggregates separated by ',') and this theory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ private static void PreProcessTypes(Dictionary<string, MetadataType> types, Meta
types.Add(xDataType.Name, xDataType);
types.Add(xCompiledBindings.Name, xCompiledBindings);

metadata.AddType("", new MetadataType("xmlns") { IsXamlDirective = true });
//metadata.AddType("", new MetadataType("xmlns") { IsXamlDirective = true });
}

private static void PostProcessTypes(Dictionary<string, MetadataType> types, Metadata metadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,12 @@ private static Dictionary<string, string> GetNamespaceAliases(string xml)
attributeSuffix = "";
attributeOffset = 0;
}

if (state.AttributeName?.Contains('.') == true)
var attributeName = state.AttributeName;
if (attributeName?.Contains('.') == true)
{
var dotPos = state.AttributeName.IndexOf('.');
var dotPos = attributeName.IndexOf('.');
curStart += dotPos + 1;
var split = state.AttributeName.Split(new[] { '.' }, 2);
var split = attributeName.Split(new[] { '.' }, 2);
completions.AddRange(_helper.FilterPropertyNames(split[0], split[1], attached: true, hasSetter: true)
.Select(x => new Completion(x, x + attributeSuffix, x, CompletionKind.AttachedProperty, x.Length + attributeOffset)));

Expand All @@ -287,7 +287,7 @@ private static Dictionary<string, string> GetNamespaceAliases(string xml)
}
else if (state.TagName is not null)
{
completions.AddRange(_helper.FilterPropertyNames(state.TagName, state.AttributeName, attached: false, hasSetter: true)
completions.AddRange(_helper.FilterPropertyNames(state.TagName, attributeName, attached: false, hasSetter: true)
.Select(x => new Completion(x, x + attributeSuffix, x, CompletionKind.Property, x.Length + attributeOffset)));

// Special case for "<On " here, 'Options' property is get only list property
Expand All @@ -301,21 +301,27 @@ private static Dictionary<string, string> GetNamespaceAliases(string xml)
CompletionKind.Property, 9 /*recommendedCursorOffset*/));
}

completions.AddRange(_helper.FilterEventNames(state.TagName, state.AttributeName, attached: false)
completions.AddRange(_helper.FilterEventNames(state.TagName, attributeName, attached: false)
.Select(v => new Completion(v, v + attributeSuffix, v, CompletionKind.Event, v.Length + attributeOffset)));

var targetType = _helper.LookupType(state.TagName);
if (targetType is not null)
{
completions.AddRange(
_helper.FilterTypes(state.AttributeName, xamlDirectiveOnly: true)
_helper.FilterTypes(attributeName, xamlDirectiveOnly: true)
.Where(t => t.Value.IsValidForXamlContextFunc?.Invoke(currentAssemblyName, targetType, null) ?? true)
.Select(v => new Completion(v.Key, v.Key + attributeSuffix, v.Key, CompletionKind.Class, v.Key.Length + attributeOffset)));

if (targetType.IsAvaloniaObjectType)
{
if (string.IsNullOrEmpty(attributeName) || "xmlns".StartsWith(attributeName,StringComparison.OrdinalIgnoreCase))
{
completions.Add(new("xmlns:", CompletionKind.Class));
}
completions.AddRange(
_helper.FilterTypeNames(state.AttributeName, withAttachedPropertiesOrEventsOnly: true)
_helper.FilterTypeNames(attributeName, withAttachedPropertiesOrEventsOnly: true)
.Select(v => new Completion(v, v + ".", v, CompletionKind.Class)));
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/CompletionEngineTests/AdvancedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public void xmlns_Directive_Should_Be_Completed()
{
var compl = GetCompletionsFor("<UserControl x").Completions;

Assert.Contains(compl, v => v.InsertText == "xmlns=\"\"");
Assert.Contains(compl, v => v.InsertText == "xmlns:");
}

[Fact]
Expand Down

0 comments on commit 8d3834d

Please sign in to comment.