comment_scanner 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +64 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/comment_scanner.gemspec +24 -0
- data/lib/comment_scanner.rb +64 -0
- data/lib/comment_scanner/version.rb +3 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6acae07fa33acf2ff66a424478b786ab17164e71
|
4
|
+
data.tar.gz: 15f63073ce565a3e9a983e9fde7625eb4839ef3c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: de3063156882e26fa4b67eb9f5a681b951bdee43243b9b396318a904e0aaf4ef2418cbc7a0d76692238274748bd6928e75c84a3cf288e6c4c90ba4e6505f73ed
|
7
|
+
data.tar.gz: 4c8826f7c65398d5e68a53d4b39013e4c2481687fb7ed5c9d4274b9d88dd427bc7eee4044f749a31a363275f31d349b6ebe1f22763c930f259620b9f43cfa4b5
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# CommentScanner
|
2
|
+
|
3
|
+
Detect comment blocks in Ruby source.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'comment_scanner'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install comment_scanner
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Given a source file that looks like this:
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
class Sample
|
27
|
+
def test
|
28
|
+
# Alpha
|
29
|
+
# Beta
|
30
|
+
assert true
|
31
|
+
assert false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
Read the file in: `src = IO.read("sample.rb")`, and then use `CommentScanner` to find comment blocks.
|
37
|
+
|
38
|
+
``` ruby
|
39
|
+
scanner = CommentScanner.new(src)
|
40
|
+
scanner.before(4) #=> "# Alpha\n# Beta"
|
41
|
+
scanner.before(1) #=> nil
|
42
|
+
scanner.after(1) #=> "# Alpha\n# Beta"
|
43
|
+
```
|
44
|
+
|
45
|
+
Instantiate CommentScanner with a `:skip` parameter to ignore blocks of code before it starts matching.
|
46
|
+
For instance, ignore leading assertions in the example:
|
47
|
+
|
48
|
+
``` ruby
|
49
|
+
scanner = CommentScanner.new(src, skip: /^\s+assert/)
|
50
|
+
scanner.before(4) #=> "# Alpha\n# Beta"
|
51
|
+
scanner.before(5) #=> "# Alpha\n# Beta"
|
52
|
+
scanner.before(6) #=> nil
|
53
|
+
```
|
54
|
+
|
55
|
+
## Development
|
56
|
+
|
57
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
58
|
+
|
59
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/adamsanderson/comment_scanner.
|
64
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "comment_scanner"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'comment_scanner/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "comment_scanner"
|
8
|
+
spec.version = CommentScanner::VERSION
|
9
|
+
spec.authors = ["Adam Sanderson"]
|
10
|
+
spec.email = ["netghost@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Detect comment blocks in Ruby source."
|
13
|
+
spec.description = "Detects leading and trailing comment blocks in Ruby source. This may be useful for annotating exception and profiling traces."
|
14
|
+
spec.homepage = "https://github.com/adamsanderson/comment_scanner"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "minitest"
|
24
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require "comment_scanner/version"
|
2
|
+
|
3
|
+
# CommentScanner will scan for comment blocks in Ruby source.
|
4
|
+
#
|
5
|
+
class CommentScanner
|
6
|
+
attr_reader :source
|
7
|
+
attr_reader :skip_pattern
|
8
|
+
|
9
|
+
COMMENT_REGEXP = /^\s*#/
|
10
|
+
|
11
|
+
# Creates a new CommentScanner for +source+.
|
12
|
+
#
|
13
|
+
# Options:
|
14
|
+
# [:skip] Skip lines matching this pattern before searching for comments.
|
15
|
+
#
|
16
|
+
def initialize(source, options={})
|
17
|
+
@source = source.is_a?(Array) ? source : source.each_line.to_a
|
18
|
+
@skip_pattern = options[:skip]
|
19
|
+
end
|
20
|
+
|
21
|
+
# Detect comment blocks immediately before +index+.
|
22
|
+
#
|
23
|
+
# Note that +index+ is 0 indexed, where as an editor or stack trace is
|
24
|
+
# typically starts at 1.
|
25
|
+
#
|
26
|
+
# # Returns +nil+ if there is no match.
|
27
|
+
def before(index)
|
28
|
+
lines = source[0...index].reverse
|
29
|
+
matches = match_comments(lines)
|
30
|
+
|
31
|
+
strip_lines(matches).reverse.join.chomp unless matches.empty?
|
32
|
+
end
|
33
|
+
|
34
|
+
# Detect comment blocks immediately after +index+. This is generally
|
35
|
+
# less useful, you're probably looking for CommentScanner#before.
|
36
|
+
#
|
37
|
+
# Note that +index+ is 0 indexed, where as an editor or stack trace is
|
38
|
+
# typically starts at 1.
|
39
|
+
#
|
40
|
+
# Returns +nil+ if there is no match.
|
41
|
+
def after(index)
|
42
|
+
lines = source[index+1..-1]
|
43
|
+
matches = match_comments(lines)
|
44
|
+
|
45
|
+
strip_lines(matches).join.chomp unless matches.empty?
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
# First drops skipped lines, then detects a contiguous block of comments.
|
51
|
+
def match_comments(lines)
|
52
|
+
if skip_pattern
|
53
|
+
lines = lines.drop_while{|line| skip_pattern.match(line) }
|
54
|
+
end
|
55
|
+
|
56
|
+
lines.take_while{|line| COMMENT_REGEXP.match(line) }
|
57
|
+
end
|
58
|
+
|
59
|
+
# Strips leading whitespace from comments.
|
60
|
+
def strip_lines(lines)
|
61
|
+
lines.map(&:lstrip)
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: comment_scanner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Sanderson
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Detects leading and trailing comment blocks in Ruby source. This may
|
56
|
+
be useful for annotating exception and profiling traces.
|
57
|
+
email:
|
58
|
+
- netghost@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- comment_scanner.gemspec
|
71
|
+
- lib/comment_scanner.rb
|
72
|
+
- lib/comment_scanner/version.rb
|
73
|
+
homepage: https://github.com/adamsanderson/comment_scanner
|
74
|
+
licenses: []
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.4.5.1
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Detect comment blocks in Ruby source.
|
96
|
+
test_files: []
|