Skip to content

Commit

Permalink
Attempt to sorbetize #1
Browse files Browse the repository at this point in the history
  • Loading branch information
halostatue committed Jul 3, 2020
1 parent ba11b59 commit 476b27e
Show file tree
Hide file tree
Showing 31 changed files with 17,227 additions and 103 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ research/
spec/ldap.yml
website/index.html
.byebug_history
sorbet/rbi/hidden-definitions/errors.txt
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ elsif RUBY_VERSION >= '2.0'
gem 'simplecov', '~> 0.18'

gem 'byebug'

if RUBY_VERSION >= '2.6'
gem 'sorbet'
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ _spec = Hoe.spec 'diff-lcs' do
self.readme_file = 'README.rdoc'
self.licenses = ['MIT', 'Artistic-2.0', 'GPL-2.0+']

extra_deps << ['sorbet-runtime', '~> 0.5']

extra_dev_deps << ['hoe-doofus', '~> 1.0']
extra_dev_deps << ['hoe-gemspec2', '~> 1.1']
extra_dev_deps << ['hoe-git', '~> 1.6']
Expand Down
1 change: 1 addition & 0 deletions autotest/discover.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# typed: ignore

Autotest.add_discovery { 'rspec2' }
5 changes: 4 additions & 1 deletion diff-lcs.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.metadata = { "bug_tracker_uri" => "https://github.com/halostatue/diff-lcs/issues", "homepage_uri" => "https://github.com/halostatue/diff-lcs", "source_code_uri" => "https://github.com/halostatue/diff-lcs" } if s.respond_to? :metadata=
s.require_paths = ["lib".freeze]
s.authors = ["Austin Ziegler".freeze]
s.date = "2020-07-01"
s.date = "2020-07-02"
s.description = "Diff::LCS computes the difference between two Enumerable sequences using the\nMcIlroy-Hunt longest common subsequence (LCS) algorithm. It includes utilities\nto create a simple HTML diff output format and a standard diff-like tool.\n\nThis is release 1.4.3, providing a simple extension that allows for\nDiff::LCS::Change objects to be treated implicitly as arrays and fixes a\nnumber of formatting issues.\n\nRuby versions below 2.5 are soft-deprecated, which means that older versions\nare no longer part of the CI test suite. If any changes have been introduced\nthat break those versions, bug reports and patches will be accepted, but it\nwill be up to the reporter to verify any fixes prior to release. The next\nmajor release will completely break compatibility.".freeze
s.email = ["halostatue@gmail.com".freeze]
s.executables = ["htmldiff".freeze, "ldiff".freeze]
Expand All @@ -26,6 +26,7 @@ Gem::Specification.new do |s|
s.specification_version = 4

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<sorbet-runtime>.freeze, ["~> 0.5"])
s.add_development_dependency(%q<hoe-doofus>.freeze, ["~> 1.0"])
s.add_development_dependency(%q<hoe-gemspec2>.freeze, ["~> 1.1"])
s.add_development_dependency(%q<hoe-git>.freeze, ["~> 1.6"])
Expand All @@ -35,6 +36,7 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<rdoc>.freeze, [">= 0"])
s.add_development_dependency(%q<hoe>.freeze, ["~> 3.22"])
else
s.add_dependency(%q<sorbet-runtime>.freeze, ["~> 0.5"])
s.add_dependency(%q<hoe-doofus>.freeze, ["~> 1.0"])
s.add_dependency(%q<hoe-gemspec2>.freeze, ["~> 1.1"])
s.add_dependency(%q<hoe-git>.freeze, ["~> 1.6"])
Expand All @@ -45,6 +47,7 @@ Gem::Specification.new do |s|
s.add_dependency(%q<hoe>.freeze, ["~> 3.22"])
end
else
s.add_dependency(%q<sorbet-runtime>.freeze, ["~> 0.5"])
s.add_dependency(%q<hoe-doofus>.freeze, ["~> 1.0"])
s.add_dependency(%q<hoe-gemspec2>.freeze, ["~> 1.1"])
s.add_dependency(%q<hoe-git>.freeze, ["~> 1.6"])
Expand Down
1 change: 1 addition & 0 deletions lib/diff-lcs.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# typed: strict

require 'diff/lcs'
43 changes: 26 additions & 17 deletions lib/diff/lcs.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true
# typed: true

require 'sorbet-runtime'

module Diff; end unless defined? Diff # rubocop:disable Style/Documentation

