Skip to content

Commit

Permalink
Work with newer versions of Unity
Browse files Browse the repository at this point in the history
Fixes #219
  • Loading branch information
citizenmatt committed Oct 8, 2017
1 parent cda3168 commit 8abd330
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ protected override bool IsAvailable(CSharpCodeCompletionContext context)

protected override bool AddLookupItems(CSharpCodeCompletionContext context, IItemsCollector collector)
{
IClassLikeDeclaration declaration;
bool hasVisibilityModifier;
bool hasReturnType;
if (!CheckPosition(context, out declaration, out hasVisibilityModifier, out hasReturnType))
if (!CheckPosition(context, out var classDeclaration, out var hasVisibilityModifier, out var hasReturnType))
return false;

// Don't add anything in double completion - we've already added it
Expand All @@ -75,50 +72,47 @@ protected override bool AddLookupItems(CSharpCodeCompletionContext context, IIte
if (context.BasicContext.Parameters.EvaluationMode == EvaluationMode.Light)
return false;

var typeElement = declaration.DeclaredElement;
var typeElement = classDeclaration.DeclaredElement;
if (typeElement == null)
return false;

var unityApi = context.BasicContext.Solution.GetComponent<UnityApi>();
var unityVersionApi = context.BasicContext.Solution.GetComponent<UnityVersion>();
var project = context.BasicContext.File.GetProject();
var unityVersion = unityVersionApi.GetActualVersion(project);
var unityTypes = unityApi.GetBaseUnityTypes(typeElement, unityVersion);
foreach (var unityType in unityTypes)
{
var items = new List<ILookupItem>();
var actualVersion = unityVersionApi.GetActualVersion(project);
var functions = unityApi.GetEventFunctions(typeElement, actualVersion);

foreach (var eventFunction in unityType.GetEventFunctions(unityVersion))
{
if (typeElement.Methods.Any(m => eventFunction.Match(m) != EventFunctionMatch.NoMatch))
continue;
var items = new List<ILookupItem>();
foreach (var function in functions)
{
if (typeElement.Methods.Any(m => function.Match(m) != EventFunctionMatch.NoMatch))
continue;

items.Clear();
items.Clear();

// TODO: Decide what to do with e.g. `void OnAnima{caret}`
// If we want to insert a visibility modifier, it has to go *before* the `void`,
// which means adding a behaviour here that will remove it
if (!hasVisibilityModifier && !hasReturnType)
{
var factory = CSharpLookupItemFactory.Instance;
var lookupItem = factory.CreateTextLookupItem(context.BasicContext, context.CompletionRanges,
"private ");
items.Add(lookupItem);
}
// TODO: Decide what to do with e.g. `void OnAnima{caret}`
// If we want to insert a visibility modifier, it has to go *before* the `void`,
// which means adding a behaviour here that will remove it
if (!hasVisibilityModifier && !hasReturnType)
{
var factory = CSharpLookupItemFactory.Instance;
var lookupItem = factory.CreateTextLookupItem(context.BasicContext, context.CompletionRanges,
"private ");
items.Add(lookupItem);
}

var item = CreateMethodItem(context, eventFunction, declaration);
if (item == null) continue;
var item = CreateMethodItem(context, function, classDeclaration);
if (item == null) continue;

items.Add(item);
items.Add(item);

item = CombineLookupItems(context.BasicContext, context.CompletionRanges, items, item);
item = CombineLookupItems(context.BasicContext, context.CompletionRanges, items, item);

item.Placement.Relevance |= (long) CLRLookupItemRelevance.GenerateItems;
if (eventFunction.Undocumented)
item.Placement.Location = PlacementLocation.Bottom;
item.Placement.Relevance |= (long) CLRLookupItemRelevance.GenerateItems;
if (function.Undocumented)
item.Placement.Location = PlacementLocation.Bottom;

collector.Add(item);
}
collector.Add(item);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ public override void Populate(CSharpGeneratorContext context)
if (!context.Project.IsUnityProject())
return;

var typeElement = context.ClassDeclaration.DeclaredElement as IClass;
if (typeElement == null)
if (!(context.ClassDeclaration.DeclaredElement is IClass typeElement))
return;

var unityVersion = myUnityVersion.GetActualVersion(context.Project);
var unityTypes = myUnityApi.GetBaseUnityTypes(typeElement, unityVersion).ToArray();
var eventFunctions = unityTypes.SelectMany(t => t.GetEventFunctions(unityVersion))
var eventFunctions = myUnityApi.GetEventFunctions(typeElement, unityVersion)
.Where(f => typeElement.Methods.All(m => f.Match(m) == EventFunctionMatch.NoMatch)).ToArray();

var classDeclaration = context.ClassDeclaration;
Expand Down
32 changes: 24 additions & 8 deletions resharper/src/resharper-unity/UnityApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,20 @@ public UnityApi(UnityVersion unityVersion)
[NotNull]
public IEnumerable<UnityType> GetBaseUnityTypes([NotNull] ITypeElement type)
{
var projectPsiModule = type.Module as IProjectPsiModule;
if (projectPsiModule == null)
return EmptyArray<UnityType>.Instance;
var unityVersion = myUnityVersion.GetActualVersion(projectPsiModule.Project);
return GetBaseUnityTypes(type, unityVersion);
if (type.Module is IProjectPsiModule projectPsiModule)
{
var unityVersion = myUnityVersion.GetActualVersion(projectPsiModule.Project);
return GetBaseUnityTypes(type, unityVersion);
}
return EmptyArray<UnityType>.Instance;
}

[NotNull]
public IEnumerable<UnityType> GetBaseUnityTypes([NotNull] ITypeElement type, Version unityVersion)
{
var types = myTypes.Value;
unityVersion = types.NormaliseSupportedVersion(unityVersion);
return types.Types.Where(t => t.SupportsVersion(unityVersion) && type.IsDescendantOf(t.GetTypeElement(type.Module)));
return GetBaseUnityTypes(types, type, unityVersion);
}

public bool IsUnityType([NotNull] ITypeElement type)
Expand Down Expand Up @@ -91,10 +92,20 @@ public bool IsUnityField([NotNull] IField field)
return false;
}

public IEnumerable<UnityEventFunction> GetEventFunctions(ITypeElement type, Version unityVersion)
{
var types = myTypes.Value;
unityVersion = types.NormaliseSupportedVersion(unityVersion);
foreach (var unityType in GetBaseUnityTypes(types, type, unityVersion))
{
foreach (var function in unityType.GetEventFunctions(unityVersion))
yield return function;
}
}

public UnityEventFunction GetUnityEventFunction([NotNull] IMethod method)
{
EventFunctionMatch match;
return GetUnityEventFunction(method, out match);
return GetUnityEventFunction(method, out var _);
}

public UnityEventFunction GetUnityEventFunction([NotNull] IMethod method, out EventFunctionMatch match)
Expand Down Expand Up @@ -123,5 +134,10 @@ public Version GetNormalisedActualVersion(IProject project)
{
return myTypes.Value.NormaliseSupportedVersion(myUnityVersion.GetActualVersion(project));
}

private IEnumerable<UnityType> GetBaseUnityTypes(UnityTypes types, ITypeElement type, Version normalisedVersion)
{
return types.Types.Where(t => t.SupportsVersion(normalisedVersion) && type.IsDescendantOf(t.GetTypeElement(type.Module)));
}
}
}
8 changes: 4 additions & 4 deletions resharper/src/resharper-unity/UnityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public UnityType(IClrTypeName typeName, IEnumerable<UnityEventFunction> eventFun
myEventFunctions = eventFunctions;
}

public IEnumerable<UnityEventFunction> GetEventFunctions(Version unityVersion)
public IEnumerable<UnityEventFunction> GetEventFunctions(Version normalisedUnityVersion)
{
return myEventFunctions.Where(f => f.SupportsVersion(unityVersion));
return myEventFunctions.Where(f => f.SupportsVersion(normalisedUnityVersion));
}

[CanBeNull]
Expand All @@ -35,11 +35,11 @@ public ITypeElement GetTypeElement([NotNull] IPsiModule module)
return type.GetTypeElement();
}

