class SourceRange
Represents a source range in a file.
Definitions
def self.null_range
Get a null source range.
Signature
-
returns
SourceRange A null source range.
Implementation
def self.null_range
SourceRange.new Lib.get_null_range
end
def self.from_source_range_list(range_list)
Build Ruby source ranges from a libclang source range list. The list is always disposed before returning.
Signature
-
parameter
range_listLib::CXSourceRangeList | FFI::Pointer | Nil The libclang range list.
-
returns
Array(SourceRange) The extracted source ranges.
Implementation
def self.from_source_range_list(range_list)
return [] if range_list.nil?
range_list = Lib::CXSourceRangeList.new(range_list) if range_list.is_a?(FFI::Pointer)
return [] if range_list.pointer.null?
begin
range_pointer = range_list[:ranges]
range_list[:count].times.map do |i|
range = Lib::CXSourceRange.new(range_pointer + (i * Lib::CXSourceRange.size))
SourceRange.new(Lib.get_range(Lib.get_range_start(range), Lib.get_range_end(range)))
end
ensure
Lib.dispose_source_range_list(range_list)
end
end
def initialize(range_or_begin_location, end_location = nil)
Initialize a source range.
Signature
-
parameter
range_or_begin_locationLib::CXSourceRange | SourceLocation Either a range structure or the beginning location.
-
parameter
end_locationSourceLocation | Nil The end location, or
nilif first parameter is a range.
Implementation
def initialize(range_or_begin_location, end_location = nil)
if end_location.nil?
@range = range_or_begin_location
else
@range = Lib.get_range(range_or_begin_location.location, end_location.location)
end
end
def start
Get the start location of this range.
Signature
-
returns
ExpansionLocation The start location.
Implementation
def start
@start ||= ExpansionLocation.new(Lib.get_range_start @range)
end
def end
Get the end location of this range.
Signature
-
returns
ExpansionLocation The end location.
Implementation
def end
@end ||= ExpansionLocation.new(Lib.get_range_end @range)
end
def bytesize
Get the size in bytes of the source range.
Signature
-
returns
Integer The byte size.
Implementation
def bytesize
self.end.offset - self.start.offset
end
def text
Read the text from the source file for this range.
Signature
-
returns
String | Nil The source text, or nil if the range is invalid or has no file.
Implementation
def text
file_path = self.start.file
return nil if file_path.nil?
::File.open(file_path, "rb") do |file|
file.seek(self.start.offset)
return file.read(self.bytesize)
end
end
def null?
Check if this range is null.
Signature
-
returns
Boolean True if the range is null.
Implementation
def null?
Lib.range_is_null(@range) != 0
end
attr_reader :range
Signature
-
attribute
Lib::CXSourceRange The underlying range structure.
def ==(other)
Check if this range equals another range.
Signature
-
parameter
otherSourceRange The range to compare with.
-
returns
Boolean True if the ranges are equal.
Implementation
def ==(other)
Lib.equal_range(@range, other.range) != 0
end