Class: Mbeditor::RubyDefinitionService
- Inherits:
-
Object
- Object
- Mbeditor::RubyDefinitionService
- Defined in:
- app/services/mbeditor/ruby_definition_service.rb
Overview
Searches .rb files in a workspace for definitions of a named Ruby method using Ripper's AST parser (no subprocesses).
Returns an array of hashes, each describing one definition site:
{
file: String, # workspace-relative path
line: Integer, # 1-based line number of the `def` keyword
signature: String, # trimmed source text of the def line
comments: String # leading # comment lines immediately above the def (may be empty)
}
Usage:
results = RubyDefinitionService.call(workspace_root, "my_method",
excluded_paths: %w[tmp .git])
Additional class methods for IntelliSense features:
RubyDefinitionService.defs_in_file(abs_path)
→ { "method_name" => [{ line: Integer, signature: String }, ...] }
RubyDefinitionService.module_defined_in(workspace_root, "ModuleName", ...)
→ abs_path String or nil
RubyDefinitionService.includes_in_file(abs_path)
→ ["ModuleName", ...] (include/extend/prepend calls)
Constant Summary collapse
- MAX_RESULTS =
20- MAX_COMMENT_LOOKAHEAD =
15- MAX_FILES_SCANNED =
10_000- MAX_CACHE_ENTRIES =
Upper bound on cached files. Entries carry full file contents, so this caps both the on-disk JSON and the resident hash.
2_000- PERSIST_INTERVAL =
Minimum gap between disk writes of that cache (seconds).
30
Class Attribute Summary collapse
-
.cache_path ⇒ Object
Returns the value of attribute cache_path.
Class Method Summary collapse
- .call(workspace_root, symbol, excluded_paths: [], included_dirs: []) ⇒ Object
-
.clear_cache! ⇒ Object
Exposed for tests.
-
.defs_in_file(file_path) ⇒ Object
Returns all method defs in a single file from the cache (no workspace walk).
-
.includes_in_file(file_path) ⇒ Object
Returns the list of module/class names passed to include/extend/prepend in the given file.
-
.module_defined_in(workspace_root, module_name, excluded_paths: [], included_dirs: []) ⇒ Object
Searches the cache (and triggers a workspace scan if needed) to find which file in
workspace_rootdefines the given module or class name.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(workspace_root, symbol, excluded_paths: [], included_dirs: []) ⇒ RubyDefinitionService
constructor
A new instance of RubyDefinitionService.
-
#scan_workspace ⇒ Object
Walks the entire workspace and populates the per-file cache (including the new module_names and include_calls fields) without filtering by symbol.
Constructor Details
#initialize(workspace_root, symbol, excluded_paths: [], included_dirs: []) ⇒ RubyDefinitionService
Returns a new instance of RubyDefinitionService.
190 191 192 193 194 195 196 197 198 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 190 def initialize(workspace_root, symbol, excluded_paths: [], included_dirs: []) @workspace_root = workspace_root.to_s.chomp("/") @symbol = symbol @excluded_paths = Array(excluded_paths) @included_dirs = Array(included_dirs) @exclusion_matcher = ExclusionMatcher.new(@excluded_paths, root: @workspace_root) @shared_cache = self.class.send(:file_cache) @shared_mutex = self.class.send(:mutex) end |
Class Attribute Details
.cache_path ⇒ Object
Returns the value of attribute cache_path.
56 57 58 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 56 def cache_path @cache_path end |
Class Method Details
.call(workspace_root, symbol, excluded_paths: [], included_dirs: []) ⇒ Object
58 59 60 61 62 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 58 def call(workspace_root, symbol, excluded_paths: [], included_dirs: []) new(workspace_root, symbol, excluded_paths: excluded_paths, included_dirs: included_dirs).call end |
.clear_cache! ⇒ Object
Exposed for tests.
65 66 67 68 69 70 71 72 73 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 65 def clear_cache! @mutex.synchronize { @file_cache.clear; @cache_loaded = false } @last_evict_at = nil @last_persist_at = nil path = @cache_path.to_s File.delete(path) if !path.empty? && File.exist?(path) rescue StandardError nil end |
.defs_in_file(file_path) ⇒ Object
Returns all method defs in a single file from the cache (no workspace walk). Self-warms the cache for the file if not already present. Result: { "method_name" => [{ line: Integer, signature: String }, ...] }
78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 78 def defs_in_file(file_path) load_disk_cache_once file_path = file_path.to_s entry = @mutex.synchronize { @file_cache[file_path] } entry ||= new(File.dirname(file_path), nil).send(:cache_entry_for, file_path) return {} unless entry entry[:all_defs].transform_values do |lines_arr| lines_arr.map { |line| { line: line, signature: (entry[:lines][line - 1] || "").strip } } end end |
.includes_in_file(file_path) ⇒ Object
Returns the list of module/class names passed to include/extend/prepend in the given file. Self-warms the cache for the file if not already present.
118 119 120 121 122 123 124 125 126 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 118 def includes_in_file(file_path) load_disk_cache_once file_path = file_path.to_s entry = @mutex.synchronize { @file_cache[file_path] } entry ||= new(File.dirname(file_path), nil).send(:cache_entry_for, file_path) return [] unless entry entry[:include_calls] || [] end |
.module_defined_in(workspace_root, module_name, excluded_paths: [], included_dirs: []) ⇒ Object
Searches the cache (and triggers a workspace scan if needed) to find
which file in workspace_root defines the given module or class name.
Returns the absolute file path string or nil.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 93 def module_defined_in(workspace_root, module_name, excluded_paths: [], included_dirs: []) load_disk_cache_once root_prefix = workspace_root.to_s.chomp("/") within_dirs = ->(path) { return true if included_dirs.empty? included_dirs.any? { |d| path.start_with?(File.join(root_prefix, d) + "/") } } result = @mutex.synchronize do @file_cache.find { |path, entry| within_dirs.call(path) && entry[:module_names]&.include?(module_name) } end return result[0] if result # Cache miss: scan workspace to populate cache entries with module_names. new(workspace_root, nil, excluded_paths: excluded_paths, included_dirs: included_dirs).scan_workspace result = @mutex.synchronize do @file_cache.find { |path, entry| within_dirs.call(path) && entry[:module_names]&.include?(module_name) } end result ? result[0] : nil end |
Instance Method Details
#call ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 236 def call load_disk_cache_once results = [] @new_entries = false files_scanned = 0 evict_deleted_cache_entries search_roots.each do |root| Find.find(root) do |path| if File.directory?(path) Find.prune if path != root && @exclusion_matcher.excluded?(relative_path(path)) next end next unless path.end_with?(".rb") rel = relative_path(path) next if @exclusion_matcher.excluded?(rel) files_scanned += 1 if files_scanned > MAX_FILES_SCANNED Rails.logger.warn("[mbeditor] RubyDefinitionService: workspace exceeds #{MAX_FILES_SCANNED} .rb files; stopping scan early") break end begin cached = cache_entry_for(path) next unless cached hit_lines = @symbol ? cached[:all_defs].fetch(@symbol, nil) : nil next unless hit_lines && hit_lines.any? hit_lines.each do |def_line| results << { file: rel, line: def_line, signature: (cached[:lines][def_line - 1] || "").strip, comments: extract_comments(cached[:lines], def_line) } if results.length >= MAX_RESULTS persist_cache if @new_entries return results end end rescue StandardError # Malformed file or unreadable; skip silently end end end persist_cache if @new_entries results end |
#scan_workspace ⇒ Object
Walks the entire workspace and populates the per-file cache (including the
new module_names and include_calls fields) without filtering by symbol.
Used by module_defined_in to ensure the cache is warm.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 203 def scan_workspace load_disk_cache_once @new_entries = false files_scanned = 0 evict_deleted_cache_entries search_roots.each do |root| Find.find(root) do |path| if File.directory?(path) Find.prune if path != root && @exclusion_matcher.excluded?(relative_path(path)) next end next unless path.end_with?(".rb") rel = relative_path(path) next if @exclusion_matcher.excluded?(rel) files_scanned += 1 if files_scanned > MAX_FILES_SCANNED Rails.logger.warn("[mbeditor] RubyDefinitionService: workspace exceeds #{MAX_FILES_SCANNED} .rb files; stopping scan early") break end begin cache_entry_for(path) rescue StandardError nil end end end persist_cache if @new_entries end |