public bool HasEventFunction([NotNull] IMethod method, Version unityVersion)
public bool HasEventFunction([NotNull] IMethod method, Version normalisedUnityVersion)
{
foreach (var function in myEventFunctions)
{
if (function.SupportsVersion(unityVersion) && function.Match(method) != EventFunctionMatch.NoMatch)
if (function.SupportsVersion(normalisedUnityVersion) && function.Match(method) != EventFunctionMatch.NoMatch)
return true;
}
return false;
Expand Down
28 changes: 26 additions & 2 deletions resharper/test/data/codeCompletion/List/MonoBehaviour01.cs.gold
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 725 ITEMS #
# 737 ITEMS #

A
ADBannerView
Expand Down Expand Up @@ -88,6 +88,7 @@ BlendWeights
BoneWeight
BoundingSphere
Bounds
BoundsInt
BoxCollider
BoxCollider2D
BuoyancyEffector2D
Expand Down Expand Up @@ -126,6 +127,7 @@ CollisionDetectionMode2D
CollisionFlags
Color
Color32
ColorGamut
ColorSpace
ColorUsageAttribute
ColorUtility
Expand Down Expand Up @@ -158,6 +160,7 @@ CullingGroupEvent
Cursor
CursorLockMode
CursorMode
CustomGridBrushAttribute
CustomRenderTexture
CustomRenderTextureInitializationSource
CustomRenderTextureUpdateMode
Expand Down Expand Up @@ -204,6 +207,7 @@ FontStyle
ForceMode
ForceMode2D
FrictionJoint2D
FrustumPlanes
FullScreenMovieControlMode
FullScreenMovieScalingMode
GL
Expand All @@ -230,6 +234,9 @@ GradientAlphaKey
GradientColorKey
GradientMode
Graphics
Grid
GridBrushBase
GridLayout
Gyroscope
Handheld
Hash128
Expand Down Expand Up @@ -378,6 +385,7 @@ ParticleSystemShapeMultiModeValue
ParticleSystemShapeType
ParticleSystemSimulationSpace
ParticleSystemSortMode
ParticleSystemStopAction
ParticleSystemStopBehavior
ParticleSystemSubEmitterProperties
ParticleSystemSubEmitterType
Expand All @@ -399,6 +407,7 @@ PlayerPrefs
PlayerPrefsException
PointEffector2D
PolygonCollider2D
Pose
PreferBinarySerialization
PrimitiveType
ProceduralCacheSize
Expand Down Expand Up @@ -427,6 +436,7 @@ Ray2D
RaycastHit
RaycastHit2D
Rect
RectInt
RectOffset
RectTransform
RectTransformUtility
Expand Down Expand Up @@ -576,7 +586,9 @@ UnityException
UserAuthorization
VRTextureUsage
Vector2
Vector2Int
Vector3
Vector3Int
Vector4
VerticalWrapMode
WWW
Expand Down Expand Up @@ -727,7 +739,7 @@ public override Equals(object) { ... }
public override GetHashCode() { ... }
public override ToString() { ... }
# AUTOMATIC #
# 725 ITEMS #
# 737 ITEMS #

A
ADBannerView
Expand Down Expand Up @@ -817,6 +829,7 @@ BlendWeights
BoneWeight
BoundingSphere
Bounds
BoundsInt
BoxCollider
BoxCollider2D
BuoyancyEffector2D
Expand Down Expand Up @@ -855,6 +868,7 @@ CollisionDetectionMode2D
CollisionFlags
Color
Color32
ColorGamut
ColorSpace
ColorUsageAttribute
ColorUtility
Expand Down Expand Up @@ -887,6 +901,7 @@ CullingGroupEvent
Cursor
CursorLockMode
CursorMode
CustomGridBrushAttribute
CustomRenderTexture
CustomRenderTextureInitializationSource
CustomRenderTextureUpdateMode
Expand Down Expand Up @@ -933,6 +948,7 @@ FontStyle
ForceMode
ForceMode2D
FrictionJoint2D
FrustumPlanes
FullScreenMovieControlMode
FullScreenMovieScalingMode
GL
Expand All @@ -959,6 +975,9 @@ GradientAlphaKey
GradientColorKey
GradientMode
Graphics
Grid
GridBrushBase
GridLayout
Gyroscope
Handheld
Hash128
Expand Down Expand Up @@ -1107,6 +1126,7 @@ ParticleSystemShapeMultiModeValue
ParticleSystemShapeType
ParticleSystemSimulationSpace
ParticleSystemSortMode
ParticleSystemStopAction
ParticleSystemStopBehavior
ParticleSystemSubEmitterProperties
ParticleSystemSubEmitterType
Expand All @@ -1128,6 +1148,7 @@ PlayerPrefs
PlayerPrefsException
PointEffector2D
PolygonCollider2D
Pose
PreferBinarySerialization
PrimitiveType
ProceduralCacheSize
Expand Down Expand Up @@ -1156,6 +1177,7 @@ Ray2D
RaycastHit
RaycastHit2D
Rect
RectInt
RectOffset
RectTransform
RectTransformUtility
Expand Down Expand Up @@ -1305,7 +1327,9 @@ UnityException
UserAuthorization
VRTextureUsage
Vector2
Vector2Int
Vector3
Vector3Int
Vector4
VerticalWrapMode
WWW
Expand Down
2 changes: 1 addition & 1 deletion resharper/test/src/TestUnityAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class TestUnityAttribute : TestPackagesAttribute, ITestFlavoursProvider,
{
private readonly UnityVersion myVersion;

public TestUnityAttribute() : this(UnityVersion.Unity20171)
public TestUnityAttribute() : this(UnityVersion.Unity20172)
{
}

Expand Down

0 comments on commit 8abd330

Please # to comment.