Skip to content

Commit 15d4388

Browse files
authored
Merge pull request #104 from joker1007/add-rbs
Add simple RBS signatures for generated files
2 parents f6f03c6 + f047a44 commit 15d4388

File tree

354 files changed

+6676
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

354 files changed

+6676
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
/tmp/
1010
/node_modules
1111
/vendor
12+
/.gem_rbs_collection/

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ gemspec
88
gem "benchmark-ips"
99
gem "pry-byebug"
1010
gem "activesupport"
11+
gem "steep", require: false

Steepfile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
D = Steep::Diagnostic
2+
3+
target :lib do
4+
signature "sig"
5+
6+
check "lib" # Directory name
7+
8+
# configure_code_diagnostics(D::Ruby.default) # `default` diagnostics setting (applies by default)
9+
end
10+
11+
# target :test do
12+
# signature "sig", "sig-private"
13+
#
14+
# check "test"
15+
#
16+
# # library "pathname" # Standard libraries
17+
# end

language_server-protocol.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
1414
spec.homepage = "https://github.com/mtsmfm/language_server-protocol-ruby"
1515
spec.license = "MIT"
1616

17-
spec.files = `git ls-files -z lib LICENSE.txt README.md`.split("\x0")
17+
spec.files = `git ls-files -z lib sig LICENSE.txt README.md`.split("\x0")
1818
spec.bindir = "exe"
1919
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2020
spec.require_paths = ["lib"]

rbs_collection.lock.yaml

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
path: ".gem_rbs_collection"
3+
gems:
4+
- name: abbrev
5+
version: '0'
6+
source:
7+
type: stdlib
8+
- name: activesupport
9+
version: '7.0'
10+
source:
11+
type: git
12+
name: ruby/gem_rbs_collection
13+
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
14+
remote: https://github.com/ruby/gem_rbs_collection.git
15+
repo_dir: gems
16+
- name: ast
17+
version: '2.4'
18+
source:
19+
type: git
20+
name: ruby/gem_rbs_collection
21+
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
22+
remote: https://github.com/ruby/gem_rbs_collection.git
23+
repo_dir: gems
24+
- name: base64
25+
version: '0'
26+
source:
27+
type: stdlib
28+
- name: bigdecimal
29+
version: '0'
30+
source:
31+
type: stdlib
32+
- name: concurrent-ruby
33+
version: '1.1'
34+
source:
35+
type: git
36+
name: ruby/gem_rbs_collection
37+
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
38+
remote: https://github.com/ruby/gem_rbs_collection.git
39+
repo_dir: gems
40+
- name: connection_pool
41+
version: '2.4'
42+
source:
43+
type: git
44+
name: ruby/gem_rbs_collection
45+
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
46+
remote: https://github.com/ruby/gem_rbs_collection.git
47+
repo_dir: gems
48+
- name: csv
49+
version: '0'
50+
source:
51+
type: stdlib
52+
- name: date
53+
version: '0'
54+
source:
55+
type: stdlib
56+
- name: erb
57+
version: '0'
58+
source:
59+
type: stdlib
60+
- name: fileutils
61+
version: '0'
62+
source:
63+
type: stdlib
64+
- name: forwardable
65+
version: '0'
66+
source:
67+
type: stdlib
68+
- name: i18n
69+
version: '1.10'
70+
source:
71+
type: git
72+
name: ruby/gem_rbs_collection
73+
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
74+
remote: https://github.com/ruby/gem_rbs_collection.git
75+
repo_dir: gems
76+
- name: json
77+
version: '0'
78+
source:
79+
type: stdlib
80+
- name: listen
81+
version: '3.2'
82+
source:
83+
type: git
84+
name: ruby/gem_rbs_collection
85+
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
86+
remote: https://github.com/ruby/gem_rbs_collection.git
87+
repo_dir: gems
88+
- name: logger
89+
version: '0'
90+
source:
91+
type: stdlib
92+
- name: minitest
93+
version: '0'
94+
source:
95+
type: stdlib
96+
- name: monitor
97+
version: '0'
98+
source:
99+
type: stdlib
100+
- name: mutex_m
101+
version: '0'
102+
source:
103+
type: stdlib
104+
- name: optparse
105+
version: '0'
106+
source:
107+
type: stdlib
108+
- name: parser
109+
version: '3.2'
110+
source:
111+
type: git
112+
name: ruby/gem_rbs_collection
113+
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
114+
remote: https://github.com/ruby/gem_rbs_collection.git
115+
repo_dir: gems
116+
- name: pathname
117+
version: '0'
118+
source:
119+
type: stdlib
120+
- name: rainbow
121+
version: '3.0'
122+
source:
123+
type: git
124+
name: ruby/gem_rbs_collection
125+
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
126+
remote: https://github.com/ruby/gem_rbs_collection.git
127+
repo_dir: gems
128+
- name: rake
129+
version: '13.0'
130+
source:
131+
type: git
132+
name: ruby/gem_rbs_collection
133+
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
134+
remote: https://github.com/ruby/gem_rbs_collection.git
135+
repo_dir: gems
136+
- name: rbs
137+
version: 3.4.4
138+
source:
139+
type: rubygems
140+
- name: rdoc
141+
version: '0'
142+
source:
143+
type: stdlib
144+
- name: securerandom
145+
version: '0'
146+
source:
147+
type: stdlib
148+
- name: singleton
149+
version: '0'
150+
source:
151+
type: stdlib
152+
- name: steep
153+
version: 1.6.0
154+
source:
155+
type: rubygems
156+
- name: strscan
157+
version: '0'
158+
source:
159+
type: stdlib
160+
- name: time
161+
version: '0'
162+
source:
163+
type: stdlib
164+
- name: timeout
165+
version: '0'
166+
source:
167+
type: stdlib
168+
- name: tsort
169+
version: '0'
170+
source:
171+
type: stdlib
172+
gemfile_lock_path: Gemfile.lock

