Skip to content

Instantly share code, notes, and snippets.

@BuonOmo
Created September 25, 2023 00:35
Show Gist options
  • Save BuonOmo/0425a964ef7bf2ff835f27fc68477e1b to your computer and use it in GitHub Desktop.
Save BuonOmo/0425a964ef7bf2ff835f27fc68477e1b to your computer and use it in GitHub Desktop.
Generate a `.zed/settings.json` file from `.editorconfig`
# frozen_string_literal: true
require "bundler/inline"
gemfile(true, quiet: 1) do
source "https://rubygems.org"
gem "editorconfig"
end
require "json"
config, _root = EditorConfig.parse(File.read(".editorconfig"))
LANGS = {
"*.sh" => "Shell Script", "*.bash" => "Shell Script", "*.zsh" => "Shell Script",
"*.rb" => "Ruby",
"*.json" => "JSON",
"*.js" => "JavaScript",
"*.c" => "C", "*.h" => "C", "*.[ch]" => "C", "*.[hc]" => "C",
"*.cc" => "C++", "*.cpp" => "C++",
"*.py" => "Python",
}
CONFIG_NAME_MAP = {
"indent_style" => {
key: "hard_tabs",
value: proc { _1 != "true" }
},
"indent_size" => {
key: "tab_size",
value: proc { _1 }
},
"insert_final_newline" => {
key: "ensure_final_newline_on_save",
value: proc { _1 == "true" }
},
"trim_trailing_whitespace" => {
key: "remove_trailing_whitespace_on_save",
value: proc { _1 == "true" }
},
}
settings = {}
def append_settings(settings, conf)
conf.each do |key, value|
if CONFIG_NAME_MAP.key?(key)
newkey = CONFIG_NAME_MAP[key][:key]
newvalue = CONFIG_NAME_MAP[key][:value].call(value)
settings[newkey] = newvalue
else
raise "Unhandled config: #{key}"
end
end
end
config.each do |section, conf|
if section == "*"
append_settings(settings, conf)
elsif LANGS.key?(section)
lang = LANGS[section]
settings["language_overrides"] ||= {}
settings["language_overrides"][lang] ||= {}
append_settings(settings["language_overrides"][lang], conf)
else
raise "Unhandled pattern: #{section}"
end
end
Dir.mkdir(".zed")
File.write(".zed/settings.json", JSON.pretty_generate(settings))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment