### Install and Initialize VSCode Extension Source: https://context7.com/ruby/typeprof/llms.txt Install the Ruby TypeProf VSCode extension and initialize TypeProf configuration in your project. ```sh # Install the VSCode extension code --install-extension mame.ruby-typeprof # Initialize TypeProf config in your project cd my-ruby-project typeprof --init # => Creates typeprof.conf.jsonc # Open in VSCode — TypeProf LSP starts automatically code . ``` -------------------------------- ### Install TypeProf Source: https://context7.com/ruby/typeprof/llms.txt Installs TypeProf as a RubyGem. Requires Ruby >= 3.3. Includes a command to verify the installation. ```sh gem install typeprof # Verify installation typeprof --version # => typeprof 0.32.0 ``` -------------------------------- ### TypeProf CLI: LSP Mode Source: https://context7.com/ruby/typeprof/llms.txt Starts the Language Server Protocol server for editor integration. Supports starting over stdio, a specific TCP port, or an auto-assigned port. ```sh # Start LSP server over stdio (for editors that support it) typeprof --lsp --stdio ``` ```sh # Start LSP server on a specific TCP port typeprof --lsp --port 7654 ``` ```sh # Start LSP server on an auto-assigned port (prints JSON with host/port/pid to stdout) typeprof --lsp # stdout: {"host":"127.0.0.1","port":12345,"pid":67890} ``` -------------------------------- ### Install VSCode Ruby TypeProf Extension Source: https://github.com/ruby/typeprof/blob/master/README.md Install the VSCode extension for Ruby TypeProf. This command can be used for both initial setup and development. ```sh code --install-extension mame.ruby-typeprof ``` -------------------------------- ### TypeProf::LSP::Server.start_socket Source: https://context7.com/ruby/typeprof/llms.txt Starts the LSP server on a TCP socket, printing connection details to stdout as JSON. ```APIDOC ## LSP Server — TCP Socket Transport ### Description Starts the LSP server on a TCP socket, printing connection details to stdout as JSON. ### Method Signature `TypeProf::LSP::Server.start_socket(core_options, port)` ### Parameters - **core_options** (Hash) - Options to configure the TypeProf core service. - `display_indicator` (Boolean) - `output_errors` (Boolean) - **port** (Integer) - The port to listen on. Use `0` to auto-assign a free port. ### Request Example ```ruby require "typeprof" TypeProf::LSP::Server.start_socket( { display_indicator: false, output_errors: true }, port = 0 # 0 = auto-assign a free port ) ``` ### Behavior Prints connection details to stdout in JSON format (e.g., `{"host":"127.0.0.1","port":54321,"pid":11223}`). Then, it waits for one LSP client connection and serves it. ``` -------------------------------- ### TypeProf::LSP::Server.start_stdio Source: https://context7.com/ruby/typeprof/llms.txt Starts the LSP server reading from stdin and writing to stdout, suitable for editors that launch language servers as child processes. ```APIDOC ## LSP Server — Stdio Transport ### Description Starts the LSP server reading from `$stdin` and writing to `$stdout`, suitable for editors that launch language servers as child processes. ### Method Signature `TypeProf::LSP::Server.start_stdio(core_options)` ### Parameters - **core_options** (Hash) - Options to configure the TypeProf core service. - `output_parameter_names` (Boolean) - `output_errors` (Boolean) - `display_indicator` (Boolean) - `diagnostic_severity_str` (String) - `exclude_patterns` (Array) ### Request Example ```ruby require "typeprof" TypeProf::LSP::Server.start_stdio( output_parameter_names: true, output_errors: true, display_indicator: false, diagnostic_severity_str: "warning", exclude_patterns: [], ) ``` ### Behavior Server reads JSON-RPC messages from stdin, sends responses to stdout. Handles standard LSP requests like `initialize`, `textDocument/didOpen`, `textDocument/hover`, etc. ``` -------------------------------- ### Start TypeProf LSP Server via Stdio Source: https://context7.com/ruby/typeprof/llms.txt Launch the LSP server to communicate over standard input/output. This is suitable for editors that manage language servers as child processes. Ensure `require "typeprof"` is present. ```ruby require "typeprof" TypeProf::LSP::Server.start_stdio( output_parameter_names: true, output_errors: true, display_indicator: false, diagnostic_severity_str: "warning", exclude_patterns: [], ) # Server reads JSON-RPC messages from stdin, sends responses to stdout # Handles: initialize, textDocument/didOpen, textDocument/hover, etc. ``` -------------------------------- ### Example of method call analysis in TypeProf Source: https://github.com/ruby/typeprof/blob/master/doc/doc.md Illustrates how TypeProf analyzes a method call, showing input types and return types. ```ruby def foo(n) p n #=> Integer n.to_s end p foo(42) #=> String ``` -------------------------------- ### Clone TypeProf Repository Source: https://github.com/ruby/typeprof/blob/master/README.md Clone the TypeProf repository from GitHub to start development. This is the first step in setting up the development environment. ```sh git clone https://github.com/ruby/typeprof.git ``` -------------------------------- ### Install TypeProf Gem Source: https://github.com/ruby/typeprof/blob/master/README.md Install the TypeProf gem using RubyGems. Ensure you have Ruby 3.3 or later. ```sh gem install typeprof ``` -------------------------------- ### Example of instance variable analysis in TypeProf Source: https://github.com/ruby/typeprof/blob/master/doc/doc.md Demonstrates how TypeProf tracks types assigned to instance variables, including updates and unions. ```ruby class Foo def initialize @a = 42 end attr_accessor :a end Foo.new.a = "str" p Foo.new.a #=> Integer | String ``` -------------------------------- ### Start TypeProf LSP Server via TCP Socket Source: https://context7.com/ruby/typeprof/llms.txt Initiate the LSP server on a TCP socket for network-based communication. Connection details are printed to stdout. A port of 0 automatically assigns a free port. Requires `require "typeprof"`. ```ruby require "typeprof" TypeProf::LSP::Server.start_socket( { display_indicator: false, output_errors: true }, port = 0 # 0 = auto-assign a free port ) # Prints to stdout: {"host":"127.0.0.1","port":54321,"pid":11223} # Then waits for one LSP client connection and serves it ``` -------------------------------- ### Run TypeProf Tests Source: https://github.com/ruby/typeprof/blob/master/README.md Install dependencies and run the test suite for TypeProf using Bundler and Rake. This is part of the development and testing process. ```sh bundle install bundle exec rake test ``` -------------------------------- ### TypeProf Configuration File Source: https://context7.com/ruby/typeprof/llms.txt Example `typeprof.conf.jsonc` for configuring analysis boundaries, RBS directories, and exclusions in a typical Rails-like project. ```jsonc // typeprof.conf.jsonc for a typical Rails-like project { "typeprof_version": "experimental", "rbs_dir": "sig/", "analysis_unit_dirs": [ "app/models/", "app/services/", "lib/" ], "diagnostic_severity": "warning", "exclude": [ "lib/tasks/**", "**/*_spec.rb" ] } ``` -------------------------------- ### TypeProf CLI output for method call analysis Source: https://github.com/ruby/typeprof/blob/master/doc/doc.md Shows the output of TypeProf when analyzing the example method call, including revealed types and class definitions. ```bash $ ruby exe/typeprof test.rb # Revealed types # test.rb:2 #=> Integer # test.rb:6 #=> String # Classes class Object def foo : (Integer) -> String end ``` -------------------------------- ### TypeProf Example: Inferring types from unannotated Ruby Source: https://context7.com/ruby/typeprof/llms.txt Demonstrates inferring types for a simple Ruby class and its usage. The input Ruby code is analyzed, and the output shows the inferred RBS signature. ```ruby # greeting.rb class Greeter def initialize(name) @name = name end def hello(times) "Hello, #{@name}! " * times end end g = Greeter.new("Alice") puts g.hello(3) ``` ```sh $ typeprof greeting.rb ``` ```rbs # TypeProf 0.32.0 # Classes class Greeter @name: String def initialize: (String) -> void def hello: (Integer) -> String end ``` -------------------------------- ### Example of Inferred RBS Output Source: https://context7.com/ruby/typeprof/llms.txt This Ruby code demonstrates a class with various instance variables and methods, followed by its inferred RBS output. It illustrates how TypeProf represents different abstract value kinds like Array, Symbol, and union types in RBS. ```ruby # source: types_demo.rb class Container def initialize @items = [] # Array[untyped] initially @label = :default # Symbol @active = true # true | false -> bool end def add(item) @items << item end def process(&block) @items.map(&block) end def tagged(tag) tag ? @label : nil # :default | nil end end ``` ```rbs # Inferred output: class Container @items: Array[untyped] @label: :default @active: true def initialize: -> void def add: (untyped) -> Array[untyped] def process: { (untyped) -> untyped } -> Array[untyped] def tagged: (untyped) -> (:default | nil) end ``` -------------------------------- ### TypeProf Hover: Get Type at Cursor Position Source: https://context7.com/ruby/typeprof/llms.txt Retrieves the inferred type (as a string) for the expression at a given source position. This is used by the LSP `textDocument/hover` handler. Requires the service to be initialized and the file updated. ```ruby require "typeprof" service = TypeProf::Core::Service.new(display_indicator: false) service.update_rb_file("example.rb", <<~RUBY) def add(x, y) x + y end add(1, 2) RUBY pos = TypeProf::CodePosition.new(1, 7) # line 1 (0-based), char 7 — on `x` result = service.hover("example.rb", pos) puts result # => "Integer" pos2 = TypeProf::CodePosition.new(0, 4) # hover over `add` method name result2 = service.hover("example.rb", pos2) puts result2 # => "Object#add : (Integer, Integer) -> Integer" ``` -------------------------------- ### Initialize TypeProf Configuration Source: https://github.com/ruby/typeprof/blob/master/README.md Run this command in your project root to create a `typeprof.conf.jsonc` file. Refer to the configuration file for available options. ```sh typeprof --init ``` -------------------------------- ### Initialize TypeProf Service and Add Workspace Source: https://context7.com/ruby/typeprof/llms.txt Initializes the TypeProf service with custom options and adds a workspace for analysis. The workspace includes Ruby source directories and RBS declaration directories. ```ruby service = TypeProf::Core::Service.new( output_parameter_names: true, output_source_locations: false, output_errors: false, display_indicator: false, exclude_patterns: ["test/**"], rbs_collection: nil, # or an RBS::Collection::Config::Lockfile instance ) service.add_workspace("lib/", "sig/") ``` -------------------------------- ### Basic Scenario File Structure Source: https://github.com/ruby/typeprof/blob/master/doc/report_guide.md A minimal scenario file includes an 'update' section for input code and an 'assert' section for expected type signatures in RBS syntax. ```ruby ## update def foo(n) n.to_s end foo(42) ## assert class Object def foo: (Integer) -> String end ``` -------------------------------- ### Analyze Ruby file with TypeProf CLI Source: https://github.com/ruby/typeprof/blob/master/doc/doc.md Use the TypeProf CLI to analyze a single Ruby file. ```bash $ typeprof app.rb ``` -------------------------------- ### TypeProf LSP Server Capabilities Source: https://context7.com/ruby/typeprof/llms.txt This JSON structure outlines the capabilities advertised by the TypeProf LSP server in response to an 'initialize' request. It details supported features like hover, definition, and completion providers. ```jsonc // Server capabilities (returned in response to initialize) { "positionEncoding": "utf-16", "textDocumentSync": { "openClose": true, "change": 2 }, // Incremental "hoverProvider": true, "definitionProvider": true, "typeDefinitionProvider": true, "completionProvider": { "triggerCharacters": [".", ":"] }, "codeLensProvider": { "resolveProvider": false }, "renameProvider": { "prepareProvider": false }, "referencesProvider": true, "executeCommandProvider": { "commands": [ "typeprof.createPrototypeRBS", "typeprof.enableSignature", "typeprof.disableSignature" ] } } ``` -------------------------------- ### Run a Single Scenario File Source: https://github.com/ruby/typeprof/blob/master/doc/report_guide.md Execute a specific scenario file using the TypeProf scenario runner script. ```sh ruby tool/scenario_runner.rb path/to/your_scenario.rb ``` -------------------------------- ### TypeProf CLI output for instance variable analysis Source: https://github.com/ruby/typeprof/blob/master/doc/doc.md Presents the TypeProf analysis results for instance variables, showing revealed types and the updated class definition. ```bash $ ruby exe/typeprof test.rb # Revealed types # test.rb:11 #=> Integer | String # Classes class Foo attr_accessor a : Integer | String def initialize : -> Integer end ``` -------------------------------- ### TypeProf::Core::Service#completion Source: https://context7.com/ruby/typeprof/llms.txt Provides method completion suggestions based on the context at a given cursor position and trigger character. ```APIDOC ## `TypeProf::Core::Service#completion` — Method Completion **`service.completion(path, trigger, pos)`** — yields `(method_id, signature_hint)` pairs for all methods available at the given position. ```ruby service = TypeProf::Core::Service.new(display_indicator: false) service.update_rb_file("demo.rb", <<~RUBY) x = "hello" x. # cursor here with trigger "." RUBY pos = TypeProf::CodePosition.new(1, 3) # after the dot service.completion("demo.rb", ".", pos) do |mid, hint| puts "#{mid}: #{hint}" # upcase: () -> String # downcase: () -> String # length: () -> Integer # ... (all String instance methods) end ``` ``` -------------------------------- ### TypeProf Definitions: Go-to-Definition Source: https://context7.com/ruby/typeprof/llms.txt Returns a list of `[path, code_range]` for the symbol at the given position, enabling the LSP `textDocument/definition` handler. Requires the service to be initialized and the file updated. ```ruby service = TypeProf::Core::Service.new(display_indicator: false) service.update_rb_file("foo.rb", <<~RUBY) class Foo def bar 42 end end Foo.new.bar # cursor here -> jumps to def bar RUBY pos = TypeProf::CodePosition.new(5, 10) # position of `bar` call defs = service.definitions("foo.rb", pos) defs.each do |path, range| puts "Defined at #{path}:#{range}" # => Defined at foo.rb:2:2..4:5 end ``` -------------------------------- ### Run All Tests Source: https://github.com/ruby/typeprof/blob/master/doc/report_guide.md Execute all defined tests, including scenario files, using the Rake build tool. ```sh bundle exec rake test ``` -------------------------------- ### Analyze Ruby file with RBS signatures using TypeProf CLI Source: https://github.com/ruby/typeprof/blob/master/doc/doc.md Analyze a Ruby file, providing method type signatures from an RBS file. ```bash $ typeprof sig/app.rbs app.rb ``` -------------------------------- ### TypeProf CLI: Generate Project Config Source: https://context7.com/ruby/typeprof/llms.txt Generates a `typeprof.conf.jsonc` configuration file by auto-detecting `app/` and `lib/` directories. The generated file specifies analysis units, RBS directory, and diagnostic severity. ```sh $ typeprof --init # => Writes typeprof.conf.jsonc ``` ```jsonc // typeprof.conf.jsonc (generated) { "typeprof_version": "experimental", "analysis_unit_dirs": ["lib/"], "rbs_dir": "sig/", "diagnostic_severity": "warning" } ``` -------------------------------- ### Scenario File for Diagnostics Source: https://github.com/ruby/typeprof/blob/master/doc/report_guide.md Use the 'diagnostics' section to verify that TypeProf reports type errors at the expected locations. ```ruby ## update def foo(x) x end foo(1, 2) ## diagnostics (5,0)-(5,3): wrong number of arguments (2 for 1) ``` -------------------------------- ### Open TypeProf in VSCode Source: https://github.com/ruby/typeprof/blob/master/README.md Open the cloned TypeProf repository in VSCode. This command assumes you are in the directory containing the repository. ```sh code typeprof ``` -------------------------------- ### TypeProf References: Find All References Source: https://context7.com/ruby/typeprof/llms.txt Returns a list of `[path, code_range]` for all usages of the method or constant at the given position. This is useful for finding all occurrences of a symbol. ```ruby service = TypeProf::Core::Service.new(display_indicator: false) service.update_rb_file("app.rb", <<~RUBY) def greet(name) "Hello, #{name}" end greet("Alice") greet("Bob") RUBY pos = TypeProf::CodePosition.new(0, 4) # cursor on `greet` definition refs = service.references("app.rb", pos) refs.each do |path, range| puts "#{path}:#{range}" # app.rb: the definition site + both call sites end ``` -------------------------------- ### Generate RBS signatures with TypeProf CLI Source: https://github.com/ruby/typeprof/blob/master/doc/doc.md Analyze a Ruby file and output generated RBS signatures to a specified file. ```bash $ typeprof sig/app.rbs app.rb -o sig/app.gen.rbs ``` -------------------------------- ### TypeProf::Core::Service#code_lens Source: https://context7.com/ruby/typeprof/llms.txt Generates inline type signature hints for method definitions within a file, suitable for editor display. ```APIDOC ## `TypeProf::Core::Service#code_lens` — Inline Type Signature Hints **`service.code_lens(path)`** — yields `(code_range, hint_string)` for each method definition in the file, providing inline RBS signature annotations for editor display. ```ruby service = TypeProf::Core::Service.new(display_indicator: false) service.update_rb_file("calc.rb", <<~RUBY) class Calc def add(a, b) a + b end def double(n) n * 2 end end RUBY service.code_lens("calc.rb") do |range, hint| puts "Line #{range.first.line}: #{hint}" # Line 1: (Integer, Integer) -> Integer # Line 5: (Integer) -> Integer end ``` ``` -------------------------------- ### LSP Server — Supported Protocol Messages Source: https://context7.com/ruby/typeprof/llms.txt Details the standard and custom LSP commands supported by the TypeProf LSP server. ```APIDOC ## LSP — Supported Protocol Messages ### Description TypeProf's LSP server advertises and handles the following JSON-RPC methods, including custom commands for specific TypeProf features. ### Server Capabilities (from `initialize` response) ```jsonc { "positionEncoding": "utf-16", "textDocumentSync": { "openClose": true, "change": 2 }, // Incremental "hoverProvider": true, "definitionProvider": true, "typeDefinitionProvider": true, "completionProvider": { "triggerCharacters": [".", ":"] }, "codeLensProvider": { "resolveProvider": false }, "renameProvider": { "prepareProvider": false }, "referencesProvider": true, "executeCommandProvider": { "commands": [ "typeprof.createPrototypeRBS", "typeprof.enableSignature", "typeprof.disableSignature" ] } } ``` ### Custom LSP Commands (via `workspace/executeCommand`) 1. **`typeprof.enableSignature`** * **Description**: Enable inline RBS signature code lens hints. * **Arguments**: None. 2. **`typeprof.disableSignature`** * **Description**: Disable inline RBS signature code lens hints. * **Arguments**: None. 3. **`typeprof.createPrototypeRBS`** * **Description**: Insert an inferred `#:: ` annotation at a cursor position. * **Arguments**: An object containing `uri` and `position`. * `uri` (String): The document URI. * `position` (Object): The cursor position. * `line` (Integer): The line number. * `character` (Integer): The character position. * **Example Argument**: `{"uri": "file:///path/to/file.rb", "position": {"line": 5, "character": 2}}` ``` -------------------------------- ### TypeProf::Core::Service#references Source: https://context7.com/ruby/typeprof/llms.txt Finds all usages (references) of a method or constant at a given cursor position. ```APIDOC ## `TypeProf::Core::Service#references` — Find All References **`service.references(path, pos)`** — returns `[[path, code_range], ...]` for all usages of the method or constant at the given position. ```ruby service = TypeProf::Core::Service.new(display_indicator: false) service.update_rb_file("app.rb", <<~RUBY) def greet(name) "Hello, #{name}" end greet("Alice") greet("Bob") RUBY pos = TypeProf::CodePosition.new(0, 4) # cursor on `greet` definition refs = service.references("app.rb", pos) refs.each do |path, range| puts "#{path}:#{range}" # app.rb: the definition site + both call sites end ``` ``` -------------------------------- ### Analyze Ruby File and Report Diagnostics Source: https://context7.com/ruby/typeprof/llms.txt Use `TypeProf::Core::Service` to update a Ruby file and process diagnostics. Diagnostics can be converted to LSP format for editor integration. ```ruby service = TypeProf::Core::Service.new(display_indicator: false, output_errors: true) service.update_rb_file("err.rb", <<~RUBY) def add(x, y) x + y end add(1, "oops") # TypeProf may flag mismatched types RUBY service.process_diagnostic_paths { |path| path == "err.rb" } service.diagnostics("err.rb") do |diag| puts diag.code_range puts diag.msg # Convert to LSP wire format lsp_diag = diag.to_lsp(severity: :warning) # => { # range: { start: { line: 3, character: 0 }, end: { line: 3, character: 14 } }, # source: "TypeProf", # message: "...", # severity: 2, # warning # } end ``` -------------------------------- ### TypeProf Configuration: `typeprof.conf.jsonc` Source: https://context7.com/ruby/typeprof/llms.txt Defines project-wide settings for TypeProf analysis, including RBS directory, source directories, and diagnostic severity levels. Supports excluding specific files or directories. ```jsonc // typeprof.conf.jsonc { // Must always be "experimental" "typeprof_version": "experimental", // Directory containing hand-written or generated RBS type declarations "rbs_dir": "sig/", // Ruby source directories to analyze; each is treated as an independent analysis unit "analysis_unit_dirs": [ "lib/", "app/models/" ], // Diagnostic severity threshold: "error" | "warning" | "info" | "hint" | "none" "diagnostic_severity": "warning", // Glob patterns to exclude from analysis (optional) "exclude": [ "lib/**/*_test.rb", "lib/generated/**" ] } ``` -------------------------------- ### Scenario File with Multiple Updates Source: https://github.com/ruby/typeprof/blob/master/doc/report_guide.md Test how TypeProf handles code changes by including multiple 'update' and 'assert' pairs in a single scenario file. ```ruby ## update def foo = "symbol#{ 42 }" ## assert class Object def foo: -> String end ## update def foo = :"symbol#{ 42 }" ## assert class Object def foo: -> Symbol end ``` -------------------------------- ### TypeProf Code Lens: Inline Type Signature Hints Source: https://context7.com/ruby/typeprof/llms.txt Yields `(code_range, hint_string)` for each method definition in the file, providing inline RBS signature annotations for editor display. This helps visualize method signatures directly in the code. ```ruby service = TypeProf::Core::Service.new(display_indicator: false) service.update_rb_file("calc.rb", <<~RUBY) class Calc def add(a, b) a + b end def double(n) n * 2 end end RUBY service.code_lens("calc.rb") do |range, hint| puts "Line #{range.first.line}: #{hint}" # Line 1: (Integer, Integer) -> Integer # Line 5: (Integer) -> Integer end ``` -------------------------------- ### Custom LSP Commands for TypeProf Source: https://context7.com/ruby/typeprof/llms.txt These JSON objects represent custom commands that can be sent to the TypeProf LSP server via 'workspace/executeCommand'. They enable actions like managing signature hints or generating prototype RBS annotations. ```jsonc // Enable inline RBS signature code lens hints { "command": "typeprof.enableSignature" } ``` ```jsonc // Disable inline RBS signature code lens hints { "command": "typeprof.disableSignature" } ``` ```jsonc // Insert an inferred #: annotation at a cursor position { "command": "typeprof.createPrototypeRBS", "arguments": [{ "uri": "file:///path/to/file.rb", "position": { "line": 5, "character": 2 } }] } ``` -------------------------------- ### TypeProf Completion: Method Completion Source: https://context7.com/ruby/typeprof/llms.txt Yields `(method_id, signature_hint)` pairs for all methods available at the given position, triggered by a specific character. Useful for autocompletion features. ```ruby service = TypeProf::Core::Service.new(display_indicator: false) service.update_rb_file("demo.rb", <<~RUBY) x = "hello" x. # cursor here with trigger "." RUBY pos = TypeProf::CodePosition.new(1, 3) # after the dot service.completion("demo.rb", ".", pos) do |mid, hint| puts "#{mid}: #{hint}" # upcase: () -> String # downcase: () -> String # length: () -> Integer # ... (all String instance methods) end ``` -------------------------------- ### TypeProf Programmatic API: `TypeProf::Core::Service` Source: https://context7.com/ruby/typeprof/llms.txt Provides programmatic access to the TypeProf analysis engine for integration into other tools. Requires the 'typeprof' gem to be required. ```ruby require "typeprof" ``` -------------------------------- ### TypeProf Scenario Test Format Source: https://context7.com/ruby/typeprof/llms.txt Scenario files use embedded assertions for testing. Sections include `## update`, `## assert`, and optional `## hover`. ```ruby # scenario/method/keywords_example.rb ## update def search(query, limit: 10, exact: false) [query, limit, exact] end search("ruby", limit: 20) ## assert class Object def search: (String, ?limit: Integer, ?exact: false) -> [String, Integer, false] end ## hover: [A] # Place [A] marker on `query` parameter -> hovering shows: String ``` -------------------------------- ### TypeProf CLI: Batch Type Analysis Source: https://context7.com/ruby/typeprof/llms.txt Analyzes Ruby source files and outputs inferred RBS type signatures. Supports various options for output control, error reporting, and file exclusion. ```sh # Analyze a single Ruby file, print RBS to stdout typeprof app.rb ``` ```sh # Provide existing RBS declarations to guide inference; output to a file typeprof sig/app.rbs app.rb -o sig/app.gen.rbs ``` ```sh # Analyze all files in a directory, show verbose errors and parameter names typeprof lib/ --show-errors --show-parameter-names ``` ```sh # Quiet mode (no progress spinner), include source locations in output typeprof app.rb --quiet --show-source-locations ``` ```sh # Exclude test files from analysis typeprof lib/ --exclude "lib/**/*_test.rb" --exclude "lib/**/spec/**" ``` ```sh # Use a specific rbs_collection.yaml lock file typeprof --collection rbs_collection.lock.yaml lib/ app.rb ``` ```sh # Show type-coverage statistics typeprof lib/ --show-stats ``` ```sh # Enable StackProf CPU profiling during analysis typeprof lib/ --stackprof cpu ``` -------------------------------- ### Resolve Type Definitions with `type_definitions` Source: https://context7.com/ruby/typeprof/llms.txt Use `service.type_definitions` to find the source location of an expression's inferred type. Requires initializing the service and updating Ruby files. ```ruby service = TypeProf::Core::Service.new(display_indicator: false) service.update_rb_file("main.rb", <<~RUBY) class Dog def name; "Rex"; end end d = Dog.new d.name # hover over `d` -> type is Dog -> jumps to class Dog definition RUBY pos = TypeProf::CodePosition.new(3, 0) # on `d = Dog.new` type_defs = service.type_definitions("main.rb", pos) type_defs.each do |path, range| puts "Type defined at #{path}:#{range}" # => Type defined at main.rb:0:0..2:3 end ``` -------------------------------- ### Batch Analysis and Statistics Collection Source: https://context7.com/ruby/typeprof/llms.txt Performs batch analysis on a list of files, writing the RBS output to standard output. It also demonstrates collecting and formatting type-coverage statistics for specified files. ```ruby service.batch(["lib/greeter.rb", "lib/app.rb"], $stdout) stats = service.collect_stats(["lib/greeter.rb"]) puts service.format_stats(stats) ``` -------------------------------- ### TypeProf::Core::Service#definitions Source: https://context7.com/ruby/typeprof/llms.txt Finds the definition locations for a symbol at a given cursor position. This method is used by the Language Server Protocol's `textDocument/definition` handler. ```APIDOC ## `TypeProf::Core::Service#definitions` — Go-to-Definition **`service.definitions(path, pos)`** — returns `[[path, code_range], ...]` for the symbol at the given position. Used by the LSP `textDocument/definition` handler. ```ruby service = TypeProf::Core::Service.new(display_indicator: false) service.update_rb_file("foo.rb", <<~RUBY) class Foo def bar 42 end end Foo.new.bar # cursor here -> jumps to def bar RUBY pos = TypeProf::CodePosition.new(5, 10) # position of `bar` call defs = service.definitions("foo.rb", pos) defs.each do |path, range| puts "Defined at #{path}:#{range}" # => Defined at foo.rb:2:2..4:5 end ``` ``` -------------------------------- ### Update File and Dump Declarations Source: https://context7.com/ruby/typeprof/llms.txt Incrementally updates a single file with new content and then dumps the inferred RBS declarations for that file. Returns true if the analysis graph changed after the update. ```ruby changed = service.update_file("lib/greeter.rb", <<~RUBY) class Greeter def initialize(name) @name = name end def greet "Hello, #{@name}" end end RUBY puts changed # => true if the analysis graph changed puts service.dump_declarations("lib/greeter.rb") ``` -------------------------------- ### TypeProf Abstract Value Kinds Source: https://context7.com/ruby/typeprof/llms.txt This Ruby code lists and describes the seven abstract type kinds used by TypeProf to represent Ruby values. These types are accessible via `TypeProf::Core::Type` and include Instance, Singleton, Symbol, Array, Hash, Proc, and Bot. ```ruby # Type::Instance — most common; an object of a class # Shows as class name, with special cases for nil/true/false # e.g., Integer, String, nil, true, Array[Integer] # Type::Singleton — the class object itself (not an instance) # Shows as: singleton(Foo) # Used when a method returns a class constant, e.g., `def foo; MyClass; end` # Type::Symbol — a concrete symbol literal (kept as-is for keyword arg tracking) # Shows as: :foo # Type::Array — array with typed elements # Shows as: [Integer, String] (tuple) or Array[Integer] (uniform) # Type::Hash — hash with typed key/value # Type::Record — shape/record type for hashes with known key shapes # Shows as: { name: String, age: Integer } # Type::Proc — a closure/block value # Shows as anonymous proc type in RBS: ^(Float) -> String # Type::Bot — bottom type, represents unreachable code paths # Union types arise automatically when multiple types flow into one variable: # e.g., rand < 0.5 ? 42 : "str" => Integer | String ``` -------------------------------- ### TypeProf::Core::Service#type_definitions Source: https://context7.com/ruby/typeprof/llms.txt Resolves the inferred type of an expression and returns the source location where that class/module is defined. ```APIDOC ## `TypeProf::Core::Service#type_definitions` — Go-to-Type-Definition ### Description Resolves the inferred type of an expression and returns the source location where that class/module is defined. ### Method Signature `service.type_definitions(path, pos)` ### Parameters - **path** (String) - The path to the Ruby file. - **pos** (TypeProf::CodePosition) - The code position (line, column) of the expression. ### Request Example ```ruby service = TypeProf::Core::Service.new(display_indicator: false) service.update_rb_file("main.rb", <<~RUBY) class Dog def name; "Rex"; end end d = Dog.new d.name # hover over `d` -> type is Dog -> jumps to class Dog definition RUBY pos = TypeProf::CodePosition.new(3, 0) # on `d = Dog.new` type_defs = service.type_definitions("main.rb", pos) ``` ### Response Example ```ruby type_defs.each do |path, range| puts "Type defined at #{path}:#{range}" # => Type defined at main.rb:0:0..2:3 end ``` ``` -------------------------------- ### TypeProf::Core::Service#hover Source: https://context7.com/ruby/typeprof/llms.txt Retrieves the inferred type for an expression at a specific cursor position in a file. This method is used by the Language Server Protocol's `textDocument/hover` handler. ```APIDOC ## `TypeProf::Core::Service#hover` — Type at Cursor Position **`service.hover(path, pos)`** — returns the inferred type (as a string) for the expression at the given source position. Used by the LSP `textDocument/hover` handler. ```ruby require "typeprof" service = TypeProf::Core::Service.new(display_indicator: false) service.update_rb_file("example.rb", <<~RUBY) def add(x, y) x + y end add(1, 2) RUBY pos = TypeProf::CodePosition.new(1, 7) # line 1 (0-based), char 7 — on `x` result = service.hover("example.rb", pos) puts result # => "Integer" pos2 = TypeProf::CodePosition.new(0, 4) # hover over `add` method name result2 = service.hover("example.rb", pos2) puts result2 # => "Object#add : (Integer, Integer) -> Integer" ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.