Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
KaanOzkan committed Oct 29, 2024
1 parent 7e68f5c commit 09ac1f6
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/ruby_lsp/tapioca/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def workspace_did_change_watched_files(changes)
current_checksum = Zlib.crc32(content).to_s

if change[:type] == Constant::FileChangeType::CHANGED && @file_checksums[path] == current_checksum
$stderr.puts "File has not changed. Skipping #{path}"
next
# $stderr.puts "File has not changed. Skipping #{path}"
# next
end

@file_checksums[path] = current_checksum
Expand Down
15 changes: 15 additions & 0 deletions lib/ruby_lsp/tapioca/server_addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# frozen_string_literal: true

require "tapioca/internal"
require "objspace"

$runs = 1

module RubyLsp
module Tapioca
Expand All @@ -15,13 +18,25 @@ def execute(request, params)
when "dsl"
dsl(params)
end
reset_runtime_tracker_state
puts "Mixins to constants size: #{ObjectSpace.memsize_of(::Tapioca::Runtime::Trackers::Mixin.instance_variable_get(:@mixins_to_constants))}"
end

private

def dsl(params)
GC.compact
ObjectSpace.trace_object_allocations_start
load("tapioca/cli.rb") # Reload the CLI to reset thor defaults between requests
::Tapioca::Cli.start(["dsl", "--lsp_addon", "--workers=1"] + params[:constants])
# Object.const_get("Shop")
ObjectSpace.trace_object_allocations_stop
ObjectSpace.dump_all(output: File.open("tmp/tapioca-#{$runs}", "w+"))
$runs += 1
end

def reset_runtime_tracker_state
::Tapioca::Runtime::Trackers.reset_state
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/tapioca/commands/abstract_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ def generate_dsl_rbi_files(outpath, quiet:)
files_to_purge = existing_rbi_files - generated_files

files_to_purge
ensure
# pipeline.active_compilers.each(&:reset_state) if @lsp_addon
@pipeline = nil if @lsp_addon
end

sig { returns(T::Array[String]) }
Expand Down
7 changes: 4 additions & 3 deletions lib/tapioca/helpers/rbi_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ def as_non_nilable_type(type)
sig { params(name: String).returns(T::Boolean) }
def valid_method_name?(name)
# try to parse a method definition with this name
iseq = RubyVM::InstructionSequence.compile("def #{name}; end", nil, nil, 0, false)
# iseq = RubyVM::InstructionSequence.compile("def #{name}; end", nil, nil, 0, false)
# pull out the first operation in the instruction sequence and its first argument
op, arg, _data = iseq.to_a.dig(-1, 0)
# op, arg, _data = iseq.to_a.dig(-1, 0)
# make sure that the operation is a method definition and the method that was
# defined has the expected name, for example, for `def !foo; end` we don't get
# a syntax error but instead get a method defined as `"foo"`
op == :definemethod && arg == name.to_sym
# op == :definemethod && arg == name.to_sym
true
rescue SyntaxError
false
end
Expand Down
5 changes: 5 additions & 0 deletions lib/tapioca/runtime/trackers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def disable_all!
def register_tracker(tracker)
@trackers << tracker
end

sig { void }
def reset_state
@trackers.each(&:reset_state)
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/tapioca/runtime/trackers/autoload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def with_disabled_exits(&block)
Kernel.define_method(:abort, original_abort)
end
end

sig { override.void }
def reset_state
@constant_names_registered_for_autoload = []
end
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/tapioca/runtime/trackers/constant_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class ConstantLocation < T::Struct
end

class << self
extend T::Sig

def disable!
@class_tracepoint.disable
@creturn_tracepoint.disable
Expand All @@ -75,6 +77,11 @@ def files_for(klass)
def locations_for(klass)
@class_files.fetch(klass, Set.new)
end

sig { override.void }
def reset_state
@class_files = {}
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/tapioca/runtime/trackers/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ def mixin_location(mixin, mixin_type, constant)
find_or_initialize_mixin_lookup(mixin).dig(mixin_type, constant)
end

sig { override.void }
def reset_state
@constants_to_mixin_locations = {}
@mixins_to_constants = {}
end

private

sig { params(constant: Module, mixin: Module, mixin_type: Type, location: String).void }
Expand Down
5 changes: 5 additions & 0 deletions lib/tapioca/runtime/trackers/required_ancestor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ def required_ancestors_by(mod)
nil
end
end

sig { override.void }
def reset_state
@required_ancestors_map = {}
end
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/tapioca/runtime/trackers/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def with_disabled_tracker(&block)
ensure
@enabled = original_state
end

sig { abstract.void }
def reset_state; end
end
end
end
Expand Down

0 comments on commit 09ac1f6

Please sign in to comment.