IEnumerable <Snippet> GetSnippets(IndexReader stringReader, string path)
        {
            var language = GetLanguageFromPath(path);

            var loopStack = new LoopStack();

            while (true)
            {
                var line = stringReader.ReadLine();
                if (line == null)
                {
                    if (loopStack.IsInSnippet)
                    {
                        var current = loopStack.Current;
                        yield return(Snippet.BuildError(
                                         error: "Snippet was not closed",
                                         path: path,
                                         lineNumberInError: current.StartLine + 1,
                                         key: current.Key));
                    }
                    break;
                }

                var trimmedLine = line.Trim()
                                  .Replace("  ", " ")
                                  .ToLowerInvariant();
                string version;
                Func <string, bool> endFunc;
                string key;
                if (StartEndTester.IsStart(trimmedLine, out version, out key, out endFunc))
                {
                    loopStack.Push(endFunc, key, version, stringReader.Index);
                    continue;
                }
                if (loopStack.IsInSnippet)
                {
                    if (!loopStack.Current.EndFunc(trimmedLine))
                    {
                        loopStack.AppendLine(line);
                        continue;
                    }

                    yield return(BuildSnippet(stringReader, path, loopStack.Current, language));

                    loopStack.Pop();
                }
            }
        }
예제 #2
0
        static IEnumerable <Snippet> GetSnippets(TextReader stringReader, string path, int maxWidth, string newLine)
        {
            var       language  = GetLanguageFromPath(path);
            LoopStack loopStack = new();
            var       index     = 0;

            while (true)
            {
                index++;
                var line = stringReader.ReadLine();
                if (line == null)
                {
                    if (loopStack.IsInSnippet)
                    {
                        var current = loopStack.Current;
                        yield return(Snippet.BuildError(
                                         error: "Snippet was not closed",
                                         path: path,
                                         lineNumberInError: current.StartLine + 1,
                                         key: current.Key));
                    }

                    break;
                }

                var trimmedLine = line.Trim();

                if (StartEndTester.IsStart(trimmedLine, path, out var key, out var endFunc))
                {
                    loopStack.Push(endFunc, key, index, maxWidth, newLine);
                    continue;
                }

                if (!loopStack.IsInSnippet)
                {
                    continue;
                }

                if (!loopStack.Current.EndFunc(trimmedLine))
                {
                    Snippet?error = null;
                    try
                    {
                        loopStack.AppendLine(line);
                    }
                    catch (LineTooLongException exception)
                    {
                        var current = loopStack.Current;
                        error = Snippet.BuildError(
                            error: "Line too long: " + exception.Line,
                            path: path,
                            lineNumberInError: current.StartLine + 1,
                            key: current.Key);
                    }

                    if (error != null)
                    {
                        yield return(error);

                        break;
                    }

                    continue;
                }

                yield return(BuildSnippet(path, loopStack, language, index));

                loopStack.Pop();
            }
        }