예제 #1
0
        public static TabSwitcher Create(bool selectNext)
        {
            if (trueFocusedWindow)
            {
                trueFocusedWindow.Focus();
                trueFocusedWindow = null;
            }

            guids = FGCodeWindow.GetGuidHistory();
            if (guids.Count == 0)
            {
                return(null);
            }

            items = new GUIContent[guids.Count];
            for (var i = 0; i < items.Length; ++i)
            {
                var path       = AssetDatabase.GUIDToAssetPath(guids[i]);
                var textBuffer = FGTextBufferManager.TryGetBuffer(path);
                var fileName   = System.IO.Path.GetFileName(path);
                var name       = textBuffer != null && textBuffer.IsModified ? "*" + fileName : fileName;
                items[i] = new GUIContent(name, AssetDatabase.GetCachedIcon(path), path);
            }
            selected = selectNext ? (items.Length > 1 ? 1 : 0) : items.Length - 1;
            if (!(EditorWindow.focusedWindow is FGCodeWindow))
            {
                selected = 0;
            }

            var numRows = Mathf.Min(items.Length, 10);
            var height  = itemHeight * numRows;
            var width   = lastWidth;

            owner = EditorWindow.focusedWindow;
            var  center   = owner.position.center;
            Rect position = new Rect((int)(center.x - 0.5f * width), (int)(center.y - height * 0.5f), width, height);

            var window = CreatePopup <TabSwitcher>();

            window.hideFlags = HideFlags.HideAndDontSave;

#if UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_5_0
            window.title = "";
#else
            window.titleContent.text = "";
#endif
            window.minSize = Vector2.one;

            window.position       = position;
            window.wantsMouseMove = false;
            window.ShowPopup();
            //window.Repaint();
            //window.Focus();

            //if (window.owner != null)
            //	window.owner.Focus();

            EditorApplication.modifierKeysChanged += window.OnModifierKeysChanged;
            return(window);
        }
예제 #2
0
        public static IList <string> GetOrReadAllLinesForPath(string assetPath)
        {
            if (string.IsNullOrEmpty(assetPath))
            {
                return(null);
            }

            string[] lines = null;
            try
            {
                var textBuffer = FGTextBufferManager.TryGetBuffer(assetPath);
                if (textBuffer != null)
                {
                    return(textBuffer.lines);
                }

                lines = File.ReadAllLines(assetPath);
            }
            catch (IOException e)
            {
                Debug.LogError(e);
                return(null);
            }
            return(lines);
        }
예제 #3
0
 public static FGTextBufferManager Instance()
 {
     if (_instance == null)
     {
         _instance           = ScriptableObject.CreateInstance <FGTextBufferManager>();
         _instance.hideFlags = HideFlags.HideAndDontSave;
     }
     return(_instance);
 }
예제 #4
0
        private static string GetSnippetsPath()
        {
            var managerScript = MonoScript.FromScriptableObject(FGTextBufferManager.Instance());

            if (!managerScript)
            {
                return(null);
            }
            var path = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(managerScript)));

            return(path + "/CodeSnippets/");
        }
예제 #5
0
 public static FGTextBufferManager Instance()
 {
     if (_instance == null)
     {
         var allInstances = Resources.FindObjectsOfTypeAll(typeof(FGTextBufferManager)) as FGTextBufferManager[];
         if (allInstances.Length > 0)
         {
             _instance = allInstances[0];
             //Debug.LogWarning("Found existing instances - " + allInstances.Length);
         }
         else
         {
             _instance = ScriptableObject.CreateInstance <FGTextBufferManager>();
             //Debug.LogWarning("Created a new instance!");
         }
         _instance.hideFlags = HideFlags.HideAndDontSave;
     }
     return(_instance);
 }
