# Buggy code
def slice_file(
    cls, raw_str: str, templated_str: str
) -> Tuple[List[RawFileSlice], List[TemplatedFileSlice]]:
    """Slice the file to determine regions where we can fix."""
    templater_logger.info("Slicing File Template")
    templater_logger.debug("    Raw String: %r", raw_str)
    templater_logger.debug("    Templated String: %r", templated_str)
    # Slice the raw file
    raw_sliced = list(cls._slice_template(raw_str))
    # Find the literals
    literals = [
        raw_slice.raw for raw_slice in raw_sliced if raw_slice.slice_type == "literal"
    ]
    templater_logger.debug("    Literals: %s", literals)
    # Calculate occurrences
    raw_occurrences = cls._substring_occurances(raw_str, literals)
    templated_occurances = cls._substring_occurances(templated_str, literals)
    templater_logger.debug(
        "    Occurances: Raw: %s, Templated: %s",
        raw_occurrences,
        templated_occurances,
    )
    # Split on invariants
    split_sliced = list(
        cls._split_invariants(
            raw_sliced,
            literals,
            raw_occurrences,
            templated_occurances,
            templated_str,
        )
    )
    templater_logger.debug("    Split Sliced: %s", split_sliced)
    # Deal with uniques and coalesce the rest
    sliced_file = list(
        cls._split_uniques_coalesce_rest(
            split_sliced, raw_occurrences, templated_occurances, templated_str
        )
    )
    templater_logger.debug("    Fully Sliced: %s", sliced_file)
    return raw_sliced, sliced_file


# Traceback (IndexError)
Traceback (most recent call last):
File "/Users/niallwoodward/dev/pull_requests/dbt_sqlfluff_venv/bin/sqlfluff", line 11, in <module>
load_entry_point('sqlfluff', 'console_scripts', 'sqlfluff')()
File "/Users/niallwoodward/dev/pull_requests/dbt_sqlfluff_venv/lib/python3.6/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/Users/niallwoodward/dev/pull_requests/dbt_sqlfluff_venv/lib/python3.6/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/Users/niallwoodward/dev/pull_requests/dbt_sqlfluff_venv/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/niallwoodward/dev/pull_requests/dbt_sqlfluff_venv/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/niallwoodward/dev/pull_requests/dbt_sqlfluff_venv/lib/python3.6/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/cli/commands.py", line 323, in lint
ignore_files=not disregard_sqlfluffignores,
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/core/linter.py", line 1161, in lint_paths
ignore_files=ignore_files,
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/core/linter.py", line 1139, in lint_path
target_file.read(), fname=fname, fix=fix, config=config
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/core/linter.py", line 939, in lint_string
parsed = self.parse_string(in_str=in_str, fname=fname, config=config)
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/core/linter.py", line 723, in parse_string
tokens, lex_vs = lexer.lex(templated_file)
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/core/parser/lexer.py", line 343, in lex
return self.enrich_segments(segment_buff, raw), violations
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/core/parser/lexer.py", line 373, in enrich_segments
templated_slice
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/core/templaters/base.py", line 283, in templated_slice_to_source_slice
elif start_slices[0][0] == "literal":
IndexError: list index out of range

# Fixed code
def slice_file(
    cls, raw_str: str, templated_str: str, config=None
) -> Tuple[List[RawFileSlice], List[TemplatedFileSlice], str]:
    """Slice the file to determine regions where we can fix."""
    templater_logger.info("Slicing File Template")
    templater_logger.debug("    Raw String: %r", raw_str)
    templater_logger.debug("    Templated String: %r", templated_str)
    # Slice the raw file
    raw_sliced = list(cls._slice_template(raw_str))
    # Find the literals
    literals = [
        raw_slice.raw for raw_slice in raw_sliced if raw_slice.slice_type == "literal"
    ]
    templater_logger.debug("    Literals: %s", literals)
    for loop_idx in range(2):
        templater_logger.debug("    # Slice Loop %s", loop_idx)
        # Calculate occurrences
        raw_occurrences = cls._substring_occurances(raw_str, literals)
        templated_occurances = cls._substring_occurances(templated_str, literals)
        templater_logger.debug(
            "    Occurances: Raw: %s, Templated: %s",
            raw_occurrences,
            templated_occurances,
        )
        # Split on invariants
        split_sliced = list(
            cls._split_invariants(
                raw_sliced,
                literals,
                raw_occurrences,
                templated_occurances,
                templated_str,
            )
        )
        templater_logger.debug("    Split Sliced: %s", split_sliced)
        # Deal with uniques and coalesce the rest
        sliced_file = list(
            cls._split_uniques_coalesce_rest(
                split_sliced, raw_occurrences, templated_occurances, templated_str
            )
        )
        templater_logger.debug("    Fully Sliced: %s", sliced_file)
        unwrap_wrapped = (
            True
            if config is None
            else config.get("unwrap_wrapped_queries", section="templater", default=True)
        )
        sliced_file, new_templated_str = cls._check_for_wrapped(
            sliced_file, templated_str, unwrap_wrapped=unwrap_wrapped
        )
        if new_templated_str == templated_str:
            # If we didn't change it then we're done.
            break
        else:
            # If it's not equal, loop around
            templated_str = new_templated_str
    return raw_sliced, sliced_file, new_templated_str