Skip to content

Commit e410c23

Browse files
authored
Merge pull request #121 from Shopify/vs/standardize_paths
Standardize path handling
2 parents 34babd4 + dfd300a commit e410c23

File tree

4 files changed

+14
-21
lines changed

4 files changed

+14
-21
lines changed

lib/ruby_lsp/ruby_lsp_rails/hover.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def on_const(node)
4040
schema_file = model[:schema_file]
4141
content = +""
4242
if schema_file
43-
content << "[Schema](file://#{schema_file})\n\n"
43+
content << "[Schema](#{URI::Generic.build(scheme: "file", path: schema_file)})\n\n"
4444
end
4545
content << model[:columns].map { |name, type| "**#{name}**: #{type}\n" }.join("\n")
4646
contents = RubyLsp::Interface::MarkupContent.new(kind: "markdown", value: content)

lib/ruby_lsp/ruby_lsp_rails/rails_client.rb

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,23 @@ class ServerAddressUnknown < StandardError; end
1313
SERVER_NOT_RUNNING_MESSAGE = "Rails server is not running. " \
1414
"To get Rails features in the editor, boot the Rails server"
1515

16-
sig { returns(String) }
16+
sig { returns(Pathname) }
1717
attr_reader :root
1818

1919
sig { void }
2020
def initialize
21-
project_root = Pathname.new(ENV["BUNDLE_GEMFILE"]).dirname
21+
project_root = T.let(Bundler.with_unbundled_env { Bundler.default_gemfile }.dirname, Pathname)
22+
dummy_path = project_root.join("test", "dummy")
2223

23-
if project_root.basename.to_s == ".ruby-lsp"
24-
project_root = project_root.join("../")
25-
end
26-
27-
dummy_path = File.join(project_root, "test", "dummy")
28-
@root = T.let(Dir.exist?(dummy_path) ? dummy_path : project_root.to_s, String)
29-
app_uri_path = "#{@root}/tmp/app_uri.txt"
30-
31-
if File.exist?(app_uri_path)
32-
url = File.read(app_uri_path).chomp
24+
@root = T.let(dummy_path.exist? ? dummy_path : project_root, Pathname)
25+
app_uri_path = @root.join("tmp", "app_uri.txt")
3326

34-
scheme, rest = url.split("://")
35-
uri, port = T.must(rest).split(":")
27+
if app_uri_path.exist?
28+
url = URI(app_uri_path.read.chomp)
3629

37-
@ssl = T.let(scheme == "https", T::Boolean)
38-
@uri = T.let(T.must(uri), T.nilable(String))
39-
@port = T.let(T.must(port).to_i, Integer)
30+
@ssl = T.let(url.scheme == "https", T::Boolean)
31+
@uri = T.let(T.must(url.path), T.nilable(String))
32+
@port = T.let(T.must(url.port).to_i, Integer)
4033
end
4134
end
4235

lib/ruby_lsp_rails/railtie.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class Railtie < ::Rails::Railtie
2323
if defined?(::Rails::Server)
2424
ssl_enable, host, port = ::Rails::Server::Options.new.parse!(ARGV).values_at(:SSLEnable, :Host, :Port)
2525
app_uri = "#{ssl_enable ? "https" : "http"}://#{host}:#{port}"
26-
app_uri_path = "#{::Rails.root}/tmp/app_uri.txt"
27-
File.write(app_uri_path, app_uri)
26+
app_uri_path = ::Rails.root.join("tmp", "app_uri.txt")
27+
app_uri_path.write(app_uri)
2828

2929
at_exit do
3030
# The app_uri.txt file should only exist when the server is running. The extension uses its presence to

test/ruby_lsp_rails/rails_client_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class RailsClientTest < ActiveSupport::TestCase
4040
project_root = Pathname.new(previous_bundle_gemfile).dirname
4141

4242
ENV["BUNDLE_GEMFILE"] = "#{project_root}/.ruby-lsp/Gemfile"
43-
assert_equal("#{project_root}/test/dummy", RailsClient.new.root)
43+
assert_equal("#{project_root}/test/dummy", RailsClient.new.root.to_s)
4444
ensure
4545
ENV["BUNDLE_GEMFILE"] = previous_bundle_gemfile
4646
end

0 commit comments

Comments
 (0)