<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using JetBrains.Platform.RdFramework.Util;
using JetBrains.Platform.Unity.EditorPluginModel;
using JetBrains.Rider.Unity.Editor.AssetPostprocessors;
using JetBrains.Rider.Unity.Editor.NonUnity;
using JetBrains.Util.Logging;
using UnityEditor;

namespace JetBrains.Rider.Unity.Editor
{
  internal class OnOpenAssetHandler
  {
    private readonly ILog myLogger = Log.GetLog<OnOpenAssetHandler>();
    private readonly RiderPathLocator myRiderPathLocator;
    private readonly IPluginSettings myPluginSettings;
    private readonly string mySlnFile;

    public OnOpenAssetHandler(RiderPathLocator riderPathLocator, IPluginSettings pluginSettings, string slnFile)
    {
      myRiderPathLocator = riderPathLocator;
      myPluginSettings = pluginSettings;
      mySlnFile = slnFile;
    }
    
    public bool OnOpenedAsset(int instanceID, int line)
    {
      // determine asset that has been double clicked in the project view
      var selected = EditorUtility.InstanceIDToObject(instanceID);
      var assetPath = AssetDatabase.GetAssetPath(selected);

      if (string.IsNullOrEmpty(assetPath)) // RIDER-16784
        return false;

      var assetFilePath = Path.GetFullPath(assetPath);
      if (!(selected.GetType().ToString() == "UnityEditor.MonoScript" ||
            selected.GetType().ToString() == "UnityEngine.Shader" ||
            selected.GetType().ToString() == "UnityEngine.Experimental.UIElements.VisualTreeAsset" ||
            selected.GetType().ToString() == "UnityEngine.StyleSheets.StyleSheet" ||
            (selected.GetType().ToString() == "UnityEngine.TextAsset" &&
             GetExtensionStrings().Contains(Path.GetExtension(assetFilePath).Substring(1))
            )))
        return false;

      return OnOpenedAsset(assetFilePath, line);
    }

    private static string[] GetExtensionStrings()
    {
      var extensionStrings = new[] {"ts", "bjs", "javascript", "json", "html", "shader", "template", "compute", "cginc", "hlsl", "glslinc"};
      var propertyInfo = typeof(EditorSettings)
        .GetProperty("projectGenerationUserExtensions", BindingFlags.Public | BindingFlags.Static);
      if (propertyInfo != null)
      {
        var value = propertyInfo.GetValue(null, null);
        extensionStrings = (string[]) value;
      }

      return extensionStrings;
    }

