Beispiel #1
0
        public bool TryFindLastLineInClass(string typename, string[] srcFileLines, out int lastLine)
        {
            lastLine = int.MaxValue;
            if (srcFileLines == null)
            {
                return(false);
            }
            var srcFile = srcFileLines.ToArray();

            if (String.IsNullOrWhiteSpace(typename))
            {
                return(false);
            }
            if (srcFile.Length <= 0)
            {
                return(false);
            }

            lastLine = srcFile.Length;

            //these preserve all lines
            srcFile = RemoveLineComments(srcFile, LINE_COMMENT_CHAR_SEQ);
            srcFile = RemoveBlockComments(srcFile);

            var joinedContent = string.Join("\n", srcFile);
            var myXDocFrame   = new XDocFrame(C_OPEN_CURLY, C_CLOSE_CURLY);

            var mytokens = myXDocFrame.FindEnclosingTokens(joinedContent);

            if (mytokens == null || mytokens.Count < 1)
            {
                return(false);
            }
            var targetToken = NfReflect.GetTypeNameWithoutNamespace(typename) == typename ? mytokens[0] : mytokens[1];

            var joinedContentAsChars = joinedContent.ToCharArray();

            //need to count the number of newlines from the bottom up to token's end
            for (var i = joinedContent.Length - 1; i >= targetToken.End; i--)
            {
                if (joinedContentAsChars[i] != '\n')
                {
                    continue;
                }
                lastLine -= 1;
            }
            //add one more
            if (lastLine < srcFile.Length)
            {
                lastLine -= 1;
            }

            return(lastLine < srcFile.Length);
        }
Beispiel #2
0
        public bool TryFindFirstLineInClass(string typename, string[] srcFileLines, out int firstLine)
        {
            firstLine = 1;
            if (srcFileLines == null)
            {
                return(false);
            }
            var srcFile = srcFileLines.ToArray();

            if (String.IsNullOrWhiteSpace(typename))
            {
                return(false);
            }
            if (srcFile.Length <= 0)
            {
                return(false);
            }

            //these preserve all lines
            srcFile = RemoveLineComments(srcFile, LINE_COMMENT_CHAR_SEQ);
            srcFile = RemoveBlockComments(srcFile);

            var joinedContent = String.Join("\n", srcFile);

            var myXDocFrame = new XDocFrame(C_OPEN_CURLY, C_CLOSE_CURLY);

            var mytokens = myXDocFrame.FindEnclosingTokens(joinedContent);

            if (mytokens == null || mytokens.Count < 1)
            {
                return(false);
            }
            var targetToken = NfReflect.GetTypeNameWithoutNamespace(typename) == typename ? mytokens[0] : mytokens[1];

            var joinedContentAsChars = joinedContent.ToCharArray();

            //need to count the number of newlines up to the token's start
            for (var i = 0; i <= targetToken.Start; i++)
            {
                if (joinedContentAsChars[i] != '\n')
                {
                    continue;
                }
                firstLine += 1;
            }

            return(firstLine > 1);
        }