Skip to content

Commit

Permalink
feat(formatter): add "except" and "only" rubocop formatter options
Browse files Browse the repository at this point in the history
They take a list or comma separated string of cops, which will be passed
to rubocop as --except and/or --only.

The "except" option is useful to disable cops which can cause annoyance
when using solargraph's formatter as a before save hook in text
editors.

I'm less aware of what the "only" option might be useful for, but might
as well add it so both are covered.
  • Loading branch information
jimeh committed Jan 28, 2021
1 parent 10fd861 commit 7122c49
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/solargraph/language_server/message/text_document/formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,26 @@ def config_for(file_uri)
end

def cli_args file, config
[
args = [
config['cops'] == 'all' ? '--auto-correct-all' : '--auto-correct',
'--cache', 'false',
'--format', 'Solargraph::LanguageServer::Message::' \
'TextDocument::Formatting::BlankRubocopFormatter',
config['extra_args'],
file
].flatten
]

['except', 'only'].each do |arg|
cops = cop_list(config[arg])
args += ["--#{arg}", cops] if cops
end

args += config['extra_args'] if config['extra_args']
args + [file]
end

def cop_list(value)
value = value.join(',') if value.respond_to?(:join)
return nil if value == '' || !value.is_a?(String)
value
end

# @param original [String]
Expand Down
2 changes: 2 additions & 0 deletions lib/solargraph/workspace/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ def default_config
'formatter' => {
'rubocop' => {
'cops' => 'safe',
'except' => [],
'only' => [],
'extra_args' =>[]
}
},
Expand Down

0 comments on commit 7122c49

Please sign in to comment.