예제 #1
0
        public IEnumerable <string> FindReferences(string editorText, string pattern, ResolveResult target, string fileName)
        {
            var references = new List <string>();

            IDocument document = null;

            Type patternType = target.GetType();

            string targetReflectionName = target.ToString();

            if (target is MemberResolveResult) //the caret is on the member implementation
            {
                targetReflectionName = targetReflectionName.Replace("MemberResolveResult", "CSharpInvocationResolveResult");
                patternType          = typeof(CSharpInvocationResolveResult);
            }

            //we are looking for the member invocation code.
            //For example: "[CSharpInvocationResolveResult [Method Test.Who():System.Void]]"
            foreach (Match m in Regex.Matches(editorText, pattern))
            {
                var match = ResolveFromPosition(editorText, m.Index, fileName);
                if (match != null && match.GetType() == patternType && match.ToString() == targetReflectionName)
                {
                    if (document == null)
                    {
                        document = new ReadOnlyDocument(editorText);
                    }

                    int    position = m.Index;
                    var    location = document.GetLocation(position);
                    var    line     = document.GetLineByOffset(position);
                    string lineText = editorText.GetTextOf(line);

                    Tuple <int, int> decoration = CSScriptHelper.GetDecorationInfo(editorText);

                    if (decoration.Item1 != -1) //the file content is no the actual one but an auto-generated (decorated)
                    {
                        if (position > decoration.Item1)
                        {
                            position -= decoration.Item2;

                            string actualText = File.ReadAllText(fileName);
                            if (actualText != editorText)
                            {
                                document = new ReadOnlyDocument(actualText);
                                location = document.GetLocation(position);
                                line     = document.GetLineByOffset(position);
                                lineText = actualText.GetTextOf(line);
                            }
                        }
                    }

                    references.Add(string.Format("{0}({1},{2}): {3}", fileName, location.Line, location.Column, lineText.Trim()));
                }
            }

            return(references);
        }