Class: PlatformosCheck::LanguageServer::DocumentLinkProvider

Inherits:
Object
  • Object
show all
Includes:
URIHelper, PositionHelper, RegexHelpers
Defined in:
lib/platformos_check/language_server/document_link_provider.rb

Constant Summary

Constants included from RegexHelpers

RegexHelpers::HTML_LIQUID_PLACEHOLDER, RegexHelpers::LIQUID_TAG, RegexHelpers::LIQUID_TAG_OR_VARIABLE, RegexHelpers::LIQUID_VARIABLE, RegexHelpers::START_OR_END_QUOTE

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from URIHelper

#file_path, #file_uri

Methods included from PositionHelper

#bounded, #from_index_to_row_column, #from_row_column_to_index

Methods included from RegexHelpers

#matches

Constructor Details

#initialize(storage = InMemoryStorage.new) ⇒ DocumentLinkProvider

Returns a new instance of DocumentLinkProvider.



22
23
24
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 22

def initialize(storage = InMemoryStorage.new)
  @storage = storage
end

Class Attribute Details

.app_file_typeObject

Returns the value of attribute app_file_type.



11
12
13
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 11

def app_file_type
  @app_file_type
end

.default_dirObject

Returns the value of attribute default_dir.



11
12
13
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 11

def default_dir
  @default_dir
end

.default_extensionObject

Returns the value of attribute default_extension.



11
12
13
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 11

def default_extension
  @default_extension
end

.partial_regexpObject

Returns the value of attribute partial_regexp.



11
12
13
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 11

def partial_regexp
  @partial_regexp
end

Class Method Details

.allObject



13
14
15
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 13

def all
  @all ||= []
end

.inherited(subclass) ⇒ Object



17
18
19
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 17

def inherited(subclass)
  all << subclass
end

Instance Method Details

#app_file_typeObject



26
27
28
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 26

def app_file_type
  self.class.app_file_type
end

#default_dirObject



30
31
32
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 30

def default_dir
  self.class.default_dir
end

#default_extensionObject



34
35
36
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 34

def default_extension
  self.class.default_extension
end

#default_relative_path(partial) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 77

def default_relative_path(partial)
  return Pathname.new("app/#{default_dir}/#{partial}#{default_extension}") unless partial.start_with?('modules/')

  partial_components = partial.split(File::SEPARATOR)
  module_prefix = partial_components.shift(2).join(File::SEPARATOR)
  partial_without_module = partial_components.join(File::SEPARATOR)

  Pathname.new("#{module_prefix}/public/#{default_dir}/#{partial_without_module}#{default_extension}")
end


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 42

def document_links(buffer, platformos_app)
  matches(buffer, partial_regexp).map do |match|
    start_row, start_column = from_index_to_row_column(
      buffer,
      match.begin(:partial)
    )

    end_row, end_column = from_index_to_row_column(
      buffer,
      match.end(:partial)
    )

    {
      target: file_link(match[:partial], platformos_app),
      range: {
        start: {
          line: start_row,
          character: start_column
        },
        end: {
          line: end_row,
          character: end_column
        }
      }
    }
  end
end


70
71
72
73
74
75
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 70

def file_link(partial, platformos_app)
  relative_path = platformos_app.send(app_file_type).detect { |f| f.name == partial }&.relative_path
  relative_path ||= default_relative_path(partial)

  file_uri(@storage.path(relative_path))
end

#partial_regexpObject



38
39
40
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 38

def partial_regexp
  self.class.partial_regexp
end