aboutsummaryrefslogtreecommitdiff
path: root/cmake/Format.cmake
diff options
context:
space:
mode:
authordundargoc <33953936+dundargoc@users.noreply.github.com>2022-08-02 12:32:57 +0200
committerGitHub <noreply@github.com>2022-08-02 12:32:57 +0200
commit8ce7e7409f10f0a90ed8aa3f6f179c4b5d44eacb (patch)
treedc7bcb32002aa0866cb6a7c6fe72dc3a8fc90979 /cmake/Format.cmake
parentc223875a65dd703e42956e064239369817faa0b2 (diff)
downloadrneovim-8ce7e7409f10f0a90ed8aa3f6f179c4b5d44eacb.tar.gz
rneovim-8ce7e7409f10f0a90ed8aa3f6f179c4b5d44eacb.tar.bz2
rneovim-8ce7e7409f10f0a90ed8aa3f6f179c4b5d44eacb.zip
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.
Diffstat (limited to 'cmake/Format.cmake')
-rw-r--r--cmake/Format.cmake67
1 files changed, 67 insertions, 0 deletions
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()