Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion app/lib/language_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@ def self.data
base = YAML.load_file(Rails.root.join("config/languages.yml"))
custom_path = Rails.root.join("config/languages_custom.yml")
custom = File.exist?(custom_path) ? YAML.load_file(custom_path) : {}
base.deep_merge(custom)
merged = base.deep_merge(custom) { |_key, base_val, custom_val|
base_val.is_a?(Array) && custom_val.is_a?(Array) ? base_val | custom_val : custom_val
}
custom.each do |name, info|
next unless info.key?("extensions")
merged_extensions = merged.dig(name, "extensions") || []
merged_extensions.each do |ext|
merged.each do |other_name, other_info|
next if other_name == name
other_info["extensions"]&.delete(ext)
end
end
end
merged
end
end

Expand Down
6 changes: 6 additions & 0 deletions config/languages_custom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ Lapse:
AsciiDoc:
extensions:
- ".ad"

Assembly:
extensions:
- ".asm"
- ".s"
- ".S"
Comment thread
SeradedStripes marked this conversation as resolved.
33 changes: 33 additions & 0 deletions test/lib/language_utils_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "test_helper"

class LanguageUtilsTest < Minitest::Test
def setup
LanguageUtils.instance_variable_set(:@data, nil)
LanguageUtils.instance_variable_set(:@extension_map, nil)
LanguageUtils.instance_variable_set(:@alias_map, nil)
LanguageUtils.instance_variable_set(:@filename_map, nil)
end

def teardown
LanguageUtils.instance_variable_set(:@data, nil)
LanguageUtils.instance_variable_set(:@extension_map, nil)
LanguageUtils.instance_variable_set(:@alias_map, nil)
LanguageUtils.instance_variable_set(:@filename_map, nil)
end

def test_custom_assembly_extensions_override_other_languages
assert_equal "Assembly", LanguageUtils.detect_from_extension("foo.asm")
assert_equal "Assembly", LanguageUtils.detect_from_extension("foo.a51")
assert_equal "Assembly", LanguageUtils.detect_from_extension("foo.nasm")
assert_equal "Assembly", LanguageUtils.detect_from_extension("foo.s")
assert_equal "Assembly", LanguageUtils.detect_from_extension("foo.S")
end

def test_custom_language_additions
assert_equal "AsciiDoc", LanguageUtils.detect_from_extension("foo.ad")
end

def test_custom_language_without_extension_conflict
assert_equal "Lapse", LanguageUtils.find_name("Lapse")
end
end