Skip to content

Commit 7b9a92d

Browse files
authored
Merge pull request swiftlang#136 from google/colordiff
Add a script to view before/after formatting diffs for debugging.
2 parents 7ac5318 + 3fecde5 commit 7b9a92d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Scripts/format-diff.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
#===----------------------------------------------------------------------===#
3+
#
4+
# This source file is part of the Swift Formatter open source project.
5+
#
6+
# Copyright (c) 2018 Apple Inc. and the Swift Formatter project authors
7+
# Licensed under Apache License v2.0
8+
#
9+
# See LICENSE.txt for license information
10+
# See CONTRIBUTORS.txt for the list of Swift Formatter project authors
11+
#
12+
# SPDX-License-Identifier: Apache-2.0
13+
#
14+
#===----------------------------------------------------------------------===#
15+
16+
# SYNOPSIS
17+
# format-diff.sh FILE [OPTION]...
18+
#
19+
# DESCRIPTION
20+
# Runs the formatter and displays a side-by-side diff of the original file
21+
# and the formatted results. The script will use `colordiff` for the output
22+
# if it is present; otherwise, regular `diff` will be used.
23+
#
24+
# The first argument to this script must be the `.swift` source file to be
25+
# formatted. Any remaining arguments after that will be passed directly to
26+
# `swift-format`.
27+
28+
set -euo pipefail
29+
30+
SRCFILE="$1" ; shift
31+
32+
# Use `colordiff` if it's present; otherwise, fall back to `diff`.
33+
if which colordiff >/dev/null ; then
34+
DIFF="$(which colordiff)"
35+
else
36+
DIFF="$(which diff)"
37+
fi
38+
39+
# Make sure the formatter is built in debug mode so we can reference the
40+
# executable easily.
41+
swift build --product swift-format
42+
43+
# Run a side-by-side diff with the original source file on the left and the
44+
# formatted output on the right.
45+
"$DIFF" -y -W 210 "$SRCFILE" <(.build/debug/swift-format -p "$@" "$SRCFILE")

0 commit comments

Comments
 (0)