예제 #6
0
        public static List <SymbolDeclaration> FindDeclarations(SymbolDefinition symbol)
        {
            var candidates = FindDefinitionCandidates(symbol);

            foreach (var c in candidates)
            {
                var asset = AssetDatabase.LoadAssetAtPath(c, typeof(TextAsset)) as TextAsset;
                if (!asset)
                {
                    continue;
                }
                var buffer = FGTextBufferManager.GetBuffer(asset);
                buffer.LoadImmediately();
            }

            var newSymbol = FindNewSymbol(symbol);

            return(newSymbol == null ? null : newSymbol.declarations);
        }
예제 #7
0
        public static IList <string> GetOrReadAllLines(string assetGuid)
        {
            string[] lines;
            try
            {
                var assetPath  = AssetDatabase.GUIDToAssetPath(assetGuid);
                var textBuffer = FGTextBufferManager.TryGetBuffer(assetPath);
                if (textBuffer != null)
                {
                    return(textBuffer.lines);
                }

                lines = File.ReadAllLines(assetPath);
            }
            catch (IOException e)
            {
                Debug.LogError(e);
                return(null);
            }
            return(lines);
        }
예제 #8
0
 private void OnEnable()
 {
     hideFlags = HideFlags.HideAndDontSave;
     if (_instance == null)
     _instance = this;
     else if (_instance != this)
     Debug.LogError("Multiple Managers!!!");
 }
