diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..0514a27e6 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +insert_final_newline = true +end_of_line = lf + +[*.lua] +indent_style = space +indent_size = 2 diff --git a/.github/workflows/pre-commit-autoupdate.yml b/.github/workflows/pre-commit-autoupdate.yml new file mode 100644 index 000000000..a0e2a7683 --- /dev/null +++ b/.github/workflows/pre-commit-autoupdate.yml @@ -0,0 +1,27 @@ +name: Pre-commit autoupdate +on: + schedule: + - cron: "0 0 * * *" + workflow_dispatch: +permissions: + contents: write + pull-requests: write +jobs: + auto-update: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4.5.0 + - run: pip install pre-commit + shell: bash + - run: pre-commit autoupdate + shell: bash + - run: pre-commit run --all-files + shell: bash + - uses: peter-evans/create-pull-request@v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + branch: update/pre-commit-hooks + title: "chore: update pre-commit hooks" + commit-message: "chore: update pre-commit hooks" + body: Update versions of pre-commit hooks to latest version. diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 000000000..eeebf4a85 --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,7 @@ +max_line_length = 120 + +globals = { + "vim", + "jit", + "bit", +} diff --git a/.luarc.json b/.luarc.json new file mode 100644 index 000000000..bcb6b47a5 --- /dev/null +++ b/.luarc.json @@ -0,0 +1,12 @@ +{ + "$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json", + "runtime.version" : "Lua 5.1", + "diagnostics": { + "globals": [ + "vim", + "jit", + "bit" + ] + } +} + diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..8db5f9b95 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,21 @@ +repos: + - repo: https://github.com/johnnymorganz/stylua + rev: v0.17.0 + hooks: + - id: stylua-github + - repo: local + hooks: + - id: luacheck + name: Luacheck + description: Lints Lua files using Luacheck. + entry: luacheck -- + language: system + types: [lua] + - repo: local + hooks: + - id: lighttheme + name: Light Color Scheme Generator + description: Ensures Light Color Scheme version has been generated. + entry: nvim --headless -c 'source scripts/generate_colors.lua' -c 'qall' + language: system + types: [lua] diff --git a/.stylua.toml b/.stylua.toml new file mode 100644 index 000000000..ecb6dca5a --- /dev/null +++ b/.stylua.toml @@ -0,0 +1,6 @@ +column_width = 120 +line_endings = "Unix" +indent_type = "Spaces" +indent_width = 2 +quote_style = "AutoPreferDouble" +call_parentheses = "None" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c45f11032..b6c4b0faf 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,6 +10,43 @@ Please name your commits and the PR simply e.g. update makefile icon update .kt colors +## Prerequisites + +Add [vim-colortemplate](https://github.com/lifepillar/vim-colortemplate) to &runtimepath. The easiest way to do this is via your package manager. + +Code is formatted using stylua and linted using luacheck. + +You can install these with: +```sh +cargo install stylua +luarocks install luacheck +``` + +or via your OS package manager e.g. Arch linux: +```sh +pacman -S stylua +pacman -S luacheck +``` + +## Building + +Following your changes, please run: + +```sh +make +``` + +This will: +1. Generate cterm colors +2. Generate light color variants +3. Check style +4. Lint + +You can automatically fix any style issues via: +```sh +make style-fix +``` + ## Generate Colors Add or update icons in `scripts/nvim-web-devicons.lua`. @@ -37,8 +74,6 @@ ___Key/value pairs must appear in the same exact order!___ - `cterm_color` must be below `color`, and it must contain a number (any number) - the correct value for `cterm_color` will be generated by the script -Add [vim-colortemplate](https://github.com/lifepillar/vim-colortemplate) to &runtimepath. The easiest way to do this is via your package manager. - Ensure your current working directory is the repo root. Run `make`. This will: - Update `cterm_color` based on `color` @@ -46,10 +81,6 @@ Run `make`. This will: Please commit both `nvim-web-devicons.lua` and `nvim-web-devicons-light.lua` -## Styling And Format - -TODO @alex-courtis - ## Pull Request Please reference any issues in the description e.g. "resolves #1234". diff --git a/Makefile b/Makefile index 44ab4a642..bdfcc998f 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,15 @@ +all: colors style-check lint + colors: nvim --headless -c 'source scripts/generate_colors.lua' -c 'qall' -all: colors +style-check: + stylua . --check + +style-fix: + stylua . + +lint: + luacheck . + +.PHONY: all colors style-check style-fix lint diff --git a/lua/nvim-web-devicons-light.lua b/lua/nvim-web-devicons-light.lua index 36ac72159..2462161e9 100644 --- a/lua/nvim-web-devicons-light.lua +++ b/lua/nvim-web-devicons-light.lua @@ -203,15 +203,15 @@ local icons_by_filename = { cterm_color = "22", name = "Vimrc", }, - ['package.json'] = { + ["package.json"] = { icon = "", color = "#ae1d38", - name = "PackageJson" + name = "PackageJson", }, - ['package-lock.json'] = { + ["package-lock.json"] = { icon = "", color = "#7a0d21", - name = "PackageLockJson" + name = "PackageLockJson", }, ["node_modules"] = { icon = "", @@ -591,7 +591,7 @@ local icons_by_file_extension = { color = "#33312b", icon = "🌜", cterm_color = "236", - name = "Fennel" + name = "Fennel", }, ["fish"] = { icon = "", @@ -669,13 +669,13 @@ local icons_by_file_extension = { icon = "", color = "#ac2880", cterm_color = "126", - name = "GraphQL" + name = "GraphQL", }, ["gql"] = { icon = "", color = "#ac2880", cterm_color = "126", - name = "GraphQL" + name = "GraphQL", }, ["h"] = { icon = "", @@ -825,13 +825,13 @@ local icons_by_file_extension = { icon = "", color = "#158197", cterm_color = "31", - name = "JavaScriptReactTest" + name = "JavaScriptReactTest", }, ["spec.jsx"] = { icon = "", color = "#158197", cterm_color = "31", - name = "JavaScriptReactSpec" + name = "JavaScriptReactSpec", }, ["ksh"] = { icon = "", @@ -1544,5 +1544,5 @@ local icons_by_file_extension = { return { icons_by_filename = icons_by_filename, - icons_by_file_extension = icons_by_file_extension + icons_by_file_extension = icons_by_file_extension, } diff --git a/lua/nvim-web-devicons.lua b/lua/nvim-web-devicons.lua index 6286dc09e..9d54e7502 100644 --- a/lua/nvim-web-devicons.lua +++ b/lua/nvim-web-devicons.lua @@ -1,36 +1,4 @@ --- References: --- 1. Linguist: https://github.com/github/linguist --- 2. coc-explorer: https://github.com/weirongxu/coc-explorer/blob/59bd41f8fffdc871fbd77ac443548426bd31d2c3/src/icons.nerdfont.json#L2 --- 3. chad-tree: https://github.com/ms-jpq/chadtree/blob/f9f333c062/artifacts/icons.json --- jquery = { --- icon = "", --- color = "#1B75BB" --- }, --- angular = { --- icon = "", --- color = "#E23237" --- }, --- backbone = { --- icon = "", --- color = "#0071B5" --- }, --- requirejs = { --- icon = "", --- color = "#F44A41" --- }, --- materialize = { --- icon = "", --- color = "#EE6E73" --- }, --- mootools = { --- icon = "", --- color = "#ECECEC" --- }, --- puppet = { --- icon = "", --- color = "#ffa61a" --- }, - +-- exact match by file name local icons_by_filename = { [".babelrc"] = { icon = "ﬥ", @@ -236,15 +204,15 @@ local icons_by_filename = { cterm_color = "28", name = "Vimrc", }, - ['package.json'] = { + ["package.json"] = { icon = "", color = "#e8274b", - name = "PackageJson" + name = "PackageJson", }, - ['package-lock.json'] = { + ["package-lock.json"] = { icon = "", color = "#7a0d21", - name = "PackageLockJson" + name = "PackageLockJson", }, ["node_modules"] = { icon = "", @@ -320,6 +288,7 @@ local icons_by_filename = { }, } +-- fuzzy match by extension local icons_by_file_extension = { ["ai"] = { icon = "", @@ -624,7 +593,7 @@ local icons_by_file_extension = { color = "#fff3d7", icon = "🌜", cterm_color = "230", - name = "Fennel" + name = "Fennel", }, ["fish"] = { icon = "", @@ -702,13 +671,13 @@ local icons_by_file_extension = { icon = "", color = "#e535ab", cterm_color = "199", - name = "GraphQL" + name = "GraphQL", }, ["gql"] = { icon = "", color = "#e535ab", cterm_color = "199", - name = "GraphQL" + name = "GraphQL", }, ["h"] = { icon = "", @@ -858,13 +827,13 @@ local icons_by_file_extension = { icon = "", color = "#20c2e3", cterm_color = "45", - name = "JavaScriptReactTest" + name = "JavaScriptReactTest", }, ["spec.jsx"] = { icon = "", color = "#20c2e3", cterm_color = "45", - name = "JavaScriptReactSpec" + name = "JavaScriptReactSpec", }, ["ksh"] = { icon = "", @@ -1581,7 +1550,7 @@ local icons -- Set the current icons tables, depending on the 'background' option. local function refresh_icons() local by_filename, by_file_extension - if vim.o.background == 'light' then + if vim.o.background == "light" then by_filename = require("nvim-web-devicons-light").icons_by_filename by_file_extension = require("nvim-web-devicons-light").icons_by_file_extension else @@ -1788,7 +1757,7 @@ local global_opts = { local function get_highlight_name(data) if not global_opts.color_icons then - data = default_icon + data = default_icon end return data.name and "DevIcon" .. data.name @@ -1797,21 +1766,23 @@ end local nvim_set_hl = vim.api.nvim_set_hl local function set_up_highlight(icon_data) if not global_opts.color_icons then - icon_data = default_icon + icon_data = default_icon end local hl_group = get_highlight_name(icon_data) if hl_group and (icon_data.color or icon_data.cterm_color) then - nvim_set_hl(0, get_highlight_name(icon_data), { - fg = icon_data.color, - ctermfg = tonumber(icon_data.cterm_color), - }) + nvim_set_hl(0, get_highlight_name(icon_data), { + fg = icon_data.color, + ctermfg = tonumber(icon_data.cterm_color), + }) end end local nvim_get_hl_by_name = vim.api.nvim_get_hl_by_name local function highlight_exists(group) - if not group then return end + if not group then + return + end local ok, hl = pcall(nvim_get_hl_by_name, group, true) return ok and not (hl or {})[true] @@ -1834,7 +1805,7 @@ end local function get_highlight_foreground(icon_data) if not global_opts.color_icons then - icon_data = default_icon + icon_data = default_icon end return string.format("#%06x", nvim_get_hl_by_name(get_highlight_name(icon_data), true).foreground) @@ -1842,7 +1813,7 @@ end local function get_highlight_ctermfg(icon_data) if not global_opts.color_icons then - icon_data = default_icon + icon_data = default_icon end return nvim_get_hl_by_name(get_highlight_name(icon_data), false).foreground @@ -1877,13 +1848,8 @@ local function setup(opts) local user_filename_icons = user_icons.override_by_filename local user_file_ext_icons = user_icons.override_by_extension - icons = vim.tbl_extend( - "force", - icons, - user_icons.override or {}, - user_filename_icons or {}, - user_file_ext_icons or {} - ) + icons = + vim.tbl_extend("force", icons, user_icons.override or {}, user_filename_icons or {}, user_file_ext_icons or {}) if user_filename_icons then icons_by_filename = vim.tbl_extend("force", icons_by_filename, user_filename_icons) @@ -1897,9 +1863,9 @@ local function setup(opts) set_up_highlights() vim.api.nvim_create_autocmd("ColorScheme", { - desc = "Re-apply icon colors after changing colorschemes", - group = vim.api.nvim_create_augroup("NvimWebDevicons", { clear = true }), - callback = set_up_highlights, + desc = "Re-apply icon colors after changing colorschemes", + group = vim.api.nvim_create_augroup("NvimWebDevicons", { clear = true }), + callback = set_up_highlights, }) end @@ -1913,7 +1879,7 @@ local function iterate_multi_dotted_extension(name, icon_table) return nil end - local compound_ext = name:match("%.(.*)") + local compound_ext = name:match "%.(.*)" local icon = icon_table[compound_ext] if icon then return icon @@ -1935,7 +1901,7 @@ end local function get_icon(name, ext, opts) if type(name) == "string" then - name = name:lower() + name = name:lower() end if not loaded then @@ -1964,7 +1930,7 @@ local function get_icon_by_filetype(ft, opts) local name = get_icon_name_by_filetype(ft) opts = opts or {} opts.strict = false - return get_icon(name or '', nil, opts) + return get_icon(name or "", nil, opts) end local function get_icon_colors(name, ext, opts) @@ -1994,7 +1960,7 @@ end local function get_icon_colors_by_filetype(ft, opts) local name = get_icon_name_by_filetype(ft) - return get_icon_colors(name or '', nil, opts) + return get_icon_colors(name or "", nil, opts) end local function get_icon_color(name, ext, opts) @@ -2006,7 +1972,7 @@ local function get_icon_color_by_filetype(ft, opts) local name = get_icon_name_by_filetype(ft) opts = opts or {} opts.strict = false - return get_icon_color(name or '', nil, opts) + return get_icon_color(name or "", nil, opts) end local function get_icon_cterm_color(name, ext, opts) @@ -2016,13 +1982,13 @@ end local function get_icon_cterm_color_by_filetype(ft, opts) local name = get_icon_name_by_filetype(ft) - return get_icon_cterm_color(name or '', nil, opts) + return get_icon_cterm_color(name or "", nil, opts) end local function set_icon(user_icons) icons = vim.tbl_extend("force", icons, user_icons or {}) if not global_opts.color_icons then - return + return end for _, icon_data in pairs(user_icons) do @@ -2065,4 +2031,3 @@ return { end, set_up_highlights = set_up_highlights, } - diff --git a/scripts/filetype-generator.sh b/scripts/filetype-generator.sh index 4b0036478..902c6ef4a 100755 --- a/scripts/filetype-generator.sh +++ b/scripts/filetype-generator.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh ## Credit: @B0o (Maddison Hellstrom) diff --git a/scripts/generate_colors.lua b/scripts/generate_colors.lua index 22389a19c..2cf7fb827 100644 --- a/scripts/generate_colors.lua +++ b/scripts/generate_colors.lua @@ -10,11 +10,11 @@ local fn = vim.fn if not jit then - error("Neovim must be LuaJIT-enabled to source this script") + error "Neovim must be LuaJIT-enabled to source this script" end -if fn.filereadable("lua/nvim-web-devicons.lua") == 0 then - error("lua/nvim-web-devicons.lua not found") +if fn.filereadable "lua/nvim-web-devicons.lua" == 0 then + error "lua/nvim-web-devicons.lua not found" end local rc, err = pcall(vim.fn["colortemplate#colorspace#approx"], "#000000") @@ -33,7 +33,7 @@ local light12 = 255 / 2 local light13 = 255 / 3 local function darken_color(rrggbb) - local r, g, b = rrggbb:match"%#(%x%x)(%x%x)(%x%x)" + local r, g, b = rrggbb:match "%#(%x%x)(%x%x)(%x%x)" r, g, b = tonumber("0x" .. r), tonumber("0x" .. g), tonumber("0x" .. b) -- luminance formula: see https://stackoverflow.com/a/596243 local lum = 0.299 * r + 0.587 * g + 0.114 * b @@ -65,35 +65,38 @@ end local function update_cterm_colors() -- move to first line - vim.cmd(":1") + vim.cmd ":1" local last = 0 while true do - local cur = fn.search("^\\s*color =") + local cur = fn.search "^\\s*color =" if cur < last then break end last = cur - local color = fn.getline("."):match("%#......") - if fn.search("^\\s*cterm_color") < cur then + local color = fn.getline("."):match "%#......" + if fn.search "^\\s*cterm_color" < cur then break end - if fn.getline("."):find("%d") then - fn.setline(".", fn.substitute(fn.getline("."), '\\d\\+', function() - return tostring(vim.fn["colortemplate#colorspace#approx"](color).index) - end, "")) + if fn.getline("."):find "%d" then + fn.setline( + ".", + fn.substitute(fn.getline ".", "\\d\\+", function() + return tostring(vim.fn["colortemplate#colorspace#approx"](color).index) + end, "") + ) end end end local function generate_lines() - local start = fn.line(".") - 1 - fn.search("^}") - local finish = fn.line(".") + local start = fn.line "." - 1 + fn.search "^}" + local finish = fn.line "." local lines = vim.api.nvim_buf_get_lines(fn.bufnr(), start, finish, true) for i = 1, #lines do - if lines[i]:find("^%s*color =") then - lines[i] = lines[i]:gsub('%#%x+', function(m) + if lines[i]:find "^%s*color =" then + lines[i] = lines[i]:gsub("%#%x+", function(m) return darken_color(m) end) end @@ -106,60 +109,60 @@ end -- Generate file with icons for light backgrounds -------------------------------------------------------------------------------- -vim.cmd("noswapfile drop lua/nvim-web-devicons.lua") +vim.cmd "noswapfile drop lua/nvim-web-devicons.lua" -print("Generating file with icons for light backgrounds...") +print "Generating file with icons for light backgrounds..." -- move to first line -vim.cmd(":1") +vim.cmd ":1" -- first table -if fn.search("^local icons_by_filename") == 0 then - error("Table not found!") +if fn.search "^local icons_by_filename" == 0 then + error "Table not found!" end local lines = generate_lines() -- second table -if fn.search("^local icons_by_file_extension") == 0 then - error("Table not found!") +if fn.search "^local icons_by_file_extension" == 0 then + error "Table not found!" end local lines2 = generate_lines() table.insert(lines2, "return {") table.insert(lines2, " icons_by_filename = icons_by_filename,") -table.insert(lines2, " icons_by_file_extension = icons_by_file_extension") +table.insert(lines2, " icons_by_file_extension = icons_by_file_extension,") table.insert(lines2, "}") -- write both tables to file fn.writefile(lines, "lua/nvim-web-devicons-light.lua") fn.writefile(lines2, "lua/nvim-web-devicons-light.lua", "a") -print("Finished creating new file!") +print "Finished creating new file!" -------------------------------------------------------------------------------- -- Update cterm colors for dark background -------------------------------------------------------------------------------- -print("Generating cterm colors for dark background...\n") +print "Generating cterm colors for dark background...\n" update_cterm_colors() -vim.cmd("wall!") -print("Finished!") +vim.cmd "wall!" +print "Finished!" -------------------------------------------------------------------------------- -- Generate cterm colors for light background -------------------------------------------------------------------------------- -if fn.filereadable("lua/nvim-web-devicons-light.lua") == 0 then - error("lua/nvim-web-devicons-light.lua not found!") +if fn.filereadable "lua/nvim-web-devicons-light.lua" == 0 then + error "lua/nvim-web-devicons-light.lua not found!" end -vim.cmd("noswapfile drop lua/nvim-web-devicons-light.lua") +vim.cmd "noswapfile drop lua/nvim-web-devicons-light.lua" -print("Generating cterm colors for light background...\n") +print "Generating cterm colors for light background...\n" update_cterm_colors() -vim.cmd(":1") -vim.cmd("wall!") -print("Finished!") +vim.cmd ":1" +vim.cmd "wall!" +print "Finished!\n"