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_dirnames: %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.
-
.file_cache ⇒ Object
readonly
Returns the value of attribute file_cache.
-
.mutex ⇒ Object
readonly
Returns the value of attribute mutex.
Class Method Summary collapse
- .call(workspace_root, symbol, excluded_dirnames: [], 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, from the cache.
-
.load_disk_cache_once ⇒ Object
Load the JSON cache from disk exactly once per process (double-checked under the mutex so concurrent first-calls don’t double-load).
-
.module_defined_in(workspace_root, module_name, excluded_dirnames: [], 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. -
.persist_cache ⇒ Object
Atomically write the in-memory cache to disk (tmp-file + rename).
-
.scan(workspace_root, excluded_dirnames: [], excluded_paths: [], included_dirs: []) ⇒ Object
Convenience wrapper: scan the whole workspace to warm the cache.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(workspace_root, symbol, excluded_dirnames: [], 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_dirnames: [], excluded_paths: [], included_dirs: []) ⇒ RubyDefinitionService
Returns a new instance of RubyDefinitionService.
171 172 173 174 175 176 177 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 171 def initialize(workspace_root, symbol, excluded_dirnames: [], excluded_paths: [], included_dirs: []) @workspace_root = workspace_root.to_s.chomp("/") @symbol = symbol @excluded_dirnames = Array(excluded_dirnames) @excluded_paths = Array(excluded_paths) @included_dirs = Array(included_dirs) end |
Class Attribute Details
.cache_path ⇒ Object
Returns the value of attribute cache_path.
52 53 54 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 52 def cache_path @cache_path end |
.file_cache ⇒ Object (readonly)
Returns the value of attribute file_cache.
51 52 53 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 51 def file_cache @file_cache end |
.mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
51 52 53 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 51 def mutex @mutex end |
Class Method Details
.call(workspace_root, symbol, excluded_dirnames: [], excluded_paths: [], included_dirs: []) ⇒ Object
54 55 56 57 58 59 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 54 def call(workspace_root, symbol, excluded_dirnames: [], excluded_paths: [], included_dirs: []) new(workspace_root, symbol, excluded_dirnames: excluded_dirnames, excluded_paths: excluded_paths, included_dirs: included_dirs).call end |
.clear_cache! ⇒ Object
Exposed for tests.
102 103 104 105 106 107 108 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 102 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). The file must already be cached; returns {} if not found. Result: { “method_name” => [{ line: Integer, signature: String }, …] }
113 114 115 116 117 118 119 120 121 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 113 def defs_in_file(file_path) load_disk_cache_once entry = @mutex.synchronize { @file_cache[file_path.to_s] } 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, from the cache. The file must already be cached; returns [] if not found.
153 154 155 156 157 158 159 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 153 def includes_in_file(file_path) load_disk_cache_once entry = @mutex.synchronize { @file_cache[file_path.to_s] } return [] unless entry entry[:include_calls] || [] end |
.load_disk_cache_once ⇒ Object
Load the JSON cache from disk exactly once per process (double-checked under the mutex so concurrent first-calls don’t double-load).
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 63 def load_disk_cache_once return if @cache_loaded @mutex.synchronize do return if @cache_loaded @cache_loaded = true path = @cache_path.to_s return if path.empty? || !File.exist?(path) raw = JSON.parse(File.read(path)) raw.each do |abs_path, entry| @file_cache[abs_path] = { mtime: entry["mtime"].to_f, lines: entry["lines"], all_defs: entry["all_defs"], module_names: entry["module_names"], # nil for old-format entries → triggers re-parse include_calls: entry["include_calls"] # nil for old-format entries → triggers re-parse } end rescue StandardError nil # corrupted or incompatible cache file — start fresh end end |
.module_defined_in(workspace_root, module_name, excluded_dirnames: [], 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.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 126 def module_defined_in(workspace_root, module_name, excluded_dirnames: [], 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_dirnames: excluded_dirnames, 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 |
.persist_cache ⇒ Object
Atomically write the in-memory cache to disk (tmp-file + rename).
89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 89 def persist_cache path = @cache_path.to_s return if path.empty? snapshot = @mutex.synchronize { @file_cache.dup } tmp_path = "#{path}.tmp" File.write(tmp_path, JSON.generate(snapshot)) File.rename(tmp_path, path) rescue StandardError nil end |
.scan(workspace_root, excluded_dirnames: [], excluded_paths: [], included_dirs: []) ⇒ Object
Convenience wrapper: scan the whole workspace to warm the cache. Fast on subsequent calls (only re-parses files whose mtime changed).
163 164 165 166 167 168 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 163 def scan(workspace_root, excluded_dirnames: [], excluded_paths: [], included_dirs: []) new(workspace_root, nil, excluded_dirnames: excluded_dirnames, excluded_paths: excluded_paths, included_dirs: included_dirs).scan_workspace end |
Instance Method Details
#call ⇒ Object
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 265 266 267 268 269 270 271 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 217 def call self.class.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| # Prune excluded directories if File.directory?(path) dirname = File.basename(path) rel_dir = relative_path(path) Find.prune if path != root && excluded_dir?(dirname, rel_dir) next end next unless path.end_with?(".rb") rel = relative_path(path) next if excluded_rel_path?(rel, File.basename(path)) 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 self.class.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.
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 212 213 214 215 |
# File 'app/services/mbeditor/ruby_definition_service.rb', line 182 def scan_workspace self.class.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) dirname = File.basename(path) rel_dir = relative_path(path) Find.prune if path != root && excluded_dir?(dirname, rel_dir) next end next unless path.end_with?(".rb") rel = relative_path(path) next if excluded_rel_path?(rel, File.basename(path)) 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 self.class.persist_cache if @new_entries end |