예제 #9
0
        static FindResultsWindow.ResultType ValidateResultAsReference(string assetGuid, TextPosition location, int length, ref SymbolDefinition referencedSymbol)
        {
            var assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);

            if (string.IsNullOrEmpty(assetPath))
            {
                return(FindResultsWindow.ResultType.RemoveResult);
            }

            var isCsScript = assetPath.EndsWith(".cs", System.StringComparison.OrdinalIgnoreCase);

            var buffer = FGTextBufferManager.GetBuffer(assetGuid);

            if (buffer == null)
            {
                return(FindResultsWindow.ResultType.RemoveResult);
            }

            if (buffer.Parser == null)
            {
                buffer.LoadImmediately();
                referencedSymbol = referencedSymbol.Rebind();
            }

            var formatedLine = buffer.formatedLines[location.line];

            var textLine    = buffer.lines[location.line];
            var isVarResult = length == 3 && (referencedSymbol is TypeDefinitionBase) && textLine.Substring(location.index, length) == "var";

            if (isCsScript)
            {
                if (formatedLine.regionTree.kind > FGTextBuffer.RegionTree.Kind.LastActive)
                {
                    if (isVarResult)
                    {
                        return(FindResultsWindow.ResultType.RemoveResult);
                    }
                    return(FindResultsWindow.ResultType.InactiveCode);
                }
            }
            else if (isVarResult)
            {
                return(FindResultsWindow.ResultType.RemoveResult);
            }

            int  tokenIndex;
            bool atTokenEnd;
            var  token = buffer.GetTokenAt(new TextPosition(location.line, location.index + 1), out location.line, out tokenIndex, out atTokenEnd);

            switch (token.tokenKind)
            {
            case SyntaxToken.Kind.Preprocessor:
                return(FindResultsWindow.ResultType.RemoveResult);

            case SyntaxToken.Kind.Comment:
            case SyntaxToken.Kind.PreprocessorArguments:
            case SyntaxToken.Kind.PreprocessorSymbol:
                if (isVarResult)
                {
                    return(FindResultsWindow.ResultType.RemoveResult);
                }
                return(FindResultsWindow.ResultType.Comment);

            case SyntaxToken.Kind.StringLiteral:
            case SyntaxToken.Kind.VerbatimStringLiteral:
                if (isVarResult)
                {
                    return(FindResultsWindow.ResultType.RemoveResult);
                }
                return(FindResultsWindow.ResultType.String);
            }

            if (!isCsScript || token.parent == null)
            {
                return(FindResultsWindow.ResultType.UnresolvedSymbol);
            }

            var resolvedSymbol = token.parent.resolvedSymbol;

            if (resolvedSymbol == null || resolvedSymbol.kind == SymbolKind.Error)
            {
                FGResolver.ResolveNode(token.parent.parent);
            }

            if (resolvedSymbol != null && resolvedSymbol.kind == SymbolKind.MethodGroup && token.parent.parent != null)
            {
                var nextLeaf = token.parent.parent.FindNextLeaf();
                if (nextLeaf != null && nextLeaf.IsLit("("))
                {
                    var nextNode = nextLeaf.parent;
                    if (nextNode.RuleName == "arguments")
                    {
                        FGResolver.ResolveNode(nextNode);
                        if (token.parent != null)
                        {
                            if (token.parent.resolvedSymbol == null || token.parent.resolvedSymbol.kind == SymbolKind.Error)
                            {
                                token.parent.resolvedSymbol = resolvedSymbol;
                            }
                        }
                    }
                }
            }

            resolvedSymbol = token.parent != null ? token.parent.resolvedSymbol : null;
            if (resolvedSymbol == null || resolvedSymbol.kind == SymbolKind.Error)
            {
                return(FindResultsWindow.ResultType.UnresolvedSymbol);
            }

            if (resolvedSymbol.kind == SymbolKind.Constructor || resolvedSymbol.kind == SymbolKind.Destructor)
            {
                resolvedSymbol = resolvedSymbol.parentSymbol;
            }
            if (resolvedSymbol == null || resolvedSymbol.kind == SymbolKind.Error)
            {
                return(FindResultsWindow.ResultType.UnresolvedSymbol);
            }

            var constructedSymbol = resolvedSymbol;

            resolvedSymbol = resolvedSymbol.GetGenericSymbol();

            if (referencedSymbol.kind == SymbolKind.MethodGroup && resolvedSymbol.kind == SymbolKind.Method)
            {
                resolvedSymbol = resolvedSymbol.parentSymbol;
            }

            if (resolvedSymbol != referencedSymbol)
            {
                var typeArgument    = referencedSymbol as TypeDefinitionBase;
                var constructedType = constructedSymbol as ConstructedTypeDefinition;
                if (isVarResult && typeArgument != null && constructedType != null)
                {
                    if (IsUsedAsTypeArgument(typeArgument.GetGenericSymbol() as TypeDefinitionBase, constructedType))
                    {
                        return(FindResultsWindow.ResultType.VarTemplateReference);
                    }
                }

                if (resolvedSymbol.kind == SymbolKind.Method && referencedSymbol.kind == SymbolKind.Method)
                {
                    if (resolvedSymbol.parentSymbol == referencedSymbol.parentSymbol)
                    {
                        return(FindResultsWindow.ResultType.MethodOverload);
                    }

                    var resolvedMethod   = resolvedSymbol as MethodDefinition;
                    var referencedMethod = referencedSymbol as MethodDefinition;
                    if (resolvedMethod != null && referencedMethod != null)
                    {
                        var resolvedType = resolvedMethod.parentSymbol as TypeDefinitionBase
                                           ?? resolvedMethod.parentSymbol.parentSymbol as TypeDefinitionBase;
                        var referencedType = referencedMethod.parentSymbol as TypeDefinitionBase
                                             ?? referencedMethod.parentSymbol.parentSymbol as TypeDefinitionBase;

                        var isInterface = resolvedType.kind == SymbolKind.Interface || referencedType.kind == SymbolKind.Interface;

                        var resolvedIsVirtual   = isInterface || resolvedMethod.IsOverride || resolvedMethod.IsVirtual || resolvedMethod.IsAbstract;
                        var referencedIsVirtual = isInterface || referencedMethod.IsOverride || referencedMethod.IsVirtual || referencedMethod.IsAbstract;
                        if (resolvedIsVirtual && referencedIsVirtual)
                        {
                            if (System.Linq.Enumerable.SequenceEqual(
                                    System.Linq.Enumerable.Select(resolvedMethod.GetParameters(), x => x.TypeOf()),
                                    System.Linq.Enumerable.Select(referencedMethod.GetParameters(), x => x.TypeOf())))
                            {
                                if (resolvedType.DerivesFrom(referencedType))
                                {
                                    return(FindResultsWindow.ResultType.OverridingMethod);
                                }
                                if (referencedType.DerivesFrom(resolvedType))
                                {
                                    return(FindResultsWindow.ResultType.OverriddenMethod);
                                }
                            }
                        }
                    }
                }

                if (resolvedSymbol.kind != SymbolKind.MethodGroup || referencedSymbol.parentSymbol != resolvedSymbol)
                {
                    return(FindResultsWindow.ResultType.RemoveResult);
                }
            }

            if (isVarResult)
            {
                return(FindResultsWindow.ResultType.VarReference);
            }

            if (FGResolver.IsWriteReference(token))
            {
                return(FindResultsWindow.ResultType.WriteReference);
            }
            return(FindResultsWindow.ResultType.ReadReference);
        }
	public static FGTextBufferManager Instance()
	{
		if (_instance == null)
		{
			_instance = ScriptableObject.CreateInstance<FGTextBufferManager>();
			_instance.hideFlags = HideFlags.HideAndDontSave;
		}
		return _instance;
	}
