-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_controller_view.rb
43 lines (33 loc) · 1.2 KB
/
find_controller_view.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require "active_support/all"
ControllerView = Struct.new(:path)
class FindControllerView
attr_reader :line_text, :file_uri, :file_name, :root_path
private :line_text, :file_uri, :file_name, :root_path
def initialize(line_text:, file_uri:, root_path:)
@line_text = line_text.strip
@file_uri = file_uri.gsub("file://", "")
@file_name = @file_uri.split("/")[-1]
@root_path = root_path
end
def call
return unless file_name.include?('controller') && line_text.start_with?('def ')
controller_view = ControllerView.new
view_name = line_text.gsub('def ', '').strip + '.html.erb'
controller_namespace = file_name.gsub('_controller.rb', '')
controllers_path = file_uri.gsub(root_path, '') # /app/controllers...
views_path = controllers_path.gsub('controllers', 'views') # /app/views...
view = "#{views_path.gsub(file_name, controller_namespace)}/#{view_name}"
view_path = root_path + view
log("VIEW: #{view_path}")
if File.exist?(view_path)
controller_view.path = view_path
end
controller_view
end
private
def log(message)
File.open("/home/catalin/.local/state/nvim/lsp.log", "a") do |f|
f.write "#{message}\n"
end
end
end