Skip to content

Commit

Permalink
Feature/add docs links part 4 (#232)
Browse files Browse the repository at this point in the history
## Target
<!--
  Why are you making this change?
 -->
Add docs links part 4
#### Open Questions
<!-- OPTIONAL
- [ ] Use the GitHub checklists to spark discussion on issues that may
arise from your approach. Please tick the box and explain your answer.
-->

## Checklist
<!--
It serves as a gentle reminder for common tasks. Confirm it's done and
check everything that applies.
-->
- [x] Documentation updated
- [x] Tests cover new or modified code
- [x] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] New dependencies added
- [ ] Includes breaking changes
- [ ] Version bumped

## Visuals
<!-- OPTIONAL
Show results both before and after this change. When the output changes,
it can be a screenshot of a trace, metric, or log illustrating the
change.
-->
  • Loading branch information
MaxymGorn authored Oct 7, 2023
1 parent dfec6e5 commit abb9722
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/Cropper.Blazor/Client/Components/Docs/ApiProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class ApiProperty
{
public string Name { get; set; }
public Type Type { get; set; }
public PropertyInfo PropertyInfo { get; set; }
public PropertyInfo? PropertyInfo { get; set; }
public string Description { get; set; }
public object Default { get; set; }
public bool IsTwoWay { get; set; }
Expand Down
13 changes: 12 additions & 1 deletion src/Cropper.Blazor/Client/Components/Docs/DocsApi.razor
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@
else
{
<MudText Typo="Typo.h5">
Contract <b>@TypeNameHelper.GetTypeDisplay(Type, false, true)</b>
@if (Type.IsEnum)
{
<div>
Enum <b>@TypeNameHelper.GetTypeDisplay(Type, false, true)</b>
</div>
}
else
{
<div>
Contract <b>@TypeNameHelper.GetTypeDisplay(Type, false, true)</b>
</div>
}
</MudText>
}
<DocsPageContent>
Expand Down
38 changes: 31 additions & 7 deletions src/Cropper.Blazor/Client/Components/Docs/DocsApi.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ private IEnumerable<ApiMethod> GetMethods()

private bool IsEventCallback(PropertyInfo? propertyInfo)
{
return propertyInfo!.PropertyType.Name.Contains("EventCallback")
|| propertyInfo!.PropertyType.Name.Contains("Action")
|| propertyInfo!.PropertyType.Name.Contains("Func");
return (propertyInfo!.PropertyType.Name.Contains("EventCallback") && (propertyInfo!.PropertyType.FullName ?? "").Contains(typeof(EventCallback).Namespace))
|| (propertyInfo!.PropertyType.Name.Contains("Action") && (propertyInfo!.PropertyType.FullName ?? "").Contains(typeof(Action).Namespace))
|| (propertyInfo!.PropertyType.Name.Contains("Func") && (propertyInfo!.PropertyType.FullName ?? "").Contains(typeof(Func<>).Namespace));
}

private IEnumerable<ApiProperty> GetProperties()
Expand All @@ -108,12 +108,24 @@ private IEnumerable<ApiProperty> GetProperties()
.GetPropertyInfosWithAttribute<ParameterAttribute>();
}

foreach (var info in types.OrderBy(x => x.Name))
if (Type.IsEnum)
{
if (info.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length == 0
&& !IsEventCallback(info))
foreach (var info in Enum.GetValues(Type))
{
yield return ToApiProperty(info, saveTypename);
string? enumDisplayStatus = Convert.ChangeType(info, Type).ToString();
yield return ToApiProperty(Type, enumDisplayStatus, ((int)info).ToString());
}
}
else
{

foreach (var info in types.OrderBy(x => x.Name))
{
if (info.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length == 0
&& !IsEventCallback(info))
{
yield return ToApiProperty(info, saveTypename);
}
}
}
}
Expand All @@ -131,6 +143,18 @@ private ApiProperty ToApiProperty(PropertyInfo info, string saveTypename)
};
}

private ApiProperty ToApiProperty(Type type, string? enumDisplayStatus, string value)
{
return new ApiProperty
{
Name = enumDisplayStatus,
PropertyInfo = null,
Default = value,
Description = DocStrings.GetEnumDescription(type.Name, enumDisplayStatus),
Type = type
};
}