rbs_collection.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Download sources
2+
sources:
3+
- type: git
4+
name: ruby/gem_rbs_collection
5+
remote: https://github.com/ruby/gem_rbs_collection.git
6+
revision: main
7+
repo_dir: gems
8+
9+
# You can specify local directories as sources also.
10+
# - type: local
11+
# path: path/to/your/local/repository
12+
13+
# A directory to install the downloaded RBSs
14+
path: .gem_rbs_collection
15+
16+
gems:
17+
# If you want to avoid installing rbs files for gems, you can specify them here.
18+
- name: language_server-protocol
19+
ignore: true

scripts/generateFiles.ts

+116
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,40 @@ Handlebars.registerHelper("isliteral", (s: string) => {
267267
return s.match(/'.*'/) || s.match(/".*"/) || s.match(/\d+/);
268268
});
269269

270+
271+
272+
const typeMapping = new Map<string, string>([
273+
["string", "String"],
274+
["number", "Integer"],
275+
["boolean", "bool"],
276+
["null", "nil"],
277+
["undefined", "nil"],
278+
["any", "untyped"],
279+
["LSPAny", "untyped"],
280+
]);
281+
282+
const convertTypeToRbs: ((type: string) => string) = (type) => {
283+
if (type.endsWith("[]")) {
284+
return `Array[${convertTypeToRbs(type.slice(0, -2))}]`;
285+
}
286+
let rbsType = typeMapping.get(type);
287+
if (rbsType != null) {
288+
return rbsType;
289+
}
290+
291+
return "untyped";
292+
};
293+
294+
Handlebars.registerHelper("method_signature", (members: InterfaceResult["members"]) => {
295+
return members
296+
.map(
297+
(member) =>
298+
`${member.optional ? "?" : ""}${snake(member.name)}: ${convertTypeToRbs(member.type)}${member.nullable ? "?" : ""}`
299+
)
300+
.join(", ");
301+
});
302+
Handlebars.registerHelper("convertTypeToRbs", convertTypeToRbs);
303+
270304
(async () => {
271305
const data = globSync(
272306
path.join(
@@ -369,6 +403,45 @@ end
369403
);
370404
});
371405

406+
interfaces.forEach((definition) => {
407+
createFile(
408+
path.join(
409+
rootDir,
410+
"sig",
411+
"language_server",
412+
"protocol",
413+
"interface",
414+
`${snake(definition.interface.name)}.rbs`
415+
),
416+
Handlebars.compile(
417+
`
418+
module LanguageServer
419+
module Protocol
420+
module Interface
421+
{{#if definition.interface.documentation}}
422+
#
423+
{{comment definition.interface.documentation indent=6}}
424+
#
425+
{{/if}}
426+
class {{definition.interface.name}}
427+
def initialize: ({{method_signature definition.allMembers}}) -> void
428+
429+
@attributes: Hash[Symbol, untyped]
430+
attr_reader attributes: Hash[Symbol, untyped]
431+
432+
def to_hash: () -> Hash[Symbol, untyped]
433+
434+
def to_json: (*untyped) -> String
435+
end
436+
end
437+
end
438+
end
439+
`.slice(1),
440+
{ noEscape: true }
441+
)({ definition })
442+
);
443+
});
444+
372445
modules.forEach((definition) => {
373446
createFile(
374447
path.join(
@@ -412,6 +485,49 @@ end
412485
);
413486
});
414487

488+
modules.forEach((definition) => {
489+
createFile(
490+
path.join(
491+
rootDir,
492+
"sig",
493+
"language_server",
494+
"protocol",
495+
"constant",
496+
`${snake(definition.module.name)}.rbs`
497+
),
498+
Handlebars.compile(
499+
`
500+
module LanguageServer
501+
module Protocol
502+
module Constant
503+
{{#if definition.module.documentation}}
504+
#
505+
{{comment definition.module.documentation indent=6}}
506+
#
507+
{{/if}}
508+
module {{definition.module.name}}
509+
{{#each definition.members}}
510+
{{#if documentation}}
511+
#
512+
{{comment documentation indent=8}}
513+
#
514+
{{/if}}
515+
{{#if (isliteral value)}}
516+
{{const name}}: {{value}}
517+
{{else}}
518+
{{const name}}: {{convertTypeToRbs type}}
519+
{{/if}}
520+
{{/each}}
521+
end
522+
end
523+
end
524+
end
525+
`.slice(1),
526+
{ noEscape: true }
527+
)({ definition })
528+
);
529+
});
530+
415531
createFile(
416532
path.join(rootDir, "lib", "language_server", "protocol", "interface.rb"),
417533
Handlebars.compile(

0 commit comments

Comments
 (0)