From e5d7003b02c9af96c51ea5638e07eea25057a216 Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Mon, 4 Dec 2023 20:21:38 +0100 Subject: [PATCH] build: rework formatting to use add_glob_target This will ensure that we can pass flags and make adjustments from the top level cmake file instead of digging through the cmake directory. More importantly, this will only format files that have been changed. This has a slightly higher initial cost compared to previous solution as all files must be initially formatted, but the gained speed up should more than make up for it quickly. `make formatlua` is always run due to a quirk of stylua of always changing modification time of the file regardless if there were any changes. This is not a major blocker as stylua is very fast. --- .luacheckrc | 1 + CMakeLists.txt | 17 ++++------- cmake/Format.cmake | 65 ----------------------------------------- cmake/Util.cmake | 1 + src/nvim/CMakeLists.txt | 11 ++++--- 5 files changed, 13 insertions(+), 82 deletions(-) delete mode 100644 cmake/Format.cmake diff --git a/.luacheckrc b/.luacheckrc index 701f461dc4..d54c61e9e7 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -47,5 +47,6 @@ exclude_files = { 'runtime/lua/vim/_meta/vimfn.lua', 'runtime/lua/vim/_meta/api.lua', 'runtime/lua/vim/re.lua', + 'runtime/lua/coxpcall.lua', 'src/nvim/eval.lua', } diff --git a/CMakeLists.txt b/CMakeLists.txt index 475a1a2c3e..6701b0211b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -226,9 +226,6 @@ add_glob_target( FLAGS -ll ${PROJECT_SOURCE_DIR}/test/lua_runner.lua ${CMAKE_BINARY_DIR}/usr luacheck -q GLOB_DIRS runtime/ scripts/ src/ test/ GLOB_PAT *.lua - EXCLUDE - runtime/lua/vim/_meta/.* - runtime/lua/coxpcall.lua TOUCH_STRATEGY SINGLE) add_dependencies(lintlua-luacheck lua-dev-deps) @@ -238,8 +235,6 @@ add_glob_target( FLAGS --color=always --check --respect-ignores GLOB_DIRS runtime/ GLOB_PAT *.lua - EXCLUDE - /runtime/lua/vim/_meta TOUCH_STRATEGY SINGLE) add_custom_target(lintlua) @@ -261,12 +256,12 @@ add_custom_target(lint) add_dependencies(lint lintc lintlua lintsh lintcommit) # Format -add_custom_target(formatlua - COMMAND ${CMAKE_COMMAND} - -D FORMAT_PRG=${STYLUA_PRG} - -D LANG=lua - -P ${PROJECT_SOURCE_DIR}/cmake/Format.cmake - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}) +add_glob_target( + TARGET formatlua + COMMAND ${STYLUA_PRG} + FLAGS --respect-ignores + GLOB_DIRS runtime + GLOB_PAT *.lua) add_custom_target(format) add_dependencies(format formatc formatlua) diff --git a/cmake/Format.cmake b/cmake/Format.cmake deleted file mode 100644 index 7097e5766f..0000000000 --- a/cmake/Format.cmake +++ /dev/null @@ -1,65 +0,0 @@ -# Returns a list of all files that has been changed in current branch compared -# to master branch. This includes unstaged, staged and committed files. -function(get_changed_files outvar) - execute_process( - COMMAND git branch --show-current - OUTPUT_VARIABLE current_branch - OUTPUT_STRIP_TRAILING_WHITESPACE) - - execute_process( - COMMAND git merge-base master HEAD - OUTPUT_VARIABLE ancestor_commit - OUTPUT_STRIP_TRAILING_WHITESPACE) - - # Changed files that have been committed - execute_process( - COMMAND git diff --diff-filter=d --name-only ${ancestor_commit}...${current_branch} - OUTPUT_VARIABLE committed_files - OUTPUT_STRIP_TRAILING_WHITESPACE) - separate_arguments(committed_files NATIVE_COMMAND ${committed_files}) - - # Unstaged files - execute_process( - COMMAND git diff --diff-filter=d --name-only - OUTPUT_VARIABLE unstaged_files - OUTPUT_STRIP_TRAILING_WHITESPACE) - separate_arguments(unstaged_files NATIVE_COMMAND ${unstaged_files}) - - # Staged files - execute_process( - COMMAND git diff --diff-filter=d --cached --name-only - OUTPUT_VARIABLE staged_files - OUTPUT_STRIP_TRAILING_WHITESPACE) - separate_arguments(staged_files NATIVE_COMMAND ${staged_files}) - - set(files ${committed_files} ${unstaged_files} ${staged_files}) - list(REMOVE_DUPLICATES files) - - set(${outvar} "${files}" PARENT_SCOPE) -endfunction() - -get_changed_files(changed_files) - -if(LANG STREQUAL c) - list(FILTER changed_files INCLUDE REGEX "\\.[ch]$") - list(FILTER changed_files INCLUDE REGEX "^src/nvim/") - - if(changed_files) - if(FORMAT_PRG) - execute_process(COMMAND ${FORMAT_PRG} -c "src/uncrustify.cfg" --replace --no-backup ${changed_files}) - else() - message(STATUS "Uncrustify not found. Skip formatting C files.") - endif() - endif() -elseif(LANG STREQUAL lua) - list(FILTER changed_files INCLUDE REGEX "\\.lua$") - list(FILTER changed_files INCLUDE REGEX "^runtime/") - - if(changed_files) - if(FORMAT_PRG) - execute_process(COMMAND ${FORMAT_PRG} ${changed_files}) - else() - message(STATUS "Stylua not found. Skip formatting lua files.") - endif() - endif() -endif() diff --git a/cmake/Util.cmake b/cmake/Util.cmake index 01d34d6752..4723ad7e08 100644 --- a/cmake/Util.cmake +++ b/cmake/Util.cmake @@ -75,6 +75,7 @@ function(add_glob_target) list(APPEND ARG_FILES ${globfiles}) endforeach() + list(APPEND ARG_EXCLUDE runtime/lua/vim/_meta) # only generated files, always ignore foreach(exclude_pattern ${ARG_EXCLUDE}) list(FILTER ARG_FILES EXCLUDE REGEX ${exclude_pattern}) endforeach() diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index 6a01632040..0cdce539eb 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -879,12 +879,11 @@ add_glob_target( FLAGS -c ${UNCRUSTIFY_CONFIG} -q --check FILES ${LINT_NVIM_SOURCES}) -add_custom_target(formatc - COMMAND ${CMAKE_COMMAND} - -D FORMAT_PRG=${UNCRUSTIFY_PRG} - -D LANG=c - -P ${PROJECT_SOURCE_DIR}/cmake/Format.cmake - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}) +add_glob_target( + TARGET formatc + COMMAND ${UNCRUSTIFY_PRG} + FLAGS -c ${UNCRUSTIFY_CONFIG} --replace --no-backup + FILES ${LINT_NVIM_SOURCES}) add_dependencies(lintc-uncrustify uncrustify_update_config) add_dependencies(formatc uncrustify_update_config)