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.
This commit is contained in:
dundargoc 2023-12-04 20:21:38 +01:00 committed by GitHub
parent 66f1563c7a
commit e5d7003b02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 82 deletions

View File

@ -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',
}

View File

@ -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)

View File

@ -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()

View File

@ -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()

View File

@ -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)