Skip to content

Commit

Permalink
Merge pull request #500 from crystal-ameba/issue-496
Browse files Browse the repository at this point in the history
Adjust cursor position by the indent size only if valid
  • Loading branch information
Sija authored Nov 18, 2024
2 parents 2066b02 + 6b97036 commit a5c99e2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 0 additions & 1 deletion spec/ameba/config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module Ameba
it "loads default globs when config has no value" do
yml = YAML.parse <<-CONFIG
# Empty config with comment
CONFIG
config = Config.new(yml)
config.globs.should eq Config::DEFAULT_GLOBS
Expand Down
9 changes: 9 additions & 0 deletions spec/ameba/formatter/util_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ module Ameba::Formatter
subject.affected_code(code, location).should be_nil
end

it "works with file-wide location (1, 1) + indented code" do
code = <<-CRYSTAL
a = 1
CRYSTAL
location = Crystal::Location.new("filename", 1, 1)
subject.deansify(subject.affected_code(code, location))
.should eq "> a = 1\n ^\n"
end

it "returns correct line if it is found" do
code = <<-CRYSTAL
a = 1
Expand Down
12 changes: 7 additions & 5 deletions src/ameba/formatter/util.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,22 @@ module Ameba::Formatter
affected_line = trim(affected_line, max_length, ellipsis)
end

position = prompt.size + column
position -= 1

show_context = context_lines > 0

if show_context
pre_context, post_context =
context(lines, lineno, context_lines)

position = prompt.size + column
position -= 1
else
affected_line_size, affected_line =
affected_line.size, affected_line.lstrip

position = column - (affected_line_size - affected_line.size) + prompt.size
position -= 1
indent_size_diff = affected_line_size - affected_line.size
if column > indent_size_diff
position -= indent_size_diff
end
end

String.build do |str|
Expand Down

0 comments on commit a5c99e2

Please sign in to comment.