    [UsedImplicitly] // https://github.com/JetBrains/resharper-unity/issues/475
    public bool OnOpenedAsset(string assetFilePath, int line)
    {
      var modifiedSource = EditorPrefs.GetBool(ModificationPostProcessor.ModifiedSource, false);
      myLogger.Verbose("ModifiedSource: {0} EditorApplication.isPlaying: {1} EditorPrefsWrapper.AutoRefresh: {2}",
        modifiedSource, EditorApplication.isPlaying, EditorPrefsWrapper.AutoRefresh);

      if (modifiedSource && !EditorApplication.isPlaying && EditorPrefsWrapper.AutoRefresh || !File.Exists(PluginEntryPoint.SlnFile))
      {
        UnityUtils.SyncSolution(); // added to handle opening file, which was just recently created.
        EditorPrefs.SetBool(ModificationPostProcessor.ModifiedSource, false);
      }

      var models = PluginEntryPoint.UnityModels.Where(a=>!a.Lifetime.IsTerminated).ToArray();
      if (models.Any())
      {
        var model = models.First().Model;
        if (PluginEntryPoint.CheckConnectedToBackendSync(model))
        {
          const int column = 0;
          myLogger.Verbose("Calling OpenFileLineCol: {0}, {1}, {2}", assetFilePath, line, column);
          if (model.RiderProcessId.HasValue())
            ActivateWindow(model.RiderProcessId.Value);
          else
            ActivateWindow();
          <commit_msg>fast-fix list of extensions for fast opening<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using JetBrains.Platform.RdFramework.Util;
using JetBrains.Platform.Unity.EditorPluginModel;
using JetBrains.Rider.Unity.Editor.AssetPostprocessors;
using JetBrains.Rider.Unity.Editor.NonUnity;
using JetBrains.Util.Logging;
using UnityEditor;

namespace JetBrains.Rider.Unity.Editor
{
  internal class OnOpenAssetHandler
  {
    private readonly ILog myLogger = Log.GetLog<OnOpenAssetHandler>();
    private readonly RiderPathLocator myRiderPathLocator;
    private readonly IPluginSettings myPluginSettings;
    private readonly string mySlnFile;

    public OnOpenAssetHandler(RiderPathLocator riderPathLocator, IPluginSettings pluginSettings, string slnFile)
    {
      myRiderPathLocator = riderPathLocator;
      myPluginSettings = pluginSettings;
      mySlnFile = slnFile;
    }
    
    public bool OnOpenedAsset(int instanceID, int line)
    {
      // determine asset that has been double clicked in the project view
      var selected = EditorUtility.InstanceIDToObject(instanceID);
      var assetPath = AssetDatabase.GetAssetPath(selected);

      if (string.IsNullOrEmpty(assetPath)) // RIDER-16784
        return false;

      var assetFilePath = Path.GetFullPath(assetPath);
      if (!(selected.GetType().ToString() == "UnityEditor.MonoScript" ||
            selected.GetType().ToString() == "UnityEngine.Shader" ||
            selected.GetType().ToString() == "UnityEngine.Experimental.UIElements.VisualTreeAsset" ||
            selected.GetType().ToString() == "UnityEngine.StyleSheets.StyleSheet" ||
            (selected.GetType().ToString() == "UnityEngine.TextAsset" &&
             GetExtensionStrings().Contains(Path.GetExtension(assetFilePath).Substring(1))
            )))
        return false;

      return OnOpenedAsset(assetFilePath, line);
    }

    private static string[] GetExtensionStrings()
    {
      var extensionStrings = new[] {"ts", "bjs", "javascript", "json", "html", "shader"};
      var propertyInfo = typeof(EditorSettings)
        .GetProperty("projectGenerationUserExtensions", BindingFlags.Public | BindingFlags.Static);
      if (propertyInfo != null)
      {
        var value = propertyInfo.GetValue(null, null);
        extensionStrings = (string[]) value;
      }
      
      // https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/VisualStudioIntegration/SolutionSynchronizer.cs#L50
      var builtinSupportedExtensions = new[] {"template", "compute", "cginc", "hlsl", "glslinc"}; // todo: get it via reflection
      var list = extensionStrings.ToList();
      list.AddRange(builtinSupportedExtensions);
      extensionStrings = list.ToArray();

      return extensionStrings;
    }

    [UsedImplicitly] // https://github.com/JetBrains/resharper-unity/issues/475
    public bool OnOpenedAsset(string assetFilePath, int line)
    {
      var modifiedSource = EditorPrefs.GetBool(ModificationPostProcessor.ModifiedSource, false);
      myLogger.Verbose("ModifiedSource: {0} EditorApplication.isPlaying: {1} EditorPrefsWrapper.AutoRefresh: {2}",
        modifiedSource, EditorApplication.isPlaying, EditorPrefsWrapper.AutoRefresh);

      if (modifiedSource && !EditorApplication.isPlaying && EditorPrefsWrapper.AutoRefresh || !File.Exists(PluginEntryPoint.SlnFile))
      {
        UnityUtils.SyncSolution(); // added to handle opening file, which was just recently created.
        EditorPrefs.SetBool(ModificationPostProcessor.ModifiedSource, false);
      }

      var models = PluginEntryPoint.UnityModels.Where(a=>!a.Lifetime.IsTerminated).ToArray();
      if (models.Any())
      {
        var model = models.First().Model;
        if (PluginEntryPoint.CheckConnectedToBackendSync(model))
        {
          const int column = 0;
          myLogger.Verbose("Calling OpenFileLineCol: {0}, {1}, {2}", assetFilePath, line, column);
          if (model.RiderProcessId.HasValue())
            ActivateWindow(model.RiderProcessId.Value);
          else
            ActivateWindow();
          
{'fraction_non_alphanumeric': 0.10719677906391545, 'fraction_numerical': 0.004277805737292401, 'mean_word_length': 3.602779386218877, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 3, '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': '3004104', 'n_tokens_mistral': 2223, 'n_tokens_neox': 2163, 'n_words': 439}