# frozen_string_literal: true

# Monkey-patch to fix Solargraph 0.48.0 bug with Ruby 3.4+
# See: https://github.com/castwide/solargraph/issues/733
# Fixed in Solargraph PR #735 (merged Jan 7, 2025) but not in 0.48.0
# https://github.com/castwide/solargraph/pull/735/files
#
# Bug: node_range method crashes when passed nil node
# This happens, for example,
# with rescue clauses that don't assign exception to a variable
# Example: "rescue NameError" instead of "rescue NameError => e"

if defined?(RubyVM::AbstractSyntaxTree)
	require "solargraph"

	module Solargraph
		module Parser
			module Rubyvm
				module ClassMethods
					# Fix from PR #735: Add nil check to node_range
					def node_range(node)
						if node.nil?
							nil
						else
							st = Position.new(node.first_lineno - 1, node.first_column)
							en = Position.new(node.last_lineno - 1, node.last_column)
							Range.new(st, en)
						end
					end
				end
			end
		end
	end
end
