diff options
-rw-r--r-- | CMakeLists.txt | 14 | ||||
-rw-r--r-- | CONTRIBUTING.md | 5 | ||||
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | cmake/Format.cmake | 67 | ||||
-rwxr-xr-x | scripts/uncrustify.sh | 11 | ||||
-rwxr-xr-x | src/nvim/CMakeLists.txt | 9 |
6 files changed, 95 insertions, 15 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c54e5749b..caf9658699 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() - diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e26d0d63c5..17622fa33a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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`: ``` @@ -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 diff --git a/cmake/Format.cmake b/cmake/Format.cmake new file mode 100644 index 0000000000..4115e66705 --- /dev/null +++ b/cmake/Format.cmake @@ -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() diff --git a/scripts/uncrustify.sh b/scripts/uncrustify.sh deleted file mode 100755 index ac5d542c29..0000000000 --- a/scripts/uncrustify.sh +++ /dev/null @@ -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 diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index f302f25ad6..017883a913 100755 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -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 |