diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..62f0b20d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,7 @@ +root = true +end_of_line = lf +insert_final_newline = false +charset = utf-8 +trim_trailing_whitespace = true +indent_style = space +indent_size = 2 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..d2732e2b --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,126 @@ +name: CI +on: + pull_request: + branches: + - main + push: + branches: + - main +concurrency: + group: ${{ github.ref }}-ci + cancel-in-progress: true +jobs: + pr_conventional_commit: + name: PR Conventional Commit + if: ${{ github.ref != 'refs/heads/main' }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: ytanikin/PRConventionalCommits@1.1.0 + with: + task_types: '["feat","fix","docs","test","ci","refactor","perf","chore","revert","break"]' + luacheck: + name: Lua check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: stevearc/nvim-typecheck-action@v1 + with: + path: lua + level: Information + configpath: ".luarc.json" + neodev-version: stable + - uses: lunarmodules/luacheck@v1 + with: + args: lua --config .luacheckrc + - uses: JohnnyMorganz/stylua-action@v3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + version: latest + args: --config-path .stylua.toml ./lua ./test + - name: Add json.lua + if: ${{ github.ref != 'refs/heads/main' }} + shell: bash + run: | + echo "pwd" + echo $PWD + git clone --depth=1 https://github.com/actboy168/json.lua.git ~/.json.lua + cp ~/.json.lua/json.lua ./lua/actboy168_json.lua + - uses: stefanzweifel/git-auto-commit-action@v4 + if: ${{ github.ref != 'refs/heads/main' }} + with: + commit_message: "chore(pr): auto-commit" + unit_test: + name: Unit Test + strategy: + matrix: + nvim_version: [stable, nightly, v0.7.0] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: rhysd/action-setup-vim@v1 + id: vim + with: + neovim: true + version: ${{ matrix.nvim_version }} + - uses: leafo/gh-actions-lua@v10 + with: + luaVersion: "luajit-2.1.0-beta3" + - uses: leafo/gh-actions-luarocks@v4 + - name: Run test cases + shell: bash + run: | + luarocks install luacheck + luarocks install luacov + luarocks install cluacov + luarocks install vusted + git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf + ~/.fzf/install + export PATH="$HOME/.fzf/bin:$PATH" + vusted --coverage --shuffle ./test + - name: Generate coverage reports + shell: bash + run: | + echo "ls ." + ls -l . + echo "run luacov" + luacov + echo "ls ." + ls -l . + echo "cat ./luacov.report.out" + cat ./luacov.report.out + - uses: codecov/codecov-action@v3 + with: + files: luacov.report.out + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + release: + name: Release + if: ${{ github.ref == 'refs/heads/main' }} + needs: + - luacheck + - unit_test + runs-on: ubuntu-latest + steps: + - uses: google-github-actions/release-please-action@v4 + id: release + with: + release-type: simple + - uses: actions/checkout@v4 + - uses: rickstaa/action-create-tag@v1 + if: ${{ steps.release.outputs.release_created }} + with: + tag: stable + message: "Current stable release: ${{ steps.release.outputs.tag_name }}" + tag_exists_error: false + force_push_tag: true + - uses: nvim-neorocks/luarocks-tag-release@v5 + if: ${{ steps.release.outputs.release_created }} + env: + LUAROCKS_API_KEY: ${{ secrets.LUAROCKS_API_KEY }} + with: + name: linrongbin16-ci-template.nvim + version: ${{ steps.release.outputs.tag_name }} + labels: | + neovim + vim diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..6f4ab5e2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,43 @@ +# Compiled Lua sources +luac.out + +# luarocks build files +*.src.rock +*.zip +*.tar.gz + +# Object files +*.o +*.os +*.ko +*.obj +*.elf + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo +*.def +*.exp + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# macOS +.DS_Store diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 00000000..7f8e1705 --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,4 @@ +globals = { "vim", "describe", "before_each", "it", "assert" } +max_line_length = 500 +unused = false +unused_args = false diff --git a/.luacov b/.luacov new file mode 100644 index 00000000..7899a5e8 --- /dev/null +++ b/.luacov @@ -0,0 +1,8 @@ +modules = { + ["ci-template"] = "lua/ci-template.lua", + ["ci-template.*"] = "lua", +} + +exclude = { + "lua/actboy168_json.lua", +} diff --git a/.luarc.json b/.luarc.json new file mode 100644 index 00000000..f176d472 --- /dev/null +++ b/.luarc.json @@ -0,0 +1,4 @@ +{ + "diagnostics.globals": ["vim", "describe", "before_each", "it", "jit"], + "workspace.checkThirdParty": "Disable" +} diff --git a/.nvim.lua b/.nvim.lua new file mode 100644 index 00000000..ed7f6bb9 --- /dev/null +++ b/.nvim.lua @@ -0,0 +1,3 @@ +vim.opt.tabstop = 2 +vim.opt.softtabstop = 2 +vim.opt.shiftwidth = 2 diff --git a/.stylua.toml b/.stylua.toml new file mode 100644 index 00000000..e272b1dc --- /dev/null +++ b/.stylua.toml @@ -0,0 +1,4 @@ +column_width = 80 +line_endings = "Unix" +indent_type = "Spaces" +indent_width = 2 diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..06b4b699 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,166 @@ +# Changelog + +## [1.5.5](https://github.com/linrongbin16/ci-template.nvim/compare/v1.5.4...v1.5.5) (2023-12-11) + + +### Bug Fixes + +* **docs:** typing ([#31](https://github.com/linrongbin16/ci-template.nvim/issues/31)) ([e875d30](https://github.com/linrongbin16/ci-template.nvim/commit/e875d3065a5ef294818302b8d8d32ea99e1e0c57)) + + +### Performance Improvements + +* **docs:** squash and merge PRs ([#33](https://github.com/linrongbin16/ci-template.nvim/issues/33)) ([46f3103](https://github.com/linrongbin16/ci-template.nvim/commit/46f310397e53b91620e49da4348a59132469470f)) +* **docs:** use template ([#30](https://github.com/linrongbin16/ci-template.nvim/issues/30)) ([122fe28](https://github.com/linrongbin16/ci-template.nvim/commit/122fe2810fd770b04d855f51bd11784f67421523)) + +## [1.5.4](https://github.com/linrongbin16/ci-template.nvim/compare/v1.5.3...v1.5.4) (2023-12-11) + + +### Performance Improvements + +* **exrc:** add indent size for project local options ([#28](https://github.com/linrongbin16/ci-template.nvim/issues/28)) ([852dadb](https://github.com/linrongbin16/ci-template.nvim/commit/852dadb2d78e08e3cca43b3ab551802bb8f81a25)) + +## [1.5.3](https://github.com/linrongbin16/ci-template.nvim/compare/v1.5.2...v1.5.3) (2023-12-11) + + +### Performance Improvements + +* **docs:** add development ([#26](https://github.com/linrongbin16/ci-template.nvim/issues/26)) ([e99aae2](https://github.com/linrongbin16/ci-template.nvim/commit/e99aae254f369428fb0365f281b212297751cc61)) + +## [1.5.2](https://github.com/linrongbin16/ci-template.nvim/compare/v1.5.1...v1.5.2) (2023-12-11) + + +### Performance Improvements + +* **docs:** add usage ([#24](https://github.com/linrongbin16/ci-template.nvim/issues/24)) ([93bb632](https://github.com/linrongbin16/ci-template.nvim/commit/93bb6326f4fba51441c790bb62144844d6b309d5)) + +## [1.5.1](https://github.com/linrongbin16/ci-template.nvim/compare/v1.5.0...v1.5.1) (2023-12-09) + + +### Performance Improvements + +* **codecov:** exclude json.lua file ([#20](https://github.com/linrongbin16/ci-template.nvim/issues/20)) ([e860434](https://github.com/linrongbin16/ci-template.nvim/commit/e860434f681b4fa4edf229726fbb5a76d3268cf0)) + +## [1.5.0](https://github.com/linrongbin16/ci-template.nvim/compare/v1.4.1...v1.5.0) (2023-12-09) + + +### Features + +* **docs:** badges ([1db7f39](https://github.com/linrongbin16/ci-template.nvim/commit/1db7f393ca59eae83e0d86d4d9fbac218918deb3)) +* **docs:** badges ([ef6f6ef](https://github.com/linrongbin16/ci-template.nvim/commit/ef6f6efae82ec5c55fb4fad188877051d3ab0d81)) + + +### Bug Fixes + +* **docs:** fix minimal require Neovim version ([#19](https://github.com/linrongbin16/ci-template.nvim/issues/19)) ([bce58e3](https://github.com/linrongbin16/ci-template.nvim/commit/bce58e3a601db958b08abf0fbef2bd6801149194)) + +## [1.4.1](https://github.com/linrongbin16/ci-template.nvim/compare/v1.4.0...v1.4.1) (2023-12-09) + + +### Bug Fixes + +* **ci:** remove personal token from release-please ([733ff89](https://github.com/linrongbin16/ci-template.nvim/commit/733ff89d298ea27bfdbd4bc3b44b078679742180)) + +## [1.4.0](https://github.com/linrongbin16/ci-template.nvim/compare/v1.3.0...v1.4.0) (2023-12-09) + + +### Features + +* use personal token for please-release ([92ee485](https://github.com/linrongbin16/ci-template.nvim/commit/92ee4855ec842794a353ffd5b3f343dd02a689fb)) +* use personal token for please-release ([bfd498a](https://github.com/linrongbin16/ci-template.nvim/commit/bfd498aab95bcda96214327e590b737a553fdcf4)) + + +### Bug Fixes + +* ci ([219a0fe](https://github.com/linrongbin16/ci-template.nvim/commit/219a0fe86ddd2b1dcd4c4f8779e8aa4778959785)) +* **ci:** revert luarocks ([1d36abd](https://github.com/linrongbin16/ci-template.nvim/commit/1d36abd0da12b7f716dfc560c21f53721d56e0d2)) +* **ci:** revert luarocks ([97f2bb8](https://github.com/linrongbin16/ci-template.nvim/commit/97f2bb81c05c575c783606e133ff4c78e3195c52)) +* Update ci.yml ([219a0fe](https://github.com/linrongbin16/ci-template.nvim/commit/219a0fe86ddd2b1dcd4c4f8779e8aa4778959785)) + + +### Performance Improvements + +* Create ci-template.lua ([6127e76](https://github.com/linrongbin16/ci-template.nvim/commit/6127e76bac797180d97e1ec75901c2dea6fc5622)) +* Create version.txt ([bd7241f](https://github.com/linrongbin16/ci-template.nvim/commit/bd7241f213cef3e44577069f4a59a67b78e93d2e)) +* explicit name for luarocks ([ea4eb52](https://github.com/linrongbin16/ci-template.nvim/commit/ea4eb526bc46e117d83a80935defd2c1efd34c13)) +* explicit name for luarocks ([ea4eb52](https://github.com/linrongbin16/ci-template.nvim/commit/ea4eb526bc46e117d83a80935defd2c1efd34c13)) +* explicit name for luarocks ([54a8618](https://github.com/linrongbin16/ci-template.nvim/commit/54a861862a90928b87a9bf70066ea1ed94adee9c)) +* rename to ci-template ([6127e76](https://github.com/linrongbin16/ci-template.nvim/commit/6127e76bac797180d97e1ec75901c2dea6fc5622)) +* version.txt ([bd7241f](https://github.com/linrongbin16/ci-template.nvim/commit/bd7241f213cef3e44577069f4a59a67b78e93d2e)) + +## [1.3.0](https://github.com/linrongbin16/ci-template.nvim/compare/v1.2.0...v1.3.0) (2023-12-09) + + +### Features + +* use personal token for please-release ([92ee485](https://github.com/linrongbin16/ci-template.nvim/commit/92ee4855ec842794a353ffd5b3f343dd02a689fb)) +* use personal token for please-release ([bfd498a](https://github.com/linrongbin16/ci-template.nvim/commit/bfd498aab95bcda96214327e590b737a553fdcf4)) + + +### Bug Fixes + +* ci ([219a0fe](https://github.com/linrongbin16/ci-template.nvim/commit/219a0fe86ddd2b1dcd4c4f8779e8aa4778959785)) +* Update ci.yml ([219a0fe](https://github.com/linrongbin16/ci-template.nvim/commit/219a0fe86ddd2b1dcd4c4f8779e8aa4778959785)) + + +### Performance Improvements + +* Create ci-template.lua ([6127e76](https://github.com/linrongbin16/ci-template.nvim/commit/6127e76bac797180d97e1ec75901c2dea6fc5622)) +* Create version.txt ([bd7241f](https://github.com/linrongbin16/ci-template.nvim/commit/bd7241f213cef3e44577069f4a59a67b78e93d2e)) +* explicit name for luarocks ([ea4eb52](https://github.com/linrongbin16/ci-template.nvim/commit/ea4eb526bc46e117d83a80935defd2c1efd34c13)) +* explicit name for luarocks ([ea4eb52](https://github.com/linrongbin16/ci-template.nvim/commit/ea4eb526bc46e117d83a80935defd2c1efd34c13)) +* explicit name for luarocks ([54a8618](https://github.com/linrongbin16/ci-template.nvim/commit/54a861862a90928b87a9bf70066ea1ed94adee9c)) +* rename to ci-template ([6127e76](https://github.com/linrongbin16/ci-template.nvim/commit/6127e76bac797180d97e1ec75901c2dea6fc5622)) +* version.txt ([bd7241f](https://github.com/linrongbin16/ci-template.nvim/commit/bd7241f213cef3e44577069f4a59a67b78e93d2e)) + +## [1.2.0](https://github.com/linrongbin16/ci-template.nvim/compare/v1.1.0...v1.2.0) (2023-12-09) + + +### Features + +* use personal token for please-release ([92ee485](https://github.com/linrongbin16/ci-template.nvim/commit/92ee4855ec842794a353ffd5b3f343dd02a689fb)) +* use personal token for please-release ([bfd498a](https://github.com/linrongbin16/ci-template.nvim/commit/bfd498aab95bcda96214327e590b737a553fdcf4)) + + +### Bug Fixes + +* ci ([219a0fe](https://github.com/linrongbin16/ci-template.nvim/commit/219a0fe86ddd2b1dcd4c4f8779e8aa4778959785)) +* Update ci.yml ([219a0fe](https://github.com/linrongbin16/ci-template.nvim/commit/219a0fe86ddd2b1dcd4c4f8779e8aa4778959785)) + + +### Performance Improvements + +* Create ci-template.lua ([6127e76](https://github.com/linrongbin16/ci-template.nvim/commit/6127e76bac797180d97e1ec75901c2dea6fc5622)) +* Create version.txt ([bd7241f](https://github.com/linrongbin16/ci-template.nvim/commit/bd7241f213cef3e44577069f4a59a67b78e93d2e)) +* explicit name for luarocks ([ea4eb52](https://github.com/linrongbin16/ci-template.nvim/commit/ea4eb526bc46e117d83a80935defd2c1efd34c13)) +* explicit name for luarocks ([ea4eb52](https://github.com/linrongbin16/ci-template.nvim/commit/ea4eb526bc46e117d83a80935defd2c1efd34c13)) +* explicit name for luarocks ([54a8618](https://github.com/linrongbin16/ci-template.nvim/commit/54a861862a90928b87a9bf70066ea1ed94adee9c)) +* rename to ci-template ([6127e76](https://github.com/linrongbin16/ci-template.nvim/commit/6127e76bac797180d97e1ec75901c2dea6fc5622)) +* version.txt ([bd7241f](https://github.com/linrongbin16/ci-template.nvim/commit/bd7241f213cef3e44577069f4a59a67b78e93d2e)) + +## [1.1.0](https://github.com/linrongbin16/ci-template.nvim/compare/v1.0.0...v1.1.0) (2023-12-09) + + +### Features + +* use personal token for please-release ([92ee485](https://github.com/linrongbin16/ci-template.nvim/commit/92ee4855ec842794a353ffd5b3f343dd02a689fb)) +* use personal token for please-release ([bfd498a](https://github.com/linrongbin16/ci-template.nvim/commit/bfd498aab95bcda96214327e590b737a553fdcf4)) + +## 1.0.0 (2023-12-09) + + +### Bug Fixes + +* ci ([219a0fe](https://github.com/linrongbin16/ci-template.nvim/commit/219a0fe86ddd2b1dcd4c4f8779e8aa4778959785)) +* Update ci.yml ([219a0fe](https://github.com/linrongbin16/ci-template.nvim/commit/219a0fe86ddd2b1dcd4c4f8779e8aa4778959785)) + + +### Performance Improvements + +* Create ci-template.lua ([6127e76](https://github.com/linrongbin16/ci-template.nvim/commit/6127e76bac797180d97e1ec75901c2dea6fc5622)) +* Create version.txt ([bd7241f](https://github.com/linrongbin16/ci-template.nvim/commit/bd7241f213cef3e44577069f4a59a67b78e93d2e)) +* explicit name for luarocks ([ea4eb52](https://github.com/linrongbin16/ci-template.nvim/commit/ea4eb526bc46e117d83a80935defd2c1efd34c13)) +* explicit name for luarocks ([ea4eb52](https://github.com/linrongbin16/ci-template.nvim/commit/ea4eb526bc46e117d83a80935defd2c1efd34c13)) +* explicit name for luarocks ([54a8618](https://github.com/linrongbin16/ci-template.nvim/commit/54a861862a90928b87a9bf70066ea1ed94adee9c)) +* rename to ci-template ([6127e76](https://github.com/linrongbin16/ci-template.nvim/commit/6127e76bac797180d97e1ec75901c2dea6fc5622)) +* version.txt ([bd7241f](https://github.com/linrongbin16/ci-template.nvim/commit/bd7241f213cef3e44577069f4a59a67b78e93d2e)) diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..3d51ed3a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 linrongbin16 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 00000000..27b69166 --- /dev/null +++ b/README.md @@ -0,0 +1,86 @@ +# ci-template.nvim + +
+ ++CI template for Neovim plugin project. +
+ +## Table of Contents + +- [Requirements](#requirements) +- [Actions](#actions) +- [Documents](#documents) +- [Usage](#usage) + - [Initialize](#initialize) + - [Development](#development) + +## Requirements + +- [CodeCov](https://about.codecov.io/) token: upload CodeCov report. +- [LuaRocks](https://luarocks.org/) API token: upload LuaRocks package. + +## Actions + +It runs the following actions: + +For PR branch: + +1. conventional PR commits check. +2. luacheck. +3. LuaLs annotations typecheck. +4. stylua code format. +5. download and install json.lua as an embed json library (for low-version Neovim). +6. run vusted (busted) unit tests for 3 version of Neovim: minimal required (0.7+), stable and nightly. + +Additionally for main branch: + +1. release-please (highly recommend [squash and merge commits](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/incorporating-changes-from-a-pull-request/about-pull-request-merges#squash-and-merge-your-commits) when merge PRs). +2. upload luarocks package (only for created tags). + +## Documents + +It provides 4 badges for README.md: + +1. Minimal required Neovim version. +1. LuaRocks package version. +1. GitHub CI running status. +1. Code coverage. + +## Usage + +### Initialize + +1. Click the **_Use this template_** button (in the top right) to create new Neovim plugin project. +2. Remove the [CHANGELOG.md](https://github.com/linrongbin16/ci-template.nvim/blob/8ba994d7a64c52bb3a4a046068a510f54219aacd/CHANGELOG.md?plain=1#L1) (it's only for **_this_** project, you don't want it). +3. Replace the word `linrongbin16`: + - `README.md`: [badges](https://github.com/linrongbin16/ci-template.nvim/blob/9313f7927b133abe342ee4fa1758fb438c6a9c57/README.md?plain=1#L4-L7). + - `LICENSE`: [user name](https://github.com/linrongbin16/ci-template.nvim/blob/9313f7927b133abe342ee4fa1758fb438c6a9c57/LICENSE?plain=1#L3). + - `ci.yml`: [luarocks package](https://github.com/linrongbin16/ci-template.nvim/blob/d4a39cecc5384884d2c1d9d205d3503ab266ec21/.github/workflows/ci.yml?plain=1#L122). +4. Replace the word `ci-template`: + - `ci.yml`: [luarocks package](https://github.com/linrongbin16/ci-template.nvim/blob/d4a39cecc5384884d2c1d9d205d3503ab266ec21/.github/workflows/ci.yml?plain=1#L122). + - `.luacov`: [lua modules](https://github.com/linrongbin16/ci-template.nvim/blob/792fcc25184f0ac3f20c2037ed6a4ae48f4c28d3/.luacov?plain=1#L2-L3). +5. Rename the file name: + - [ci-template.lua](https://github.com/linrongbin16/ci-template.nvim/blob/203b21999ccd1e43a7e3d5d26e690ff75aeee403/lua/ci-template.lua). + - [ci_template_spec.lua](https://github.com/linrongbin16/ci-template.nvim/blob/203b21999ccd1e43a7e3d5d26e690ff75aeee403/test/ci_template_spec.lua). +6. Reset the version: + - `version.txt`: [version number](https://github.com/linrongbin16/ci-template.nvim/blob/792fcc25184f0ac3f20c2037ed6a4ae48f4c28d3/version.txt?plain=1#L1). +7. Reset the indent size (by default 2): + - `.editorconfig`: [indent_size](https://github.com/linrongbin16/ci-template.nvim/blob/7de9e40f84d53d9d07d3206e4979347a942cbd30/.editorconfig?plain=1#L7). + - `.stylua.toml`: [indent_size](https://github.com/linrongbin16/ci-template.nvim/blob/792fcc25184f0ac3f20c2037ed6a4ae48f4c28d3/.stylua.toml?plain=1#L4). + - `.nvim.lua` (optional if you enabled `exrc`): [shiftwidth](https://github.com/linrongbin16/ci-template.nvim/blob/b752ecd228a2a3307123315f22bee97bf8760544/.nvim.lua?plain=1#L1-L3). + +### Development + +Setup the plugin development with: + +- [lua-language-server](https://github.com/LuaLS/lua-language-server): language server. +- [stylua](https://github.com/JohnnyMorganz/StyLua): code format. +- [luacheck](https://github.com/lunarmodules/luacheck): code static check. +- [luarocks](https://luarocks.org/): package management for vusted/busted/luacov. +- [vusted](https://github.com/notomo/vusted): unit test. diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 00000000..6e8b822f --- /dev/null +++ b/codecov.yml @@ -0,0 +1,10 @@ +coverage: + status: + project: + default: + threshold: 90% + patch: + default: + threshold: 90% +ignore: + - "lua/actboy168_json.lua" diff --git a/lua/actboy168_json.lua b/lua/actboy168_json.lua new file mode 100644 index 00000000..42e27aa2 --- /dev/null +++ b/lua/actboy168_json.lua @@ -0,0 +1,557 @@ +local type = type +local next = next +local error = error +local tonumber = tonumber +local tostring = tostring +local table_concat = table.concat +local table_sort = table.sort +local string_char = string.char +local string_byte = string.byte +local string_find = string.find +local string_match = string.match +local string_gsub = string.gsub +local string_sub = string.sub +local string_format = string.format +local setmetatable = setmetatable +local getmetatable = getmetatable +local huge = math.huge +local tiny = -huge + +local utf8_char +local math_type + +if _VERSION == "Lua 5.1" or _VERSION == "Lua 5.2" then + local math_floor = math.floor + function utf8_char(c) + if c <= 0x7f then + return string_char(c) + elseif c <= 0x7ff then + return string_char(math_floor(c / 64) + 192, c % 64 + 128) + elseif c <= 0xffff then + return string_char( + math_floor(c / 4096) + 224, + math_floor(c % 4096 / 64) + 128, + c % 64 + 128 + ) + elseif c <= 0x10ffff then + return string_char( + math_floor(c / 262144) + 240, + math_floor(c % 262144 / 4096) + 128, + math_floor(c % 4096 / 64) + 128, + c % 64 + 128 + ) + end + error(string_format("invalid UTF-8 code '%x'", c)) + end + + function math_type(v) + if v >= -2147483648 and v <= 2147483647 and math_floor(v) == v then + return "integer" + end + return "float" + end +else + utf8_char = utf8.char + math_type = math.type +end + +local json = {} + +json.supportSparseArray = true + +local objectMt = {} + +function json.createEmptyObject() + return setmetatable({}, objectMt) +end + +function json.isObject(t) + if t[1] ~= nil then + return false + end + return next(t) ~= nil or getmetatable(t) == objectMt +end + +if debug and debug.upvalueid then + -- Generate a lightuserdata + json.null = debug.upvalueid(json.createEmptyObject, 1) +else + json.null = function () end +end + +-- json.encode -- +local statusVisited +local statusBuilder + +local encode_map = {} + +local encode_escape_map = { + ["\""] = "\\\"", + ["\\"] = "\\\\", + ["/"] = "\\/", + ["\b"] = "\\b", + ["\f"] = "\\f", + ["\n"] = "\\n", + ["\r"] = "\\r", + ["\t"] = "\\t", +} + +local decode_escape_set = {} +local decode_escape_map = {} +for k, v in next, encode_escape_map do + decode_escape_map[v] = k + decode_escape_set[string_byte(v, 2)] = true +end + +for i = 0, 31 do + local c = string_char(i) + if not encode_escape_map[c] then + encode_escape_map[c] = string_format("\\u%04x", i) + end +end + +local function encode(v) + local res = encode_map[type(v)](v) + statusBuilder[#statusBuilder+1] = res +end + +encode_map["nil"] = function () + return "null" +end + +local function encode_string(v) + return string_gsub(v, '[%z\1-\31\\"]', encode_escape_map) +end + +function encode_map.string(v) + statusBuilder[#statusBuilder+1] = '"' + statusBuilder[#statusBuilder+1] = encode_string(v) + return '"' +end + +local function convertreal(v) + local g = string_format("%.16g", v) + if tonumber(g) == v then + return g + end + return string_format("%.17g", v) +end + +if string_match(tostring(1 / 2), "%p") == "," then + local _convertreal = convertreal + function convertreal(v) + return string_gsub(_convertreal(v), ",", ".") + end +end + +function encode_map.number(v) + if v ~= v or v <= tiny or v >= huge then + error("unexpected number value '"..tostring(v).."'") + end + if math_type(v) == "integer" then + return string_format("%d", v) + end + return convertreal(v) +end + +function encode_map.boolean(v) + if v then + return "true" + else + return "false" + end +end + +function encode_map.table(t) + local first_val = next(t) + if first_val == nil then + if getmetatable(t) == objectMt then + return "{}" + else + return "[]" + end + end + if statusVisited[t] then + error("circular reference") + end + statusVisited[t] = true + if type(first_val) == "string" then + local keys = {} + for k in next, t do + if type(k) ~= "string" then + error("invalid table: mixed or invalid key types: "..k) + end + keys[#keys+1] = k + end + table_sort(keys) + do + local k = keys[1] + statusBuilder[#statusBuilder+1] = '{"' + statusBuilder[#statusBuilder+1] = encode_string(k) + statusBuilder[#statusBuilder+1] = '":' + encode(t[k]) + end + for i = 2, #keys do + local k = keys[i] + statusBuilder[#statusBuilder+1] = ',"' + statusBuilder[#statusBuilder+1] = encode_string(k) + statusBuilder[#statusBuilder+1] = '":' + encode(t[k]) + end + statusVisited[t] = nil + return "}" + elseif json.supportSparseArray then + local max = 0 + for k in next, t do + if math_type(k) ~= "integer" or k <= 0 then + error("invalid table: mixed or invalid key types: "..k) + end + if max < k then + max = k + end + end + statusBuilder[#statusBuilder+1] = "[" + encode(t[1]) + for i = 2, max do + statusBuilder[#statusBuilder+1] = "," + encode(t[i]) + end + statusVisited[t] = nil + return "]" + else + if t[1] == nil then + error("invalid table: sparse array is not supported") + end + if jit and t[0] ~= nil then + -- 0 is the first index in luajit + error("invalid table: mixed or invalid key types: "..0) + end + statusBuilder[#statusBuilder+1] = "[" + encode(t[1]) + local count = 2 + while t[count] ~= nil do + statusBuilder[#statusBuilder+1] = "," + encode(t[count]) + count = count + 1 + end + if next(t, count - 1) ~= nil then + local k = next(t, count - 1) + if type(k) == "number" then + error("invalid table: sparse array is not supported") + else + error("invalid table: mixed or invalid key types: "..k) + end + end + statusVisited[t] = nil + return "]" + end +end + +local function encode_unexpected(v) + if v == json.null then + return "null" + else + error("unexpected type '"..type(v).."'") + end +end +encode_map["function"] = encode_unexpected +encode_map["userdata"] = encode_unexpected +encode_map["thread"] = encode_unexpected + +function json.encode(v) + statusVisited = {} + statusBuilder = {} + encode(v) + return table_concat(statusBuilder) +end + +json._encode_map = encode_map +json._encode_string = encode_string + +-- json.decode -- + +local statusBuf +local statusPos +local statusTop +local statusAry = {} +local statusRef = {} + +local function find_line() + local line = 1 + local pos = 1 + while true do + local f, _, nl1, nl2 = string_find(statusBuf, "([\n\r])([\n\r]?)", pos) + if not f then + return line, statusPos - pos + 1 + end + local newpos = f + ((nl1 == nl2 or nl2 == "") and 1 or 2) + if newpos > statusPos then + return line, statusPos - pos + 1 + end + pos = newpos + line = line + 1 + end +end + +local function decode_error(msg) + error(string_format("ERROR: %s at line %d col %d", msg, find_line()), 2) +end + +local function get_word() + return string_match(statusBuf, "^[^ \t\r\n%]},]*", statusPos) +end + +local function next_byte() + local pos = string_find(statusBuf, "[^ \t\r\n]", statusPos) + if pos then + statusPos = pos + return string_byte(statusBuf, pos) + end + return -1 +end + +local function consume_byte(c) + local _, pos = string_find(statusBuf, c, statusPos) + if pos then + statusPos = pos + 1 + return true + end +end + +local function expect_byte(c) + local _, pos = string_find(statusBuf, c, statusPos) + if not pos then + decode_error(string_format("expected '%s'", string_sub(c, #c))) + end + statusPos = pos +end + +local function decode_unicode_surrogate(s1, s2) + return utf8_char(0x10000 + (tonumber(s1, 16) - 0xd800) * 0x400 + (tonumber(s2, 16) - 0xdc00)) +end + +local function decode_unicode_escape(s) + return utf8_char(tonumber(s, 16)) +end + +local function decode_string() + local has_unicode_escape = false + local has_escape = false + local i = statusPos + 1 + while true do + i = string_find(statusBuf, '[%z\1-\31\\"]', i) + if not i then + decode_error "expected closing quote for string" + end + local x = string_byte(statusBuf, i) + if x < 32 then + statusPos = i + decode_error "control character in string" + end + if x == 34 --[[ '"' ]] then + local s = string_sub(statusBuf, statusPos + 1, i - 1) + if has_unicode_escape then + s = string_gsub(string_gsub(s + , "\\u([dD][89aAbB]%x%x)\\u([dD][c-fC-F]%x%x)", decode_unicode_surrogate) + , "\\u(%x%x%x%x)", decode_unicode_escape) + end + if has_escape then + s = string_gsub(s, "\\.", decode_escape_map) + end + statusPos = i + 1 + return s + end + --assert(x == 92 --[[ "\\" ]]) + local nx = string_byte(statusBuf, i + 1) + if nx == 117 --[[ "u" ]] then + if not string_match(statusBuf, "^%x%x%x%x", i + 2) then + statusPos = i + decode_error "invalid unicode escape in string" + end + has_unicode_escape = true + i = i + 6 + else + if not decode_escape_set[nx] then + statusPos = i + decode_error("invalid escape char '"..(nx and string_char(nx) or "