Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ruby-syntax-tree/syntax_tree
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.0.0
Choose a base ref
...
head repository: ruby-syntax-tree/syntax_tree
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.1.0
Choose a head ref
  • 3 commits
  • 4 files changed
  • 1 contributor

Commits on Dec 8, 2021

  1. Verified

    This commit was signed with the committer’s verified signature.
    kddnewton Kevin Newton
    Copy the full SHA
    e438697 View commit details
  2. Verified

    This commit was signed with the committer’s verified signature.
    kddnewton Kevin Newton
    Copy the full SHA
    30ebbf5 View commit details
  3. Bump to v1.1.0

    kddnewton committed Dec 8, 2021

    Verified

    This commit was signed with the committer’s verified signature.
    kddnewton Kevin Newton
    Copy the full SHA
    cb72445 View commit details
Showing with 48 additions and 21 deletions.
  1. +8 −1 CHANGELOG.md
  2. +1 −1 Gemfile.lock
  3. +38 −18 lib/syntax_tree/cli.rb
  4. +1 −1 lib/syntax_tree/version.rb
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -6,7 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

## [1.0.0]
## [1.1.0] - 2021-12-08

### Added

- Better handling for formatting files with errors.
- Colorize the output snippet using IRB.

## [1.0.0] - 2021-12-08

### Added

2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
syntax_tree (1.0.0)
syntax_tree (1.1.0)

GEM
remote: https://rubygems.org/
56 changes: 38 additions & 18 deletions lib/syntax_tree/cli.rb
Original file line number Diff line number Diff line change
@@ -126,6 +126,9 @@ def run(filepath, source)
delta = ((Time.now - start) * 1000).round

puts "\r#{color} #{delta}ms"
rescue
puts "\r#{filepath}"
raise
end
end

@@ -177,25 +180,12 @@ def run(argv)
action.run(filepath, source)
rescue ParseError => error
warn("Error: #{error.message}")
lines = source.lines

maximum = [error.lineno + 3, lines.length].min
digits = Math.log10(maximum).ceil

([error.lineno - 3, 0].max...maximum).each do |line_index|
line_number = line_index + 1

if line_number == error.lineno
part1 = Color.red(">")
part2 = Color.gray("%#{digits}d |" % line_number)
warn("#{part1} #{part2} #{lines[line_index]}")

part3 = Color.gray(" %#{digits}s |" % " ")
warn("#{part3} #{" " * error.column}#{Color.red("^")}")
else
prefix = Color.gray(" %#{digits}d |" % line_number)
warn("#{prefix} #{lines[line_index]}")
end
if error.lineno
highlight_error(error, source)
else
warn(error.message)
warn(error.backtrace)
end

errored = true
@@ -232,6 +222,36 @@ def source_for(filepath)

File.read(filepath, encoding: encoding)
end

# Highlights a snippet from a source and parse error.
def highlight_error(error, source)
lines = source.lines

maximum = [error.lineno + 3, lines.length].min
digits = Math.log10(maximum).ceil

([error.lineno - 3, 0].max...maximum).each do |line_index|
line_number = line_index + 1

if line_number == error.lineno
part1 = Color.red(">")
part2 = Color.gray("%#{digits}d |" % line_number)
warn("#{part1} #{part2} #{colorize_line(lines[line_index])}")

part3 = Color.gray(" %#{digits}s |" % " ")
warn("#{part3} #{" " * error.column}#{Color.red("^")}")
else
prefix = Color.gray(" %#{digits}d |" % line_number)
warn("#{prefix} #{colorize_line(lines[line_index])}")
end
end
end

# Take a line of Ruby source and colorize the output.
def colorize_line(line)
require "irb"
IRB::Color.colorize_code(line, complete: false, ignore_error: true)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/syntax_tree/version.rb
Original file line number Diff line number Diff line change
@@ -3,5 +3,5 @@
require "ripper"

class SyntaxTree < Ripper
VERSION = "1.0.0"
VERSION = "1.1.0"
end