Skip to content

Commit

Permalink
Merge pull request #9649 from osyo-manga/fix_when_highlights_contain_…
Browse files Browse the repository at this point in the history
…multibyte_characters

Fix when highlights contain multibyte characters
  • Loading branch information
koic committed Mar 31, 2021
2 parents 9758a56 + ed98f9d commit 4711f1c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
@@ -0,0 +1 @@
* [#9649](https://github.com/rubocop/rubocop/pull/9649): Fix when highlights contain multibyte characters. ([@osyo-manga][])
6 changes: 4 additions & 2 deletions lib/rubocop/formatter/clang_style_formatter.rb
Expand Up @@ -49,8 +49,10 @@ def report_line(location)
end

def report_highlighted_area(highlighted_area)
output.puts("#{' ' * highlighted_area.begin_pos}" \
"#{'^' * highlighted_area.size}")
space_area = highlighted_area.source_buffer.slice(0...highlighted_area.begin_pos)
source_area = highlighted_area.source
output.puts("#{' ' * Unicode::DisplayWidth.of(space_area)}" \
"#{'^' * Unicode::DisplayWidth.of(source_area)}")
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/rubocop/formatter/tap_formatter.rb
Expand Up @@ -37,8 +37,10 @@ def report_line(location)
end

def report_highlighted_area(highlighted_area)
output.puts("# #{' ' * highlighted_area.begin_pos}" \
"#{'^' * highlighted_area.size}")
space_area = highlighted_area.source_buffer.slice(0...highlighted_area.begin_pos)
source_area = highlighted_area.source
output.puts("# #{' ' * Unicode::DisplayWidth.of(space_area)}" \
"#{'^' * Unicode::DisplayWidth.of(source_area)}")
end

def report_offense(file, offense)
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/formatter/clang_style_formatter_spec.rb
Expand Up @@ -123,5 +123,27 @@
.to include(': [Corrected] This is a message.')
end
end

context 'when the source contains multibyte characters' do
let(:source) do
<<~RUBY
do_something("あああ", ["いいい"])
RUBY
end

it 'displays text containing the offending source line' do
location = source_range(source.index('[')..source.index(']'))

cop.add_offense(nil, location: location, message: 'message 1')
formatter.report_file('test', cop.offenses)

expect(output.string)
.to eq <<~OUTPUT
test:1:21: C: message 1
do_something("あああ", ["いいい"])
^^^^^^^^^^
OUTPUT
end
end
end
end
29 changes: 29 additions & 0 deletions spec/rubocop/formatter/tap_formatter_spec.rb
Expand Up @@ -143,4 +143,33 @@
end
end
end

describe '#report_file', :config do
let(:cop_class) { RuboCop::Cop::Cop }
let(:output) { StringIO.new }

before { cop.send(:begin_investigation, processed_source) }

context 'when the source contains multibyte characters' do
let(:source) do
<<~RUBY
do_something("あああ", ["いいい"])
RUBY
end

it 'displays text containing the offending source line' do
location = source_range(source.index('[')..source.index(']'))

cop.add_offense(nil, location: location, message: 'message 1')
formatter.report_file('test', cop.offenses)

expect(output.string)
.to eq <<~OUTPUT
# test:1:21: C: message 1
# do_something("あああ", ["いいい"])
# ^^^^^^^^^^
OUTPUT
end
end
end
end

0 comments on commit 4711f1c

Please sign in to comment.