private string AnalyseMethodDocumentation(string documentation, string occurrence, string parameter = "")
{
try
Expand Down
55 changes: 41 additions & 14 deletions src/Cropper.Blazor/Client/Components/Docs/DocsTypeInfo.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,33 @@

@if (_nonnullableType.IsEnum)
{
<MudTooltip>
<ChildContent>@Type.GetTypeDisplayName()</ChildContent>
<TooltipContent>
enumeration type
<MudText Align="Align.Left" Typo="Typo.body2">
@foreach (var name in Enum.GetNames(_nonnullableType))
{
@(_nonnullableType.Name + "." + name)

<br />
}
</MudText>
</TooltipContent>
</MudTooltip>
@if (IsDeniedTooltipContent(Type.GetTypeDisplayName()))
{
<MudTooltip>
<ChildContent>
@(new MarkupString(ShowType()))
</ChildContent>
</MudTooltip>
}
else
{
<MudTooltip>
<ChildContent>
@(new MarkupString(ShowType()))
</ChildContent>
<TooltipContent>
enumeration type
<MudText Align="Align.Left" Typo="Typo.body2">
@foreach (var name in Enum.GetNames(_nonnullableType))
{
@(_nonnullableType.Name + "." + name)

<br />
}
</MudText>
</TooltipContent>
</MudTooltip>
}
}
else
{
Expand All @@ -42,4 +55,18 @@ else
{
_nonnullableType = Nullable.GetUnderlyingType(Type) ?? Type;
}

private string ShowType()
{
string typeDisplayName = Type.GetTypeDisplayName();

if (IsDeniedTooltipContent(typeDisplayName))
{
return typeDisplayName;
}

return typeDisplayName.CreateLink();
}

private bool IsDeniedTooltipContent(string typeDisplayName) => typeDisplayName == Type.Name;
}
16 changes: 14 additions & 2 deletions src/Cropper.Blazor/Client/Models/DocStrings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Cropper.Blazor.Shared.Extensions;
using System.Reflection;
using System.Reflection;
using System.Text.RegularExpressions;
using Cropper.Blazor.Shared.Extensions;

namespace Cropper.Blazor.Client.Models
{
Expand Down Expand Up @@ -35,6 +35,18 @@ public static string GetMemberDescription(string saveTypename, MemberInfo member
throw new Exception("Implemented only for properties and methods.");
}

return GetDocStrings(name);
}

public static string GetEnumDescription(string enumName, string enumValue)
{
string name = enumName + "_enum_" + enumValue;

return GetDocStrings(name);
}

private static string GetDocStrings(string name)
{
var field = typeof(DocStrings).GetField(name, BindingFlags.Public | BindingFlags.Static | BindingFlags.GetField);

if (field == null)
Expand Down
26 changes: 23 additions & 3 deletions src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/DocStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ public bool Execute()
cb.AddLine("{");
cb.IndentLevel++;

var assembly = typeof(CropperComponent).Assembly;
var types = assembly.GetTypes().OrderBy(t => GetSaveTypename(t));
Assembly assembly = typeof(CropperComponent).Assembly;
IOrderedEnumerable<Type> types = assembly.GetTypes().OrderBy(t => GetSaveTypename(t));

foreach (var type in types)
{
foreach (var property in type.GetPropertyInfosWithAttribute<ParameterAttribute>())
{
string doc = property.GetDocumentation() ?? "";
doc = NormalizeWord(doc);
doc = ConvertCrefToHTML(doc);
doc = ConvertMarkdownToHTML(doc);

Expand All @@ -64,8 +65,9 @@ public bool Execute()

bool isProperty = method.Name.StartsWith("get_");

var doc = method.GetDocumentation(isProperty) ?? "";
string doc = method.GetDocumentation(isProperty) ?? "";
string formattedReturnSignature = method.GetFormattedReturnSignature();

doc = ConvertSeeTagsForMethod(doc, formattedReturnSignature);
doc = NormalizeWord(doc);
doc = ConvertCrefToHTML(doc);
Expand All @@ -81,6 +83,24 @@ public bool Execute()
}
}
}

if (type.IsEnum)
{
string[] enumNames = type.GetEnumNames();

foreach (string enumName in enumNames)
{
Enum enumValue = (Enum)Enum.Parse(type, enumName);
string doc = enumValue.GetDocumentation();
doc = NormalizeWord(doc);
doc = ConvertCrefToHTML(doc);
doc = ConvertMarkdownToHTML(doc);

string description = EscapeDescription(doc);

cb.AddLine($"public const string {GetSaveTypename(type)}_enum_{enumName} = @\"{description}\";\n");
}
}
}

cb.IndentLevel--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ public static string GetFormattedReturnSignature(this Type type, bool callable =
}

// Return final result
Console.WriteLine(stringBuilder.ToString());
return stringBuilder.ToString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,19 @@ public static string GetDocumentation(this PropertyInfo propertyInfo)
return documentation;
}

/// <summary>Gets the XML documentation on a enum.</summary>
/// <param name="fieldInfo">The enum to get the XML documentation of.</param>
/// <returns>The XML documentation on the enum.</returns>
/// <remarks>The XML documentation must be loaded into memory for this function to work.</remarks>
public static string GetDocumentation(this Enum fieldInfo)
{
Type type = fieldInfo.GetType();
LoadXmlDocumentation(type.Assembly);
var key = "F:" + XmlDocumentationKeyHelper(type.FullName, fieldInfo.ToString());
LoadedXmlDocumentation.TryGetValue(key, out var documentation);
return documentation;
}

/// <summary>Gets the XML documentation on a field.</summary>
/// <param name="fieldInfo">The field to get the XML documentation of.</param>
/// <returns>The XML documentation on the field.</returns>
Expand Down

0 comments on commit abb9722

Please sign in to comment.