Skip to content
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 support for inline_mode #45

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ certain directories or files such as `Pods`, you can use the `exclude` parameter
swiftformat.exclude = %w(Pods/** Carthage/** Sources/Nope.swift **/*_autogenerated.swift)
```

If you want the format results shows in the diff instead of a comment, you can use inline_mode option. Violations that are out of the diff, will be shown in danger's fail or warn section.
```ruby
swiftformat.inline_mode = true
```

The `exclude` option takes an array of glob patterns; you can find additional documentation on the patterns
[here](https://ruby-doc.org/core-2.6.3/File.html#method-c-fnmatch).

Expand Down
48 changes: 35 additions & 13 deletions lib/swiftformat/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DangerSwiftformat < Plugin
#
# @return [void]
#
def check_format(fail_on_error: false)
def check_format(fail_on_error: false, inline_mode: false)
# Check if SwiftFormat is installed
raise "Could not find SwiftFormat executable" unless swiftformat.installed?

Expand All @@ -56,20 +56,24 @@ def check_format(fail_on_error: false)
# Stop processing if the errors array is empty
return if results[:errors].empty?

# Process the errors
message = "### SwiftFormat found issues:\n\n"
message << "| File | Rules |\n"
message << "| ---- | ----- |\n"
results[:errors].each do |error|
message << "| #{error[:file].gsub("#{Dir.pwd}/", '')} | #{error[:rules].join(', ')} |\n"
end

unless additional_message.nil?
message << "\n" << additional_message
if inline_mode
send_inline_comment(results, fail_on_error ? :fail : :warn)
else
# Process the errors
message = "### SwiftFormat found issues:\n\n"
message << "| File | Rules |\n"
message << "| ---- | ----- |\n"
results[:errors].each do |error|
message << "| #{error[:file].gsub("#{Dir.pwd}/", '')} | #{error[:rules].join(', ')} |\n"
end

unless additional_message.nil?
message << "\n" << additional_message
end

markdown message
end

markdown message

if fail_on_error
fail "SwiftFormat found issues"
end
Expand All @@ -95,6 +99,24 @@ def find_swift_files
.sort
end

# Send inline comment with danger's warn or fail method
#
# @return [void]
def send_inline_comment(results, method)
results[:errors].each do |error|
file = error[:file]
file_components = file.split(':')
line = file_components[1]
filename = file_components.first.split('/').last
file_path = file_components.first

message = "#{error[:rules].join(', ')}".dup
message << " `#{filename}:#{line}`" # file:line for pasting into Xcode Quick Open

send(method, message, file: file_path, line: line)
end
end

# Constructs the SwiftFormat class
#
# @return [SwiftFormat]
Expand Down