Rubocop line length: How to ignore lines with comments? - ruby

I would like Rubocop to ignore lines with comments (just a comment or some code with an end of line comment) when checking if a line is too long. Is there a way to do this?

There is a way to ignore cops on a per line basis.
There is also a way to do it via configuration file.
Run rubocop --auto-gen-config and it will generate a file that you can use to disable the offenses.
The command also gives a hint on what to do to load those options.
On a line per line basis, you can enable and disable the cops as well.
# rubocop:disable RuleByName
This is a long line
# rubocop:enable RuleByName
You can also do more than one rule at a time in your code.
# rubocop:disable BlockComments, AsciiComments
By using an inline directive, the directive becomes valid only for that
line, and it would look like this:
# Thanks to #jnt30 for the comment!
method(argument) # rubocop:disable SomeRule, SomeOtherRule
You can read a ton more about RuboCop in its official manual.
To find all the rule names its worth looking in the rubocop config files
cyberwiz says - "run rubocop -D when I need the rule names rather than looking in the documentation." Update: This is now the default behavior without the flag.
The -D is now default, so we would get that for "free" now.

It's possible to define regex patterns to automatically ignore certain lines in rubocop.yml, so you could choose to ignore all lines starting with a # character:
Layout/LineLength:
Max: 80
AllowedPatterns: ['\A#']
This could be improved so that "indented" comment lines (i.e. whitespace followed by a # character) are also ignored, if that's what you want.
Note that this doesn't account for lines of code that end with a comment, though:
some_code(that_does_something) # This line would NOT be ignored by Rubocop.

You can use the following comment with rubocop to ignore a specific rule:
# rubocop:disable Layout/LineLength
def this_could_be_a_very_long_line_that_extends_forever_into_infinity
end
# rubocop:enable Layout/LineLength
You can also ignore whole files by adding them to .rubocop.yml:
AllCops:
Exclude:
- path/to/file.rb

i think the basic idea here is that you want to enforce line length, no matter what is after n characters. the default to 80 characters is some cargo cult for old terminal windows that could only hold that number of chars. the only option that i saw in the code is an option to allow urls that might exceed the character limit.
you can ignore whole files, i guess that's not what you are looking for.

The following configuration worked for me:
Layout/LineLength:
AllowedPatterns: ['^(\s*#)']
This regex only works when the entire line is commented out. Code followed by a long comment on the same line will still trigger a Rubocop lint error, which is by design.

Related

Why is rubocop asking me to put // around regex when i'm using %r already?

I have the following regex
regexp = %r{
((returned|undelivered)\smail|mail\sdelivery(\sfailed)?)
}x
But when I run rubocop on it, it complains that I need to "Use // around regular expression."
How can I get around it?
You can disable (and enable) any rubocop cop by adding a .rubocop.yml file to the root of your project folder and setting up the appropriate configurations. To see what you can do, check out the global default.yml in your rubocop package. It's fully commented.
For this particular problem, create a .rubocop.yml and...
To disable the cop completely:
Style/RegexpLiteral:
Enabled: false
To always use %r:
Style/RegexpLiteral:
EnforcedStyle: percent_r
I don't run rubocop so not sure this will solve your problem. You can use // instead of {} to surround the regex when using %r:
regexp = %r/((returned|undelivered)\smail|mail\sdelivery(\sfailed)?)/x
You can use multiline regexp with /.../x either:
regexp = /
((returned|undelivered)
\s
mail|mail
\s
delivery
(\sfailed)?)
/x
See more in Rubocop gem doc
when I run rubocop on it, it complains that I need to "Use // around regular expression."
How can I get around it?
I think the message is pretty clear: to get around it, you can use // around the regular expression:
regexp = /((returned|undelivered)\smail|mail\sdelivery(\sfailed)?)/x

.. literalinclude:: from markers

I am using python-sphinx for documenting a C code. I am not interested in features offered by doxygen, however I sometime would like to include some snippets from the code-base.
The .. literalinclude:: looks great, but the :lines: option is a bit weak especially if the code is edited afterwards.
I am thinking about a new option such as :marker-start: <<<HERE and :marker-end: <<<END.
What would be the easiest way to achieve this behavior?
You can use the start-after and end-before options. Below is an example.
example.txt:
# START
first line
second line
# END
third line
Markup:
.. literalinclude:: example.txt
:start-after: # START
:end-before: # END
This will appear in the output:
first line
second line
See https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-literalinclude.

Add end of line comment to vim syntax

Not every command in Vim allows you to add end-of-line comments. Sometimes the " is valid as an argument, so it would be ambiguous. However, if you insert a pipe, the command is ended and you can insert a comment. So you can actually achieve reliable end of line comments in vim thusly:
noremap ' ` |" Use single quote as alternate range key
Neat, right? But the syntax/vim.vim file doesn't recognize this as an end of line comment. How do I tell Vim to recognize this syntax?
I found this in syntax/vim.vim:
syn match vimLineComment +^[ \t:]*".*$+ contains=#vimCommentGroup,vimCommentString,vimCommentTitle
I tried adding something like this to my ~/.vimrc, but there is no effect. VimScript is hard. :/
syntax match vimLineComment '|".*$+'
Any ideas?
you cannot use in-line comments for maps
:h map-comments
you will see:
*map-comments*
It is not possible to put a comment after these commands, because the '"'
character is considered to be part of the {lhs} or {rhs}.
I hope this answers your question.
hack
Okay, you may have good reason to do that.
Only define syn match vimLineComment is not enough, you have to overwrite the vimMapRhs syntax. so these two lines will make |"foo bar highlighted as comment:
syn match vimMapRhs '.*\ze|\s*".*'
syn match vimLineComment '|\s*".*$'
this may change the "comment" highlight, but I don't recommend to do it.
Background
vim 7.3 all platforms
vimscript language (used in vim files)
Problem
The vimscript language supports comments, but end of line comments do not always work predictably, because end of line comments can be mistakenly interpreted by vim as part of the command.
Adding end of line comments is problemmatic in vimscript, because it does not work with all commands.
Solutions
1) use the pipe character :help :bar to create a separate Ex command
this is the solution enumerated by #sidewaysmilk
2) simply add the comment below the relevant vimscript command on the next line
3) use the execute command (see :help :execute )
Pitfalls
Solution 1) is a somewhat unconventional use of the pipe (aka :bar)
Not all commands support the pipe character (see e.g. :help :execute)
Solution 2) may not be desirable for the readability of the vimscript, and it does not directly solve the issue in the OP
searching for this feature on the internet is tricky because it turns up links related to comments in general-purpose programming contexts, unrelated to vimscript
See also
Vim help links (enter these directly into vim Cmdline-mode):
:help vim-script-intro | /comments for some commands
:help :bar
:help Command-line-mode
Web links:
https://en.wikipedia.org/wiki/Vimscript
https://duckduckgo.com/?q=vim+vim-script-intro+comments
+1 fot "This is not "neat" at all":
noremap ' ` |" Use single quote as alternate range key
My preference is to use end of line comment (without adding |) where vimL allows.
The pain point is:
It's hard to remember when vimL allows that. (so some people never uses end of line comment in vimL, which may narrow his choice of formating)
Inspired by the OP, we can utilize the syntax highlight. (But I don't know how to implement yet)
Below is some information which seems to be needed:
Relevant lines in the syntax.vim:
/home/linuxbrew/.linuxbrew/Cellar/neovim/0.6.1/share/nvim/runtime/syntax/syntax.vim
syn region vimString start="^\s*\\\z(['"]\)" skip='\\\\\|\\\z1' end="\z1" oneline keepend contains=#vimStringGroup,vimContinue
syn match vimComment excludenl +\s"[^\-:.%#=*].*$+lc=1 contains=#vimCommentGroup,vimCommentString
syn match vimComment +\<endif\s\+".*$+lc=5 contains=#vimCommentGroup,vimCommentString
syn match vimComment +\<else\s\+".*$+lc=4 contains=#vimCommentGroup,vimCommentString
syn region vimCommentString contained oneline start='\S\s\+"'ms=e end='"'
hi def link vimCommentString vimString
" end of line comment
syn match vimLineComment +^[ \t:]*".*$+ contains=#vimCommentGroup,vimCommentString,vimCommentTitle
hi def link vimLineComment vimComment
syn match vim9LineComment +^[ \t:]\+#.*$+ contains=#vimCommentGroup,vimCommentString,vimCommentTitle
hi def link vim9Comment Comment
syn match vimCommentTitle '"\s*\%([sS]:\|\h\w*#\)\=\u\w*\(\s\+\u\w*\)*:'hs=s+1 contained contains=vimCommentTitleLeader,vimTodo,#vimCommentGroup
hi def link vimCommentTitle PreProc
syn match vimCommentTitleLeader '"\s\+'ms=s+1 contained
syn match vimContinue "^\s*\\"

