<commit_before>﻿using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Foundatio.Parsers.LuceneQueries.Extensions;
using Foundatio.Parsers.LuceneQueries.Nodes;

namespace Foundatio.Parsers.LuceneQueries.Visitors {
    public class DebugQueryVisitor : QueryNodeVisitorWithResultBase<string> {
        private readonly StringBuilder _builder = new StringBuilder();
        private readonly IndentedTextWriter _writer;

        public DebugQueryVisitor() {
            _writer = new IndentedTextWriter(new StringWriter(_builder));
        }

        public override async Task VisitAsync(GroupNode node, IQueryVisitorContext context) {
            await _writer.WriteLineAsync("Group:").ConfigureAwait(false);
            _writer.Indent++;
            _writer.WriteLineIf(node.IsNegated.HasValue, "IsNegated: {0}", node.IsNegated);
            _writer.WriteLineIf(node.GetOriginalField() != null, "Field: {0}", node.GetOriginalField());
            _writer.WriteLineIf(node.Prefix != null, "Prefix: {0}", node.Prefix);
            _writer.WriteLineIf(node.Boost != null, "Boost: {0}", node.Boost);
            _writer.WriteLineIf(node.Proximity != null, "Proximity: {0}", node.Proximity);

            _writer.WriteIf(node.Left != null, "Left - ");
            if (node.Left != null)
                await node.Left.AcceptAsync(this, context).ConfigureAwait(false);

            _writer.WriteIf(node.Right != null, "Right - ");
            if (node.Right != null)
                await node.Right.AcceptAsync(this, context).ConfigureAwait(false);

            _writer.WriteLineIf(node.Operator != GroupOperator.Default, "Operator: {0}", node.Operator);
            _writer.WriteLineIf(node.HasParens, "Parens: true");

            WriteData(node);

            _writer.Indent--;
        }

        public override void Visit(TermNode node, IQueryVisitorContext context) {
            _writer.WriteLine("Term: ");
            _writer.Indent++;
            _writer.WriteLineIf(node.GetOriginalField() != null, "Field: {0}", node.GetOriginalField());
            _writer.WriteLineIf(node.IsNegated.HasValue, "IsNegated: {0}", node.IsNegated);
            _writer.WriteLineIf(node.Prefix != null, "Prefix: {0}", node.Prefix);
            _writer.WriteLine("IsQuoted: {0}", node.IsQuotedTerm);
            _writer.WriteLineIf(node.Term != null, "Term: {0}", node.Term);
            _writer.WriteLineIf(node.Boost != null, "Boost: {0}", node.Boost);
            _writer.WriteLineIf(node.Proximity != null, "Proximity: {0}", node.Proximity);

            WriteData(node);

            _writer.Indent--;
        }

        public override void Visit(TermRangeNode node, IQueryVisitorContext context) {
            _writer.WriteLine("Term Range: ");
            _writer.Indent++;
            _writer.WriteLineIf(node.Field != null, "Field: {0}", node.Field);
            _writer.WriteLineIf(node.GetOriginalField() != null, "Original Field: {0}", node.GetOriginalField());
            _writer.WriteLineIf(node.IsNegated.HasValue, "IsNegated: {0}", node.IsNegated);
            _writer.WriteLineIf(node.Prefix != null, "Prefix: {0}", node.Prefix);
            _writer.WriteLineIf(node.Operator != null, "Operator: {0}", node.Operator);
            _writer.WriteLineIf(node.Max != null, "Max: {0}", node.Max);
            _writer.WriteLineIf(node.Min != null, "Min: {0}", node.Min);<commit_msg>Make DebugQueryVisitor show the original field on groups and terms. Closes #60<commit_after>﻿using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Foundatio.Parsers.LuceneQueries.Extensions;
using Foundatio.Parsers.LuceneQueries.Nodes;

namespace Foundatio.Parsers.LuceneQueries.Visitors {
    public class DebugQueryVisitor : QueryNodeVisitorWithResultBase<string> {
        private readonly StringBuilder _builder = new StringBuilder();
        private readonly IndentedTextWriter _writer;

        public DebugQueryVisitor() {
            _writer = new IndentedTextWriter(new StringWriter(_builder));
        }

        public override async Task VisitAsync(GroupNode node, IQueryVisitorContext context) {
            await _writer.WriteLineAsync("Group:").ConfigureAwait(false);
            _writer.Indent++;
            _writer.WriteLineIf(node.IsNegated.HasValue, "IsNegated: {0}", node.IsNegated);
            _writer.WriteLineIf(node.Field != null, "Field: {0}", node.Field);
            _writer.WriteLineIf(node.GetOriginalField() != null, "Original Field: {0}", node.GetOriginalField());
            _writer.WriteLineIf(node.Prefix != null, "Prefix: {0}", node.Prefix);
            _writer.WriteLineIf(node.Boost != null, "Boost: {0}", node.Boost);
            _writer.WriteLineIf(node.Proximity != null, "Proximity: {0}", node.Proximity);

            _writer.WriteIf(node.Left != null, "Left - ");
            if (node.Left != null)
                await node.Left.AcceptAsync(this, context).ConfigureAwait(false);

            _writer.WriteIf(node.Right != null, "Right - ");
            if (node.Right != null)
                await node.Right.AcceptAsync(this, context).ConfigureAwait(false);

            _writer.WriteLineIf(node.Operator != GroupOperator.Default, "Operator: {0}", node.Operator);
            _writer.WriteLineIf(node.HasParens, "Parens: true");

            WriteData(node);

            _writer.Indent--;
        }

        public override void Visit(TermNode node, IQueryVisitorContext context) {
            _writer.WriteLine("Term: ");
            _writer.Indent++;
            _writer.WriteLineIf(node.Field != null, "Field: {0}", node.Field);
            _writer.WriteLineIf(node.GetOriginalField() != null, "Original Field: {0}", node.GetOriginalField());
            _writer.WriteLineIf(node.IsNegated.HasValue, "IsNegated: {0}", node.IsNegated);
            _writer.WriteLineIf(node.Prefix != null, "Prefix: {0}", node.Prefix);
            _writer.WriteLine("IsQuoted: {0}", node.IsQuotedTerm);
            _writer.WriteLineIf(node.Term != null, "Term: {0}", node.Term);
            _writer.WriteLineIf(node.Boost != null, "Boost: {0}", node.Boost);
            _writer.WriteLineIf(node.Proximity != null, "Proximity: {0}", node.Proximity);

            WriteData(node);

            _writer.Indent--;
        }

        public override void Visit(TermRangeNode node, IQueryVisitorContext context) {
            _writer.WriteLine("Term Range: ");
            _writer.Indent++;
            _writer.WriteLineIf(node.Field != null, "Field: {0}", node.Field);
            _writer.WriteLineIf(node.GetOriginalField() != null, "Original Field: {0}", node.GetOriginalField());
            _writer.WriteLineIf(node.IsNegated.HasValue, "IsNegated: {0}", node.IsNegated);
            _writer.WriteLineIf(node.Prefix != null, "Prefix: {0}", node.Prefix);
            _writer.WriteLineIf(node.Operator != null, "Operator: {0}", node.Operator);
            _writer.WriteLineIf(node.Max != null, "Max: {0}", node.Max);
            _writer.WriteLineIf(node.Min != null, "Min: {0}", node.Min);
{'fraction_non_alphanumeric': 0.13782456140350877, 'fraction_numerical': 0.006175438596491228, 'mean_word_length': 2.9089413055403184, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3140000', 'n_tokens_mistral': 2030, 'n_tokens_neox': 2010, 'n_words': 396}