Add caching for code docs in Editor
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using FlaxEditor.Scripting;
|
using FlaxEditor.Scripting;
|
||||||
using FlaxEngine;
|
using FlaxEngine;
|
||||||
@@ -12,6 +13,9 @@ namespace FlaxEditor.Modules.SourceCodeEditing
|
|||||||
/// <seealso cref="FlaxEditor.Modules.EditorModule" />
|
/// <seealso cref="FlaxEditor.Modules.EditorModule" />
|
||||||
public sealed class CodeDocsModule : EditorModule
|
public sealed class CodeDocsModule : EditorModule
|
||||||
{
|
{
|
||||||
|
private Dictionary<ScriptType, string> _typeCache = new Dictionary<ScriptType, string>();
|
||||||
|
private Dictionary<ScriptMemberInfo, string> _memberCache = new Dictionary<ScriptMemberInfo, string>();
|
||||||
|
|
||||||
internal CodeDocsModule(Editor editor)
|
internal CodeDocsModule(Editor editor)
|
||||||
: base(editor)
|
: base(editor)
|
||||||
{
|
{
|
||||||
@@ -25,30 +29,59 @@ namespace FlaxEditor.Modules.SourceCodeEditing
|
|||||||
/// <returns>The documentation tooltip.</returns>
|
/// <returns>The documentation tooltip.</returns>
|
||||||
public string GetTooltip(ScriptType type, object[] attributes = null)
|
public string GetTooltip(ScriptType type, object[] attributes = null)
|
||||||
{
|
{
|
||||||
|
if (_typeCache.TryGetValue(type, out var text))
|
||||||
|
return text;
|
||||||
|
|
||||||
if (attributes == null)
|
if (attributes == null)
|
||||||
attributes = type.GetAttributes(false);
|
attributes = type.GetAttributes(false);
|
||||||
var text = type.TypeName;
|
text = type.TypeName;
|
||||||
var tooltip = (TooltipAttribute)attributes.FirstOrDefault(x => x is TooltipAttribute);
|
var tooltip = (TooltipAttribute)attributes.FirstOrDefault(x => x is TooltipAttribute);
|
||||||
if (tooltip != null)
|
if (tooltip != null)
|
||||||
text += '\n' + tooltip.Text;
|
text += '\n' + tooltip.Text;
|
||||||
|
_typeCache.Add(type, text);
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the tooltip text for the type.
|
/// Gets the tooltip text for the type member.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="type">The type.</param>
|
/// <param name="member">The type member.</param>
|
||||||
/// <param name="attributes">The type attributes. Optional, if null type attributes will be used.</param>
|
/// <param name="attributes">The member attributes. Optional, if null member attributes will be used.</param>
|
||||||
/// <returns>The documentation tooltip.</returns>
|
/// <returns>The documentation tooltip.</returns>
|
||||||
public string GetTooltip(ScriptMemberInfo type, object[] attributes = null)
|
public string GetTooltip(ScriptMemberInfo member, object[] attributes = null)
|
||||||
{
|
{
|
||||||
|
if (_memberCache.TryGetValue(member, out var text))
|
||||||
|
return text;
|
||||||
|
|
||||||
if (attributes == null)
|
if (attributes == null)
|
||||||
attributes = type.GetAttributes(true);
|
attributes = member.GetAttributes(true);
|
||||||
string text = null;
|
|
||||||
var tooltip = (TooltipAttribute)attributes.FirstOrDefault(x => x is TooltipAttribute);
|
var tooltip = (TooltipAttribute)attributes.FirstOrDefault(x => x is TooltipAttribute);
|
||||||
if (tooltip != null)
|
if (tooltip != null)
|
||||||
text = tooltip.Text;
|
text = tooltip.Text;
|
||||||
|
_memberCache.Add(member, text);
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void OnInit()
|
||||||
|
{
|
||||||
|
base.OnInit();
|
||||||
|
|
||||||
|
Editor.CodeEditing.TypesCleared += OnTypesCleared;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnTypesCleared()
|
||||||
|
{
|
||||||
|
_typeCache.Clear();
|
||||||
|
_memberCache.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void OnExit()
|
||||||
|
{
|
||||||
|
OnTypesCleared();
|
||||||
|
|
||||||
|
base.OnExit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user