How to prevent rubocop to add a space after hash

Rubocop changes #!/usr/bin/ruby to
# !/usr/bin/ruby
adds a space after the hash when I use rubocop -a, how can I avoid this
You'll have to disable the LeadingCommentSpace cop. It ensures that there is a space between # and the text that follows it. Puts something like this in your .rubocop.yml:
LeadingCommentSpace:
enabled: false
What you've stumbled upon is a bug, that I'll fix in the next RuboCop release (I'm its author). Obviously #! should be treated specially.

How to add comments to an Exuberant Ctags config file?

What character can I use to put comments in an Exuberant Ctags .ctags file?
I would like to add comments with explanations, and perhaps to disable some regexps.
But I can't find any comment character which ctags-exuberant accepts!
I keep getting the warning:
ctags: Warning: Ignoring non-option in /home/joey/.ctags
which is better than an error, but still a little annoying.
I have tried # // /* ... */ and ; as comments, but ctags tries to parse them all!
Here is an example file with some comments which ctags will complain about:
# Add some more rules for Javascript
--langmap=javascript:+.jpp
--regex-javascript=/^[ \t]*var ([a-zA-Z_$][0-9a-zA-Z_$]*).*$/\1/v,variable/
--regex-javascript=/^[ \t]*this\.([a-zA-Z_$][0-9a-zA-Z_$]*)[ \t]*=.*$/\1/e,export/
--regex-javascript=/^[ \t]*([a-zA-Z_$][0-9a-zA-Z_$]*):.*$/\1/p,property/
--regex-javascript=/^\<function\>[ \t]*([a-zA-Z_$][0-9a-zA-Z_$]*)/\1/f,function/
# Define tags for the Coffeescript language
--langdef=coffee
--langmap=coffee:.coffee
--regex-coffee=/^class #?([a-zA-Z_$][0-9a-zA-Z_$]*)( extends [a-zA-Z_$][0-9a-zA-Z_$]*)?$/\1/c,class/
--regex-coffee=/^[ \t]*(#|this\.)([a-zA-Z_$][0-9a-zA-Z_$]*).*$/\2/e,export/
--regex-coffee=/^[ \t]*#?([a-zA-Z_$][0-9a-zA-Z_$]*):.*[-=]>.*$/\1/f,function/
--regex-coffee=/^[ \t]*([a-zA-Z_$][0-9a-zA-Z_$]*)[ \t]+=.*[-=]>.*$/\1/f,function/
--regex-coffee=/^[ \t]*([a-zA-Z_$][0-9a-zA-Z_$]*)[ \t]+=[^->\n]*$/\1/v,variable/
--regex-coffee=/^[ \t]*#?([a-zA-Z_$][0-9a-zA-Z_$]*):.*$/\1/p,property/
You can't! I looked through the source code (thanks to apt-get source). There are no checks for lines to ignore. The relevant code is in parseFileOptions() in options.c
But sometimes comments are a neccessity, so as a workaround I put a comment in as a regexp, in such as way that it is unlikely to ever match anything.
--regex-coffee=/^(COMMENT: Disable next line when using prop tag)/\1/X,XXX/
The ^ helps the match to fail quickly, whilst the ( ) wrapper is purely for visual effect.
Your comment should be a valid regexp, to avoid warnings on stderr. (That means unescaped /s must be avoided, and if you use any [ ] ( or )s they should be paired up.) See Tom's solution to avoid these restrictions.
As #joeytwiddle points out, comments are not supported by the parser, but there is a work-around.
Example .ctags file:
--regex-C=/$x/x/x/e/ The ctags parser currently doesn't support comments
--regex-C=/$x/x/x/e/ This is a work-around which works with '/' characters
--regex-C=/$x/x/x/e/ http://stackoverflow.com/questions/10973224/how-to-add-comments-to-an-exuberant-ctags-config-file
--regex-C=/$x/x/x/e/
--regex-C=/$x/x/x/e/ You can add whatever comment text you want here.
You can use '#' as the start of comment if you are using Universal-ctag(https://ctags.io).
Given that comments don't work, what about a .ctags.readme file...
For most things you don't actually need a comment, e.g. you don't really need the comment below.
# Define tags for the Coffeescript language
--langdef=coffee
--langmap=coffee:.coffee
I can see however that you might want to add comments explaining some mind bending regex, so for each line that absolutely needs it you can copy paste it into the .ctags.readme file as a markdown file:
Forgive me father for I have regexed
It was purely because I wanted some lovely coffee properties
```
--regex-coffee=/^[ \t]*#?([a-zA-Z_$][0-9a-zA-Z_$]*):.*$/\1/p,property/
```
Keeping .ctags.readme and .ctags in sync
You could have a block at the bottom of the ctags file separated with a line break, then delete this final block.
If you only have the one line break in your .ctags file this sed will delete all the lines after the line break.
Then do some grepping for the --regex lines to append the lines from .ctags.readme into .ctags.
sed -i '/^\s*$/,$d' .ctags
grep "^--regex" .ctags.readme >> .ctags

Resources