prompt: Extra space when first field moved to new line in a WITH statement
  Note, the query below uses a `WITH` statement. If I just try to fix the SQL within the CTE, this works fine.
  
  Given the following SQL:
  
  ```sql
  WITH example AS (
      SELECT my_id,
          other_thing,
          one_more
      FROM
          my_table
  )
  
  SELECT *
  FROM example
  ```
  
  ## Expected Behaviour
  
  after running `sqlfluff fix` I'd expect (`my_id` gets moved down and indented properly):
  
  ```sql
  WITH example AS (
      SELECT
          my_id,
          other_thing,
          one_more
      FROM
          my_table
  )
  
  SELECT *
  FROM example
  ```
  
  ## Observed Behaviour
  
  after running `sqlfluff fix` we get (notice that `my_id` is indented one extra space)
  
  ```sql
  WITH example AS (
      SELECT
           my_id,
          other_thing,
          one_more
      FROM
          my_table
  )
  
  SELECT *
  FROM example
  ```
  
  ## Steps to Reproduce
  
  Noted above. Create a file with the initial SQL and fun `sqfluff fix` on it.
  
  ## Dialect
  
  Running with default config.
  
  ## Version
  Include the output of `sqlfluff --version` along with your Python version
  
  sqlfluff, version 0.7.0
  Python 3.7.5
  
  ## Configuration
  
  Default config.
  
  
