ruby-lsp-rbs_rails 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +1 -0
  3. data/.vscode/extensions.json +7 -0
  4. data/.vscode/settings.json +34 -0
  5. data/CODE_OF_CONDUCT.md +132 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +41 -0
  8. data/Rakefile +27 -0
  9. data/Steepfile +9 -0
  10. data/lib/ruby_lsp/rbs_rails/addon.rb +206 -0
  11. data/lib/ruby_lsp/rbs_rails/file_writer.rb +30 -0
  12. data/lib/ruby_lsp/rbs_rails/logger.rb +22 -0
  13. data/lib/ruby_lsp/rbs_rails/version.rb +7 -0
  14. data/lib/ruby_lsp/rbs_rails.rb +10 -0
  15. data/rbs_collection.lock.yaml +284 -0
  16. data/rbs_collection.yaml +41 -0
  17. data/sig/gems/language_server-protocol.rbs +11 -0
  18. data/sig/gems/ruby-lsp.rbs +15 -0
  19. data/sig/ruby_lsp/rbs_rails/addon.rbs +63 -0
  20. data/sig/ruby_lsp/rbs_rails/file_writer.rbs +18 -0
  21. data/sig/ruby_lsp/rbs_rails/logger.rbs +15 -0
  22. data/sig/ruby_lsp/rbs_rails/version.rbs +7 -0
  23. data/sig/ruby_lsp/rbs_rails.rbs +8 -0
  24. data/spec/ruby_lsp/rbs_rails/addon_spec.rb +205 -0
  25. data/spec/ruby_lsp/rbs_rails/file_writer_spec.rb +59 -0
  26. data/spec/ruby_lsp/rbs_rails/logger_spec.rb +20 -0
  27. data/spec/spec_helper.rb +98 -0
  28. data/spec/test-app/.gitattributes +9 -0
  29. data/spec/test-app/.gitignore +34 -0
  30. data/spec/test-app/.ruby-version +1 -0
  31. data/spec/test-app/Gemfile +18 -0
  32. data/spec/test-app/Gemfile.lock +234 -0
  33. data/spec/test-app/README.md +24 -0
  34. data/spec/test-app/Rakefile +6 -0
  35. data/spec/test-app/app/assets/images/.keep +0 -0
  36. data/spec/test-app/app/assets/stylesheets/application.css +10 -0
  37. data/spec/test-app/app/controllers/application_controller.rb +4 -0
  38. data/spec/test-app/app/controllers/concerns/.keep +0 -0
  39. data/spec/test-app/app/helpers/application_helper.rb +2 -0
  40. data/spec/test-app/app/models/application_record.rb +3 -0
  41. data/spec/test-app/app/models/blog.rb +3 -0
  42. data/spec/test-app/app/models/concerns/.keep +0 -0
  43. data/spec/test-app/app/models/user.rb +2 -0
  44. data/spec/test-app/app/views/layouts/application.html.erb +27 -0
  45. data/spec/test-app/app/views/pwa/manifest.json.erb +22 -0
  46. data/spec/test-app/app/views/pwa/service-worker.js +26 -0
  47. data/spec/test-app/bin/dev +2 -0
  48. data/spec/test-app/bin/rails +4 -0
  49. data/spec/test-app/bin/rake +4 -0
  50. data/spec/test-app/bin/setup +34 -0
  51. data/spec/test-app/config/application.rb +42 -0
  52. data/spec/test-app/config/boot.rb +3 -0
  53. data/spec/test-app/config/credentials.yml.enc +1 -0
  54. data/spec/test-app/config/database.yml +32 -0
  55. data/spec/test-app/config/environment.rb +5 -0
  56. data/spec/test-app/config/environments/development.rb +51 -0
  57. data/spec/test-app/config/environments/production.rb +67 -0
  58. data/spec/test-app/config/environments/test.rb +42 -0
  59. data/spec/test-app/config/initializers/assets.rb +7 -0
  60. data/spec/test-app/config/initializers/content_security_policy.rb +25 -0
  61. data/spec/test-app/config/initializers/filter_parameter_logging.rb +8 -0
  62. data/spec/test-app/config/initializers/inflections.rb +16 -0
  63. data/spec/test-app/config/locales/en.yml +31 -0
  64. data/spec/test-app/config/puma.rb +38 -0
  65. data/spec/test-app/config/routes.rb +14 -0
  66. data/spec/test-app/config.ru +6 -0
  67. data/spec/test-app/db/migrate/20250907043524_create_users.rb +10 -0
  68. data/spec/test-app/db/migrate/20250907175033_create_blogs.rb +10 -0
  69. data/spec/test-app/db/schema.rb +30 -0
  70. data/spec/test-app/db/seeds.rb +9 -0
  71. data/spec/test-app/lib/tasks/.keep +0 -0
  72. data/spec/test-app/log/.keep +0 -0
  73. data/spec/test-app/public/400.html +114 -0
  74. data/spec/test-app/public/404.html +114 -0
  75. data/spec/test-app/public/406-unsupported-browser.html +114 -0
  76. data/spec/test-app/public/422.html +114 -0
  77. data/spec/test-app/public/500.html +114 -0
  78. data/spec/test-app/public/icon.png +0 -0
  79. data/spec/test-app/public/icon.svg +3 -0
  80. data/spec/test-app/public/robots.txt +1 -0
  81. data/spec/test-app/script/.keep +0 -0
  82. data/spec/test-app/storage/.keep +0 -0
  83. data/spec/test-app/test/controllers/.keep +0 -0
  84. data/spec/test-app/test/fixtures/blogs.yml +9 -0
  85. data/spec/test-app/test/fixtures/files/.keep +0 -0
  86. data/spec/test-app/test/fixtures/users.yml +9 -0
  87. data/spec/test-app/test/helpers/.keep +0 -0
  88. data/spec/test-app/test/integration/.keep +0 -0
  89. data/spec/test-app/test/models/.keep +0 -0
  90. data/spec/test-app/test/models/blog_test.rb +7 -0
  91. data/spec/test-app/test/models/user_test.rb +7 -0
  92. data/spec/test-app/test/test_helper.rb +15 -0
  93. data/spec/test-app/tmp/.keep +0 -0
  94. data/spec/test-app/tmp/pids/.keep +0 -0
  95. data/spec/test-app/tmp/storage/.keep +0 -0
  96. data/spec/test-app/vendor/.keep +0 -0
  97. metadata +211 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0a6807be8eafef2e7954f9dece7d407f145587f492b3479f483abf5123c82fcd