Expand Down Expand Up @@ -50,6 +53,9 @@ module Diff; end unless defined? Diff # rubocop:disable Style/Documentation
# a b c a x b y c z
module Diff::LCS
VERSION = '1.4.4'

SequenceT = T.type_alias { T.any(Diff::LCS, Enumerable, String) }
CallbacksT = T.type_alias { T.nilable(Module) }
end

require 'diff/lcs/callbacks'
Expand Down Expand Up @@ -85,14 +91,14 @@ def sdiff(other, callbacks = nil, &block)
# Traverses the discovered longest common subsequences between +self+ and
# +other+. See Diff::LCS#traverse_sequences.
def traverse_sequences(other, callbacks = nil, &block)
traverse_sequences(self, other, callbacks || Diff::LCS::SequenceCallbacks, &block)
Diff::LCS.traverse_sequences(self, other, callbacks || Diff::LCS::SequenceCallbacks, &block)
end

# Traverses the discovered longest common subsequences between +self+ and
# +other+ using the alternate, balanced algorithm. See
# Diff::LCS#traverse_balanced.
def traverse_balanced(other, callbacks = nil, &block)
traverse_balanced(self, other, callbacks || Diff::LCS::BalancedCallbacks, &block)
Diff::LCS.traverse_balanced(self, other, callbacks || Diff::LCS::BalancedCallbacks, &block)
end

# Attempts to patch +self+ with the provided +patchset+. A new sequence based
Expand Down Expand Up @@ -121,7 +127,7 @@ def unpatch!(patchset)
# the sequence this is used on supports #replace, the value of +self+ will be
# replaced. See Diff::LCS#patch. Does no patch direction autodiscovery.
def patch_me(patchset)
if respond_to? :replace
if respond_to?(:replace)
replace(patch!(patchset))
else
patch!(patchset)
Expand All @@ -132,16 +138,16 @@ def patch_me(patchset)
# If the sequence this is used on supports #replace, the value of +self+ will
# be replaced. See Diff::LCS#unpatch. Does no patch direction autodiscovery.
def unpatch_me(patchset)
if respond_to? :replace
if respond_to?(:replace)
replace(unpatch!(patchset))
else
unpatch!(patchset)
end
end
end

class << Diff::LCS
def lcs(seq1, seq2, &block) #:yields seq1[i] for each matched:
module Diff::LCS # rubocop:disable Style/Documentation
def self.lcs(seq1, seq2, &block) #:yields seq1[i] for each matched:
matches = Diff::LCS::Internals.lcs(seq1, seq2)
ret = []
string = seq1.kind_of? String
Expand All @@ -165,7 +171,7 @@ def lcs(seq1, seq2, &block) #:yields seq1[i] for each matched:
# Class argument is provided for +callbacks+, #diff will attempt to
# initialise it. If the +callbacks+ object (possibly initialised) responds to
# #finish, it will be called.
def diff(seq1, seq2, callbacks = nil, &block) # :yields diff changes:
def self.diff(seq1, seq2, callbacks = nil, &block) # :yields diff changes:
diff_traversal(:diff, seq1, seq2, callbacks || Diff::LCS::DiffCallbacks, &block)
end

Expand Down Expand Up @@ -197,7 +203,7 @@ def diff(seq1, seq2, callbacks = nil, &block) # :yields diff changes:
# # insert
# end
# end
def sdiff(seq1, seq2, callbacks = nil, &block) #:yields diff changes:
def self.sdiff(seq1, seq2, callbacks = nil, &block) #:yields diff changes:
diff_traversal(:sdiff, seq1, seq2, callbacks || Diff::LCS::SDiffCallbacks, &block)
end

Expand Down Expand Up @@ -281,25 +287,28 @@ def sdiff(seq1, seq2, callbacks = nil, &block) #:yields diff changes:
# <tt>callbacks#discard_b</tt> will be called after the end of the sequence
# is reached, if +a+ has not yet reached the end of +A+ or +b+ has not yet
# reached the end of +B+.
def traverse_sequences(seq1, seq2, callbacks = Diff::LCS::SequenceCallbacks) #:yields change events:
sig { params(seq1: SequenceT, seq2: SequenceT, callbacks: CallbacksT).returns(Array) }
def self.traverse_sequences(seq1, seq2, callbacks = nil) #:yields change events:
callbacks ||= Diff::LCS::SequenceCallbacks
matches = Diff::LCS::Internals.lcs(seq1, seq2)

