Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

Ruby LSP extension (#323)

Set up basic Ruby LSP add-on. This add-on defines instance variables
when you use the `prop` macro. It also sets up reader and writer methods
with the correct visibility, but only when you specify them directly in
the macro.

authored by

Joel Drapper and committed by
GitHub
0f3c6128 dc8cb94a

+71
+1
lib/literal.rb
··· 7 7 Loader = Zeitwerk::Loader.for_gem.tap do |loader| 8 8 loader.ignore("#{__dir__}/literal/rails") 9 9 loader.ignore("#{__dir__}/literal/railtie.rb") 10 + loader.ignore("#{__dir__}/ruby_lsp") 10 11 11 12 loader.inflector.inflect( 12 13 "json_data_type" => "JSONDataType"
+70
lib/ruby_lsp/literal/addon.rb
··· 1 + # frozen_string_literal: true 2 + 3 + require "ruby_lsp/addon" 4 + 5 + module RubyLsp 6 + module Literal 7 + class Addon < ::RubyLsp::Addon 8 + def activate(global_state, message_queue) 9 + end 10 + 11 + def deactivate 12 + end 13 + 14 + def name 15 + "Literal" 16 + end 17 + 18 + def version 19 + "0.1.0" 20 + end 21 + end 22 + 23 + class MyIndexingEnhancement < RubyIndexer::Enhancement 24 + def on_call_node_enter(node) 25 + name = node.name 26 + owner = @listener.current_owner 27 + location = node.location 28 + arguments = node.arguments&.arguments 29 + 30 + return unless owner 31 + return unless :prop == name 32 + 33 + args = arguments&.reject { |it| it.is_a?(Prism::KeywordHashNode) } 34 + kwargs = arguments.find { |it| it.is_a?(Prism::KeywordHashNode) }&.elements.to_h do |element| 35 + case element 36 + in { key: Prism::SymbolNode[unescaped: String => key], value: value } 37 + [key, value] 38 + end 39 + end 40 + 41 + case args 42 + in [Prism::SymbolNode[unescaped: String => prop_name], *] 43 + @listener.instance_exec do 44 + @index.add(RubyIndexer::Entry::InstanceVariable.new( 45 + "@#{prop_name}", 46 + @uri, 47 + RubyIndexer::Location.from_prism_location(node.location, @code_units_cache), 48 + collect_comments(node), 49 + owner, 50 + )) 51 + end 52 + 53 + if kwargs["reader"] in Prism::SymbolNode[unescaped: "private" | "protected" | "public" => visibility] 54 + @listener.add_method(prop_name, location, [ 55 + RubyIndexer::Entry::Signature.new([]), 56 + ], visibility: visibility.to_sym) 57 + end 58 + 59 + if kwargs["writer"] in Prism::SymbolNode[unescaped: "private" | "protected" | "public" => visibility] 60 + @listener.add_method("#{prop_name}=", location, [ 61 + RubyIndexer::Entry::Signature.new([ 62 + RubyIndexer::Entry::RequiredParameter.new(name: "value"), 63 + ]), 64 + ], visibility: visibility.to_sym) 65 + end 66 + end 67 + end 68 + end 69 + end 70 + end