class CallbackAdapter
Adapter object that owns the callback procs for a single indexing run.
Definitions
attr_reader :callbacks
Signature
-
attribute
r callbacks
-
returns
Lib::IndexerCallbacks The callback struct passed to libclang.
-
returns
def initialize(translation_unit, &block)
Build callbacks that snapshot libclang indexing payloads.
Signature
-
parameter
translation_unitTranslationUnit | Nil The translation unit for derived wrappers.
-
yields
{|event, payload| ...} The user callback.
Implementation
def initialize(translation_unit, &block)
@translation_unit = translation_unit
@block = block
@aborted = false
@callbacks = Lib::IndexerCallbacks.new
initialize_callbacks
end
def aborted?
Check if indexing was aborted by the user callback.
Signature
-
returns
Boolean True if indexing was aborted.
Implementation
def aborted?
@aborted
end
def initialize_callbacks
Install no-op libclang callbacks that forward snapshots to the Ruby block.
Implementation
def initialize_callbacks
@abort_query = proc do |_client_data, _reserved|
@aborted ? 1 : 0
end
@diagnostic = proc do |_client_data, diagnostic_set, _reserved|
diagnostics = diagnostics_from_set(diagnostic_set)
dispatch(:diagnostic, diagnostics)
end
@entered_main_file = proc do |_client_data, file, _reserved|
dispatch(:entered_main_file, FFI::Clang::File.new(file, @translation_unit))
nil
end
@pp_included_file = proc do |_client_data, info_pointer|
info = Lib::CXIdxIncludedFileInfo.new(info_pointer)
dispatch(:included_file, IncludedFile.new(info, @translation_unit))
nil
end
@imported_ast_file = proc do |_client_data, info_pointer|
info = Lib::CXIdxImportedASTFileInfo.new(info_pointer)
dispatch(:imported_ast_file, ImportedASTFile.new(info, @translation_unit))
nil
end
@started_translation_unit = proc do |_client_data, _reserved|
dispatch(:started_translation_unit, nil)
nil
end
@index_declaration = proc do |_client_data, info_pointer|
info = Lib::CXIdxDeclInfo.new(info_pointer)
dispatch(:declaration, Declaration.new(info, @translation_unit))
end
@index_entity_reference = proc do |_client_data, info_pointer|
info = Lib::CXIdxEntityRefInfo.new(info_pointer)
dispatch(:reference, EntityReference.new(info, @translation_unit))
end
@callbacks[:abort_query] = @abort_query
@callbacks[:diagnostic] = @diagnostic
@callbacks[:entered_main_file] = @entered_main_file
@callbacks[:pp_included_file] = @pp_included_file
@callbacks[:imported_ast_file] = @imported_ast_file
@callbacks[:started_translation_unit] = @started_translation_unit
@callbacks[:index_declaration] = @index_declaration
@callbacks[:index_entity_reference] = @index_entity_reference
end
def diagnostics_from_set(diagnostic_set)
Snapshot diagnostics from a diagnostic set.
Signature
-
parameter
diagnostic_setFFI::Pointer The diagnostic set pointer.
-
returns
Array(Diagnostic) Diagnostic snapshots.
Implementation
def diagnostics_from_set(diagnostic_set)
Lib.get_num_diagnostics_in_set(diagnostic_set).times.map do |i|
Diagnostic.new(Lib.get_diagnostic_in_set(diagnostic_set, i))
end
end
def dispatch(event, payload)
Dispatch an event to the Ruby block.
Signature
-
parameter
eventSymbol The event type.
-
parameter
payloadObject The event payload.
Implementation
def dispatch(event, payload)
result = @block.call(event, payload)
@aborted = true if result == :abort
end