-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to include line number for vim formatter #65
Conversation
0f173c2
to
ddf6af0
Compare
Actually, other formatters' output all have line number information, so I think we might consider just add this as default. |
This tool tries to stay as compatible as possible with So, let's not change the default in any way, but I would welcome a PR that implements the |
ddf6af0
to
935a5d0
Compare
@mislav Thank you for your review. |
lib/ripper-tags.rb
Outdated
@@ -53,6 +54,12 @@ def self.option_parser(options) | |||
options.exclude << pattern | |||
end | |||
end | |||
opts.on("--excmd (number)", "Include line number information in the tag") do |pattern| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this say --excmd=number
to match the syntax that a user would actually pass?
lib/ripper-tags/vim_formatter.rb
Outdated
@@ -59,6 +59,14 @@ def display_inheritance(tag) | |||
end | |||
end | |||
|
|||
def display_line_number(tag) | |||
if options.line_number && tag[:line] | |||
"\tline:%s" % tag[:line] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is the line:%d
syntax defined? Which types of tags processors support this field? I don't see this documented as part of "standard" tags file format: http://docs.ctags.io/en/latest/format.html
What --excmd=number
does is that, instead of generating regex patterns in display_pattern
, it outputs the line number in its place. If a function foo
was defined on line 18, the resulting tags file would be:
foo{TAB}myfile.c{TAB}18;"{OTHER FIELDS}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might go to the wrong direction. Actually, what makes ctags
output the line number is --fields
option. http://ctags.sourceforge.net/ctags.html
−−fields=[+|−]flags
| n | | Line number of tag definition
So I'm thinking if we could add the option as --fields=+n
for ripper-tags
, it would be compatible with ctags, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mind adding support for both options, since you're already halfway done? Let's support --excmd=number
as well as --fields=+n
. Please process the characters within --fields
with the same mechanism as --extra
.
test/test_formatters.rb
Outdated
@@ -63,6 +63,30 @@ def test_vim | |||
)) | |||
end | |||
|
|||
def test_vim_with_line_number | |||
vim = formatter_for(:format => 'vim', :line_number => true) | |||
assert_equal %{C\t./script.rb\t/^class C < D$/;"\tc\tline:1\tclass:A.B\tinherits:D}, vim.format(build_tag( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need this many tests with variations of different tag types like class vs. module vs. method, etc. These variations are all tested in other tests. Here, we want to focus on line numbers. So instead, lets test the output for a tag that has :line => 1
, then another line number :line => 18
, for example, then a large line number such as :line => 123456
. Nothing else other than line number needs to change in these tag definitions, to keep the tests as focused as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack
lib/ripper-tags.rb
Outdated
@@ -26,7 +26,8 @@ def self.default_options | |||
:files => %w[.], | |||
:recursive => false, | |||
:exclude => %w[.git], | |||
:all_files => false | |||
:all_files => false, | |||
:line_number => false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call this option plural: line_numbers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack
935a5d0
to
2547b7b
Compare
Here is the diff with
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you're at this, could you also add support for -n/--excmd=number
? It shouldn't be hard.
lib/ripper-tags.rb
Outdated
@@ -53,6 +54,9 @@ def self.option_parser(options) | |||
options.exclude << pattern | |||
end | |||
end | |||
opts.on("--fields=+n", "Include line number information in the tag") do |value| | |||
options.line_numbers = value == "+n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not going to work if someone passed --fields=n
or --fields=abcn
(where "abc" are flags that we currently don't support). Please copy or extract the existing --extra
mechanism for processing these flags and later just check if n
was among them.
test/test_formatters.rb
Outdated
assert_equal %{C\t./script.rb\t/^class C < D$/;"\tc\tline:1\tclass:A.B\tinherits:D}, vim.format(build_tag( | ||
:kind => 'class', :name => 'C', | ||
:pattern => "class C < D", | ||
:class => 'A::B', :inherits => 'D' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None if these tests needs either :class
nor :inherits
since those are not the features that we're interested in here. You can take those parameters out.
2547b7b
to
6aebc11
Compare
@mislav Thank you for the patient review, please take another look. |
6aebc11
to
0bf172a
Compare
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your prompt updates so far!
lib/ripper-tags.rb
Outdated
if excmd == "number" | ||
options.excmd = excmd | ||
else | ||
$stderr.puts "Error: excmd %p is not supported" % excmd |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just save any value passed to options.excmd
and not raise an error on any unsupported value.
lib/ripper-tags/default_formatter.rb
Outdated
options.fields && options.fields.include?(field) | ||
end | ||
|
||
def excmd?(excmd) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This helper is not really necessary. The caller can simply do options.excmd == 'number'
test/test_formatters.rb
Outdated
|
||
def test_vim_with_excmd_number | ||
vim = formatter_for(:format => 'vim', :excmd => "number") | ||
assert_equal %{C\t./script.rb\t/^class C < D$/;"\t1;"\tc}, vim.format(build_tag( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The --excmd=number
option enables a mode where pattern is no longer output (e.g. there is no /^class C < D$/
). In its place, the line number is output.
0bf172a
to
f312bc2
Compare
ptal @mislav |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! One tiny nitpick.
lib/ripper-tags/vim_formatter.rb
Outdated
tag.fetch(:pattern).to_s.gsub('\\','\\\\\\\\').gsub('/','\\/') | ||
def display_excmd_info(tag) | ||
if options.excmd == "number" | ||
"%s;\"" % tag.fetch(:line).to_s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can avoid to_s
with
"%d;\"" % tag.fetch(:line)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed, thanks!
👍 |
Thank you for your hard work! |
I was using tagbar with ripper-tags, but I couldn't jump to the tag definition because ripper-tags output doesn't have the line number information.
Not sure if this is just my need,
ripper-tags
might consider adding an option to support line number information. If you think this is a good feature, I could open another PR to add the-l
option.Thanks.