Beispiel #1
0
        public static void FindAllReferences(SymbolDefinition symbol, string localAssetPath)
        {
            if (symbol.kind == SymbolKind.Accessor || symbol.kind == SymbolKind.Constructor || symbol.kind == SymbolKind.Destructor)
            {
                symbol = symbol.parentSymbol;
            }
            if (symbol == null)
            {
                return;
            }

            symbol = symbol.GetGenericSymbol();

            var candidates = FindReferenceCandidates(symbol, localAssetPath);

            var searchOptions = new FindResultsWindow.SearchOptions {
                text      = symbol.name,
                matchWord = true,
                matchCase = true,
            };

            var candidateGuids = new string[candidates.Count];

            for (int i = 0; i < candidates.Count; i++)
            {
                candidateGuids[i] = AssetDatabase.AssetPathToGUID(candidates[i]);
            }

            var searchForVarRefs = symbol is TypeDefinitionBase && symbol.kind != SymbolKind.Delegate;

            if (searchForVarRefs)
            {
                searchOptions.altText1 = "var";

                var builtInTypesEnumerator = SymbolDefinition.builtInTypes.GetEnumerator();
                for (var i = 0; i < 16; i++)
                {
                    builtInTypesEnumerator.MoveNext();
                    var type = builtInTypesEnumerator.Current.Value;
                    if (type == symbol)
                    {
                        searchOptions.altText2 = builtInTypesEnumerator.Current.Key;
                        break;
                    }
                }
            }

            FindResultsWindow resultsWindow = FindResultsWindow.Create(
                "References to " + symbol.FullName,
                FindAllInSingleFile,
                candidateGuids,
                searchOptions,
                "References");

            resultsWindow.SetFilesValidator(ValidateFileForReferences);
            resultsWindow.SetResultsValidator(ValidateResultAsReference, symbol);
        }
