ruby-lsp-i18n 0.3.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 (68) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +13 -0
  3. data/.ruby-version +1 -0
  4. data/.vscode/settings.json +12 -0
  5. data/CHANGELOG.md +36 -0
  6. data/CODE_OF_CONDUCT.md +84 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +73 -0
  9. data/Rakefile +16 -0
  10. data/es.yml +4 -0
  11. data/example/app/example.rb +14 -0
  12. data/example/config/locales/a/es.yml +4 -0
  13. data/example/config/locales/es.yml +6 -0
  14. data/lib/ruby_lsp/ruby_lsp_i18n/addon.rb +138 -0
  15. data/lib/ruby_lsp/ruby_lsp_i18n/i18n_index.rb +130 -0
  16. data/lib/ruby_lsp/ruby_lsp_i18n/listeners/completion.rb +65 -0
  17. data/lib/ruby_lsp/ruby_lsp_i18n/listeners/inlay_hints.rb +118 -0
  18. data/lib/ruby_lsp/ruby_lsp_i18n/prefix_tree.rb +158 -0
  19. data/lib/ruby_lsp/ruby_lsp_i18n/requests/inlay_hints.rb +36 -0
  20. data/lib/ruby_lsp_i18n/version.rb +7 -0
  21. data/lib/ruby_lsp_i18n.rb +9 -0
  22. data/media/demo.gif +0 -0
  23. data/ruby-lsp-i18n.gemspec +35 -0
  24. data/sorbet/config +4 -0
  25. data/sorbet/rbi/annotations/.gitattributes +1 -0
  26. data/sorbet/rbi/annotations/minitest.rbi +119 -0
  27. data/sorbet/rbi/annotations/rainbow.rbi +269 -0
  28. data/sorbet/rbi/dsl/.gitattributes +1 -0
  29. data/sorbet/rbi/gems/.gitattributes +1 -0
  30. data/sorbet/rbi/gems/ast@2.4.2.rbi +585 -0
  31. data/sorbet/rbi/gems/bump@0.10.0.rbi +9 -0
  32. data/sorbet/rbi/gems/concurrent-ruby@1.3.4.rbi +406 -0
  33. data/sorbet/rbi/gems/erubi@1.13.0.rbi +150 -0
  34. data/sorbet/rbi/gems/i18n@1.14.5.rbi +2208 -0
  35. data/sorbet/rbi/gems/json@2.7.2.rbi +1562 -0
  36. data/sorbet/rbi/gems/language_server-protocol@3.17.0.3.rbi +14238 -0
  37. data/sorbet/rbi/gems/logger@1.7.0.rbi +963 -0
  38. data/sorbet/rbi/gems/minitest@5.25.1.rbi +1546 -0
  39. data/sorbet/rbi/gems/netrc@0.11.0.rbi +159 -0
  40. data/sorbet/rbi/gems/parallel@1.26.3.rbi +291 -0
  41. data/sorbet/rbi/gems/parser@3.3.4.2.rbi +5519 -0
  42. data/sorbet/rbi/gems/prism@1.2.0.rbi +39085 -0
  43. data/sorbet/rbi/gems/racc@1.8.1.rbi +162 -0
  44. data/sorbet/rbi/gems/rainbow@3.1.1.rbi +403 -0
  45. data/sorbet/rbi/gems/rake@13.2.1.rbi +3028 -0
  46. data/sorbet/rbi/gems/rbi@0.2.0.rbi +9 -0
  47. data/sorbet/rbi/gems/rbs@3.9.4.rbi +6976 -0
  48. data/sorbet/rbi/gems/regexp_parser@2.9.2.rbi +3772 -0
  49. data/sorbet/rbi/gems/rexml@3.3.6.rbi +4813 -0
  50. data/sorbet/rbi/gems/rubocop-ast@1.32.1.rbi +7079 -0
  51. data/sorbet/rbi/gems/rubocop-minitest@0.35.1.rbi +2613 -0
  52. data/sorbet/rbi/gems/rubocop-rake@0.6.0.rbi +329 -0
  53. data/sorbet/rbi/gems/rubocop-shopify@2.15.1.rbi +9 -0
  54. data/sorbet/rbi/gems/rubocop-sorbet@0.8.5.rbi +1665 -0
  55. data/sorbet/rbi/gems/rubocop@1.65.1.rbi +58100 -0
  56. data/sorbet/rbi/gems/ruby-lsp@0.24.1.rbi +7508 -0
  57. data/sorbet/rbi/gems/ruby-progressbar@1.13.0.rbi +1318 -0
  58. data/sorbet/rbi/gems/spoom@1.4.2.rbi +4932 -0
  59. data/sorbet/rbi/gems/strscan@3.1.0.rbi +9 -0
  60. data/sorbet/rbi/gems/tapioca@0.16.2.rbi +16 -0
  61. data/sorbet/rbi/gems/thor@1.3.2.rbi +4378 -0
  62. data/sorbet/rbi/gems/unicode-display_width@2.5.0.rbi +66 -0
  63. data/sorbet/rbi/gems/yard-sorbet@0.9.0.rbi +435 -0
  64. data/sorbet/rbi/gems/yard@0.9.36.rbi +18221 -0
  65. data/sorbet/rbi/shims/gems/fast_trie@0.5.1.rbi +18 -0
  66. data/sorbet/tapioca/config.yml +13 -0
  67. data/sorbet/tapioca/require.rb +6 -0
  68. metadata +113 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7f0fdc0478778d6e3c1f0a1214050b93bbf1e7fc5e88f1ca0b9600e1936d2952