instance: {"FAIL_TO_PASS": "[\"test/rules/std_L003_L036_L039_combo_test.py::test__rules__std_L003_L036_L039\"]", "PASS_TO_PASS": "[\"test/rules/std_L016_L36_combo_test.py::test__rules__std_L016_L036_long_line_lint\", \"test/rules/std_L016_L36_combo_test.py::test__rules__std_L016_L036_long_line_fix\", \"test/rules/std_L016_L36_combo_test.py::test__rules__std_L016_L036_long_line_fix2\"]", "base_commit": "a1579a16b1d8913d9d7c7d12add374a290bcc78c", "created_at": "2021-10-22T18:23:33Z", "environment_setup_commit": "67023b85c41d23d6c6d69812a41b207c4f8a9331", "hints_text": "Does running `sqlfluff fix` again correct the SQL?\n@tunetheweb yes, yes it does. Is that something that the user is supposed to do (run it multiple times) or is this indeed a bug?\nIdeally not, but there are some circumstances where it\u2019s understandable that would happen. This however seems an easy enough example where it should not happen.\nThis appears to be a combination of rules L036, L003, and L039 not playing nicely together.\r\n\r\nThe original error is rule L036 and it produces this:\r\n\r\n```sql\r\nWITH example AS (\r\n    SELECT\r\nmy_id,\r\n        other_thing,\r\n        one_more\r\n    FROM\r\n        my_table\r\n)\r\n\r\nSELECT *\r\nFROM example\r\n```\r\n\r\nThat is, it moves the `my_id` down to the newline but does not even try to fix the indentation.\r\n\r\nThen we have another run through and L003 spots the lack of indentation and fixes it by adding the first set of whitespace:\r\n\r\n```sql\r\nWITH example AS (\r\n    SELECT\r\n    my_id,\r\n        other_thing,\r\n        one_more\r\n    FROM\r\n        my_table\r\n)\r\n\r\nSELECT *\r\nFROM example\r\n```\r\n\r\nThen we have another run through and L003 spots that there still isn't enough indentation and fixes it by adding the second set of whitespace:\r\n\r\n```sql\r\nWITH example AS (\r\n    SELECT\r\n        my_id,\r\n        other_thing,\r\n        one_more\r\n    FROM\r\n        my_table\r\n)\r\n\r\nSELECT *\r\nFROM example\r\n```\r\n\r\nAt this point we're all good.\r\n\r\nHowever then L039 has a look. It never expects two sets of whitespace following a new line and is specifically coded to only assume one set of spaces (which it normally would be if the other rules hadn't interfered as it would be parsed as one big space), so it think's the second set is too much indentation, so it replaces it with a single space.\r\n\r\nThen another run and L003 and the whitespace back in so we end up with two indents, and a single space.\r\n\r\nLuckily the fix is easier than that explanation. PR coming up...\r\n\r\n", "instance_id": "sqlfluff__sqlfluff-1733", "patch": "diff --git a/src/sqlfluff/rules/L039.py b/src/sqlfluff/rules/L039.py\n--- a/src/sqlfluff/rules/L039.py\n+++ b/src/sqlfluff/rules/L039.py\n@@ -44,7 +44,9 @@ def _eval(self, context: RuleContext) -> Optional[List[LintResult]]:\n                 # This is to avoid indents\n                 if not prev_newline:\n                     prev_whitespace = seg\n-                prev_newline = False\n+                # We won't set prev_newline to False, just for whitespace\n+                # in case there's multiple indents, inserted by other rule\n+                # fixes (see #1713)\n             elif seg.is_type(\"comment\"):\n                 prev_newline = False\n                 prev_whitespace = None\n", "problem_statement": "Extra space when first field moved to new line in a WITH statement\nNote, the query below uses a `WITH` statement. If I just try to fix the SQL within the CTE, this works fine.\r\n\r\nGiven the following SQL:\r\n\r\n```sql\r\nWITH example AS (\r\n    SELECT my_id,\r\n        other_thing,\r\n        one_more\r\n    FROM\r\n        my_table\r\n)\r\n\r\nSELECT *\r\nFROM example\r\n```\r\n\r\n## Expected Behaviour\r\n\r\nafter running `sqlfluff fix` I'd expect (`my_id` gets moved down and indented properly):\r\n\r\n```sql\r\nWITH example AS (\r\n    SELECT\r\n        my_id,\r\n        other_thing,\r\n        one_more\r\n    FROM\r\n        my_table\r\n)\r\n\r\nSELECT *\r\nFROM example\r\n```\r\n\r\n## Observed Behaviour\r\n\r\nafter running `sqlfluff fix` we get (notice that `my_id` is indented one extra space)\r\n\r\n```sql\r\nWITH example AS (\r\n    SELECT\r\n         my_id,\r\n        other_thing,\r\n        one_more\r\n    FROM\r\n        my_table\r\n)\r\n\r\nSELECT *\r\nFROM example\r\n```\r\n\r\n## Steps to Reproduce\r\n\r\nNoted above. Create a file with the initial SQL and fun `sqfluff fix` on it.\r\n\r\n## Dialect\r\n\r\nRunning with default config.\r\n\r\n## Version\r\nInclude the output of `sqlfluff --version` along with your Python version\r\n\r\nsqlfluff, version 0.7.0\r\nPython 3.7.5\r\n\r\n## Configuration\r\n\r\nDefault config.\r\n\n", "repo": "sqlfluff/sqlfluff", "test_patch": "diff --git a/test/rules/std_L003_L036_L039_combo_test.py b/test/rules/std_L003_L036_L039_combo_test.py\nnew file mode 100644\n--- /dev/null\n+++ b/test/rules/std_L003_L036_L039_combo_test.py\n@@ -0,0 +1,36 @@\n+\"\"\"Tests issue #1373 doesn't reoccur.\n+\n+The combination of L003 (incorrect indentation), L036 (select targets),\n+and L039 (unnecessary white space) can result in incorrect indentation.\n+\"\"\"\n+\n+import sqlfluff\n+\n+\n+def test__rules__std_L003_L036_L039():\n+    \"\"\"Verify that double indents don't flag L039.\"\"\"\n+    sql = \"\"\"\n+    WITH example AS (\n+        SELECT my_id,\n+            other_thing,\n+            one_more\n+        FROM\n+            my_table\n+    )\n+\n+    SELECT *\n+    FROM example\\n\"\"\"\n+    fixed_sql = \"\"\"\n+    WITH example AS (\n+        SELECT\n+            my_id,\n+            other_thing,\n+            one_more\n+        FROM\n+            my_table\n+    )\n+\n+    SELECT *\n+    FROM example\\n\"\"\"\n+    result = sqlfluff.fix(sql)\n+    assert result == fixed_sql\ndiff --git a/test/rules/std_L016_L36_combo.py b/test/rules/std_L016_L36_combo_test.py\nsimilarity index 100%\nrename from test/rules/std_L016_L36_combo.py\nrename to test/rules/std_L016_L36_combo_test.py\n", "version": "0.6"}
parsed_commit_content: {"file_diffs":[{"old_file_content":"\"\"\"Implementation of Rule L039.\"\"\"\nfrom typing import List, Optional\n\nfrom sqlfluff.core.parser import WhitespaceSegment\n\nfrom sqlfluff.core.rules.base import BaseRule, LintFix, LintResult, RuleContext\nfrom sqlfluff.core.rules.doc_decorators import document_fix_compatible\n\n\n@document_fix_compatible\nclass Rule_L039(BaseRule):\n    \"\"\"Unnecessary whitespace found.\n\n    | **Anti-pattern**\n\n    .. code-block:: sql\n\n        SELECT\n            a,        b\n        FROM foo\n\n    | **Best practice**\n    | Unless an indent or preceding a comment, whitespace should\n    | be a single space.\n\n    .. code-block:: sql\n\n        SELECT\n            a, b\n        FROM foo\n    \"\"\"\n\n    def _eval(self, context: RuleContext) -> Optional[List[LintResult]]:\n        \"\"\"Unnecessary whitespace.\"\"\"\n        # For the given segment, lint whitespace directly within it.\n        prev_newline = True\n        prev_whitespace = None\n        violations = []\n        for seg in context.segment.segments:\n            if seg.is_type(\"newline\"):\n                prev_newline = True\n                prev_whitespace = None\n            elif seg.is_type(\"whitespace\"):\n                # This is to avoid indents\n                if not prev_newline:\n                    prev_whitespace = seg\n                prev_newline = False\n            elif seg.is_type(\"comment\"):\n                prev_newline = False\n                prev_whitespace = None\n            else:\n                if prev_whitespace:\n                    if prev_whitespace.raw != \" \":\n                        violations.append(\n                            LintResult(\n                                anchor=prev_whitespace,\n                                fixes=[\n                                    LintFix(\n                                        \"edit\",\n                                        prev_whitespace,\n                                        WhitespaceSegment(),\n                                    )\n                                ],\n                            )\n                        )\n                prev_newline = False\n                prev_whitespace = None\n        return violations or None\n","new_file_content":"\"\"\"Implementation of Rule L039.\"\"\"\nfrom typing import List, Optional\n\nfrom sqlfluff.core.parser import WhitespaceSegment\n\nfrom sqlfluff.core.rules.base import BaseRule, LintFix, LintResult, RuleContext\nfrom sqlfluff.core.rules.doc_decorators import document_fix_compatible\n\n\n@document_fix_compatible\nclass Rule_L039(BaseRule):\n    \"\"\"Unnecessary whitespace found.\n\n    | **Anti-pattern**\n\n    .. code-block:: sql\n\n        SELECT\n            a,        b\n        FROM foo\n\n    | **Best practice**\n    | Unless an indent or preceding a comment, whitespace should\n    | be a single space.\n\n    .. code-block:: sql\n\n        SELECT\n            a, b\n        FROM foo\n    \"\"\"\n\n    def _eval(self, context: RuleContext) -> Optional[List[LintResult]]:\n        \"\"\"Unnecessary whitespace.\"\"\"\n        # For the given segment, lint whitespace directly within it.\n        prev_newline = True\n        prev_whitespace = None\n        violations = []\n        for seg in context.segment.segments:\n            if seg.is_type(\"newline\"):\n                prev_newline = True\n                prev_whitespace = None\n            elif seg.is_type(\"whitespace\"):\n                # This is to avoid indents\n                if not prev_newline:\n                    prev_whitespace = seg\n                # We won't set prev_newline to False, just for whitespace\n                # in case there's multiple indents, inserted by other rule\n                # fixes (see #1713)\n            elif seg.is_type(\"comment\"):\n                prev_newline = False\n                prev_whitespace = None\n            else:\n                if prev_whitespace:\n                    if prev_whitespace.raw != \" \":\n                        violations.append(\n                            LintResult(\n                                anchor=prev_whitespace,\n                                fixes=[\n                                    LintFix(\n                                        \"edit\",\n                                        prev_whitespace,\n                                        WhitespaceSegment(),\n                                    )\n                                ],\n                            )\n                        )\n                prev_newline = False\n                prev_whitespace = None\n        return violations or None\n","header":{"file":{"path":"src/sqlfluff/rules/L039.py"},"misc_line":null},"index_line":null,"is_binary_file":false,"binary_line":null,"minus_file":{"path":"a/src/sqlfluff/rules/L039.py"},"plus_file":{"path":"b/src/sqlfluff/rules/L039.py"},"hunks":[{"descriptor":{"old_range":{"start":44,"length":7},"new_range":{"start":44,"length":9},"section":"def _eval(self, context: RuleContext) -> Optional[List[LintResult]]:"},"line_group":{"all_lines":[{"content":"                # This is to avoid indents","type":"context"},{"content":"                if not prev_newline:","type":"context"},{"content":"                    prev_whitespace = seg","type":"context"},{"content":"                prev_newline = False","type":"deleted"},{"content":"                # We won't set prev_newline to False, just for whitespace","type":"added"},{"content":"                # in case there's multiple indents, inserted by other rule","type":"added"},{"content":"                # fixes (see #1713)","type":"added"},{"content":"            elif seg.is_type(\"comment\"):","type":"context"},{"content":"                prev_newline = False","type":"context"},{"content":"                prev_whitespace = None","type":"context"},{"content":"","type":"context"}]},"modified_entities":[],"added_entities":[],"deleted_entities":[]}]}],"old_commit_hash":"a1579a16b1d8913d9d7c7d12add374a290bcc78c","new_commit_hash":"e3fb8535acf3cdded95476fbfc723a8359550d94","commit_message":"Ensure the temporary file is written to disk before using it","commit_date":"2021-10-26T18:45:50-04:00","metadata":{}}