Beispiel #2
0
        public FindResultsWindow ListAllResults()
        {
            string[] allTextAssetGuids;
            var      lookInOption = this.lookInOption;

            if (lookForOption == FindReplace_LookFor.AllAssets ||
                lookForOption == FindReplace_LookFor.Shaders ||
                lookForOption == FindReplace_LookFor.TextAssets)
            {
                if (lookInOption > FindReplace_LookIn.CurrentTabOnly)
                {
                    lookInOption = FindReplace_LookIn.WholeProject;
                }
            }

            if (lookInOption == FindReplace_LookIn.OpenTabsOnly)
            {
                allTextAssetGuids = (from w in FGCodeWindow.CodeWindows select w.TargetAssetGuid).Distinct().ToArray();
            }
            else if (lookInOption == FindReplace_LookIn.CurrentTabOnly)
            {
                allTextAssetGuids = new [] { editor != null ? editor.targetGuid : FGCodeWindow.GetGuidHistory().FirstOrDefault() };
            }
            else if (lookInOption != FindReplace_LookIn.WholeProject &&
                     lookForOption != FindReplace_LookFor.AllAssets &&
                     lookForOption != FindReplace_LookFor.Shaders &&
                     lookForOption != FindReplace_LookFor.TextAssets)
            {
                if (FGFindInFiles.assets != null)
                {
                    FGFindInFiles.assets.Clear();
                }

                if (lookInOption == FindReplace_LookIn.FirstPassGameAssemblies || lookInOption == FindReplace_LookIn.AllGameAssemblies)
                {
                    if (lookForOption == FindReplace_LookFor.CSharpScripts || lookForOption == FindReplace_LookFor.AllScriptTypes)
                    {
                        FGFindInFiles.FindAllAssemblyScripts(AssemblyDefinition.UnityAssembly.CSharpFirstPass);
                    }
                    if (lookForOption == FindReplace_LookFor.JSScripts || lookForOption == FindReplace_LookFor.AllScriptTypes)
                    {
                        FGFindInFiles.FindAllAssemblyScripts(AssemblyDefinition.UnityAssembly.UnityScriptFirstPass);
                    }
                    if (lookForOption == FindReplace_LookFor.BooScripts || lookForOption == FindReplace_LookFor.AllScriptTypes)
                    {
                        FGFindInFiles.FindAllAssemblyScripts(AssemblyDefinition.UnityAssembly.BooFirstPass);
                    }
                }
                if (lookInOption == FindReplace_LookIn.GameAssemblies || lookInOption == FindReplace_LookIn.AllGameAssemblies)
                {
                    if (lookForOption == FindReplace_LookFor.CSharpScripts || lookForOption == FindReplace_LookFor.AllScriptTypes)
                    {
                        FGFindInFiles.FindAllAssemblyScripts(AssemblyDefinition.UnityAssembly.CSharp);
                    }
                    if (lookForOption == FindReplace_LookFor.JSScripts || lookForOption == FindReplace_LookFor.AllScriptTypes)
                    {
                        FGFindInFiles.FindAllAssemblyScripts(AssemblyDefinition.UnityAssembly.UnityScript);
                    }
                    if (lookForOption == FindReplace_LookFor.BooScripts || lookForOption == FindReplace_LookFor.AllScriptTypes)
                    {
                        FGFindInFiles.FindAllAssemblyScripts(AssemblyDefinition.UnityAssembly.Boo);
                    }
                }
                if (lookInOption == FindReplace_LookIn.FirstPassEditorAssemblies || lookInOption == FindReplace_LookIn.AllEditorAssemblies)
                {
                    if (lookForOption == FindReplace_LookFor.CSharpScripts || lookForOption == FindReplace_LookFor.AllScriptTypes)
                    {
                        FGFindInFiles.FindAllAssemblyScripts(AssemblyDefinition.UnityAssembly.CSharpEditorFirstPass);
                    }
                    if (lookForOption == FindReplace_LookFor.JSScripts || lookForOption == FindReplace_LookFor.AllScriptTypes)
                    {
                        FGFindInFiles.FindAllAssemblyScripts(AssemblyDefinition.UnityAssembly.UnityScriptEditorFirstPass);
                    }
                    if (lookForOption == FindReplace_LookFor.BooScripts || lookForOption == FindReplace_LookFor.AllScriptTypes)
                    {
                        FGFindInFiles.FindAllAssemblyScripts(AssemblyDefinition.UnityAssembly.BooEditorFirstPass);
                    }
                }
                if (lookInOption == FindReplace_LookIn.EditorAssemblies || lookInOption == FindReplace_LookIn.AllEditorAssemblies)
                {
                    if (lookForOption == FindReplace_LookFor.CSharpScripts || lookForOption == FindReplace_LookFor.AllScriptTypes)
                    {
                        FGFindInFiles.FindAllAssemblyScripts(AssemblyDefinition.UnityAssembly.CSharpEditor);
                    }
                    if (lookForOption == FindReplace_LookFor.JSScripts || lookForOption == FindReplace_LookFor.AllScriptTypes)
                    {
                        FGFindInFiles.FindAllAssemblyScripts(AssemblyDefinition.UnityAssembly.UnityScriptEditor);
                    }
                    if (lookForOption == FindReplace_LookFor.BooScripts || lookForOption == FindReplace_LookFor.AllScriptTypes)
                    {
                        FGFindInFiles.FindAllAssemblyScripts(AssemblyDefinition.UnityAssembly.BooEditor);
                    }
                }

                allTextAssetGuids = FGFindInFiles.assets.ToArray();
            }
            else
            {
                allTextAssetGuids = FGFindInFiles.FindAllTextAssets().ToArray();

                IEnumerable <string> realTextAssets = null;
                switch (lookForOption)
                {
                case FindReplace_LookFor.AllAssets:
                    realTextAssets =
                        from guid in allTextAssetGuids
                        where !ignoreFileTypes.Contains(Path.GetExtension(AssetDatabase.GUIDToAssetPath(guid).ToLowerInvariant()))
                        select guid;
                    break;

                case FindReplace_LookFor.AllScriptTypes:
                    realTextAssets =
                        from guid in allTextAssetGuids
                        where scriptFileTypes.Contains(Path.GetExtension(AssetDatabase.GUIDToAssetPath(guid).ToLowerInvariant()))
                        select guid;
                    break;

                case FindReplace_LookFor.CSharpScripts:
                    realTextAssets =
                        from guid in allTextAssetGuids
                        where Path.GetExtension(AssetDatabase.GUIDToAssetPath(guid).ToLowerInvariant()) == ".cs"
                        select guid;
                    break;

                case FindReplace_LookFor.JSScripts:
                    realTextAssets =
                        from guid in allTextAssetGuids
                        where Path.GetExtension(AssetDatabase.GUIDToAssetPath(guid).ToLowerInvariant()) == ".js"
                        select guid;
                    break;

                case FindReplace_LookFor.BooScripts:
                    realTextAssets =
                        from guid in allTextAssetGuids
                        where Path.GetExtension(AssetDatabase.GUIDToAssetPath(guid).ToLowerInvariant()) == ".boo"
                        select guid;
                    break;

                case FindReplace_LookFor.Shaders:
                    realTextAssets =
                        from guid in allTextAssetGuids
                        where shaderFileTypes.Contains(Path.GetExtension(AssetDatabase.GUIDToAssetPath(guid).ToLowerInvariant()))
                        select guid;
                    break;

                case FindReplace_LookFor.TextAssets:
                    realTextAssets =
                        from guid in allTextAssetGuids
                        where !nonTextFileTypes.Contains(Path.GetExtension(AssetDatabase.GUIDToAssetPath(guid).ToLowerInvariant()))
                        select guid;
                    break;
                }

                allTextAssetGuids = realTextAssets.ToArray();
            }

            if (allTextAssetGuids.Length == 0 || allTextAssetGuids.Length == 1 && allTextAssetGuids[0] == null)
            {
                Debug.LogWarning("No asset matches selected searching scope!");
                return(null);
            }

            var searchOptions = new FindResultsWindow.SearchOptions {
                text      = findText,
                matchCase = matchCase,
                matchWord = matchWholeWord,
            };

            FindResultsWindow resultsWindow = FindResultsWindow.Create(
                "Searching for '" + findText + "'...",
                FGFindInFiles.FindAllInSingleFile,
                allTextAssetGuids,
                searchOptions,
                isReplace ? "<b>Replace</b>" : listResultsInNewWindow ? "" : "Find Results");

            return(resultsWindow);
        }