4
+ data.tar.gz: 26070828190643f05f77cef165b995810eec828510ae78def86cae0d65588ad9
5
+ SHA512:
6
+ metadata.gz: 5a9da889bf6f3fdb0ef5b773c7dd64c1b210cb62ff438b314b43ee1d57778324d7ada0ab36745119b9cb32b1080d6bb1988618b9a269a3232d2343d85e0bf8ce
7
+ data.tar.gz: f198ccadd9f2e09dccb32f149e030a901a8f479042d34aac0d4301bb822bfaabb4160991d913b1b3b94721de01ef848aa13beb32c389933bceb1392c56f22404
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ inherit_gem:
2
+ rubocop-shopify: rubocop.yml
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.4
@@ -0,0 +1,12 @@
1
+ {
2
+ "cSpell.words": [
3
+ "rubocop",
4
+ "shopify"
5
+ ],
6
+ "editor.inlayHints.enabled": "offUnlessPressed",
7
+ "rubyLsp.addonSettings": {
8
+ "Ruby LSP I18n": {
9
+ "enabled": true
10
+ }
11
+ }
12
+ }
data/CHANGELOG.md ADDED
@@ -0,0 +1,36 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.3.0] - 2025-07-18
4
+
5
+ - Fix inlay hints patch thanks to [@enmy](https://github.com/enmy)
6
+ - Publish to rubygems
7
+
8
+ ## [0.2.4] - 2025-01-13
9
+
10
+ - Update ruby lsp version to 0.23.0
11
+
12
+ ## [0.2.3] - 2024-11-29
13
+
14
+ - Update ruby lsp version to 0.22.0
15
+
16
+ ## [0.2.2] - 2024-11-18
17
+
18
+ - Update ruby LSP version to 0.21.3 and change runtime dependency to version constraint using the RubyLSP version constraint API to be forward compatible with new LSP versions. Now when the LSP get a breaking change the addon is disabled instead of locking the core LSP version in the editor.
19
+
20
+ ## [0.2.1] - 2024-09-09
21
+
22
+ - Fix translation missing inlay hint in scoped I18n.t calls
23
+
24
+ ## [0.2.0] - 2024-09-09
25
+
26
+ - Added enable/disable addon vía .vscode.settings.json file using <https://github.com/Shopify/ruby-lsp/pull/2513>
27
+
28
+ ## [0.1.0] - 2024-09-03
29
+
30
+ - Added inlay hints with translation value for I18n.t("key") calls
31
+ - Added autocompletion for I18n.t("key") calls
32
+ - Added "translation-missing" for I18n.t("key") calls as inlay hint
33
+ - Added file path suggestion for I18n.t("key") missing calls
34
+ - Added tooltip with translations and file path for I18n.t("key") calls
35
+
36
+ ## [0.0.0] - 2024-08-22
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible 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.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders 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, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at dedwards@buk.cl. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 domingo2000
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,73 @@
1
+ # RubyLspI18n
2
+
3
+ The `ruby-lsp-i18n` gem provides internationalization support for Ruby Lsp.
4
+
5
+ ## Installation
6
+
7
+ Add to the following line to your application's development Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruby-lsp-i18n', git: 'https://github.com/bukhr/ruby-lsp-i18n', require: false
11
+ ```
12
+
13
+ Then run the following command to install the gem:
14
+
15
+ ```bash
16
+ bundle install
17
+ ```
18
+
19
+ ## Features
20
+
21
+ - Inlay Hints for translations
22
+ - Show value as inlay hint for translation keys
23
+ - Hover with the translation value
24
+ - Hover with the file path of the translation
25
+
26
+ - Autocompletion for translation keys
27
+ - Synchronization of yml translation files
28
+
29
+ ![Ruby LSP I18n Demo](media/demo.gif)
30
+
31
+ ### Configuration
32
+
33
+ The addon is enabled by default but can be disabled using the .vscode/settings.json file in your project.
34
+
35
+ ```json
36
+ {
37
+ "rubyLsp.addonSettings": {
38
+ "Ruby LSP I18n": {
39
+ "enabled": false
40
+ }
41
+ }
42
+ }
43
+ ```
44
+
45
+ ## Development
46
+
47
+ 1. Clone the repository
48
+ 2. Install dependencies with `bundle install`
49
+ 3. Run the tests with `bundle exec rake test`
50
+ 4. Check types with `bundle exec srb tc`
51
+ 5. Run the linter with `bundle exec rubocop`
52
+ 6. Make a PR and wait for aproval
53
+ 7. Merge the PR
54
+
55
+ To install this gem onto your local machine, run `bundle exec rake install`.
56
+
57
+ ### Updating Ruby LSP
58
+
59
+ It is important to maintain this gem up to date with the latest version of Ruby Lsp. So the users of the gem have the updated features of the core LSP. To do so update the Ruby LSP version in the `ruby-lsp-i18n.gemspec` file. Then run the tests and make sure everything is working as expected.
60
+
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/bukhr/ruby-lsp-i18n>. 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/bukhr/ruby-lsp-i18n/blob/master/CODE_OF_CONDUCT.md).
65
+
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
70
+
71
+ ## Code of Conduct
72
+
73
+ Everyone interacting in the RubyLspI18n project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bukhr/ruby-lsp-i18n/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: [:test, :rubocop]
data/es.yml ADDED
@@ -0,0 +1,4 @@
1
+ es:
2
+ a: "sadfdasdasfds"
3
+ b:
4
+ c: "hello"
@@ -0,0 +1,14 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "i18n"
5
+
6
+ I18n.t("hello.world")
7
+
8
+ I18n.t("hello.alo")
9
+
10
+ I18n.t("mi.super.llave")
11
+
12
+ I18n.t("a.b")
13
+
14
+ I18n.t("mi.super.llave")
@@ -0,0 +1,4 @@
1
+ ---
2
+ es:
3
+ a:
4
+ b: "Senkuuu ishigami"
@@ -0,0 +1,6 @@
1
+ es:
2
+ hello:
3
+ world: "Hello world!"
4
+ mi:
5
+ super:
6
+ llave: "Mi super llave"
@@ -0,0 +1,138 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "ruby_lsp/addon"
5
+ require_relative "requests/inlay_hints"
6
+ require_relative "listeners/inlay_hints"
7
+ require_relative "listeners/completion"
8
+ require_relative "i18n_index"
9
+ require_relative "../../ruby_lsp_i18n/version"
10
+
11
+ RubyLsp::Addon.depend_on_ruby_lsp!("~> 0.24.0")
12
+
13
+ module RubyLsp
14
+ module RubyLspI18n
15
+ GLOB_PATH = "**/config/locales/**/es.yml"
16
+ # This class is the entry point for the addon. It is responsible for activating and deactivating the addon
17
+ class Addon < ::RubyLsp::Addon
18
+ extend T::Sig
19
+
20
+ sig { void }
21
+ def initialize
22
+ super
23
+ @i18n_index = T.let(I18nIndex.new(language: "es"), I18nIndex)
24
+ @enabled = T.let(true, T::Boolean)
25
+ end
26
+
27
+ # Performs any activation that needs to happen once when the language server is booted)}
28
+ sig { override.params(global_state: RubyLsp::GlobalState, message_queue: Thread::Queue).void }
29
+ def activate(global_state, message_queue)
30
+ settings = global_state.settings_for_addon(name) || {}
31
+ @enabled = settings[:enabled] if settings.key?(:enabled)
32
+
33
+ return unless @enabled
34
+
35
+ files = Dir[GLOB_PATH]
36
+ files.each do |file|
37
+ @i18n_index.sync_file(file)
38
+ end
39
+
40
+ message_queue << Request.new(
41
+ id: "ruby-lsp-my-gem-file-watcher",
42
+ method: "client/registerCapability",
43
+ params: Interface::RegistrationParams.new(
44
+ registrations: [
45
+ Interface::Registration.new(
46
+ id: "workspace/didChangeWatchedFilesMyGem",
47
+ method: "workspace/didChangeWatchedFiles",
48
+ register_options: Interface::DidChangeWatchedFilesRegistrationOptions.new(
49
+ watchers: [
50
+ Interface::FileSystemWatcher.new(
51
+ glob_pattern: GLOB_PATH,
52
+ kind: Constant::WatchKind::CREATE | Constant::WatchKind::CHANGE | Constant::WatchKind::DELETE,
53
+ ),
54
+ ],
55
+ ),
56
+ ),
57
+ ],
58
+ ),
59
+ )
60
+ end
61
+
62
+ # Performs any cleanup when shutting down the server, like terminating a subprocess
63
+ sig { override.void }
64
+ def deactivate; end
65
+
66
+ sig { params(changes: T::Array[T::Hash[Symbol, T.untyped]]).void }
67
+ def workspace_did_change_watched_files(changes)
68
+ changes.each do |change|
69
+ change = Interface::FileEvent.new(uri: change[:uri], type: change[:type])
70
+ uri = T.let(change.uri, String)
71
+
72
+ next unless uri.end_with?("es.yml")
73
+
74
+ path = URI.parse(uri).path
75
+
76
+ next if path.nil?
77
+
78
+ path = path.gsub(Dir.pwd + "/", "")
79
+ @i18n_index.sync_file(path)
80
+ end
81
+ end
82
+
83
+ sig { override.returns(String) }
84
+ def version
85
+ RubyLsp::RubyLspI18n::VERSION
86
+ end
87
+
88
+ # Returns the name of the addon
89
+ sig { override.returns(String) }
90
+ def name
91
+ "Ruby LSP I18n"
92
+ end
93
+
94
+ sig do
95
+ params(
96
+ response_builder: ResponseBuilders::CollectionResponseBuilder,
97
+ dispatcher: Prism::Dispatcher,
98
+ document: T.any(RubyDocument, ERBDocument),
99
+ ).void
100
+ end
101
+ def create_inlay_hints_listener(response_builder, dispatcher, document)
102
+ return unless @enabled
103
+
104
+ InlayHints.new(@i18n_index, response_builder, dispatcher, document)
105
+ end
106
+
107
+ sig do
108
+ override.params(
109
+ response_builder: RubyLsp::ResponseBuilders::CollectionResponseBuilder,
110
+ node_context: RubyLsp::NodeContext,
111
+ dispatcher: Prism::Dispatcher,
112
+ uri: URI::Generic,
113
+ ).void
114
+ end
115
+ def create_completion_listener(response_builder, node_context, dispatcher, uri)
116
+ return unless @enabled
117
+
118
+ Completion.new(@i18n_index, response_builder, dispatcher)
119
+ end
120
+ end
121
+ end
122
+ end
123
+
124
+ # Patch the InlayHints request to support addons
125
+ module RubyLsp
126
+ class Addon
127
+ extend T::Sig
128
+ sig do
129
+ params(
130
+ response_builder: ResponseBuilders::CollectionResponseBuilder,
131
+ dispatcher: Prism::Dispatcher,
132
+ document: T.any(RubyDocument, ERBDocument),
133
+ ).void
134
+ end
135
+ def create_inlay_hints_listener(response_builder, dispatcher, document)
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,130 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "prefix_tree"
5
+ module RubyLsp
6
+ module RubyLspI18n
7
+ # The index holds a data structure that maps i18n keys
8
+ # to their values and the files they are defined in.
9
+
10
+ class Entry
11
+ extend T::Sig
12
+
13
+ sig { returns(String) }
14
+ attr_reader :value
15
+
16
+ sig { returns(String) }
17
+ attr_reader :file
18
+
19
+ sig { params(value: String, file: String).void }
20
+ def initialize(value, file)
21
+ @value = value
22
+ @file = file
23
+ end
24
+ end
25
+
26
+ class I18nIndex
27
+ extend T::Sig
28
+
29
+ sig { params(language: String).void }
30
+ def initialize(language:)
31
+ @language = language
32
+ @data = T.let({}, T::Hash[String, T::Array[Entry]])
33
+
34
+ @file_keys = T.let(
35
+ Hash.new do |hash, key|
36
+ hash[key] = []
37
+ end,
38
+ T::Hash[String, T::Array[String]],
39
+ )
40
+
41
+ @keys_tree = T.let(RubyLsp::RubyLspI18n::PrefixTree.new, RubyLsp::RubyLspI18n::PrefixTree[String])
42
+ end
43
+
44
+ sig { params(key: String, value: String, file: String).void }
45
+ def add(key, value, file)
46
+ entry = Entry.new(value, file)
47
+ @data[key] ||= []
48
+ T.must(@data[key]) << entry
49
+ T.must(@file_keys[file]) << key
50
+ @keys_tree.insert(key, key)
51
+ end
52
+
53
+ sig { params(key: String, file: String).void }
54
+ def remove(key, file)
55
+ return unless @data[key]
56
+
57
+ T.must(@data[key]).delete_if { |v| v.file == file }
58
+ T.must(@file_keys[file]).delete(key)
59
+ @keys_tree.delete(key)
60
+ end
61
+
62
+ sig { params(key: String).returns(T::Array[RubyLsp::RubyLspI18n::Entry]) }
63
+ def find(key)
64
+ datum = @data.dig(key)
65
+ datum.nil? ? [] : datum
66
+ end
67
+
68
+ sig { params(prefix: String).returns(T::Array[String]) }
69
+ def find_prefix(prefix)
70
+ @keys_tree.search(prefix)
71
+ end
72
+
73
+ sig { params(key: String, value: String, file: String).void }
74
+ def update(key, value, file)
75
+ remove(key, file)
76
+ add(key, value, file)
77
+ end
78
+
79
+ sig { params(path: String).void }
80
+ def sync_file(path)
81
+ # Clean entries from the file
82
+ current_keys = get_keys_from_file(path)
83
+ current_keys.each do |key|
84
+ remove(key, path)
85
+ end
86
+
87
+ return unless File.exist?(path)
88
+
89
+ # Load translations only if the current yaml is valid
90
+ begin
91
+ translations = YAML.load_file(path, aliases: true)
92
+ rescue Psych::SyntaxError
93
+ return
94
+ end
95
+
96
+ # If there is no translations, do nothing
97
+ return unless translations.is_a?(Hash)
98
+
99
+ # If the translations are empty, do nothing
100
+ return if translations.dig(@language).nil?
101
+
102
+ # Add entries again
103
+ translations = translations.dig(@language)
104
+ return unless translations.is_a?(Hash) # Check format of the translations
105
+
106
+ process_translations(translations, path)
107
+ end
108
+
109
+ private
110
+
111
+ sig { params(file: String).returns(T::Array[String]) }
112
+ def get_keys_from_file(file)
113
+ T.must(@file_keys[file])
114
+ end
115
+
116
+ sig { params(translations: T::Hash[String, T.untyped], file: String, prefix: T.nilable(String)).void }
117
+ def process_translations(translations, file, prefix = nil)
118
+ translations.each do |key, value|
119
+ full_key = prefix ? "#{prefix}.#{key}" : key
120
+ full_key = full_key.to_s
121
+ if value.is_a?(Hash)
122
+ process_translations(value, file, full_key)
123
+ else
124
+ update(full_key, value, file)
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,65 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RubyLsp
5
+ module RubyLspI18n
6
+ class Completion
7
+ extend T::Sig
8
+ include Requests::Support::Common
9
+
10
+ sig do
11
+ params(
12
+ i18n_index: I18nIndex,
13
+ response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::CompletionItem],
14
+ dispatcher: Prism::Dispatcher,
15
+ ).void
16
+ end
17
+ def initialize(i18n_index, response_builder, dispatcher)
18
+ @i18n_index = i18n_index
19
+ @response_builder = response_builder
20
+ @dispatcher = dispatcher
21
+
22
+ dispatcher.register(
23
+ self,
24
+ :on_call_node_enter,
25
+ )
26
+ end
27
+
28
+ sig { params(node: Prism::CallNode).void }
29
+ def on_call_node_enter(node)
30
+ return unless node.name == :t
31
+
32
+ receiver = node.receiver
33
+ return unless receiver.is_a?(Prism::ConstantReadNode)
34
+
35
+ return unless receiver.name == :I18n
36
+
37
+ arguments = node.arguments
38
+ return unless arguments
39
+ return if arguments.arguments.empty?
40
+
41
+ key_node = arguments.arguments.first
42
+ return unless key_node.is_a?(Prism::StringNode)
43
+
44
+ key = key_node.unescaped
45
+ quote = T.must(key_node.opening_loc).slice
46
+ candidates = @i18n_index.find_prefix(key)
47
+
48
+ candidates.each do |candidate|
49
+ new_text = "#{quote}#{candidate}#{quote}"
50
+ response = Interface::CompletionItem.new(
51
+ label: new_text,
52
+ detail: candidate,
53
+ documentation: "candidate",
54
+ text_edit: Interface::TextEdit.new(
55
+ range: range_from_location(key_node.location),
56
+ new_text: new_text,
57
+ ),
58
+ kind: Constant::CompletionItemKind::VALUE,
59
+ )
60
+ @response_builder << response
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end