In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
+
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
This Code of Conduct applies within all project spaces, and it also applies when an individual is representing the project or its community in public spaces. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at opensource@shopify.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
+
+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project’s leadership.
Bug reports and pull requests are welcome on GitHub at github.com/Shopify/ruby-lsp-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
To set up the database for the dummy Rails app, and run the full test suite:
+
+
bundleexecrake
+
+
+
Tests are written as ActiveSupport::TestCase tests, so an individual test can be run with:
+
+
bin/rails test test/my_test.rb
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/docs/LICENSE.txt b/docs/LICENSE.txt
new file mode 100644
index 00000000..bf777b4d
--- /dev/null
+++ b/docs/LICENSE.txt
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2023-present, Shopify Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/docs/README_md.html b/docs/README_md.html
new file mode 100644
index 00000000..ae9154de
--- /dev/null
+++ b/docs/README_md.html
@@ -0,0 +1,177 @@
+
+
+
+
+
+
+ RDoc Documentation
+
+
+
+
+
+
+
+
+
+
If you haven’t already done so, you’ll need to first set up Ruby LSP.
+
+
As of v0.3.0, Ruby LSP will automatically include the Ruby LSP Rails addon in its custom bundle when a Rails app is detected. There is no need to add the gem to your bundle.
LSP tooling is typically based on static analysis, but ruby-lsp-rails actually communicates with your Rails app for some features.
+
+
When Ruby LSP Rails starts, it spawns a rails runner instance which runs {server.rb}. The addon communicates with this process over a pipe (i.e. stdin and stdout) to fetch runtime information about the application.
+
+
When extension is stopped (e.g. by quitting the editor), the server instance is shut down.
+
+
+
[!NOTE] Prior to v0.3.0, ruby-lsp-rails used a different approach which involved mounting a Rack application within the Rails app. That approach was brittle and susceptible to the application’s configuration, such as routing and middleware.
NOTE: We should avoid printing to stderr since it causes problems. We never read the standard error pipe from the client, so it will become full and eventually hang or crash. Instead, return a response with an error key.
# File lib/ruby_lsp/ruby_lsp_rails/addon.rb, line 24
+definitialize
+ super
+
+ # We first initialize the client as a NullClient, so that we can start the server in a background thread. Until
+ # the real client is initialized, features that depend on it will not be blocked by using the NullClient
+ @client = T.let(NullClient.new, RunnerClient)
+end
+
+
+
+
+
+
+
+
+
+
Public Instance Methods
+
+
+
+
+ activate(global_state, message_queue)
+
+
+
+
+
+
+
+
+
+
+
# File lib/ruby_lsp/ruby_lsp_rails/addon.rb, line 33
+defactivate(global_state, message_queue)
+ @global_state = T.let(global_state, T.nilable(RubyLsp::GlobalState))
+ $stderr.puts("Activating Ruby LSP Rails addon v#{VERSION}")
+ # Start booting the real client in a background thread. Until this completes, the client will be a NullClient
+ Thread.new { @client = RunnerClient.create_client }
+ register_additional_file_watchers(global_state:global_state, message_queue:message_queue)
+
+ T.must(@global_state).index.register_enhancement(IndexingEnhancement.new)
+end
Click on the action’s Code Lens to jump to its declaration in the routes.
+
+
+
Note: This depends on a support for the rubyLsp.openFile command. For the VS Code extension this is built-in, but for other editors this may require some custom configuration.
+
+
The code lens request informs the editor of runnable commands such as tests. It’s available for tests which inherit from ActiveSupport::TestCase or one of its descendants, such as ActionDispatch::IntegrationTest.
For the following code, Code Lenses will be added above the class definition above each test method.
+
+
Run
+classHelloTest<ActiveSupport::TestCase# <- Will show code lenses above for running or debugging the whole test
+ test"outputs hello"do# <- Will show code lenses above for running or debugging this test
+ # ...
+ end
+
+ test"outputs goodbye"do# <- Will show code lenses above for running or debugging this test
+ # ...
+ end
+end
+
# File lib/ruby_lsp/ruby_lsp_rails/code_lens.rb, line 137
+defon_class_node_enter(node)
+ class_name = node.constant_path.slice
+ superclass_name = node.superclass&.slice
+
+ ifclass_name.end_with?("Test")
+ command = "#{test_command} #{@path}"
+ add_test_code_lens(node, name:class_name, command:command, kind::group)
+ @group_id_stack.push(@group_id)
+ @group_id+=1
+ end
+
+ ifsuperclass_name&.start_with?("ActiveRecord::Migration")
+ command = "#{migrate_command} VERSION=#{migration_version}"
+ add_migrate_code_lens(node, name:class_name, command:command)
+ end
+
+ # We need to use a stack because someone could define a nested class
+ # inside a controller. When we exit that nested class declaration, we are
+ # back in a controller context. This part is used in other places in the LSP
+ @constant_name_stack<< [class_name, superclass_name]
+end
The document symbol request allows users to navigate between associations, validations, callbacks and ActiveSupport test cases with VS Code’s “Go to Symbol” feature.
# File lib/ruby_lsp/ruby_lsp_rails/runner_client.rb, line 14
+ defcreate_client
+ ifFile.exist?("bin/rails")
+ new
+ else
+ $stderr.puts(<<~MSG)
+ Ruby LSP Rails failed to locate bin/rails in the current directory: #{Dir.pwd}"
+ MSG
+ $stderr.puts("Server dependent features will not be available")
+ NullClient.new
+ end
+ rescueErrno::ENOENT, StandardError=>e# rubocop:disable Lint/ShadowedException
+ $stderr.puts("Ruby LSP Rails failed to initialize server: #{e.message}\n#{e.backtrace&.join("\n")}")
+ $stderr.puts("Server dependent features will not be available")
+ NullClient.new
+ end
+
+
+
+
+ new()
+
+
+
+
+
+
+
+
+
+
+
# File lib/ruby_lsp/ruby_lsp_rails/runner_client.rb, line 43
+definitialize
+ @mutex = T.let(Mutex.new, Mutex)
+ # Spring needs a Process session ID. It uses this ID to "attach" itself to the parent process, so that when the
+ # parent ends, the spring process ends as well. If this is not set, Spring will throw an error while trying to
+ # set its own session ID
+ begin
+ Process.setpgrp
+ Process.setsid
+ rescueErrno::EPERM
+ # If we can't set the session ID, continue
+ rescueNotImplementedError
+ # setpgrp() may be unimplemented on some platform
+ # https://github.com/Shopify/ruby-lsp-rails/issues/348
+ end
+
+ stdin, stdout, stderr, wait_thread = Bundler.with_original_envdo
+ Open3.popen3("bundle", "exec", "rails", "runner", "#{__dir__}/server.rb", "start")
+ end
+
+ @stdin = T.let(stdin, IO)
+ @stdout = T.let(stdout, IO)
+ @stderr = T.let(stderr, IO)
+ @wait_thread = T.let(wait_thread, Process::Waiter)
+ @stdin.binmode# for Windows compatibility
+ @stdout.binmode# for Windows compatibility
+
+ $stderr.puts("Ruby LSP Rails booting server")
+ count = 0
+
+ begin
+ count+=1
+ initialize_response = T.must(read_response)
+ @rails_root = T.let(initialize_response[:root], String)
+ rescueEmptyMessageError
+ $stderr.puts("Ruby LSP Rails is retrying initialize (#{count})")
+ retryifcount<MAX_RETRIES
+ end
+
+ $stderr.puts("Finished booting Ruby LSP Rails server")
+
+ unlessENV["RAILS_ENV"] =="test"
+ at_exitdo
+ if@wait_thread.alive?
+ $stderr.puts("Ruby LSP Rails is force killing the server")
+ sleep(0.5) # give the server a bit of time if we already issued a shutdown notification
+ force_kill
+ end
+ end
+ end
+rescueErrno::EPIPE, IncompleteMessageError
+ raiseInitializationError, @stderr.read
+end
# File lib/ruby_lsp/ruby_lsp_rails/runner_client.rb, line 97
+defmodel(name)
+ make_request("model", name:name)
+rescueIncompleteMessageError
+ $stderr.puts("Ruby LSP Rails failed to get model information: #{@stderr.read}")
+ nil
+end
+
+
+
+
+ route(controller:, action:)
+
+
+
+
+
+
+
+
+
+
+
# File lib/ruby_lsp/ruby_lsp_rails/runner_client.rb, line 129
+defroute(controller:, action:)
+ make_request("route_info", controller:controller, action:action)
+rescueIncompleteMessageError
+ $stderr.puts("Ruby LSP Rails failed to get route information: #{@stderr.read}")
+ nil
+end
+
+
+
+
+ route_location(name)
+
+
+
+
+
+
+
+
+
+
+
# File lib/ruby_lsp/ruby_lsp_rails/runner_client.rb, line 121
+defroute_location(name)
+ make_request("route_location", name:name)
+rescueIncompleteMessageError
+ $stderr.puts("Ruby LSP Rails failed to get route location: #{@stderr.read}")
+ nil
+end
+
+
+
+
+ shutdown()
+
+
+
+
+
+
+
+
+
+
+
# File lib/ruby_lsp/ruby_lsp_rails/runner_client.rb, line 146
+defshutdown
+ $stderr.puts("Ruby LSP Rails shutting down server")
+ send_message("shutdown")
+ sleep(0.5) # give the server a bit of time to shutdown
+ [@stdin, @stdout, @stderr].each(&:close)
+rescueIOError
+ # The server connection may have died
+ force_kill
+end
# File lib/ruby_lsp/ruby_lsp_rails/support/location_builder.rb, line 12
+defline_location_from_s(location_string)
+ *file_parts, line = location_string.split(":")
+
+ raiseArgumentError, "Invalid location string given"unlessfile_parts
+
+ # On Windows, file paths will look something like `C:/path/to/file.rb:123`. Only the last colon is the line
+ # number and all other parts compose the file path
+ file_path = file_parts.join(":")
+
+ Interface::Location.new(
+ uri:URI::Generic.from_path(path:file_path).to_s,
+ range:Interface::Range.new(
+ start:Interface::Position.new(line:Integer(line) -1, character:0),
+ end:Interface::Position.new(line:Integer(line) -1, character:0),
+ ),
+ )
+end
If you haven’t already done so, you’ll need to first set up Ruby LSP.
+
+
As of v0.3.0, Ruby LSP will automatically include the Ruby LSP Rails addon in its custom bundle when a Rails app is detected. There is no need to add the gem to your bundle.
LSP tooling is typically based on static analysis, but ruby-lsp-rails actually communicates with your Rails app for some features.
+
+
When Ruby LSP Rails starts, it spawns a rails runner instance which runs {server.rb}. The addon communicates with this process over a pipe (i.e. stdin and stdout) to fetch runtime information about the application.
+
+
When extension is stopped (e.g. by quitting the editor), the server instance is shut down.
+
+
+
[!NOTE] Prior to v0.3.0, ruby-lsp-rails used a different approach which involved mounting a Rack application within the Rails app. That approach was brittle and susceptible to the application’s configuration, such as routing and middleware.
In the interest of fostering an open and welcoming environment, …\n"},"contributing":{"title":"CONTRIBUTING","namespace":"","path":"CONTRIBUTING_md.html","snippet":"
CONTRIBUTING\n
Bug reports and pull requests are welcome on GitHub at github.com/Shopify/ruby-lsp-rails …\n"},"readme":{"title":"README","namespace":"","path":"README_md.html","snippet":"
This feature adds Code Lens features for Rails applications.\n
For Active …\n"},"rubylsp::rails::definition":{"title":"RubyLsp::Rails::Definition","namespace":"","path":"RubyLsp/Rails/Definition.html","snippet":"
\n
The definition request jumps to the\ndefinition of the symbol under the ...\n"},"rubylsp::rails::documentsymbol":{"title":"RubyLsp::Rails::DocumentSymbol","namespace":"","path":"RubyLsp/Rails/DocumentSymbol.html","snippet":"
\n
The document symbol\nrequest allows users to navigate between associations, ...\n"},"rubylsp::rails::hover":{"title":"RubyLsp::Rails::Hover","namespace":"","path":"RubyLsp/Rails/Hover.html","snippet":"
\n
Augment hover with\ninformation about a model.\n
Example\n"},"rubylsp::rails::indexingenhancement":{"title":"RubyLsp::Rails::IndexingEnhancement","namespace":"","path":"RubyLsp/Rails/IndexingEnhancement.html","snippet":""},"rubylsp::rails::railtie":{"title":"RubyLsp::Rails::Railtie","namespace":"","path":"RubyLsp/Rails/Railtie.html","snippet":""},"rubylsp::rails::runnerclient":{"title":"RubyLsp::Rails::RunnerClient","namespace":"","path":"RubyLsp/Rails/RunnerClient.html","snippet":""},"rubylsp::rails::runnerclient::emptymessageerror":{"title":"RubyLsp::Rails::RunnerClient::EmptyMessageError","namespace":"","path":"RubyLsp/Rails/RunnerClient/EmptyMessageError.html","snippet":""},"rubylsp::rails::runnerclient::incompletemessageerror":{"title":"RubyLsp::Rails::RunnerClient::IncompleteMessageError","namespace":"","path":"RubyLsp/Rails/RunnerClient/IncompleteMessageError.html","snippet":""},"rubylsp::rails::runnerclient::initializationerror":{"title":"RubyLsp::Rails::RunnerClient::InitializationError","namespace":"","path":"RubyLsp/Rails/RunnerClient/InitializationError.html","snippet":""},"rubylsp::rails::runnerclient::nullclient":{"title":"RubyLsp::Rails::RunnerClient::NullClient","namespace":"","path":"RubyLsp/Rails/RunnerClient/NullClient.html","snippet":""},"rubylsp::rails::server":{"title":"RubyLsp::Rails::Server","namespace":"","path":"RubyLsp/Rails/Server.html","snippet":""},"rubylsp::rails::support":{"title":"RubyLsp::Rails::Support","namespace":"","path":"RubyLsp/Rails/Support.html","snippet":""},"rubylsp::rails::support::associations":{"title":"RubyLsp::Rails::Support::Associations","namespace":"","path":"RubyLsp/Rails/Support/Associations.html","snippet":""},"rubylsp::rails::support::callbacks":{"title":"RubyLsp::Rails::Support::Callbacks","namespace":"","path":"RubyLsp/Rails/Support/Callbacks.html","snippet":""},"rubylsp::rails::support::locationbuilder":{"title":"RubyLsp::Rails::Support::LocationBuilder","namespace":"","path":"RubyLsp/Rails/Support/LocationBuilder.html","snippet":""},"rubylsp::rails::support::railsdocumentclient":{"title":"RubyLsp::Rails::Support::RailsDocumentClient","namespace":"","path":"RubyLsp/Rails/Support/RailsDocumentClient.html","snippet":""}}
\ No newline at end of file
diff --git a/docs/js/search_index.js.gz b/docs/js/search_index.js.gz
new file mode 100644
index 00000000..298fe81f
Binary files /dev/null and b/docs/js/search_index.js.gz differ
diff --git a/docs/js/snapper.js b/docs/js/snapper.js
new file mode 100644
index 00000000..05f2b9c9
--- /dev/null
+++ b/docs/js/snapper.js
@@ -0,0 +1,119 @@
+"use strict";
+
+let searchModal;
+let searchModalContent;
+let sideSearchInput;
+let searchInput;
+let searchResults;
+let mainContainer;
+
+function hideSearchModal() {
+ searchModal.style.display = "none";
+ mainContainer.style.filter = "none";
+}
+
+function showSearchModal() {
+ searchModal.style.display = "flex";
+ mainContainer.style.filter = "blur(8px)";
+
+ if (searchResults.innerHTML === "") {
+ // Populate the search modal with the first 10 items to start
+ const keys = Object.keys(searchIndex).slice(0, 10);
+ const initialEntries = keys.map((key) => {
+ return searchIndex[key];
+ });
+ searchResults.innerHTML = htmlForResults(initialEntries);
+ }
+
+ searchInput.value = "";
+ searchInput.focus();
+}
+
+function htmlForResults(results) {
+ // The solo closing p tag is intentional. The snippet is HTML and includes only the opening of the tag
+ return results.map((result) => {
+ let name = result.title;
+
+ if (result.namespace) {
+ name += ` (${result.namespace})`;
+ }
+ const escapedPath = result.path.replace(/[&<>"`']/g, (c) => `${c.charCodeAt(0)};`);
+
+ return `