Beispiel #1
0
        //----------------------------------
        public static CodeMapItem[] GetMapOf(string code, string codeFile)
        {
            bool injected = CSScriptHelper.DecorateIfRequired(ref code);

            CodeMapItem[] map;

            if (Config.Instance.UsingRoslyn)
            {
                map = Syntaxer.GetMapOf(code, codeFile);
            }
            // map = RoslynEngine.GetMapOf(code, injected, codeFile);
            else
            {
                map = MonoEngine.GetMapOf(code, injected, codeFile);
            }

            if (injected)
            {
                int injectedLineOffset = CSScriptHelper.GetDecorationInfo(code).Item1;
                int injectedLineNumber = code.Substring(0, injectedLineOffset).Split('\n').Count();

                map = map.Where(i => i.Line != injectedLineNumber).ToArray();

                foreach (CodeMapItem item in map)
                {
                    if (item.Line >= injectedLineNumber)
                    {
                        item.Line -= 1;
                    }
                }
            }
            return(map);
        }
Beispiel #2
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);
        }