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
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.
167 168 169 170 171 172 173 174 175 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 167 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) @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.
51 52 53 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 51 def cache_path @cache_path end |
Class Method Details
.call(workspace_root, symbol, excluded_paths: [], included_dirs: []) ⇒ Object
53 54 55 56 57 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 53 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.
60 61 62 63 64 65 66 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 60 def clear_cache! @mutex.synchronize { @file_cache.clear; @cache_loaded = false } 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 }, …] }
71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 71 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.
111 112 113 114 115 116 117 118 119 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 111 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.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 86 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
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 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 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 213 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) } return results if results.length >= MAX_RESULTS 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.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 180 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 |