From 60cc1febd3650216c1bc11d13b48976f7191b3c7 Mon Sep 17 00:00:00 2001 From: Syed Faraaz Ahmad Date: Mon, 18 Dec 2023 19:52:42 +0530 Subject: [PATCH 01/12] Add schema collector --- .../ruby_lsp_rails/schema_collector.rb | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb diff --git a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb new file mode 100644 index 00000000..00357467 --- /dev/null +++ b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb @@ -0,0 +1,21 @@ +class SchemaCollector < Prism::Visitor + attr_reader :tables + + def initialize + @tables = {} + + super + end + + def visit_call_node(node) + return if node.block.nil? + + node.block.body.child_nodes.each do |child_node| + next unless child_node.is_a?(Prism::CallNode) + next unless child_node.name == :create_table + + table_name = child_node.arguments.child_nodes.first.content + @tables[table_name.classify] = child_node.location + end + end +end From abe92294dbf9c3ce1409b4b2a8c63262cb191556 Mon Sep 17 00:00:00 2001 From: Syed Faraaz Ahmad Date: Mon, 18 Dec 2023 20:01:36 +0530 Subject: [PATCH 02/12] add types --- .../ruby_lsp_rails/schema_collector.rb | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb index 00357467..e56515ef 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb @@ -1,21 +1,31 @@ -class SchemaCollector < Prism::Visitor - attr_reader :tables +# typed: strict +# frozen_string_literal: true - def initialize - @tables = {} +module RubyLsp + class SchemaCollector < Prism::Visitor + extend T::Sig - super - end + sig { returns(T::Hash(String, Prism::Location)) } + attr_reader :tables + + sig { void } + def initialize + @tables = {} + + super + end - def visit_call_node(node) - return if node.block.nil? + sig { params(node: Prism::CallNode).void } + def visit_call_node(node) + return if node.block.nil? - node.block.body.child_nodes.each do |child_node| - next unless child_node.is_a?(Prism::CallNode) - next unless child_node.name == :create_table + node.block.body.child_nodes.each do |child_node| + next unless child_node.is_a?(Prism::CallNode) + next unless child_node.name == :create_table - table_name = child_node.arguments.child_nodes.first.content - @tables[table_name.classify] = child_node.location + table_name = child_node.arguments.child_nodes.first.content + @tables[table_name.classify] = child_node.location + end end end end From 77da13867281ee4246575cb9d120de3f61898bde Mon Sep 17 00:00:00 2001 From: Syed Faraaz Ahmad Date: Mon, 18 Dec 2023 20:02:35 +0530 Subject: [PATCH 03/12] fix operator typo --- lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb index e56515ef..f4f8f1d8 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb @@ -5,7 +5,7 @@ module RubyLsp class SchemaCollector < Prism::Visitor extend T::Sig - sig { returns(T::Hash(String, Prism::Location)) } + sig { returns(T::Hash[String, Prism::Location]) } attr_reader :tables sig { void } From e3f748fac54e7a2713325fed07056cb27dc00ba7 Mon Sep 17 00:00:00 2001 From: Syed Faraaz Ahmad Date: Mon, 18 Dec 2023 20:04:27 +0530 Subject: [PATCH 04/12] add RubyLsp::Rails module --- .../ruby_lsp_rails/schema_collector.rb | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb index f4f8f1d8..ab5deb4e 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb @@ -2,29 +2,31 @@ # frozen_string_literal: true module RubyLsp - class SchemaCollector < Prism::Visitor - extend T::Sig + module Rails + class SchemaCollector < Prism::Visitor + extend T::Sig - sig { returns(T::Hash[String, Prism::Location]) } - attr_reader :tables + sig { returns(T::Hash[String, Prism::Location]) } + attr_reader :tables - sig { void } - def initialize - @tables = {} + sig { void } + def initialize + @tables = {} - super - end + super + end - sig { params(node: Prism::CallNode).void } - def visit_call_node(node) - return if node.block.nil? + sig { params(node: Prism::CallNode).void } + def visit_call_node(node) + return if node.block.nil? - node.block.body.child_nodes.each do |child_node| - next unless child_node.is_a?(Prism::CallNode) - next unless child_node.name == :create_table + node.block.body.child_nodes.each do |child_node| + next unless child_node.is_a?(Prism::CallNode) + next unless child_node.name == :create_table - table_name = child_node.arguments.child_nodes.first.content - @tables[table_name.classify] = child_node.location + table_name = child_node.arguments.child_nodes.first.content + @tables[table_name.classify] = child_node.location + end end end end From 3d9b76225d00e81bb5b86ef50d35fce94edacc88 Mon Sep 17 00:00:00 2001 From: Syed Faraaz Ahmad Date: Wed, 10 Jan 2024 20:11:16 +0530 Subject: [PATCH 05/12] incorporate changes by @wbhouston --- lib/ruby_lsp/ruby_lsp_rails/addon.rb | 9 ++++- lib/ruby_lsp/ruby_lsp_rails/hover.rb | 16 +++++++-- .../ruby_lsp_rails/schema_collector.rb | 34 +++++++++++++++---- lib/ruby_lsp_rails/rack_app.rb | 1 + test/ruby_lsp_rails/schema_collector_test.rb | 31 +++++++++++++++++ 5 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 test/ruby_lsp_rails/schema_collector_test.rb diff --git a/lib/ruby_lsp/ruby_lsp_rails/addon.rb b/lib/ruby_lsp/ruby_lsp_rails/addon.rb index bc869440..031c01ae 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/addon.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/addon.rb @@ -4,6 +4,7 @@ require "ruby_lsp/addon" require_relative "rails_client" +require_relative "schema_collector" require_relative "hover" require_relative "code_lens" @@ -20,6 +21,7 @@ def client sig { override.params(message_queue: Thread::Queue).void } def activate(message_queue) client.check_if_server_is_running! + schema_collector.parse_schema end sig { override.void } @@ -44,13 +46,18 @@ def create_code_lens_listener(uri, dispatcher) ).returns(T.nilable(Listener[T.nilable(Interface::Hover)])) end def create_hover_listener(nesting, index, dispatcher) - Hover.new(client, nesting, index, dispatcher) + Hover.new(client, schema_collector, nesting, index, dispatcher) end sig { override.returns(String) } def name "Ruby LSP Rails" end + + sig { returns(SchemaCollector) } + def schema_collector + @schema_collector ||= T.let(SchemaCollector.new, T.nilable(SchemaCollector)) + end end end end diff --git a/lib/ruby_lsp/ruby_lsp_rails/hover.rb b/lib/ruby_lsp/ruby_lsp_rails/hover.rb index 18982df1..8ebb016e 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/hover.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/hover.rb @@ -28,16 +28,18 @@ class Hover < ::RubyLsp::Listener sig do params( client: RailsClient, + schema_collector: SchemaCollector, nesting: T::Array[String], index: RubyIndexer::Index, dispatcher: Prism::Dispatcher, ).void end - def initialize(client, nesting, index, dispatcher) + def initialize(client, schema_collector, nesting, index, dispatcher) super(dispatcher) @_response = T.let(nil, ResponseType) @client = client + @schema_collector = schema_collector @nesting = nesting @index = index dispatcher.register(self, :on_constant_path_node_enter, :on_constant_read_node_enter, :on_call_node_enter) @@ -91,8 +93,18 @@ def generate_column_content(name) return if model.nil? schema_file = model[:schema_file] + if schema_file + location = @schema_collector.tables[model[:schema_table]] + fragment = "L#{location.start_line},#{location.start_column}-"\ + "#{location.end_line},#{location.end_column}" if location + schema_uri = URI::Generic.build( + scheme: "file", + path: schema_file, + fragment: fragment + ) + end content = +"" - content << "[Schema](#{URI::Generic.build(scheme: "file", path: schema_file)})\n\n" if schema_file + content << "[Schema](#{schema_uri})\n\n" if schema_uri content << model[:columns].map { |name, type| "**#{name}**: #{type}\n" }.join("\n") content end diff --git a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb index ab5deb4e..fcd242f0 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb @@ -5,6 +5,7 @@ module RubyLsp module Rails class SchemaCollector < Prism::Visitor extend T::Sig + extend T::Generic sig { returns(T::Hash[String, Prism::Location]) } attr_reader :tables @@ -16,17 +17,36 @@ def initialize super end + sig { void } + def parse_schema + parse_result = Prism::parse_file(schema_path) + return unless parse_result.success? + + parse_result.value.accept(self) + end + sig { params(node: Prism::CallNode).void } def visit_call_node(node) - return if node.block.nil? - - node.block.body.child_nodes.each do |child_node| - next unless child_node.is_a?(Prism::CallNode) - next unless child_node.name == :create_table + if node.message == 'create_table' + first_argument = node.arguments&.arguments&.first - table_name = child_node.arguments.child_nodes.first.content - @tables[table_name.classify] = child_node.location + if first_argument&.is_a?(Prism::StringNode) + @tables[first_argument.content] = node.location + end end + + super + end + + private + + sig { returns(String) } + def schema_path + project_root = T.let( + Bundler.with_unbundled_env { Bundler.default_gemfile }.dirname, + Pathname, + ) + project_root.join('db', 'schema.rb').to_s end end end diff --git a/lib/ruby_lsp_rails/rack_app.rb b/lib/ruby_lsp_rails/rack_app.rb index 69ac22ba..90a52eaa 100644 --- a/lib/ruby_lsp_rails/rack_app.rb +++ b/lib/ruby_lsp_rails/rack_app.rb @@ -41,6 +41,7 @@ def resolve_database_info_from_model(model_name) body = JSON.dump({ columns: const.columns.map { |column| [column.name, column.type] }, schema_file: schema_file, + schema_table: const.table_name }) [200, { "Content-Type" => "application/json" }, [body]] diff --git a/test/ruby_lsp_rails/schema_collector_test.rb b/test/ruby_lsp_rails/schema_collector_test.rb new file mode 100644 index 00000000..3abdf8aa --- /dev/null +++ b/test/ruby_lsp_rails/schema_collector_test.rb @@ -0,0 +1,31 @@ +# typed: true +# frozen_string_literal: true + +require "test_helper" + +SCHEMA_FILE = <<~RUBY +ActiveRecord::Schema[7.1].define(version: 2023_12_09_114241) do + create_table "cats", force: :cascade do |t| + end + + create_table "dogs", force: :cascade do |t| + end + + add_foreign_key "cats", "dogs" +end +RUBY + +module RubyLsp + module Rails + class SchemaCollectorTest < ActiveSupport::TestCase + test "store locations of models by parsing create_table calls" do + collector = RubyLsp::Rails::SchemaCollector.new + Prism.parse(SCHEMA_FILE).value.accept(collector) + + assert_equal(collector.tables.keys, ['Cat', 'Dog']) + ensure + # T.must(queue).close + end + end + end +end From 63bf2c1eb2f9290587a5e5abdf15630f48eba95d Mon Sep 17 00:00:00 2001 From: Syed Faraaz Ahmad Date: Wed, 10 Jan 2024 20:30:31 +0530 Subject: [PATCH 06/12] fix schema collector test --- lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb | 2 +- test/ruby_lsp_rails/schema_collector_test.rb | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb index fcd242f0..d788484b 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb @@ -31,7 +31,7 @@ def visit_call_node(node) first_argument = node.arguments&.arguments&.first if first_argument&.is_a?(Prism::StringNode) - @tables[first_argument.content] = node.location + @tables[first_argument.content.classify] = node.location end end diff --git a/test/ruby_lsp_rails/schema_collector_test.rb b/test/ruby_lsp_rails/schema_collector_test.rb index 3abdf8aa..0e7bdc1a 100644 --- a/test/ruby_lsp_rails/schema_collector_test.rb +++ b/test/ruby_lsp_rails/schema_collector_test.rb @@ -22,9 +22,7 @@ class SchemaCollectorTest < ActiveSupport::TestCase collector = RubyLsp::Rails::SchemaCollector.new Prism.parse(SCHEMA_FILE).value.accept(collector) - assert_equal(collector.tables.keys, ['Cat', 'Dog']) - ensure - # T.must(queue).close + assert_equal(['Cat', 'Dog'], collector.tables.keys) end end end From da54d630429c673fa23332c2e39b3f9fac524c15 Mon Sep 17 00:00:00 2001 From: Syed Faraaz Ahmad Date: Wed, 10 Jan 2024 20:33:28 +0530 Subject: [PATCH 07/12] fix rack app test --- lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb | 2 +- test/ruby_lsp_rails/rack_app_test.rb | 1 + test/ruby_lsp_rails/schema_collector_test.rb | 4 +--- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb index d788484b..fcd242f0 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb @@ -31,7 +31,7 @@ def visit_call_node(node) first_argument = node.arguments&.arguments&.first if first_argument&.is_a?(Prism::StringNode) - @tables[first_argument.content.classify] = node.location + @tables[first_argument.content] = node.location end end diff --git a/test/ruby_lsp_rails/rack_app_test.rb b/test/ruby_lsp_rails/rack_app_test.rb index ce645b38..ec8efd6e 100644 --- a/test/ruby_lsp_rails/rack_app_test.rb +++ b/test/ruby_lsp_rails/rack_app_test.rb @@ -20,6 +20,7 @@ class RackAppTest < ActionDispatch::IntegrationTest ["created_at", "datetime"], ["updated_at", "datetime"], ], + "schema_table" => "users" }, JSON.parse(response.body), ) diff --git a/test/ruby_lsp_rails/schema_collector_test.rb b/test/ruby_lsp_rails/schema_collector_test.rb index 0e7bdc1a..f7a98f73 100644 --- a/test/ruby_lsp_rails/schema_collector_test.rb +++ b/test/ruby_lsp_rails/schema_collector_test.rb @@ -10,8 +10,6 @@ create_table "dogs", force: :cascade do |t| end - - add_foreign_key "cats", "dogs" end RUBY @@ -22,7 +20,7 @@ class SchemaCollectorTest < ActiveSupport::TestCase collector = RubyLsp::Rails::SchemaCollector.new Prism.parse(SCHEMA_FILE).value.accept(collector) - assert_equal(['Cat', 'Dog'], collector.tables.keys) + assert_equal(['cats', 'dogs'], collector.tables.keys) end end end From 4d978742f16917bb122fea5b6cc27770be2f1a5f Mon Sep 17 00:00:00 2001 From: Syed Faraaz Ahmad Date: Sat, 20 Jan 2024 17:21:34 +0530 Subject: [PATCH 08/12] Update lib/ruby_lsp/ruby_lsp_rails/hover.rb Co-authored-by: Vinicius Stock --- lib/ruby_lsp/ruby_lsp_rails/hover.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/ruby_lsp/ruby_lsp_rails/hover.rb b/lib/ruby_lsp/ruby_lsp_rails/hover.rb index 8ebb016e..9f5f1531 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/hover.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/hover.rb @@ -97,8 +97,7 @@ def generate_column_content(name) location = @schema_collector.tables[model[:schema_table]] fragment = "L#{location.start_line},#{location.start_column}-"\ "#{location.end_line},#{location.end_column}" if location - schema_uri = URI::Generic.build( - scheme: "file", + schema_uri = URI::Generic.from_path( path: schema_file, fragment: fragment ) From 5f65cb9bff765bd2e500cb0bbd7682e1b0a4c6d7 Mon Sep 17 00:00:00 2001 From: Syed Faraaz Ahmad Date: Mon, 29 Jan 2024 22:34:00 +0530 Subject: [PATCH 09/12] fix: address comments --- lib/ruby_lsp/ruby_lsp_rails/addon.rb | 10 ++++---- .../ruby_lsp_rails/schema_collector.rb | 24 ++++--------------- test/ruby_lsp_rails/schema_collector_test.rb | 2 +- 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/lib/ruby_lsp/ruby_lsp_rails/addon.rb b/lib/ruby_lsp/ruby_lsp_rails/addon.rb index 031c01ae..f92226c2 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/addon.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/addon.rb @@ -18,6 +18,11 @@ def client @client ||= T.let(RailsClient.new, T.nilable(RailsClient)) end + sig { returns(SchemaCollector) } + def schema_collector + @schema_collector ||= T.let(SchemaCollector.new(client.root), T.nilable(SchemaCollector)) + end + sig { override.params(message_queue: Thread::Queue).void } def activate(message_queue) client.check_if_server_is_running! @@ -53,11 +58,6 @@ def create_hover_listener(nesting, index, dispatcher) def name "Ruby LSP Rails" end - - sig { returns(SchemaCollector) } - def schema_collector - @schema_collector ||= T.let(SchemaCollector.new, T.nilable(SchemaCollector)) - end end end end diff --git a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb index fcd242f0..69b607b5 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb @@ -5,23 +5,20 @@ module RubyLsp module Rails class SchemaCollector < Prism::Visitor extend T::Sig - extend T::Generic sig { returns(T::Hash[String, Prism::Location]) } attr_reader :tables - sig { void } - def initialize - @tables = {} - + def initialize(project_root) super + + @tables = {} + @schema_path = project_root.join('db', 'schema.rb').to_s end sig { void } def parse_schema - parse_result = Prism::parse_file(schema_path) - return unless parse_result.success? - + parse_result = Prism::parse_file(@schema_path) parse_result.value.accept(self) end @@ -37,17 +34,6 @@ def visit_call_node(node) super end - - private - - sig { returns(String) } - def schema_path - project_root = T.let( - Bundler.with_unbundled_env { Bundler.default_gemfile }.dirname, - Pathname, - ) - project_root.join('db', 'schema.rb').to_s - end end end end diff --git a/test/ruby_lsp_rails/schema_collector_test.rb b/test/ruby_lsp_rails/schema_collector_test.rb index f7a98f73..3488244d 100644 --- a/test/ruby_lsp_rails/schema_collector_test.rb +++ b/test/ruby_lsp_rails/schema_collector_test.rb @@ -17,7 +17,7 @@ module RubyLsp module Rails class SchemaCollectorTest < ActiveSupport::TestCase test "store locations of models by parsing create_table calls" do - collector = RubyLsp::Rails::SchemaCollector.new + collector = RubyLsp::Rails::SchemaCollector.new(Pathname.new('example_app')) Prism.parse(SCHEMA_FILE).value.accept(collector) assert_equal(['cats', 'dogs'], collector.tables.keys) From 633edc597c4fc1ad014052e740f2fa4a9b4ab926 Mon Sep 17 00:00:00 2001 From: Andy Waite Date: Mon, 29 Jan 2024 13:53:49 -0500 Subject: [PATCH 10/12] Fix typing and linting --- lib/ruby_lsp/ruby_lsp_rails/hover.rb | 2 +- lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb | 11 ++++++----- lib/ruby_lsp_rails/rack_app.rb | 2 +- test/ruby_lsp_rails/rack_app_test.rb | 2 +- test/ruby_lsp_rails/schema_collector_test.rb | 14 +++++++------- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/ruby_lsp/ruby_lsp_rails/hover.rb b/lib/ruby_lsp/ruby_lsp_rails/hover.rb index 9f5f1531..200af22f 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/hover.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/hover.rb @@ -99,7 +99,7 @@ def generate_column_content(name) "#{location.end_line},#{location.end_column}" if location schema_uri = URI::Generic.from_path( path: schema_file, - fragment: fragment + fragment: fragment, ) end content = +"" diff --git a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb index 69b607b5..a0700c3c 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/schema_collector.rb @@ -9,22 +9,23 @@ class SchemaCollector < Prism::Visitor sig { returns(T::Hash[String, Prism::Location]) } attr_reader :tables + sig { params(project_root: Pathname).void } def initialize(project_root) - super + super() - @tables = {} - @schema_path = project_root.join('db', 'schema.rb').to_s + @tables = T.let({}, T::Hash[String, Prism::Location]) + @schema_path = T.let(project_root.join("db", "schema.rb").to_s, String) end sig { void } def parse_schema - parse_result = Prism::parse_file(@schema_path) + parse_result = Prism.parse_file(@schema_path) parse_result.value.accept(self) end sig { params(node: Prism::CallNode).void } def visit_call_node(node) - if node.message == 'create_table' + if node.message == "create_table" first_argument = node.arguments&.arguments&.first if first_argument&.is_a?(Prism::StringNode) diff --git a/lib/ruby_lsp_rails/rack_app.rb b/lib/ruby_lsp_rails/rack_app.rb index 90a52eaa..c1532e2e 100644 --- a/lib/ruby_lsp_rails/rack_app.rb +++ b/lib/ruby_lsp_rails/rack_app.rb @@ -41,7 +41,7 @@ def resolve_database_info_from_model(model_name) body = JSON.dump({ columns: const.columns.map { |column| [column.name, column.type] }, schema_file: schema_file, - schema_table: const.table_name + schema_table: const.table_name, }) [200, { "Content-Type" => "application/json" }, [body]] diff --git a/test/ruby_lsp_rails/rack_app_test.rb b/test/ruby_lsp_rails/rack_app_test.rb index ec8efd6e..fc954dea 100644 --- a/test/ruby_lsp_rails/rack_app_test.rb +++ b/test/ruby_lsp_rails/rack_app_test.rb @@ -20,7 +20,7 @@ class RackAppTest < ActionDispatch::IntegrationTest ["created_at", "datetime"], ["updated_at", "datetime"], ], - "schema_table" => "users" + "schema_table" => "users", }, JSON.parse(response.body), ) diff --git a/test/ruby_lsp_rails/schema_collector_test.rb b/test/ruby_lsp_rails/schema_collector_test.rb index 3488244d..58beddd9 100644 --- a/test/ruby_lsp_rails/schema_collector_test.rb +++ b/test/ruby_lsp_rails/schema_collector_test.rb @@ -4,23 +4,23 @@ require "test_helper" SCHEMA_FILE = <<~RUBY -ActiveRecord::Schema[7.1].define(version: 2023_12_09_114241) do - create_table "cats", force: :cascade do |t| - end + ActiveRecord::Schema[7.1].define(version: 2023_12_09_114241) do + create_table "cats", force: :cascade do |t| + end - create_table "dogs", force: :cascade do |t| + create_table "dogs", force: :cascade do |t| + end end -end RUBY module RubyLsp module Rails class SchemaCollectorTest < ActiveSupport::TestCase test "store locations of models by parsing create_table calls" do - collector = RubyLsp::Rails::SchemaCollector.new(Pathname.new('example_app')) + collector = RubyLsp::Rails::SchemaCollector.new(Pathname.new("example_app")) Prism.parse(SCHEMA_FILE).value.accept(collector) - assert_equal(['cats', 'dogs'], collector.tables.keys) + assert_equal(["cats", "dogs"], collector.tables.keys) end end end From 0e414a7050c2fc978ab690b28d51ea60fc4a0671 Mon Sep 17 00:00:00 2001 From: Syed Faraaz Ahmad Date: Wed, 31 Jan 2024 22:25:38 +0530 Subject: [PATCH 11/12] fix: move schema file into class --- test/ruby_lsp_rails/schema_collector_test.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/ruby_lsp_rails/schema_collector_test.rb b/test/ruby_lsp_rails/schema_collector_test.rb index 58beddd9..ea05ac8a 100644 --- a/test/ruby_lsp_rails/schema_collector_test.rb +++ b/test/ruby_lsp_rails/schema_collector_test.rb @@ -3,19 +3,19 @@ require "test_helper" -SCHEMA_FILE = <<~RUBY - ActiveRecord::Schema[7.1].define(version: 2023_12_09_114241) do - create_table "cats", force: :cascade do |t| - end - - create_table "dogs", force: :cascade do |t| - end - end -RUBY - module RubyLsp module Rails class SchemaCollectorTest < ActiveSupport::TestCase + SCHEMA_FILE = <<~RUBY + ActiveRecord::Schema[7.1].define(version: 2023_12_09_114241) do + create_table "cats", force: :cascade do |t| + end + + create_table "dogs", force: :cascade do |t| + end + end + RUBY + test "store locations of models by parsing create_table calls" do collector = RubyLsp::Rails::SchemaCollector.new(Pathname.new("example_app")) Prism.parse(SCHEMA_FILE).value.accept(collector) From c24d3d56299e0272f7645b1b302d325959a8d06b Mon Sep 17 00:00:00 2001 From: Syed Faraaz Ahmad Date: Mon, 18 Mar 2024 22:51:05 +0530 Subject: [PATCH 12/12] Update lib/ruby_lsp/ruby_lsp_rails/hover.rb Co-authored-by: Vinicius Stock --- lib/ruby_lsp/ruby_lsp_rails/hover.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ruby_lsp/ruby_lsp_rails/hover.rb b/lib/ruby_lsp/ruby_lsp_rails/hover.rb index 200af22f..3168bfe8 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/hover.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/hover.rb @@ -95,8 +95,8 @@ def generate_column_content(name) schema_file = model[:schema_file] if schema_file location = @schema_collector.tables[model[:schema_table]] - fragment = "L#{location.start_line},#{location.start_column}-"\ - "#{location.end_line},#{location.end_column}" if location + fragment = "L#{location.start_line - 1},#{location.start_column}-"\ + "#{location.end_line - 1},#{location.end_column}" if location schema_uri = URI::Generic.from_path( path: schema_file, fragment: fragment,