require "./inspection"
require "compiler/crystal/syntax"

module Lattice
  module Inspections
    # Flags source lines whose visible length exceeds the configured limit.
    #
    # Enabled via `.lattice.yml`:
    #
    #   max_line_length: 120
    #
    # When the key is absent or zero the inspection is disabled (default: off).
    # The check is a straight byte-count over the raw source lines — no AST
    # walk is needed, so it is very cheap even for large files.
    class MaxLineLengthInspection < Inspection
      property max_length : Int32 = 0

      def run(
        uri : String,
        source : String,
        lines : Array(String),
        ast : ::Crystal::ASTNode,
      ) : Array(::LSP::Diagnostic)
        return [] of ::LSP::Diagnostic if max_length <= 0

        diagnostics = [] of ::LSP::Diagnostic

        lines.each_with_index do |line, line_num|
          length = line.rstrip.size
          next if length <= max_length

          diagnostics << diagnostic(
            "Line is #{length} characters (limit: #{max_length})",
            line_num,
            max_length,
            line_num,
            length,
            ::LSP::DiagnosticSeverity::Warning,
            "lattice",
          )
        end

        diagnostics
      end
    end
  end
end