Beispiel #3
0
        internal static IEnumerable <TextPosition> FindAll(IList <string> lines, FindResultsWindow.SearchOptions search)
        {
            var length = search.text.Length;

            if (length == 0)
            {
                yield break;
            }

            var comparison = search.matchCase ? System.StringComparison.Ordinal : System.StringComparison.OrdinalIgnoreCase;

            char firstChar    = search.text[0];
            bool startsAsWord = firstChar == '_' || char.IsLetterOrDigit(firstChar);
            char lastChar     = search.text[search.text.Length - 1];
            bool endsAsWord   = lastChar == '_' || char.IsLetterOrDigit(lastChar);

            int skipThisWord = search.text.IndexOf(firstChar.ToString(), 1, comparison);

            if (skipThisWord < 0)
            {
                skipThisWord = search.text.Length;
            }

            var l = 0;
            var c = 0;

            while (l < lines.Count)
            {
                var line = lines[l];

                if (c > line.Length - length)
                {
                    c = 0;
                    ++l;
                    continue;
                }

                c = line.IndexOf(search.text, c, comparison);
                if (c < 0)
                {
                    c = 0;
                    ++l;
                    continue;
                }

                if (search.matchWord)
                {
                    if (startsAsWord && c > 0)
                    {
                        char prevChar = line[c - 1];
                        if (prevChar == '_' || char.IsLetterOrDigit(prevChar))
                        {
                            c += skipThisWord;
                            continue;
                        }
                    }
                    if (endsAsWord && c + length < line.Length)
                    {
                        char nextChar = line[c + length];
                        if (nextChar == '_' || char.IsLetterOrDigit(nextChar))
                        {
                            c += skipThisWord;
                            continue;
                        }
                    }
                }

                yield return(new TextPosition(l, c));

                c += length;
            }
        }
Beispiel #4
0
        public static void FindAllInSingleFile(
            System.Action <string, string, TextPosition, int> addResultAction,
            string assetGuid,
            FindResultsWindow.SearchOptions search)
        {
            var assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
            var isCsFile  = Path.GetExtension(assetPath).Equals(".cs", System.StringComparison.OrdinalIgnoreCase);

            var allLines = GetOrReadAllLines(assetGuid);

            if (!isCsFile || search.altText1 == null)
            {
                foreach (var textPosition in FindAll(allLines, search))
                {
                    var line = allLines[textPosition.line];
                    addResultAction(line, assetGuid, textPosition, search.text.Length);
                }
            }
            else
            {
                var results = FindAll(allLines, search).GetEnumerator();

                var altSearch = new FindResultsWindow.SearchOptions {
                    text      = search.altText1,
                    matchCase = search.matchCase,
                    matchWord = search.matchWord,
                };
                var altResults = FindAll(allLines, altSearch).GetEnumerator();

                IEnumerator <TextPosition> altResults2 = null;
                if (search.altText2 != null)
                {
                    var altSearch2 = new FindResultsWindow.SearchOptions {
                        text      = search.altText2,
                        matchCase = search.matchCase,
                        matchWord = search.matchWord,
                    };
                    altResults2 = FindAll(allLines, altSearch2).GetEnumerator();
                }

                bool more     = results.MoveNext();
                bool altMore  = altResults.MoveNext();
                bool altMore2 = altResults2 != null && altResults2.MoveNext();
                while (more || altMore || altMore2)
                {
                    if (more && (!altMore || results.Current <= altResults.Current) &&
                        (!altMore2 || results.Current <= altResults2.Current))
                    {
                        var line = allLines[results.Current.line];
                        addResultAction(line, assetGuid, results.Current, search.text.Length);
                        more = results.MoveNext();
                    }
                    else if (altMore && (!more || altResults.Current <= results.Current) &&
                             (!altMore2 || altResults.Current <= altResults2.Current))
                    {
                        var line = allLines[altResults.Current.line];
                        addResultAction(line, assetGuid, altResults.Current, search.altText1.Length);
                        altMore = altResults.MoveNext();
                    }
                    else
                    {
                        var line = allLines[altResults2.Current.line];
                        addResultAction(line, assetGuid, altResults2.Current, search.altText2.Length);
                        altMore2 = altResults2.MoveNext();
                    }
                }
            }
        }