mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
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:
parent
c223875a65
commit
8ce7e7409f
@ -675,6 +675,19 @@ add_dependencies(lintcommit nvim)
|
||||
add_custom_target(lint)
|
||||
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(
|
||||
FILES ${CMAKE_SOURCE_DIR}/src/man/nvim.1
|
||||
DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
|
||||
@ -797,4 +810,3 @@ add_custom_target(uninstall
|
||||
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
add_subdirectory(cmake.packaging)
|
||||
endif()
|
||||
|
||||
|
@ -218,6 +218,11 @@ You can lint a single file (but this will _not_ exclude legacy errors):
|
||||
|
||||
### 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
|
||||
the [style-guide]. To use the Nvim `gq` command with `uncrustify`:
|
||||
```
|
||||
|
4
Makefile
4
Makefile
@ -137,7 +137,7 @@ helphtml: | nvim build/runtime/doc/tags
|
||||
functionaltest functionaltest-lua unittest benchmark: | nvim
|
||||
$(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 $@
|
||||
|
||||
test: functionaltest unittest
|
||||
@ -174,4 +174,4 @@ $(DEPS_BUILD_DIR)/%: phony_force
|
||||
$(BUILD_TOOL) -C $(DEPS_BUILD_DIR) $(patsubst $(DEPS_BUILD_DIR)/%,%,$@)
|
||||
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
67
cmake/Format.cmake
Normal 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()
|
@ -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
|
@ -778,9 +778,16 @@ add_glob_targets(
|
||||
FLAGS -c "${PROJECT_SOURCE_DIR}/src/uncrustify.cfg" -q --check
|
||||
FILES ${LINT_NVIM_SOURCES}
|
||||
)
|
||||
|
||||
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(
|
||||
lintcfull
|
||||
COMMAND
|
||||
|
Loading…
Reference in New Issue
Block a user