Class: SyntaxTree::CLI::Search
Overview
An action of the CLI that searches for the given pattern matching pattern in the given files.
Instance Attribute Summary collapse
-
#search ⇒ Object
readonly
Returns the value of attribute search.
Attributes inherited from Action
Instance Method Summary collapse
-
#initialize(query) ⇒ Search
constructor
A new instance of Search.
- #run(item) ⇒ Object
Methods inherited from Action
Constructor Details
#initialize(query) ⇒ Search
Returns a new instance of Search.
235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/syntax_tree/cli.rb', line 235 def initialize(query) query = File.read(query) if File.readable?(query) pattern = begin Pattern.new(query).compile rescue Pattern::CompilationError => error warn(error.) exit(1) end @search = SyntaxTree::Search.new(pattern) end |
Instance Attribute Details
#search ⇒ Object (readonly)
Returns the value of attribute search.
233 234 235 |
# File 'lib/syntax_tree/cli.rb', line 233 def search @search end |
Instance Method Details
#run(item) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/syntax_tree/cli.rb', line 248 def run(item) search.scan(item.handler.parse(item.source)) do |node| location = node.location line = location.start_line bold_range = if line == location.end_line location.start_column...location.end_column else location.start_column.. end source = item.source.lines[line - 1].chomp source[bold_range] = Color.bold(source[bold_range]).to_s puts("#{item.filepath}:#{line}:#{location.start_column}: #{source}") end end |