More support for loading from reference files, now also registers UpsilonCreateStatic types

This commit is contained in:
Deukhoofd 2019-02-19 11:55:22 +01:00
parent 88d6767d56
commit f8fe056b73
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
22 changed files with 2848 additions and 487 deletions

Binary file not shown.

Binary file not shown.

View File

@ -6,7 +6,11 @@ using Mono.Cecil;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Upsilon.BaseTypes.UserData;
using Upsilon.Binder;
using Upsilon.Binder.VariableSymbols;
using Upsilon.BoundTypes;
using Upsilon.StandardLibraries;
using Type = Upsilon.BaseTypes.Type;
namespace UpsilonLanguageServer.ReferenceHandler
{
@ -42,7 +46,7 @@ namespace UpsilonLanguageServer.ReferenceHandler
private static Dictionary<string, string> _translatedType;
private static void LoadAssemblies(List<string> locations)
private static void LoadAssemblies(IReadOnlyCollection<string> locations)
{
var resolver = new DefaultAssemblyResolver();
foreach (var location in locations)
@ -52,19 +56,23 @@ namespace UpsilonLanguageServer.ReferenceHandler
}
_translatedType = new Dictionary<string, string>();
var dictionary = locations.Select(x =>
ModuleDefinition.ReadModule(x, new ReaderParameters() {AssemblyResolver = resolver}))
.SelectMany(file => file.Types, (file, type) => GetDefinitions(type))
.SelectMany(definitions => definitions)
.ToDictionary(definition => definition.Item1, definition => definition.Item2);
var types = locations.Select(x =>
ModuleDefinition.ReadModule(x, new ReaderParameters() {AssemblyResolver = resolver}))
.SelectMany(y => y.Types)
.ToArray();
foreach (var definition in dictionary)
var requiredUserDataAttribute = typeof(UpsilonUserDataAttribute).FullName;
var userDataTypes = types
.SelectMany(x => GetDefinitions(x, requiredUserDataAttribute))
.ToDictionary(x => x.Item1, x => x.Item2);
foreach (var definition in userDataTypes)
{
_translatedType.Add(definition.Value.FullName, definition.Key);
}
var result = new Dictionary<string, Data>();
foreach (var definition in dictionary)
foreach (var definition in userDataTypes)
{
var name = definition.Key;
var type = definition.Value;
@ -100,7 +108,10 @@ namespace UpsilonLanguageServer.ReferenceHandler
ActualType = y.Value.Type,
IsOptional = y.Value.IsOptional
}).ToArray(), false);
}).ToList()));
}).ToList())
{
Type = Type.Function
});
}
else
{
@ -116,37 +127,44 @@ namespace UpsilonLanguageServer.ReferenceHandler
BoundTypeHandler.LoadUserDataTypeDefinition(new UserDataBoundTypeDefinition(name, dic));
}
}
}
private static Type GetMonoType(this TypeReference type)
{
return Type.GetType(type.GetReflectionName(), true);
}
var requiredStaticAttribute = typeof(UpsilonCreateStaticAttribute).FullName;
var staticUserDataTypes = types
.SelectMany(x => GetDefinitions(x, requiredStaticAttribute))
.ToDictionary(x => x.Item1, x => x.Item2);
private static string GetReflectionName(this TypeReference type)
{
if (type.IsGenericInstance)
foreach (var staticUserDataType in staticUserDataTypes)
{
var genericInstance = (GenericInstanceType)type;
return
$"{genericInstance.Namespace}.{type.Name}[{string.Join(",", genericInstance.GenericArguments.Select(p => p.GetReflectionName()).ToArray())}]";
var variableSymbol = CreateVariableSymbol(staticUserDataType.Key, staticUserDataType.Value);
StaticScope.BoundScope.AssignToNearest(variableSymbol);
}
return type.FullName;
}
private static List<(string, TypeDefinition)> GetDefinitions(TypeDefinition typeDefinition)
private static VariableSymbol CreateVariableSymbol(string name, TypeDefinition def)
{
var type = BoundTypeParser.ParseType(name);
if (type == Type.UserData)
{
var boundTypeName = _translatedType[def.FullName];
var boundType = BoundTypeHandler.GetTypeDefinition(boundTypeName);
return new UserDataVariableSymbol(name, boundType, true);
}
return new VariableSymbol(name, type, true);
}
private static List<(string, TypeDefinition)> GetDefinitions(TypeDefinition typeDefinition,
string requiredAttribute)
{
var ls = new List<(string, TypeDefinition)>();
foreach (var nestedType in typeDefinition.NestedTypes)
{
var definitions = GetDefinitions(nestedType);
var definitions = GetDefinitions(nestedType, requiredAttribute);
if (definitions.Count > 0)
{
ls.AddRange(definitions);
}
}
var requiredAttribute = typeof(UpsilonUserDataAttribute).FullName;
if (!typeDefinition.HasCustomAttributes) return ls;
foreach (var customAttribute in typeDefinition.CustomAttributes)
{

View File

@ -7,6 +7,8 @@ using JsonRpc.Standard.Contracts;
using JsonRpc.Standard.Server;
using LanguageServer.VsCode.Contracts;
using Newtonsoft.Json.Linq;
using UpsilonLanguageServer.ReferenceHandler;
// ReSharper disable UnusedMember.Global
namespace UpsilonLanguageServer.Services
@ -38,6 +40,13 @@ namespace UpsilonLanguageServer.Services
Session.Settings.ModuleDirectory =
new DirectoryInfo(Path.Combine(rootUri.AbsolutePath, Session.Settings.ModuleDirectory))
.FullName;
var referenceConfigFile = configPath + "/references.json";
if (File.Exists(referenceConfigFile))
{
LoadReferenceLibraries.ReloadReferences(WorkspaceService.RootUri,
File.ReadAllText(referenceConfigFile));
}
}
}
catch (Exception e)

View File

@ -113,6 +113,12 @@ namespace UpsilonLanguageServer.Services
try
{
LoadReferenceLibraries.ReloadReferences(RootUri, File.ReadAllText(localPath));
foreach (var doc in Session.Documents.Values)
{
var diag = await DiagnosticProvider.LintDocument(doc.Document, doc,
Session.Settings.MaxNumberOfProblems, Session.Settings.ModuleDirectory);
await Client.Document.PublishDiagnostics(doc.Document.Uri, diag);
}
}
catch (Exception e)
{