Sha256: 43162462225ced1d22f739889d048e785bd3285e44704f47179f760e3dd37a19

Contents?: true

Size: 1.74 KB

Versions: 21

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module PlatformosCheck
  module LanguageServer
    module PartialCompletionProvider
      def completions(context)
        content = context.buffer.lines[context.line]
        cursor = context.col
        @file_name = nil

        return [] if content.nil?
        return [] unless cursor_on_quoted_argument?(content, cursor)

        files
          .select { |x| x.name.start_with?(@file_name) }
          .map { |x| file_to_completion(x, context) }
      end

      private

      def cursor_on_quoted_argument?(content, cursor)
        @match = content.match(regexp)
        return false if @match.nil?

        return false unless @match.begin(:partial) <= cursor && cursor <= @match.end(:partial)

        @file_name = @match[:partial][0, cursor - @match.begin(:partial)]
        true
      end

      def files
        raise NotImplementedError
      end

      def regexp
        raise NotImplementedError
      end

      def file_to_completion(file, context)
        {
          label: file.name,
          kind: CompletionItemKinds::TEXT,
          detail: file.source,
          textEdit: {
            newText: file.name,
            insert: {
              start: {
                line: context.line,
                character: @match.begin(:partial)
              },
              end: {
                line: context.line,
                character: @match.end(:partial)
              }
            },
            replace: {
              start: {
                line: context.line,
                character: @match.begin(:partial)
              },
              end: {
                line: context.line,
                character: @match.end(:partial)
              }
            }
          }
        }
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
platformos-check-0.4.14 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.4.13 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.4.12 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.4.11 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.4.10 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.4.9 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.4.8 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.4.7 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.4.6 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.4.5 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.4.4 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.4.3 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.4.2 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.4.1 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.4.0 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.3.3 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.3.1 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.3.0 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.2.2 lib/platformos_check/language_server/partial_completion_provider.rb
platformos-check-0.2.1 lib/platformos_check/language_server/partial_completion_provider.rb