# File lib/ruby_indexer/lib/ruby_indexer/configuration.rb, line 149defapply_config(config)
validate_config!(config)
@excluded_gems.concat(config["excluded_gems"]) ifconfig["excluded_gems"]
@included_gems.concat(config["included_gems"]) ifconfig["included_gems"]
@excluded_patterns.concat(config["excluded_patterns"]) ifconfig["excluded_patterns"]
@included_patterns.concat(config["included_patterns"]) ifconfig["included_patterns"]
@excluded_magic_comments.concat(config["excluded_magic_comments"]) ifconfig["excluded_magic_comments"]
end
indexables()
# File lib/ruby_indexer/lib/ruby_indexer/configuration.rb, line 47defindexablesexcluded_gems = @excluded_gems-@included_gemslocked_gems = Bundler.locked_gems&.specs# NOTE: indexing the patterns (both included and excluded) needs to happen before indexing gems, otherwise we risk# having duplicates if BUNDLE_PATH is set to a folder inside the project structure# Add user specified patternsindexables = @included_patterns.flat_mapdo|pattern|load_path_entry = T.let(nil, T.nilable(String))
Dir.glob(pattern, File::FNM_PATHNAME|File::FNM_EXTGLOB).map!do|path|path = File.expand_path(path)
# All entries for the same pattern match the same $LOAD_PATH entry. Since searching the $LOAD_PATH for every# entry is expensive, we memoize it until we find a path that doesn't belong to that $LOAD_PATH. This happens# on repositories that define multiple gems, like Rails. All frameworks are defined inside the Dir.pwd, but# each one of them belongs to a different $LOAD_PATH entryifload_path_entry.nil?||!path.start_with?(load_path_entry)
load_path_entry = $LOAD_PATH.find { |load_path|path.start_with?(load_path) }
endIndexablePath.new(load_path_entry, path)
endend# Remove user specified patternsindexables.reject!do|indexable|@excluded_patterns.any?do|pattern|File.fnmatch?(pattern, indexable.full_path, File::FNM_PATHNAME|File::FNM_EXTGLOB)
endend# Add default gems to the list of files to be indexedDir.glob(File.join(RbConfig::CONFIG["rubylibdir"], "*")).eachdo|default_path|# The default_path might be a Ruby file or a folder with the gem's name. For example:# bundler/# bundler.rb# psych/# psych.rbpathname = Pathname.new(default_path)
short_name = pathname.basename.to_s.delete_suffix(".rb")
# If the gem name is excluded, then we skip itnextifexcluded_gems.include?(short_name)
# If the default gem is also a part of the bundle, we skip indexing the default one and index only the one in# the bundle, which won't be in `default_path`, but will be in `Bundler.bundle_path` insteadnextiflocked_gems&.any?do|locked_spec|locked_spec.name==short_name&&!Gem::Specification.find_by_name(short_name).full_gem_path.start_with?(RbConfig::CONFIG["rubylibprefix"])
rescueGem::MissingSpecError# If a default gem is scoped to a specific platform, then `find_by_name` will raise. We want to skip those# casestrueendifpathname.directory?# If the default_path is a directory, we index all the Ruby files in itindexables.concat(
Dir.glob(File.join(default_path, "**", "*.rb"), File::FNM_PATHNAME|File::FNM_EXTGLOB).map!do|path|IndexablePath.new(RbConfig::CONFIG["rubylibdir"], path)
end,
)
elsifpathname.extname==".rb"# If the default_path is a Ruby file, we index itindexables<<IndexablePath.new(RbConfig::CONFIG["rubylibdir"], default_path)
endend# Add the locked gems to the list of files to be indexedlocked_gems&.eachdo|lazy_spec|nextifexcluded_gems.include?(lazy_spec.name)
spec = Gem::Specification.find_by_name(lazy_spec.name)
# When working on a gem, it will be included in the locked_gems list. Since these are the project's own files,# we have already included and handled exclude patterns for it and should not re-include or it'll lead to# duplicates or accidentally ignoring exclude patternsnextifspec.full_gem_path==Dir.pwdindexables.concat(
spec.require_paths.flat_mapdo|require_path|load_path_entry = File.join(spec.full_gem_path, require_path)
Dir.glob(File.join(load_path_entry, "**", "*.rb")).map! { |path|IndexablePath.new(load_path_entry, path) }
end,
)
rescueGem::MissingSpecError# If a gem is scoped only to some specific platform, then its dependencies may not be installed either, but they# are still listed in locked_gems. We can't index them because they are not installed for the platform, so we# just ignore if they're missingendindexables.uniq!(&:full_path)
indexablesend
magic_comment_regex()
# File lib/ruby_indexer/lib/ruby_indexer/configuration.rb, line 144defmagic_comment_regex@magic_comment_regex||=T.let(/^#\s*#{@excluded_magic_comments.join("|")}/, T.nilable(Regexp))
end