Class: PlatformosCheck::LanguageServer::DocumentLinkProvider

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

Defined Under Namespace

Classes: DefaultTranslationFile

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.



32
33
34
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 32

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

Class Attribute Details

.app_file_typeObject

Returns the value of attribute app_file_type.



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

def app_file_type
  @app_file_type
end

.default_dirObject

Returns the value of attribute default_dir.



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

def default_dir
  @default_dir
end

.default_extensionObject

Returns the value of attribute default_extension.



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

def default_extension
  @default_extension
end

.partial_regexpObject

Returns the value of attribute partial_regexp.



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

def partial_regexp
  @partial_regexp
end

Class Method Details

.allObject



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

def all
  @all ||= []
end

.inherited(subclass) ⇒ Object



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

def inherited(subclass)
  all << subclass
end

Instance Method Details

#app_file_typeObject



36
37
38
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 36

def app_file_type
  self.class.app_file_type
end

#default_dirObject



40
41
42
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 40

def default_dir
  self.class.default_dir
end

#default_extensionObject



44
45
46
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 44

def default_extension
  self.class.default_extension
end

#default_relative_path(partial) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 134

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


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 52

def document_links(buffer, platformos_app)
  matches(buffer, partial_regexp).map do |match|
    start_row, start_column = start_coordinates(buffer, match)

    end_row, end_column = end_coordinates(buffer, match)

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

#end_coordinates(buffer, match) ⇒ Object



81
82
83
84
85
86
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 81

def end_coordinates(buffer, match)
  from_index_to_row_column(
    buffer,
    match.end(:partial)
  )
end


88
89
90
91
92
93
94
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 88

def file_link(match, platformos_app)
  partial = match[:partial]
  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



48
49
50
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 48

def partial_regexp
  self.class.partial_regexp
end

#start_coordinates(buffer, match) ⇒ Object



74
75
76
77
78
79
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 74

def start_coordinates(buffer, match)
  from_index_to_row_column(
    buffer,
    match.begin(:partial)
  )
end

#translation_components_for_match(match) ⇒ Object

Raises:

  • (NotImplementedError)


126
127
128
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 126

def translation_components_for_match(match)
  raise NotImplementedError
end


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 96

def translation_file_link(match, platformos_app)
  @current_best_fit = platformos_app.translations.first || DefaultTranslationFile.new(platformos_app.default_language)
  @current_best_fit_level = 0
  array_of_translation_components = translation_components_for_match(match)
  platformos_app.translations.each do |translation_file|
    array_of_translation_components.each do |translation_components|
      exact_match_level = translation_components.size
      component_result = translation_file.content[platformos_app.default_language]
      next if component_result.nil?

      i = 0
      while i < exact_match_level
        component_result = yaml(component_result, translation_components[i])

        break if component_result.nil?

        i += 1
        if i > @current_best_fit_level
          @current_best_fit = translation_file
          @current_best_fit_level = i
        end

        break unless component_result.is_a?(Hash)
      end
    end
  end

  file_uri(@storage.path(@current_best_fit&.relative_path))
end

#yaml(component_result, component) ⇒ Object



130
131
132
# File 'lib/platformos_check/language_server/document_link_provider.rb', line 130

def yaml(component_result, component)
  component_result[component]
end