Syntax-aware grep for Ruby code
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is 54 commits ahead of uu59:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
bin
lib
test
.gitignore
.ruby-version
.travis.yml
Gemfile
README
Rakefile
grep-ruby-token.gemspec
token-ruby.png

README

This is syntax-aware grep for Ruby code.

Sample output:

```console
$ grep-ruby-token extract lib/grep_ruby_token.rb
lib/grep_ruby_token.rb:21
    21    def self.extract(ast, word)
    22      search(ast){|node|
    23        (node.type == :block && node.children[0].type == :send && node.children[0].children[1] == word) || # for passing a literal block
    24          (node.children.any?{|c| c == word })
    25      }.map{|node|
    26        last_heredoc_pos = search(node, return_when_finding: false){|c| (c.type == :dstr || c.type == :str)}
    27        .map{|node| node.loc.expression}.max_by{|i| i.end_pos}
    28  
    29        start = node.loc.expression
    30        finish = [last_heredoc_pos, node.loc.expression].max_by{|i|
    31          if i.nil?
    32            -1
    33          else
    34            i.end_pos
    35          end
    36        }
    37        [start, finish, node]
    38      }
    39    end

lib/grep_ruby_token.rb:44
    44      extract(ast, word).map{|(expression_start, expression_finish, node)|
```

```console
$ cd ~/src/jekyll
$ git grep-token-ruby deep_merge
lib/jekyll.rb:80
    80      config = config.deep_merge(override).stringify_keys
lib/jekyll/configuration.rb:149
   149            configuration = configuration.deep_merge(new_config)
lib/jekyll/convertible.rb:104
   104        data.deep_merge(further_data)

lib/jekyll/convertible.rb:120
   120          payload = payload.deep_merge({"content" => self.output, "page" => layout.data})
lib/jekyll/core_ext.rb:8
     8    def deep_merge(hash)
     9      target = dup
    10  
    11      hash.keys.each do |key|
    12        if hash[key].is_a? Hash and self[key].is_a? Hash
    13          target[key] = target[key].deep_merge(hash[key])
    14          next
    15        end
    16  
    17        target[key] = hash[key]
    18      end
    19  
    20      target
    21    end
lib/jekyll/page.rb:110
   110        payload = {
   111          "page" => self.to_liquid,
   112          'paginator' => pager.to_liquid
   113        }.deep_merge(site_payload)
lib/jekyll/post.rb:250
   250        payload = {
   251          "site" => { "related_posts" => related_posts(site_payload["site"]["posts"]) },
   252          "page" => self.to_liquid(EXCERPT_ATTRIBUTES_FOR_LIQUID)
   253        }.deep_merge(site_payload)
test/test_configuration.rb:147
   147        assert_equal Jekyll::Configuration::DEFAULTS.deep_merge({ "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => @paths[:other] })

test/test_configuration.rb:169
   169        assert_equal Jekyll::Configuration::DEFAULTS.deep_merge({ "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => [@paths[:default], @paths[:other]] })
test/test_kramdown.rb:31
    31        markdown = Converters::Markdown.new(@config.deep_merge(override))
test/test_pager.rb:6
     6      base = Jekyll::Configuration::DEFAULTS.deep_merge({
     7        'source'      => source_dir,
     8        'destination' => dest_dir,
     9        'paginate'    => 1
    10      })

test/test_pager.rb:11
    11      site = Jekyll::Site.new(base.deep_merge(config))
test/test_tags.rb:9
     9        Jekyll::Configuration::DEFAULTS.deep_merge({'pygments' => true}).deep_merge(override)
```

Install:

  $ bundle install
  $ bundle exec rake install

Run test:

  $ bundle exec rake test

Current restrictions:

* Keywords (e.g. def, end) can't be searched.
* Some tokens won't be highlighted.
  Currently, only tokens represented as `name` and `selector`
  in Parser::Source::Map (ParserAST::Node#loc) are supported.
  If it comes across unsupported representations,
  it warns "Can't determine which property locates the token: ... "

Credit:
This is originally written by uu59, inspired by http://hitode909.hatenablog.com/entry/2013/09/13/141403.

Similar gems:
  * ruby_scope
    * https://endofline.wordpress.com/2010/04/18/ruby_scope/
    * It has a lot of useful features, but won't work in my box (version 0.1.1).