예제 #1
0
        bool TryProcessSnippetLine(TextWriter writer, IndexReader reader, string line, List <MissingSnippet> missings, List <Snippet> used)
        {
            string key;

            if (!SnippetKeyReader.TryExtractKeyFromLine(line, out key))
            {
                return(false);
            }
            writer.WriteLine($"<!-- snippet: {key} -->");

            IReadOnlyList <Snippet> group;

            if (!snippets.TryGetValue(key, out group))
            {
                var missing = new MissingSnippet(
                    key: key,
                    line: reader.Index);
                missings.Add(missing);
                var message = $"** Could not find snippet '{key}' **";
                writer.WriteLine(message);
                return(true);
            }

            appendSnippetGroup(key, group, writer);
            used.AddRange(group);
            return(true);
        }
        ProcessResult Apply(IEnumerable<SnippetGroup> availableSnippets, TextWriter writer, IndexReader reader)
        {
            var snippets = availableSnippets.ToList();
            var missingSnippets = new List<MissingSnippet>();
            var usedSnippets = new List<SnippetGroup>();
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                string key;
                if (!keyReader(line, out key))
                {
                    writer.WriteLine(line);
                    continue;
                }
                writer.WriteLine("<!-- snippet: {0} -->", key);

                var snippetGroup = snippets.FirstOrDefault(x => x.Key == key);
                if (snippetGroup == null)
                {
                    var missingSnippet = new MissingSnippet(key: key, line: reader.Index);
                    missingSnippets.Add(missingSnippet);
                    var message = $"** Could not find key '{key}' **";
                    writer.WriteLine(message);
                    continue;
                }

                AppendGroup(snippetGroup, writer);
                if (usedSnippets.All(x => x.Key != snippetGroup.Key))
                {
                    usedSnippets.Add(snippetGroup);
                }
            }
            return new ProcessResult(missingSnippets: missingSnippets, usedSnippets: usedSnippets);
        }