CodeStyleRewriter

InfoGenerateCreated ByPackages

The `CodeStyleEnforcer` class is designed to enforce code style by rewriting code files to match a team's specified style. It inherits from `Sublayer::Generators::Base` and uses a large language model (LLM) output adapter of type `:single_string`. This class takes two file paths as arguments upon initialization: an example file that contains code adhering to the desired style and an offending file that does not. The `generate` method is available for extending functionality, potentially from a parent class. The core of the transformation happens in the `prompt` method, which constructs a prompt for the LLM to rewrite the offending file based on the style observed in the example file.

class CodeStyleEnforcer < Sublayer::Generators::Base
  llm_output_adapter type: :single_string,
    name: "styled_code",
    description: "The code rewritten to match the team's style"

  def initialize(example_file:, offending_file:)
    @example_file = example_file
    @offending_file = offending_file
  end

  def generate
    super
  end

  def prompt
    <<-PROMPT
        You are an expert in code style enforcement.

        You are tasked with taking an offending code file and rewriting it to match the style of a quintessential example file provided by the team.

        The example file content:
        #{File.read(@example_file)}

        The offending file content:
        #{File.read(@offending_file)}

        Please rewrite the offending file so that it adheres to the style demonstrated in the example file.

        Take a deep breath and think step by step before you start coding.
    PROMPT
  end
end