Class: SorbetBaml::CommentExtractor
- Inherits:
-
Object
- Object
- SorbetBaml::CommentExtractor
- Extended by:
- T::Sig
- Defined in:
- lib/sorbet_baml/comment_extractor.rb
Overview
Extracts documentation comments from Sorbet struct and enum source files
Class Method Summary collapse
- .extract_comments_from_lines(lines, class_name, comments) ⇒ Object
- .extract_enum_comments(klass) ⇒ Object
- .extract_enum_comments_from_lines(lines, class_name, comments) ⇒ Object
- .extract_field_comments(klass) ⇒ Object
- .find_source_file(klass) ⇒ Object
Class Method Details
.extract_comments_from_lines(lines, class_name, comments) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/sorbet_baml/comment_extractor.rb', line 74 def self.extract_comments_from_lines(lines, class_name, comments) in_target_class = T.let(false, T::Boolean) current_comment = T.let(nil, T.nilable(String)) brace_depth = 0 lines.each do |line| stripped = line.strip # Check if we're entering the target class if stripped.match(/^class\s+#{Regexp.escape(class_name)}\s*<\s*T::Struct/) in_target_class = true brace_depth = 0 next end next unless in_target_class # Track brace depth to handle nested classes brace_depth += stripped.count('{') brace_depth -= stripped.count('}') # Exit when we reach the end of the class break if stripped == 'end' && brace_depth == 0 # Extract comment if stripped.start_with?('#') comment_text = T.must(stripped[1..-1]).strip current_comment = current_comment ? "#{current_comment} #{comment_text}" : comment_text elsif stripped.match(/^const\s+:(\w+)/) && current_comment field_name = T.must(stripped.match(/^const\s+:(\w+)/))[1] comments[T.must(field_name)] = current_comment current_comment = nil elsif !stripped.empty? && !stripped.start_with?('#') # Reset comment if we hit non-comment, non-const line current_comment = nil end end end |
.extract_enum_comments(klass) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sorbet_baml/comment_extractor.rb', line 28 def self.extract_enum_comments(klass) comments = {} source_file = find_source_file(klass) return comments unless source_file && File.exist?(source_file) lines = File.readlines(source_file) extract_enum_comments_from_lines(lines, T.must(T.must(klass.name).split('::').last), comments) comments end |
.extract_enum_comments_from_lines(lines, class_name, comments) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/sorbet_baml/comment_extractor.rb', line 114 def self.extract_enum_comments_from_lines(lines, class_name, comments) in_target_class = T.let(false, T::Boolean) in_enums_block = T.let(false, T::Boolean) current_comment = T.let(nil, T.nilable(String)) lines.each do |line| stripped = line.strip # Check if we're entering the target enum class if stripped.match(/^class\s+#{Regexp.escape(class_name)}\s*<\s*T::Enum/) in_target_class = true next end next unless in_target_class # Check if we're in the enums block if stripped == 'enums do' in_enums_block = true next end # Exit enums block if in_enums_block && stripped == 'end' in_enums_block = false next end # Exit class break if stripped == 'end' && !in_enums_block next unless in_enums_block # Extract comment if stripped.start_with?('#') comment_text = T.must(stripped[1..-1]).strip current_comment = current_comment ? "#{current_comment} #{comment_text}" : comment_text elsif stripped.match(/^(\w+)\s*=\s*new/) && current_comment enum_name = T.must(stripped.match(/^(\w+)\s*=\s*new/))[1] comments[T.must(enum_name)] = current_comment current_comment = nil elsif !stripped.empty? && !stripped.start_with?('#') # Reset comment if we hit non-comment, non-enum line current_comment = nil end end end |
.extract_field_comments(klass) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/sorbet_baml/comment_extractor.rb', line 10 def self.extract_field_comments(klass) # First try to get descriptions from the description extractor (extra field) descriptions = DescriptionExtractor.extract_prop_descriptions(klass) # Then fall back to comment-based extraction for any missing descriptions comments = {} source_file = find_source_file(klass) if source_file && File.exist?(source_file) lines = File.readlines(source_file) extract_comments_from_lines(lines, T.must(T.must(klass.name).split('::').last), comments) end # Merge with priority: description parameters > comments descriptions.merge(comments) { |_key, desc_param, _comment| desc_param } end |
.find_source_file(klass) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sorbet_baml/comment_extractor.rb', line 41 def self.find_source_file(klass) # Try to find where the class was defined # This is a heuristic approach since Ruby doesn't provide reliable source location for classes # Method 1: Check if any methods have source location begin if klass.respond_to?(:new) && klass.method(:new).respond_to?(:source_location) location = klass.method(:new).source_location return location[0] if location end rescue StandardError # Ignore errors end # Method 2: Look at the current call stack for files that might contain the class caller_locations.each do |location| file_path = location.absolute_path || location.path next unless file_path && File.exist?(file_path) # Read the file and check if it contains the class definition begin content = File.read(file_path) class_name = T.must(klass.name).split('::').last return file_path if content.match(/class\s+#{Regexp.escape(T.must(class_name))}\s*</) rescue StandardError # Ignore file read errors end end nil end |