From e9009b39db0fd3e7e98dacaa0d289287738b41b0 Mon Sep 17 00:00:00 2001 From: Maple Ong Date: Thu, 12 May 2022 20:48:44 -0400 Subject: [PATCH] Move encoder into the Requests::Support namespace --- lib/ruby_lsp/encoder/relative.rb | 50 --------------------- lib/ruby_lsp/handler.rb | 3 +- lib/ruby_lsp/requests.rb | 1 + test/requests/semantic_highlighting_test.rb | 3 +- 4 files changed, 4 insertions(+), 53 deletions(-) delete mode 100644 lib/ruby_lsp/encoder/relative.rb diff --git a/lib/ruby_lsp/encoder/relative.rb b/lib/ruby_lsp/encoder/relative.rb deleted file mode 100644 index 83957d08cb..0000000000 --- a/lib/ruby_lsp/encoder/relative.rb +++ /dev/null @@ -1,50 +0,0 @@ -# frozen_string_literal: true - -module RubyLsp - module Encoder - class Relative - def initialize - @current_row = 0 - @current_column = 0 - end - - def encode(tokens) - tokens = sort(tokens) - - delta = tokens.flat_map do |token| - compute_delta(token) - end - - LanguageServer::Protocol::Interface::SemanticTokens.new(data: delta) - end - - # The delta array is computed according to the LSP specification: - # > The protocol for the token format relative uses relative - # > positions, because most tokens remain stable relative to - # > each other when edits are made in a file. This simplifies - # > the computation of a delta if a server supports it. So each - # > token is represented using 5 integers. - - # For more information on how each number is calculated, read: - # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_semanticTokens - def compute_delta(token) - row = token.location.start_line - 1 - column = token.location.start_column - delta_line = row - @current_row - - delta_column = column - delta_column -= @current_column if delta_line == 0 - - [delta_line, delta_column, token.length, token.type, token.modifier] - ensure - @current_row = row - @current_column = column - end - - def sort(tokens) - tokens.sort_by { |token| token.location.start_column } - .sort_by { |token| token.location.start_line } - end - end - end -end diff --git a/lib/ruby_lsp/handler.rb b/lib/ruby_lsp/handler.rb index 47c4c111f8..cd0ea0e956 100644 --- a/lib/ruby_lsp/handler.rb +++ b/lib/ruby_lsp/handler.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require "ruby_lsp/encoder/relative" require "ruby_lsp/requests" require "ruby_lsp/store" require "benchmark" @@ -126,7 +125,7 @@ def respond_with_selection_ranges(uri, positions) def respond_with_semantic_highlighting(uri) store.cache_fetch(uri, :semantic_highlighting) do |document| - Requests::SemanticHighlighting.new(document, encoder: Encoder::Relative).run + Requests::SemanticHighlighting.new(document, encoder: Requests::Support::SemanticTokenEncoder).run end end diff --git a/lib/ruby_lsp/requests.rb b/lib/ruby_lsp/requests.rb index d8ac795bc8..0dcbe1eb2f 100644 --- a/lib/ruby_lsp/requests.rb +++ b/lib/ruby_lsp/requests.rb @@ -15,6 +15,7 @@ module Requests module Support autoload :RuboCopDiagnostic, "ruby_lsp/requests/support/rubocop_diagnostic" autoload :SelectionRange, "ruby_lsp/requests/support/selection_range" + autoload :SemanticTokenEncoder, "ruby_lsp/requests/support/semantic_token_encoder" autoload :SyntaxErrorDiagnostic, "ruby_lsp/requests/support/syntax_error_diagnostic" end end diff --git a/test/requests/semantic_highlighting_test.rb b/test/requests/semantic_highlighting_test.rb index 7f382339e7..273630e527 100644 --- a/test/requests/semantic_highlighting_test.rb +++ b/test/requests/semantic_highlighting_test.rb @@ -223,7 +223,8 @@ def some_method def assert_tokens(expected, source_code) document = RubyLsp::Document.new(source_code) - tokens = RubyLsp::Requests::SemanticHighlighting.new(document, encoder: RubyLsp::Encoder::Relative).run + tokens = RubyLsp::Requests::SemanticHighlighting.new(document, + encoder: RubyLsp::Requests::Support::SemanticTokenEncoder).run assert_equal( inline_tokens(expected),