run_finished_a = run_finished_b = false
string = seq1.kind_of?(String)
string = String === seq1 # rubocop:disable Style/CaseEquality

a_size = seq1.size
b_size = seq2.size
a_size = seq1.count
b_size = seq2.count
ai = bj = 0

event = T.let(nil, T.nilable(Diff::LCS::ContextChange))

(0..matches.size).each do |i|
b_line = matches[i]

ax = string ? seq1[i, 1] : seq1[i]
bx = string ? seq2[bj, 1] : seq2[bj]

if b_line.nil?
unless ax.nil? or (string and ax.empty?)
unless ax.nil? or (string and T.must(ax).empty?)
event = Diff::LCS::ContextChange.new('-', i, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_a(event)
Expand Down Expand Up @@ -472,7 +481,7 @@ def traverse_sequences(seq1, seq2, callbacks = Diff::LCS::SequenceCallbacks) #:y
#
# Note that +i+ and +j+ may not be the same index position, even if +a+ and
# +b+ are considered to be pointing to matching or changed elements.
def traverse_balanced(seq1, seq2, callbacks = Diff::LCS::BalancedCallbacks)
def self.traverse_balanced(seq1, seq2, callbacks = Diff::LCS::BalancedCallbacks)
matches = Diff::LCS::Internals.lcs(seq1, seq2)
a_size = seq1.size
b_size = seq2.size
Expand Down Expand Up @@ -621,7 +630,7 @@ def traverse_balanced(seq1, seq2, callbacks = Diff::LCS::BalancedCallbacks)
# containing either Diff::LCS::Change objects (or a subclass) or the array
# representations of those objects. Prior to application, array
# representations of Diff::LCS::Change objects will be reified.
def patch(src, patchset, direction = nil)
def self.patch(src, patchset, direction = nil)
# Normalize the patchset.
has_changes, patchset = Diff::LCS::Internals.analyze_patchset(patchset)

Expand Down Expand Up @@ -725,13 +734,13 @@ def patch(src, patchset, direction = nil)

# Given a set of patchset, convert the current version to the prior version.
# Does no auto-discovery.
def unpatch!(src, patchset)
def self.unpatch!(src, patchset)
patch(src, patchset, :unpatch)
end

# Given a set of patchset, convert the current version to the next version.
# Does no auto-discovery.
def patch!(src, patchset)
def self.patch!(src, patchset)
patch(src, patchset, :patch)
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/diff/lcs/array.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
# typed: strict

require 'diff/lcs'

Expand Down
1 change: 1 addition & 0 deletions lib/diff/lcs/backports.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
# typed: true

unless 0.respond_to?(:positive?)
class Fixnum # rubocop:disable Lint/UnifiedInteger, Style/Documentation
Expand Down
30 changes: 24 additions & 6 deletions lib/diff/lcs/block.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
# frozen_string_literal: true
# typed: strict

# A block is an operation removing, adding, or changing a group of items.
# Basically, this is just a list of changes, where each change adds or
# deletes a single item. Used by bin/ldiff.
class Diff::LCS::Block
attr_reader :changes, :insert, :remove
extend T::Sig

ArrayOfChange = T.type_alias { T::Array[Diff::LCS::Change] }

# The full set of changes in the block.
sig { returns(ArrayOfChange) }
attr_reader :changes

# The insertions in the block.
sig { returns(ArrayOfChange) }
attr_reader :insert

# The deletions in the block.
sig { returns(ArrayOfChange) }
attr_reader :remove

sig { params(chunk: ArrayOfChange).void }
def initialize(chunk)
@changes = []
@insert = []
@remove = []
@changes = T.let([], ArrayOfChange)
@insert = T.let([], ArrayOfChange)
@remove = T.let([], ArrayOfChange)

chunk.each do |item|
@changes << item
Expand All @@ -18,12 +34,14 @@ def initialize(chunk)
end
end

sig { returns(Integer) }
def diff_size
@insert.size - @remove.size
insert.size - remove.size
end

sig { returns(String) }
def op
case [@remove.empty?, @insert.empty?]
case [remove.empty?, insert.empty?]
when [false, false]
'!'
when [false, true]
Expand Down
1 change: 1 addition & 0 deletions lib/diff/lcs/callbacks.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
# typed: true

require 'diff/lcs/change'

Expand Down
Loading

0 comments on commit 476b27e

Please sign in to comment.