4
+ data.tar.gz: 655631f5c84a05809618caea098372b31ef66bafe3438b397a5196857e637c4b
5
+ SHA512:
6
+ metadata.gz: cf007dab5c26385442747ad0ffcfc5ba9de143faa4f40b8ef2408441f9d8283b2547016c2ddc66325c9608126120d58e94af24b58981791b7ea3d57d2a203a50
7
+ data.tar.gz: 8ce1e341a731b7bccfb844e269b29b44bb2425e81a18079e8c1826163c7e663690d0237ad7891602a6c824d2beb861ba26bd38e345427849624bd3818bfb27b7
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ {
2
+ "recommendations": [
3
+ "shopify.ruby-lsp",
4
+ "soutaro.steep-vscode",
5
+ "tk0miya.rbs-helper"
6
+ ]
7
+ }
@@ -0,0 +1,34 @@
1
+ {
2
+ "[ruby]": {
3
+ "editor.defaultFormatter": "Shopify.ruby-lsp"
4
+ },
5
+ "cSpell.words": [
6
+ "actioncable",
7
+ "actionmailer",
8
+ "actiontext",
9
+ "activejob",
10
+ "activemodel",
11
+ "activerecord",
12
+ "activestorage",
13
+ "autoloaders",
14
+ "bindir",
15
+ "fnmatch",
16
+ "forwardable",
17
+ "generatable",
18
+ "Isig",
19
+ "KOMIYA",
20
+ "lsp",
21
+ "mkpath",
22
+ "pocke",
23
+ "popen",
24
+ "readlines",
25
+ "rmtree",
26
+ "rubocop",
27
+ "rubygems",
28
+ "Takeshi",
29
+ "tk0miya",
30
+ "utime"
31
+ ],
32
+ "rbs-helper.rbs-inline-on-save": true,
33
+ "rbs-helper.rbs-inline-signature-directory": "sig/"
34
+ }
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Takeshi KOMIYA
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Ruby LSP addon for RBS Rails
2
+
3
+ ruby-lsp-rbs_rails is a Ruby LSP addon for RBS Rails that generates RBS definitions on the fly for Rails applications.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ ```ruby
10
+ # Gemfile
11
+ group :development do
12
+ gem 'ruby-lsp-rbs_rails', require: false
13
+
14
+ gem 'rbs_rails', github: 'pocke/rbs_rails' # 0.13 (unreleased) or later is required
15
+ end
16
+ ```
17
+
18
+ After running `bundle install`, restart your Ruby LSP.
19
+
20
+ ## Features
21
+
22
+ * Generate RBS definitions for models on saving a file (a.k.a. `app/models/**/*.rb`)
23
+ * Generate RBS definitions for path helpers on saving a file (a.k.a. `config/routes.rb`)
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and submit a pull request to the repo. It will be released to [rubygems.org](https://rubygems.org) after the PR merged.
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tk0miya/ruby-lsp-rbs_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/tk0miya/ruby-lsp-rbs_rails/blob/main/CODE_OF_CONDUCT.md).
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
38
+
39
+ ## Code of Conduct
40
+
41
+ Everyone interacting in the Ruby::Lsp::RbsRails project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tk0miya/ruby-lsp-rbs_rails/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: %i[rubocop rbs:all]
9
+
10
+ namespace :rbs do
11
+ task all: %i[install check validate]
12
+
13
+ desc "Install RBS signatures"
14
+ task :install do
15
+ sh "bundle exec rbs collection install --frozen"
16
+ end
17
+
18
+ desc "Type check with Steep"
19
+ task :check do
20
+ sh "bundle exec steep check"
21
+ end
22
+
23
+ desc "Validate RBS files"
24
+ task :validate do
25
+ sh "bundle exec rbs -Isig validate"
26
+ end
27
+ end
data/Steepfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # D = Steep::Diagnostic
4
+
5
+ target :lib do
6
+ signature "sig"
7
+
8
+ check "lib"
9
+ end
@@ -0,0 +1,206 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "language_server-protocol"
4
+ require "ruby_lsp/addon"
5
+
6
+ require_relative "file_writer"
7
+ require_relative "logger"
8
+
9
+ module RubyLsp
10
+ module RbsRails
11
+ class Addon < ::RubyLsp::Addon
12
+ include LanguageServer::Protocol::Constant
13
+
14
+ attr_reader :global_state #: GlobalState
15
+ attr_reader :logger #: Logger
16
+
17
+ # @rbs global_state: GlobalState
18
+ # @rbs message_queue: Thread::Queue
19
+ def activate(global_state, message_queue) #: void
20
+ @global_state = global_state
21
+ @logger = Logger.new(message_queue)
22
+
23
+ load_application
24
+ load_rbs_rails_config
25
+ rescue LoadError
26
+ logger.info("Rails application not found. Skip to activate rbs_rails addon.")
27
+ end
28
+
29
+ def deactivate #: void
30
+ end
31
+
32
+ def name #: String
33
+ "ruby-lsp-rbs_rails"
34
+ end
35
+
36
+ def version #: String
37
+ VERSION
38
+ end
39
+
40
+ # @rbs changes: Array[{ uri: String, type: Integer }]
41
+ def workspace_did_change_watched_files(changes) #: void
42
+ return unless defined?(::Rails)
43
+
44
+ ::Rails.application.reloader.wrap do
45
+ changes.each do |change|
46
+ case change[:type]
47
+ when FileChangeType::CREATED, FileChangeType::CHANGED
48
+ generate_signature(change[:uri])
49
+ when FileChangeType::DELETED
50
+ delete_signature(change[:uri])
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ # @rbs @workspace_path: Pathname?
59
+
60
+ def workspace_path #: Pathname
61
+ @workspace_path ||= Pathname.new(global_state.workspace_path)
62
+ end
63
+
64
+ # Load Rails application and enable reloading
65
+ def load_application #: void
66
+ require_relative workspace_path.join("config/application").to_s
67
+
68
+ install_hooks
69
+
70
+ ::Rails.application.initialize! unless ::Rails.application.initialized?
71
+ ::Rails.env = "development"
72
+ ::Rails.autoloaders.main.enable_reloading
73
+ end
74
+
75
+ def install_hooks #: void
76
+ require "rbs_rails/active_record/enum"
77
+ end
78
+
79
+ def load_rbs_rails_config #: void
80
+ # Load rbs_rails lazily (after install hooks)
81
+ require "rbs_rails"
82
+ require "rbs_rails/cli/configuration"
83
+
84
+ if workspace_path.join(".rbs_rails.rb").exist?
85
+ load workspace_path.join(".rbs_rails.rb").to_s
86
+ elsif workspace_path.join("config/rbs_rails.rb").exist?
87
+ load workspace_path.join("config/rbs_rails.rb").to_s
88
+ end
89
+ end
90
+
91
+ def config #: ::RbsRails::CLI::Configuration
92
+ ::RbsRails::CLI::Configuration.instance
93
+ end
94
+
95
+ # @rbs uri: String
96
+ def generate_signature(uri) #: void
97
+ path = uri_to_path(uri)
98
+ return unless path
99
+
100
+ case path.to_s
101
+ when "db/schema.rb"
102
+ generate_all_model_signatures
103
+ when %r{^config/(routes\.rb|routes/.*\.rb)$}
104
+ generate_path_helpers_signature
105
+ else
106
+ klass = constantize(path)
107
+ return unless klass
108
+
109
+ generate_signature0(klass)
110
+ end
111
+ rescue StandardError => e
112
+ logger.info("Failed to generate signature for #{path}: #{e.message}")
113
+ end
114
+
115
+ def generate_all_model_signatures #: void
116
+ ::Rails.application.eager_load!
117
+ ::ActiveRecord::Base.descendants.each do |klass|
118
+ generate_signature0(klass)
119
+ end
120
+ end
121
+
122
+ def generate_path_helpers_signature #: void
123
+ rbs_path = config.signature_root_dir / "path_helpers.rbs"
124
+ rbs_path.dirname.mkpath
125
+
126
+ sig = ::RbsRails::PathHelpers.generate
127
+ FileWriter.new(rbs_path).write sig
128
+ logger.info("Updated RBS signature: #{rbs_path}")
129
+ end
130
+
131
+ # @rbs klass: Class
132
+ def generate_signature0(klass) #: void
133
+ return unless klass < ::ActiveRecord::Base
134
+ return if config.ignored_model?(klass)
135
+ return unless ::RbsRails::ActiveRecord.generatable?(klass)
136
+
137
+ rbs_path = get_rbs_path_for_model(klass)
138
+ rbs_path.dirname.mkpath
139
+
140
+ sig = ::RbsRails::ActiveRecord.class_to_rbs(klass)
141
+ FileWriter.new(rbs_path).write sig
142
+ logger.info("Updated RBS signature: #{rbs_path}")
143
+ end
144
+
145
+ # @rbs uri: String
146
+ def delete_signature(uri) #: void
147
+ path = uri_to_path(uri)
148
+ return unless path
149
+ return unless path.extname == ".rb"
150
+
151
+ rbs_path = config.signature_root_dir / path.sub_ext(".rbs")
152
+ return unless rbs_path.exist?
153
+
154
+ rbs_path.delete
155
+ logger.info("Deleted RBS signature: #{rbs_path}")
156
+ rescue StandardError => e
157
+ logger.info("Failed to delete signature for #{path}: #{e.message}")
158
+ end
159
+
160
+ # @rbs uri: String
161
+ def uri_to_path(uri) #: Pathname?
162
+ path = uri.delete_prefix("file://")
163
+
164
+ # Ignore if the given path is not under Rails.root
165
+ return nil unless path.start_with?(::Rails.root.to_s + File::SEPARATOR)
166
+
167
+ Pathname.new(path).relative_path_from(::Rails.root)
168
+ end
169
+
170
+ # @rbs path: Pathname
171
+ def constantize(path) #: Class?
172
+ # If the specified file is placed under autoload_paths
173
+ ::Rails.application.config.autoload_paths.each do |autoload_path|
174
+ # @type var autoload_path: String
175
+ next unless path.to_s.start_with?(autoload_path + File::SEPARATOR)
176
+
177
+ relative_path = path.relative_path_from(autoload_path)
178
+ return relative_path.to_s.chomp(".rb").classify.constantize
179
+ end
180
+
181
+ # If not, the file must be placed under app/*/ directories
182
+ relative_path = path.to_s.sub(%r{^app/.*?/}, "")
183
+ relative_path.chomp(".rb").classify.constantize
184
+ rescue NameError
185
+ nil
186
+ end
187
+
188
+ # @rbs klass: Class
189
+ def get_rbs_path_for_model(klass) #: Pathname
190
+ path, _line = begin
191
+ Object.const_source_location(klass.name)
192
+ rescue StandardError
193
+ nil
194
+ end
195
+
196
+ rbs_path = if path && Pathname.new(path).fnmatch?(::Rails.root.join("**").to_s)
197
+ Pathname.new(path).relative_path_from(::Rails.root).sub_ext(".rbs")
198
+ else
199
+ "app/models/#{klass.name.underscore}.rbs"
200
+ end
201
+
202
+ config.signature_root_dir / rbs_path
203
+ end
204
+ end
205
+ end
206
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLsp
4
+ module RbsRails
5
+ # To avoid unnecessary type reloading by type checkers and other utilities,
6
+ # FileWriter modifies the target file only if its content has been changed.
7
+ #
8
+ # See https://github.com/pocke/rbs_rails/pull/346
9
+ class FileWriter
10
+ attr_reader :path #: Pathname
11
+
12
+ # @rbs path: Pathname
13
+ def initialize(path) #: void
14
+ @path = path
15
+ end
16
+
17
+ def write(content) #: void
18
+ original_content = begin
19
+ path.read
20
+ rescue StandardError
21
+ nil
22
+ end
23
+
24
+ return unless original_content != content
25
+
26
+ path.write(content)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "language_server/protocol"
4
+ require "ruby_lsp/utils"
5
+
6
+ module RubyLsp
7
+ module RbsRails
8
+ class Logger
9
+ attr_reader :message_queue #: Thread::Queue
10
+
11
+ # @rbs message_queue: Thread::Queue
12
+ def initialize(message_queue) #: void
13
+ @message_queue = message_queue
14
+ end
15
+
16
+ # @rbs message: String
17
+ def info(message) #: void
18
+ message_queue << Notification.window_log_message("rbs_rails: #{message}")
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLsp
4
+ module RbsRails
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rbs_rails/version"
4
+
5
+ module RubyLsp
6
+ module RbsRails
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
10
+ end