Class: RubyLsp::Extension

Inherits:
Object
  • Object
show all
Extended by:
T::Helpers, T::Sig
Defined in:
lib/ruby_lsp/extension.rb

Overview

To register an extension, inherit from this class and implement both ‘name` and `activate`

# Example

“‘ruby module MyGem

class MyExtension < Extension
  def activate
    # Perform any relevant initialization
  end

  def name
    "My extension name"
  end
end

end “‘

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExtension

Returns a new instance of Extension.


67
68
69
# File 'lib/ruby_lsp/extension.rb', line 67

def initialize
  @errors = T.let([], T::Array[StandardError])
end

Class Method Details

.extensionsObject


39
40
41
# File 'lib/ruby_lsp/extension.rb', line 39

def extensions
  @extensions ||= T.let([], T.nilable(T::Array[Extension]))
end

.inherited(child_class) ⇒ Object


33
34
35
36
# File 'lib/ruby_lsp/extension.rb', line 33

def inherited(child_class)
  extensions << child_class.new
  super
end

.load_extensionsObject


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby_lsp/extension.rb', line 45

def load_extensions
  # Require all extensions entry points, which should be placed under
  # `some_gem/lib/ruby_lsp/your_gem_name/extension.rb`
  Gem.find_files("ruby_lsp/**/extension.rb").each do |extension|
    require File.expand_path(extension)
  rescue => e
    warn(e.message)
    warn(e.backtrace.to_s) # rubocop:disable Lint/RedundantStringCoercion
  end

  # Activate each one of the discovered extensions. If any problems occur in the extensions, we don't want to
  # fail to boot the server
  extensions.each do |extension|
    extension.activate
    nil
  rescue => e
    extension.add_error(e)
  end
end

Instance Method Details

#activateObject


98
# File 'lib/ruby_lsp/extension.rb', line 98

def activate; end

#add_error(error) ⇒ Object


72
73
74
75
# File 'lib/ruby_lsp/extension.rb', line 72

def add_error(error)
  @errors << error
  self
end

#backtracesObject


91
92
93
# File 'lib/ruby_lsp/extension.rb', line 91

def backtraces
  @errors.filter_map(&:backtrace).join("\n\n")
end

#error?Boolean

Returns:

  • (Boolean)

78
79
80
# File 'lib/ruby_lsp/extension.rb', line 78

def error?
  @errors.any?
end

#formatted_errorsObject


83
84
85
86
87
88
# File 'lib/ruby_lsp/extension.rb', line 83

def formatted_errors
  <<~ERRORS
    #{name}:
      #{@errors.map(&:message).join("\n")}
  ERRORS
end

#nameObject


102
# File 'lib/ruby_lsp/extension.rb', line 102

def name; end