Sha256: 09e83397e18e7981745b9e3334f0e7b5c0b6b1c0a3a21ab6e86c9b9552733e5b

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module ThemeCheck
  module LanguageServer
    class CompletionEngine
      include PositionHelper

      def initialize(storage)
        @storage = storage
        @providers = CompletionProvider.all.map { |x| x.new(storage) }
      end

      def completions(relative_path, line, col)
        buffer = @storage.read(relative_path)
        cursor = from_line_column_to_index(buffer, line, col)
        token = find_token(buffer, cursor)
        return [] if token.nil?

        @providers.flat_map do |p|
          p.completions(
            token.content,
            cursor - token.start
          )
        end
      end

      def find_token(buffer, cursor)
        Tokens.new(buffer).find do |token|
          # Here we include the next character and exclude the first
          # one becase when we want to autocomplete inside a token
          # and at most 1 outside it since the cursor could be placed
          # at the end of the token.
          token.start < cursor && cursor <= token.end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
theme-check-0.8.0 lib/theme_check/language_server/completion_engine.rb
theme-check-0.7.3 lib/theme_check/language_server/completion_engine.rb
theme-check-0.7.2 lib/theme_check/language_server/completion_engine.rb
theme-check-0.7.1 lib/theme_check/language_server/completion_engine.rb
theme-check-0.7.0 lib/theme_check/language_server/completion_engine.rb