mj 0.4.1 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff905fc5978244944098033bf6ecfb671f1130c336e2f77c19beefab3c927dfe
4
- data.tar.gz: 0560a8a156cb45bc2e8c459b021e721118e304abf55e92d25039cd9c9277ee24
3
+ metadata.gz: 040511b6ef46ad0760a5958920e53ec8d00fa3f0697c9098004e649808c41f73
4
+ data.tar.gz: b9eaa497531aa4856674cf0f5d80825769e0b431ee8c304cf052680f8d22ee2c
5
5
  SHA512:
6
- metadata.gz: b20134878fb83b50a0d9dda518dfc81500af893f522616605835e1ee2f6d78bbe824a9302d07131926d799bf308ee463efbfa9a0b4d37dce793fd2ab614c91d8
7
- data.tar.gz: 30f12048aa54fa5070895859a99f0ffdd66bf35962d82e98952fc889b8a23afd9f4b650a89bea6645f28434210b173d90460196ab20a72f93fe68368cc50d87d
6
+ metadata.gz: 1e0c951f24b70e6d25746e51f3cf0a37c9fd781818763e26115f6199a157da4cc3f6590cba8819f56d5ecbdb7de8523e509b3f29dd5362340cebc7d1646c3028
7
+ data.tar.gz: 112bc4d98c34cd67f5ef1cae09646e06a920ab2b5ed27010130bbd926dc23d033bdf1a329d9fd0d4e56466dfb086747a40a3414200a5a441839f1a397dd90fdd
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mj (0.4.1)
4
+ mj (0.5.1)
5
5
  thor (~> 1.2.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -25,9 +25,76 @@ Or install it yourself as:
25
25
  ## Usage
26
26
 
27
27
  ```bash
28
- mj --help
28
+ mj help
29
29
  ```
30
30
 
31
+ ### Alternative file
32
+
33
+ Examples:
34
+
35
+ ```
36
+ $ mj alternative_file list app/components/attribute_component.rb
37
+ app/components/attribute_component.html.erb
38
+ app/components/attribute_component.rb
39
+ lib/components/attribute_component.rb
40
+ spec/components/attribute_component_spec.rb
41
+ test/components/attribute_component_test.rb
42
+
43
+ $ mj alternative_file list app/components/attribute_component.rb --exists
44
+ app/components/attribute_component.html.erb
45
+ app/components/attribute_component.rb
46
+ spec/components/attribute_component_spec.rb
47
+
48
+ $ mj alternative_file list app/components/attribute_component.rb --types=spec
49
+ spec/components/attribute_component_spec.rb
50
+
51
+ $ mj alternative_file next app/components/attribute_component.rb
52
+ lib/components/attribute_component.rb
53
+
54
+ $ mj alternative_file prev app/components/attribute_component.rb
55
+ app/components/attribute_component.html.erb
56
+ ```
57
+
58
+ Why? Because you can integrate that command with your IDE/Text Editor. For instance, here's my [neovim integration](https://github.com/mjacobus/dotfiles/blob/d8ceda659dc9b587ab22b05fc15eac2fa5b477d7/neovim/.config/nvim/init.lua#L31-L63):
59
+
60
+ ```lua
61
+ vimp.nnoremap('<leader>ak', function()
62
+ open_mj_alternative_file('next', '--exists')
63
+ end)
64
+
65
+ vimp.nnoremap('<leader>aj', function()
66
+ open_mj_alternative_file('prev', '--exists')
67
+ end)
68
+
69
+ function open_mj_alternative_file(subcommand, options)
70
+ file_path = vim.fn.expand('%')
71
+ files = mj_alternative_file(file_path, subcommand, options)
72
+ files = vim.split(files, ' ')
73
+ file = files[1]
74
+
75
+
76
+ if file ~= '' then
77
+ vim.api.nvim_command('e ' .. file)
78
+ end
79
+ end
80
+
81
+ function mj_alternative_file(file, subcommand, options)
82
+ local cmd = 'mj alternative_file ' .. subcommand .. ' ' .. file .. ' ' .. options
83
+ return execute_command(cmd)
84
+ end
85
+
86
+ function execute_command(cmd)
87
+ print("cmd: " .. cmd)
88
+ local f = io.popen(cmd)
89
+ local s = f:read('*a')
90
+ f:close()
91
+ return s
92
+ end
93
+ ```
94
+
95
+ This way I can use `<leader>a{direction}`, where `k` is `next`, and `j` is `previous` alternative file.
96
+
97
+
31
98
  ## Development
32
99
 
33
100
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -41,6 +41,10 @@ module Mj
41
41
  next_file(@items.reverse, reference_file)
42
42
  end
43
43
 
44
+ def sorted_by_path
45
+ new(@items.sort_by(&:path))
46
+ end
47
+
44
48
  private
45
49
 
46
50
  def new(*args)
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module AlternativeFile
5
+ module Commands
6
+ class ListCommand
7
+ attr_reader :file
8
+
9
+ def initialize(file, options)
10
+ @file = CurrentFile.new(file.to_s)
11
+ @options = options
12
+ end
13
+
14
+ def exists?
15
+ @options[:exists]
16
+ end
17
+
18
+ def types
19
+ @options[:types].to_s.split(",")
20
+ end
21
+
22
+ def debug?
23
+ @options[:debug]
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module AlternativeFile
5
+ module Commands
6
+ class ListCommandHandler
7
+ def initialize(resolvers:)
8
+ @resolvers = resolvers
9
+ end
10
+
11
+ # rubocop:disable Metrics/MethodLength
12
+ def handle(command)
13
+ candidates = @resolvers.resolve(command.file)
14
+
15
+ if command.types.any?
16
+ candidates = candidates.of_types(command.types)
17
+ end
18
+
19
+ if command.exists?
20
+ candidates = candidates.existing
21
+ end
22
+
23
+ unless command.debug?
24
+ candidates = candidates.unique
25
+ end
26
+
27
+ candidates.sorted_by_path
28
+ end
29
+ # rubocop:enable Metrics/MethodLength
30
+ end
31
+ end
32
+ end
33
+ end
@@ -19,6 +19,8 @@ module Mj
19
19
 
20
20
  # lib files
21
21
  add_candidate("lib/#{ruby_file.class_path}.rb", "lib", to: candidates)
22
+ add_candidate("spec/lib/#{ruby_file.class_path}_spec.rb", :spec, to: candidates)
23
+ add_candidate("test/lib/#{ruby_file.class_path}_test.rb", :minitest, to: candidates)
22
24
  end
23
25
  end
24
26
  end
@@ -10,6 +10,8 @@ require_relative "resolvers/ruby/rails_resolver"
10
10
  require_relative "resolvers/ruby/rails_controller_resolver"
11
11
  require_relative "resolvers/ruby/view_component_resolver"
12
12
  require_relative "resolvers/ruby/ruby_file"
13
+ require_relative "commands/list_command_handler"
14
+ require_relative "commands/list_command"
13
15
 
14
16
  module Mj
15
17
  module AlternativeFile
@@ -19,8 +21,10 @@ module Mj
19
21
  option :exists, type: :boolean, banner: "files that exist", aliases: :e
20
22
  option :debug, type: :boolean, banner: "print debug information", aliases: :d
21
23
  def list(reference_file)
22
- file = CurrentFile.new(reference_file)
23
- print_candidates(resolve(file))
24
+ handler = Commands::ListCommandHandler.new(resolvers: self.class.resolvers)
25
+ command = Commands::ListCommand.new(reference_file, options)
26
+ candidates = handler.handle(command)
27
+ print_candidates(candidates)
24
28
  end
25
29
 
26
30
  desc "next <reference-file>", "Next alternative file"
@@ -28,8 +32,10 @@ module Mj
28
32
  option :exists, type: :boolean, banner: "files that exist", aliases: :e
29
33
  option :debug, type: :boolean, banner: "print debug information", aliases: :d
30
34
  def next(reference_file)
31
- file = CurrentFile.new(reference_file)
32
- candidate = resolve(file).after(file)
35
+ handler = Commands::ListCommandHandler.new(resolvers: self.class.resolvers)
36
+ command = Commands::ListCommand.new(reference_file, options)
37
+ candidates = handler.handle(command)
38
+ candidate = candidates.after(command.file)
33
39
  print_candidates([candidate].compact)
34
40
  end
35
41
 
@@ -38,8 +44,10 @@ module Mj
38
44
  option :exists, type: :boolean, banner: "files that exist", aliases: :e
39
45
  option :debug, type: :boolean, banner: "print debug information", aliases: :d
40
46
  def prev(reference_file)
41
- file = CurrentFile.new(reference_file)
42
- candidate = resolve(file).before(file)
47
+ handler = Commands::ListCommandHandler.new(resolvers: self.class.resolvers)
48
+ command = Commands::ListCommand.new(reference_file, options)
49
+ candidates = handler.handle(command)
50
+ candidate = candidates.before(command.file)
43
51
  print_candidates([candidate].compact)
44
52
  end
45
53
 
@@ -54,28 +62,8 @@ module Mj
54
62
  private
55
63
 
56
64
  def print_candidates(candidates)
57
- $stdout.puts candidates.map { |i| i.to_s(debug: options[:debug]) }.join(" ")
65
+ $stdout.puts candidates.map { |i| i.to_s(debug: options[:debug]) }.join("\n")
58
66
  end
59
-
60
- # rubocop:disable Metrics/MethodLength
61
- def resolve(file)
62
- candidates = self.class.resolvers.resolve(file)
63
-
64
- if options[:types]
65
- candidates = candidates.of_types(options[:types].to_s.split(","))
66
- end
67
-
68
- if options[:exists]
69
- candidates = candidates.existing
70
- end
71
-
72
- unless options[:debug]
73
- candidates = candidates.unique
74
- end
75
-
76
- candidates
77
- end
78
- # rubocop:enable Metrics/MethodLength
79
67
  end
80
68
  end
81
69
  end
data/lib/mj/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mj
4
- VERSION = "0.4.1"
4
+ VERSION = "0.5.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mj
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Jacobus
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-14 00:00:00.000000000 Z
11
+ date: 2022-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -47,6 +47,8 @@ files:
47
47
  - lib/mj.rb
48
48
  - lib/mj/alternative_file/candidate.rb
49
49
  - lib/mj/alternative_file/candidates.rb
50
+ - lib/mj/alternative_file/commands/list_command.rb
51
+ - lib/mj/alternative_file/commands/list_command_handler.rb
50
52
  - lib/mj/alternative_file/current_file.rb
51
53
  - lib/mj/alternative_file/resolver.rb
52
54
  - lib/mj/alternative_file/resolvers/base.rb