<commit_before>﻿//
// Copyright (c) XSharp B.V.  All Rights Reserved.  
// Licensed under the Apache License, Version 2.0.  
// See License.txt in the project root for license information.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace XSharpModel
{
    /// <summary>
    /// We have one SystemTypeController in memory : It will handle all references types for all projects
    /// Assemblies are stored inside a List of AssemblyInfo
    /// </summary>
    public class SystemTypeController
    {
        internal static Dictionary<String, AssemblyInfo> assemblies;

        static SystemTypeController()
        {
            assemblies = new Dictionary<string, AssemblyInfo>(StringComparer.OrdinalIgnoreCase);
        }

  
        public static AssemblyInfo LoadAssembly(VSLangProj.Reference reference)
        {
            string path = reference.Path;
            if (String.IsNullOrEmpty(path))
            {
                return new AssemblyInfo(reference);
            }
            return LoadAssembly(path);
        }
        public static AssemblyInfo LoadAssembly(string cFileName)
        {
            DateTime lastWriteTime = File.GetLastWriteTime(cFileName);
            AssemblyInfo assembly;
            if (assemblies.ContainsKey(cFileName))
            {
                assembly = assemblies[cFileName];
            }
            else
            {
                assembly = new AssemblyInfo(cFileName, DateTime.MinValue);
                assemblies.Add(cFileName, assembly);
            }
            if (lastWriteTime != assembly.Modified)
            {
                assembly.UpdateAssembly();
            }
            return assembly;
        }

        public static void RemoveAssembly(string cFileName)
        {
            if (assemblies.ContainsKey(cFileName))
            {
                assemblies.Remove(cFileName);
            }
        }

        public Type FindType(string typeName, IList<string> usings, IList<AssemblyInfo> assemblies)
        {
            Type result = Lookup(typeName,assemblies);
            if (result != null)
                return result;
            // try to find with explicit usings
            foreach (var name in usings)
            {
                var fullname = name + "." + typeName;
                result = Lookup(fullname, assemblies);
                if (result != null)
                    return result;
            }
            // try to find with implicit namespaces
            foreach (var asm in assemblies)
            {
                foreach (var ns in asm.ImplicitNamespaces)
                {
                    var fullname = ns + "." + typeName;
                    result = Lookup(fullname, assemblies);
                    if (result != null)
                        return result;

                }
            }
            return null;
        }

        public static Type Lookup(string typeName, IList<AssemblyInfo> assemblies)
        {
            System.Type sType = null;
            foreach (AssemblyInfo assembly in assemblies)
            {
                //
                if (assembly.Types.Count == 0)
                {
                    assembly.UpdateAssembly();
                }
                if (assembly.Types.TryGetValue(typeName, out sType))
                {
                    break;
                }
                sType = assembly.Assembly.GetType(typeName);
                if (sType != null)
                {
                    break;
                }
            }
            return sType;
        }

        public List<String> GetNamespaces (IList<AssemblyInfo> assemblies)<commit_msg>Added error handling in FindType for null Assemblies<commit_after>﻿//
// Copyright (c) XSharp B.V.  All Rights Reserved.  
// Licensed under the Apache License, Version 2.0.  
// See License.txt in the project root for license information.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace XSharpModel
{
    /// <summary>
    /// We have one SystemTypeController in memory : It will handle all references types for all projects
    /// Assemblies are stored inside a List of AssemblyInfo
    /// </summary>
    public class SystemTypeController
    {
        internal static Dictionary<String, AssemblyInfo> assemblies;

        static SystemTypeController()
        {
            assemblies = new Dictionary<string, AssemblyInfo>(StringComparer.OrdinalIgnoreCase);
        }

  
        public static AssemblyInfo LoadAssembly(VSLangProj.Reference reference)
        {
            string path = reference.Path;
            if (String.IsNullOrEmpty(path))
            {
                return new AssemblyInfo(reference);
            }
            return LoadAssembly(path);
        }
        public static AssemblyInfo LoadAssembly(string cFileName)
        {
            DateTime lastWriteTime = File.GetLastWriteTime(cFileName);
            AssemblyInfo assembly;
            if (assemblies.ContainsKey(cFileName))
            {
                assembly = assemblies[cFileName];
            }
            else
            {
                assembly = new AssemblyInfo(cFileName, DateTime.MinValue);
                assemblies.Add(cFileName, assembly);
            }
            if (lastWriteTime != assembly.Modified)
            {
                assembly.UpdateAssembly();
            }
            return assembly;
        }

        public static void RemoveAssembly(string cFileName)
        {
            if (assemblies.ContainsKey(cFileName))
            {
                assemblies.Remove(cFileName);
            }
        }

        public Type FindType(string typeName, IList<string> usings, IList<AssemblyInfo> assemblies)
        {
            Type result = Lookup(typeName,assemblies);
            if (result != null)
                return result;
            // try to find with explicit usings
            foreach (var name in usings)
            {
                var fullname = name + "." + typeName;
                result = Lookup(fullname, assemblies);
                if (result != null)
                    return result;
            }
            // try to find with implicit namespaces
            foreach (var asm in assemblies)
            {
                foreach (var ns in asm.ImplicitNamespaces)
                {
                    var fullname = ns + "." + typeName;
                    result = Lookup(fullname, assemblies);
                    if (result != null)
                        return result;

                }
            }
            return null;
        }

        public static Type Lookup(string typeName, IList<AssemblyInfo> assemblies)
        {
            System.Type sType = null;
            foreach (AssemblyInfo assembly in assemblies)
            {
                //
                if (assembly.Types.Count == 0)
                {
                    assembly.UpdateAssembly();
                }
                if (assembly.Types.TryGetValue(typeName, out sType))
                {
                    break;
                }
                if (assembly.Assembly != null)
                {
                    sType = assembly.Assembly.GetType(typeName);
                }
                if (sType != null)
                {
                    break;
                }
            }
            return sType;
        }

        public List<String> GetNamespaces (IList<AssemblyInfo> assemblies)
{'fraction_non_alphanumeric': 0.07590802194930756, 'fraction_numerical': 0.0007839038411288215, 'mean_word_length': 1.3914401749453296, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '927333', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1745, 'n_words': 479}