build: add formatting targets for c and lua files (#19488)

The targets will only format files that have been changed in current
branch compared to the master branch. This includes unstaged, staged and
committed files.

Add following make and cmake targets:
formatc   - format changed c files
formatlua - format changed lua files
format    - run formatc and formatlua

Remove scripts/uncrustify.sh as this deprecates it.
This commit is contained in:
dundargoc 2022-08-02 12:32:57 +02:00 committed by GitHub
parent c223875a65
commit 8ce7e7409f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 95 additions and 15 deletions

View File

@ -675,6 +675,19 @@ add_dependencies(lintcommit nvim)
add_custom_target(lint) add_custom_target(lint)
add_dependencies(lint check-single-includes lintc lintlua lintpy lintsh lintcommit lintuncrustify) add_dependencies(lint check-single-includes lintc lintlua lintpy lintsh lintcommit lintuncrustify)
#
# 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_custom_target(format)
add_dependencies(format formatc formatlua)
install_helper( install_helper(
FILES ${CMAKE_SOURCE_DIR}/src/man/nvim.1 FILES ${CMAKE_SOURCE_DIR}/src/man/nvim.1
DESTINATION ${CMAKE_INSTALL_MANDIR}/man1) DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
@ -797,4 +810,3 @@ add_custom_target(uninstall
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory(cmake.packaging) add_subdirectory(cmake.packaging)
endif() endif()

View File

@ -218,6 +218,11 @@ You can lint a single file (but this will _not_ exclude legacy errors):
### Style ### Style
- You can format files by using:
```
make format
```
This will format changed Lua and C files with all appropriate flags set.
- Style rules are (mostly) defined by `src/uncrustify.cfg` which tries to match - Style rules are (mostly) defined by `src/uncrustify.cfg` which tries to match
the [style-guide]. To use the Nvim `gq` command with `uncrustify`: the [style-guide]. To use the Nvim `gq` command with `uncrustify`:
``` ```

View File

@ -137,7 +137,7 @@ helphtml: | nvim build/runtime/doc/tags
functionaltest functionaltest-lua unittest benchmark: | nvim functionaltest functionaltest-lua unittest benchmark: | nvim
$(BUILD_TOOL) -C build $@ $(BUILD_TOOL) -C build $@
lintlua lintsh lintpy lintuncrustify lintc lintcfull check-single-includes generated-sources lintcommit lint: | build/.ran-cmake lintlua lintsh lintpy lintuncrustify lintc lintcfull check-single-includes generated-sources lintcommit lint formatc formatlua format: | build/.ran-cmake
$(CMAKE_PRG) --build build --target $@ $(CMAKE_PRG) --build build --target $@
test: functionaltest unittest test: functionaltest unittest
@ -174,4 +174,4 @@ $(DEPS_BUILD_DIR)/%: phony_force
$(BUILD_TOOL) -C $(DEPS_BUILD_DIR) $(patsubst $(DEPS_BUILD_DIR)/%,%,$@) $(BUILD_TOOL) -C $(DEPS_BUILD_DIR) $(patsubst $(DEPS_BUILD_DIR)/%,%,$@)
endif endif
.PHONY: test lintlua lintpy lintsh functionaltest unittest lint lintc clean distclean nvim libnvim cmake deps install appimage checkprefix lintcommit .PHONY: test lintlua lintpy lintsh functionaltest unittest lint lintc clean distclean nvim libnvim cmake deps install appimage checkprefix lintcommit formatc formatlua format

67
cmake/Format.cmake Normal file
View File

@ -0,0 +1,67 @@
# 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)
set(default_branch master)
execute_process(
COMMAND git branch --show-current
OUTPUT_VARIABLE current_branch
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(
COMMAND git merge-base ${default_branch} ${current_branch}
OUTPUT_VARIABLE ancestor_commit
OUTPUT_STRIP_TRAILING_WHITESPACE)
# Changed files that have been committed
execute_process(
COMMAND git diff --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 --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 --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

@ -1,11 +0,0 @@
#!/usr/bin/env bash
set -e
# Check that you have uncrustify
hash uncrustify
COMMITISH="${1:-master}"
for file in $(git diff --diff-filter=d --name-only $COMMITISH | grep '\.[ch]$'); do
uncrustify -c src/uncrustify.cfg -l C --replace --no-backup "$file"
done

View File

@ -778,9 +778,16 @@ add_glob_targets(
FLAGS -c "${PROJECT_SOURCE_DIR}/src/uncrustify.cfg" -q --check FLAGS -c "${PROJECT_SOURCE_DIR}/src/uncrustify.cfg" -q --check
FILES ${LINT_NVIM_SOURCES} FILES ${LINT_NVIM_SOURCES}
) )
add_dependencies(lintuncrustify uncrustify-version) add_dependencies(lintuncrustify uncrustify-version)
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_dependencies(formatc uncrustify-version)
add_custom_target( add_custom_target(
lintcfull lintcfull
COMMAND COMMAND