예제 #11
0
        private void ReplaceAll()
        {
            Unsubscribe();

            FGTextBuffer buffer         = null;
            string       guid           = null;
            bool         canEditAll     = true;
            var          canEditBuffers = new HashSet <FGTextBuffer>();

            for (var i = flatResults.Count; i-- > 0;)
            {
                var result = flatResults[i];
                if (!result.selected)
                {
                    continue;
                }

                if (result.assetGuid != guid)
                {
                    guid   = result.assetGuid;
                    buffer = FGTextBufferManager.GetBuffer(guid);
                    if (buffer.IsLoading)
                    {
                        buffer.LoadImmediately();
                    }

                    if (FGCodeWindow.CodeWindows.All(x => x.TargetAssetGuid != guid))
                    {
                        var wnd = FGCodeWindow.OpenAssetInTab(guid);
                        if (wnd)
                        {
                            wnd.OnFirstUpdate();
                        }
                    }
                    if (!buffer.TryEdit())
                    {
                        canEditAll = false;
                    }
                    else
                    {
                        canEditBuffers.Add(buffer);
                    }
                }
            }

            if (!canEditAll)
            {
                if (!EditorUtility.DisplayDialog("Replace", "Some assets are locked and cannot be edited!", "Continue Anyway", "Cancel"))
                {
                    Close();
                }
            }

            buffer = null;
            guid   = null;
            var updatedLines = new HashSet <int>();

            for (var i = flatResults.Count; i-- > 0;)
            {
                var result = flatResults[i];
                if (!result.selected)
                {
                    continue;
                }

                if (result.assetGuid != guid)
                {
                    if (buffer != null)
                    {
                        foreach (var line in updatedLines)
                        {
                            buffer.UpdateHighlighting(line, line);
                        }
                        buffer.EndEdit();
                        FGTextBufferManager.AddBufferToGlobalUndo(buffer);
                        updatedLines.Clear();
                    }

                    guid   = result.assetGuid;
                    buffer = FGTextBufferManager.GetBuffer(guid);

                    if (buffer != null)
                    {
                        if (canEditBuffers.Contains(buffer))
                        {
                            buffer.BeginEdit("*Replace All");
                        }
                        else
                        {
                            buffer = null;
                        }
                    }
                }

                if (buffer != null)
                {
                    var from = new FGTextBuffer.CaretPos {
                        line = result.line, characterIndex = result.characterIndex
                    };
                    var to = new FGTextBuffer.CaretPos {
                        line = result.line, characterIndex = result.characterIndex + result.length
                    };
                    var insertAt = buffer.DeleteText(from, to);
                    if (replaceText != "")
                    {
                        buffer.InsertText(insertAt, replaceText);
                    }

                    updatedLines.Add(result.line);
                }
            }
            if (buffer != null)
            {
                foreach (var line in updatedLines)
                {
                    buffer.UpdateHighlighting(line, line);
                }
                buffer.EndEdit();
                FGTextBufferManager.AddBufferToGlobalUndo(buffer);
            }

            FGTextBufferManager.RecordGlobalUndo();
            Close();
        }