Skip to content

Commit

Permalink
Merge pull request #104 from joker1007/add-rbs
Browse files Browse the repository at this point in the history
Add simple RBS signatures for generated files
  • Loading branch information
mtsmfm authored Jun 19, 2024
2 parents f6f03c6 + f047a44 commit 15d4388
Show file tree
Hide file tree
Showing 354 changed files with 6,676 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
/tmp/
/node_modules
/vendor
/.gem_rbs_collection/
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ gemspec
gem "benchmark-ips"
gem "pry-byebug"
gem "activesupport"
gem "steep", require: false
17 changes: 17 additions & 0 deletions Steepfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
D = Steep::Diagnostic

target :lib do
signature "sig"

check "lib" # Directory name

# configure_code_diagnostics(D::Ruby.default) # `default` diagnostics setting (applies by default)
end

# target :test do
# signature "sig", "sig-private"
#
# check "test"
#
# # library "pathname" # Standard libraries
# end
2 changes: 1 addition & 1 deletion language_server-protocol.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
spec.homepage = "https://github.com/mtsmfm/language_server-protocol-ruby"
spec.license = "MIT"

spec.files = `git ls-files -z lib LICENSE.txt README.md`.split("\x0")
spec.files = `git ls-files -z lib sig LICENSE.txt README.md`.split("\x0")
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
Expand Down
172 changes: 172 additions & 0 deletions rbs_collection.lock.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
---
path: ".gem_rbs_collection"
gems:
- name: abbrev
version: '0'
source:
type: stdlib
- name: activesupport
version: '7.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: ast
version: '2.4'
source:
type: git
name: ruby/gem_rbs_collection
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: base64
version: '0'
source:
type: stdlib
- name: bigdecimal
version: '0'
source:
type: stdlib
- name: concurrent-ruby
version: '1.1'
source:
type: git
name: ruby/gem_rbs_collection
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: connection_pool
version: '2.4'
source:
type: git
name: ruby/gem_rbs_collection
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: csv
version: '0'
source:
type: stdlib
- name: date
version: '0'
source:
type: stdlib
- name: erb
version: '0'
source:
type: stdlib
- name: fileutils
version: '0'
source:
type: stdlib
- name: forwardable
version: '0'
source:
type: stdlib
- name: i18n
version: '1.10'
source:
type: git
name: ruby/gem_rbs_collection
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: json
version: '0'
source:
type: stdlib
- name: listen
version: '3.2'
source:
type: git
name: ruby/gem_rbs_collection
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: logger
version: '0'
source:
type: stdlib
- name: minitest
version: '0'
source:
type: stdlib
- name: monitor
version: '0'
source:
type: stdlib
- name: mutex_m
version: '0'
source:
type: stdlib
- name: optparse
version: '0'
source:
type: stdlib
- name: parser
version: '3.2'
source:
type: git
name: ruby/gem_rbs_collection
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: pathname
version: '0'
source:
type: stdlib
- name: rainbow
version: '3.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: rake
version: '13.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 438153d157e1e1ed6b640da973e33f333e57e6d9
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: rbs
version: 3.4.4
source:
type: rubygems
- name: rdoc
version: '0'
source:
type: stdlib
- name: securerandom
version: '0'
source:
type: stdlib
- name: singleton
version: '0'
source:
type: stdlib
- name: steep
version: 1.6.0
source:
type: rubygems
- name: strscan
version: '0'
source:
type: stdlib
- name: time
version: '0'
source:
type: stdlib
- name: timeout
version: '0'
source:
type: stdlib
- name: tsort
version: '0'
source:
type: stdlib
gemfile_lock_path: Gemfile.lock
19 changes: 19 additions & 0 deletions rbs_collection.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Download sources
sources:
- type: git
name: ruby/gem_rbs_collection
remote: https://github.com/ruby/gem_rbs_collection.git
revision: main
repo_dir: gems

# You can specify local directories as sources also.
# - type: local
# path: path/to/your/local/repository

# A directory to install the downloaded RBSs
path: .gem_rbs_collection

gems:
# If you want to avoid installing rbs files for gems, you can specify them here.
- name: language_server-protocol
ignore: true
116 changes: 116 additions & 0 deletions scripts/generateFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,40 @@ Handlebars.registerHelper("isliteral", (s: string) => {
return s.match(/'.*'/) || s.match(/".*"/) || s.match(/\d+/);
});



const typeMapping = new Map<string, string>([
["string", "String"],
["number", "Integer"],
["boolean", "bool"],
["null", "nil"],
["undefined", "nil"],
["any", "untyped"],
["LSPAny", "untyped"],
]);

const convertTypeToRbs: ((type: string) => string) = (type) => {
if (type.endsWith("[]")) {
return `Array[${convertTypeToRbs(type.slice(0, -2))}]`;
}
let rbsType = typeMapping.get(type);
if (rbsType != null) {
return rbsType;
}

return "untyped";
};

Handlebars.registerHelper("method_signature", (members: InterfaceResult["members"]) => {
return members
.map(
(member) =>
`${member.optional ? "?" : ""}${snake(member.name)}: ${convertTypeToRbs(member.type)}${member.nullable ? "?" : ""}`
)
.join(", ");
});
Handlebars.registerHelper("convertTypeToRbs", convertTypeToRbs);

(async () => {
const data = globSync(
path.join(
Expand Down Expand Up @@ -369,6 +403,45 @@ end
);
});

interfaces.forEach((definition) => {
createFile(
path.join(
rootDir,
"sig",
"language_server",
"protocol",
"interface",
`${snake(definition.interface.name)}.rbs`
),
Handlebars.compile(
`
module LanguageServer
module Protocol
module Interface
{{#if definition.interface.documentation}}
#
{{comment definition.interface.documentation indent=6}}
#
{{/if}}
class {{definition.interface.name}}
def initialize: ({{method_signature definition.allMembers}}) -> void
@attributes: Hash[Symbol, untyped]
attr_reader attributes: Hash[Symbol, untyped]
def to_hash: () -> Hash[Symbol, untyped]
def to_json: (*untyped) -> String
end
end
end
end
`.slice(1),
{ noEscape: true }
)({ definition })
);
});

modules.forEach((definition) => {
createFile(
path.join(
Expand Down Expand Up @@ -412,6 +485,49 @@ end
);
});

modules.forEach((definition) => {
createFile(
path.join(
rootDir,
"sig",
"language_server",
"protocol",
"constant",
`${snake(definition.module.name)}.rbs`
),
Handlebars.compile(
`
module LanguageServer
module Protocol
module Constant
{{#if definition.module.documentation}}
#
{{comment definition.module.documentation indent=6}}
#
{{/if}}
module {{definition.module.name}}
{{#each definition.members}}
{{#if documentation}}
#
{{comment documentation indent=8}}
#
{{/if}}
{{#if (isliteral value)}}
{{const name}}: {{value}}
{{else}}
{{const name}}: {{convertTypeToRbs type}}
{{/if}}
{{/each}}
end
end
end
end
`.slice(1),
{ noEscape: true }
)({ definition })
);
});

createFile(
path.join(rootDir, "lib", "language_server", "protocol", "interface.rb"),
Handlebars.compile(
Expand Down
Loading

0 comments on commit 15d4388

Please sign in to comment.