Repository: sqlfluff/sqlfluff

Issue:
Issue linting with macro at top of file in 2.0.0a6
### Search before asking

- [X] I searched the [issues](https://github.com/sqlfluff/sqlfluff/issues) and found no similar issues.


### What Happened

Attempting to lint a file with a macro at the top in dbt project

### Expected Behaviour

No issues/failures

### Observed Behaviour

Lint the file produced this error message: `NotImplementedError: Unexpected templated indent. Report this as a bug on GitHub. Segment: <WhitespaceSegment: ([L:  1, P:  1]) '    '>`


### How to reproduce

Within a dbt project, create the following file

`models/test.sql`
```sql
{{ dev_config() }}

select col_1 from my_table
```

Where `dev_config` is something like this:

`macros/dev_config.sql`
```sql
{% macro dev_config() %}
    {% if var('dev', false) %}
        {{ config(materialized='table') }}
    {% endif %}
{% endmacro %}
```

Then lint
```sh
$ sqlfluff lint model/test.sql
```


### Dialect

ANSI

### Version

2.0.0a6

### Configuration

`.sqlfluff`
```
[sqlfluff]
dialect = ansi
templater = dbt
# aliasing.table: oracle doesn't do explicit table aliases
# structure.column_order: we want columns in a particular order, to make output easier to validate
exclude_rules = aliasing.table,structure.column_order
max_line_length = 125

[sqlfluff:templater:dbt]
profiles_dir = .

[sqlfluff:indentation]
indented_joins = True

[sqlfluff:rules:capitalisation.keywords]
capitalisation_policy = lower

[sqlfluff:rules:capitalisation.identifiers]
extended_capitalisation_policy = lower

[sqlfluff:rules:capitalisation.functions]
capitalisation_policy = lower

[sqlfluff:rules:capitalisation.literals]
capitalisation_policy = lower

[sqlfluff:rules:references.keywords]
ignore_words = procedures
```


### Are you willing to work on and submit a PR to address the issue?

- [x] Yes I am willing to submit a PR!

### Code of Conduct

- [X] I agree to follow this project's [Code of Conduct](https://github.com/sqlfluff/sqlfluff/blob/main/CODE_OF_CONDUCT.md)


Discussion:
coveralls: ## Pull Request Test Coverage Report for [Build 4388428941](https://coveralls.io/builds/57750727)

* **1** of **1**   **(100.0%)**  changed or added relevant line in **1** file are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage remained the same at **100.0%**

---



|  Totals | [![Coverage Status](https://coveralls.io/builds/57750727/badge)](https://coveralls.io/builds/57750727) |
| :-- | --: |
| Change from base [Build 4387552045](https://coveralls.io/builds/57746984): |  0.0% |
| Covered Lines: | 17357 |
| Relevant Lines: | 17357 |

---
##### 💛  - [Coveralls](https://coveralls.io)

codecov[bot]: ## [Codecov](https://codecov.io/gh/sqlfluff/sqlfluff/pull/4491?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff) Report
Patch coverage: **`100.00`**% and no project coverage change.
> Comparison is base [(`b48a6d8`)](https://codecov.io/gh/sqlfluff/sqlfluff/commit/b48a6d8bce37b0e71ed6fb58a0cd246d03185ab1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff) 100.00% compared to head [(`146ab7c`)](https://codecov.io/gh/sqlfluff/sqlfluff/pull/4491?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff) 100.00%.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##              main     #4491   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          204       204           
  Lines        15524     15524           
=========================================
  Hits         15524     15524           
```


| [Impacted Files](https://codecov.io/gh/sqlfluff/sqlfluff/pull/4491?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff) | Coverage Δ | |
|---|---|---|
| [src/sqlfluff/utils/reflow/reindent.py](https://codecov.io/gh/sqlfluff/sqlfluff/pull/4491?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff#diff-c3JjL3NxbGZsdWZmL3V0aWxzL3JlZmxvdy9yZWluZGVudC5weQ==) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff)

</details>

[:umbrella: View full report in Codecov by Sentry](https://codecov.io/gh/sqlfluff/sqlfluff/pull/4491?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff).


Patch:
diff --git a/src/sqlfluff/utils/reflow/reindent.py b/src/sqlfluff/utils/reflow/reindent.py
index 087c6ee74..c7453cc0f 100644
--- a/src/sqlfluff/utils/reflow/reindent.py
+++ b/src/sqlfluff/utils/reflow/reindent.py
@@ -803,7 +803,7 @@ def _deduce_line_current_indent(
         else:
             reflow_logger.debug("    Handling as initial leading whitespace")
             for indent_seg in elements[0].segments[::-1]:
-                if indent_seg.is_type("whitespace"):
+                if indent_seg.is_type("whitespace") and not indent_seg.is_templated:
                     break
             # Handle edge case of no whitespace, but with newline.
             if not indent_seg.is_type("whitespace"):
@@ -819,6 +819,7 @@ def _deduce_line_current_indent(
         # It's a consumed indent.
         return cast(TemplateSegment, indent_seg).source_str.split("\n")[-1] or ""
     elif not indent_seg.pos_marker or not indent_seg.is_templated:
+        # It's a literal
         assert "\n" not in indent_seg.raw, f"Found newline in indent: {indent_seg}"
         return indent_seg.raw
     else:  # pragma: no cover


Test patch:
diff --git a/test/utils/reflow/reindent_test.py b/test/utils/reflow/reindent_test.py
index 91eeaa5ab..fea23bc18 100644
--- a/test/utils/reflow/reindent_test.py
+++ b/test/utils/reflow/reindent_test.py
@@ -610,6 +610,14 @@ def test_reflow__crawl_indent_points(raw_sql_in, points_out, default_config, cap
             "    \n"
             "  \n",
         ),
+        # Test leading templated newlines.
+        # https://github.com/sqlfluff/sqlfluff/issues/4485
+        (
+            "{{ '\\n   \\n   ' }}\nSELECT 1",
+            # NOTE: This looks a little strange, but what's important
+            # here is that it doesn't raise an exception.
+            "\n   \n   \nSELECT 1",
+        ),
     ],
 )
 def test_reflow__lint_indent_points(raw_sql_in, raw_sql_out, default_config, caplog):