Sha256: 6ea26f5993f9a1be22937756d94706e848a5719dca711939ebc984e8a4ae979e

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

# typed: strict
# frozen_string_literal: true

require "ruby_lsp/listeners/semantic_highlighting"

module RubyLsp
  module Requests
    # ![Semantic highlighting demo](../../semantic_highlighting.gif)
    #
    # The [semantic
    # highlighting](https://microsoft.github.io/language-server-protocol/specification#textDocument_semanticTokens)
    # request informs the editor of the correct token types to provide consistent and accurate highlighting for themes.
    #
    # # Example
    #
    # ```ruby
    # def foo
    #   var = 1 # --> semantic highlighting: local variable
    #   some_invocation # --> semantic highlighting: method invocation
    #   var # --> semantic highlighting: local variable
    # end
    # ```
    class SemanticHighlighting < Request
      extend T::Sig

      class << self
        extend T::Sig

        sig { returns(Interface::SemanticTokensRegistrationOptions) }
        def provider
          Interface::SemanticTokensRegistrationOptions.new(
            document_selector: [{ language: "ruby" }],
            legend: Interface::SemanticTokensLegend.new(
              token_types: ResponseBuilders::SemanticHighlighting::TOKEN_TYPES.keys,
              token_modifiers: ResponseBuilders::SemanticHighlighting::TOKEN_MODIFIERS.keys,
            ),
            range: true,
            full: { delta: false },
          )
        end
      end

      sig { params(dispatcher: Prism::Dispatcher, range: T.nilable(T::Range[Integer])).void }
      def initialize(dispatcher, range: nil)
        super()
        @response_builder = T.let(ResponseBuilders::SemanticHighlighting.new, ResponseBuilders::SemanticHighlighting)
        Listeners::SemanticHighlighting.new(dispatcher, @response_builder, range: range)

        Addon.addons.each do |addon|
          addon.create_semantic_highlighting_listener(@response_builder, dispatcher)
        end
      end

      sig { override.returns(Interface::SemanticTokens) }
      def perform
        @response_builder.response
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ruby-lsp-0.16.4 lib/ruby_lsp/requests/semantic_highlighting.rb
ruby-lsp-0.16.3 lib/ruby_lsp/requests/semantic_highlighting.rb
ruby-lsp-0.16.2 lib/ruby_lsp/requests/semantic_highlighting.rb
ruby-lsp-0.16.1 lib/ruby_lsp/requests/semantic_highlighting.rb
ruby-lsp-0.16.0 lib/ruby_lsp/requests/semantic_highlighting.rb