Sha256: 9dd3eae9d9aaff6a0bfad492df7baf34fd07898d0aba6fe72c502041610d7532

Contents?: true

Size: 1.56 KB

Versions: 28

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module ThemeCheck
  module LanguageServer
    class FilterCompletionProvider < CompletionProvider
      NAMED_FILTER = /#{Liquid::FilterSeparator}\s*(\w+)/o

      def completions(content, cursor)
        return [] unless can_complete?(content, cursor)
        available_labels
          .select { |w| w.start_with?(partial(content, cursor)) }
          .map { |filter| filter_to_completion(filter) }
      end

      def can_complete?(content, cursor)
        content.match?(Liquid::FilterSeparator) && (
          cursor_on_start_content?(content, cursor, Liquid::FilterSeparator) ||
          cursor_on_filter?(content, cursor)
        )
      end

      private

      def available_labels
        @labels ||= ShopifyLiquid::Filter.labels - ShopifyLiquid::DeprecatedFilter.labels
      end

      def cursor_on_filter?(content, cursor)
        return false unless content.match?(NAMED_FILTER)
        matches(content, NAMED_FILTER).any? do |match|
          match.begin(1) <= cursor && cursor < match.end(1) + 1 # including next character
        end
      end

      def partial(content, cursor)
        return '' unless content.match?(NAMED_FILTER)
        partial_match = matches(content, NAMED_FILTER).find do |match|
          match.begin(1) <= cursor && cursor < match.end(1) + 1 # including next character
        end
        return '' if partial_match.nil?
        partial_match[1]
      end

      def filter_to_completion(filter)
        {
          label: filter,
          kind: CompletionItemKinds::FUNCTION,
        }
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
theme-check-1.11.0 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.10.3 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.10.2 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.10.1 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.10.0 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.9.2 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.9.1 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.9.0 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.8.0 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.7.2 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.7.1 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.7.0 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.6.2 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.6.1 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.6.0 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.5.2 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.5.1 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.5.0 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.4.0 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb
theme-check-1.3.0 lib/theme_check/language_server/completion_providers/filter_completion_provider.rb