Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the --caret-diagnostic flag #3256

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,14 @@
self.FILE = "\033[33m"
self.WWORD = "\033[31m"
self.FWORD = "\033[32m"
self.CARET = "\033[1;36m"
self.DISABLE = "\033[0m"

def disable(self) -> None:
self.FILE = ""
self.WWORD = ""
self.FWORD = ""
self.CARET = ""
self.DISABLE = ""


Expand Down Expand Up @@ -540,6 +542,11 @@
action="store_true",
help="output just a single line for each misspelling in stdin mode",
)
parser.add_argument(
"--caret-diagnostic",
action="store_true",
help="use the diagnostic message format of modern compilers",
)
parser.add_argument("--config", type=str, help="path to config file.")
parser.add_argument("--toml", type=str, help="path to a pyproject.toml file.")
parser.add_argument("files", nargs="*", help="files or directories to check")
Expand Down Expand Up @@ -919,6 +926,7 @@
)
for match in check_matches:
word = match.group()
column = match.start()
lword = word.lower()
if lword in misspellings:
# Sometimes we find a 'misspelling' which is actually a valid word
Expand Down Expand Up @@ -973,8 +981,10 @@

cfilename = f"{colors.FILE}{filename}{colors.DISABLE}"
cline = f"{colors.FILE}{i + 1}{colors.DISABLE}"
ccolumn = f"{colors.FILE}{column + 1}{colors.DISABLE}"
cwrongword = f"{colors.WWORD}{word}{colors.DISABLE}"
crightword = f"{colors.FWORD}{fixword}{colors.DISABLE}"
ccaret = f"{colors.CARET}^{colors.DISABLE}"

reason = misspellings[lword].reason
if reason:
Expand All @@ -993,10 +1003,22 @@
if (not context_shown) and (context is not None):
print_context(lines, i, context)
if filename != "-":
print(
f"{cfilename}:{cline}: {cwrongword} "
f"==> {crightword}{creason}"
)
if options.caret_diagnostic:
ntabs = line[:column].count("\t")

Check warning on line 1007 in codespell_lib/_codespell.py

View check run for this annotation

Codecov / codecov/patch

codespell_lib/_codespell.py#L1007

Added line #L1007 was not covered by tests
if ntabs > 0:
line = line.replace("\t", " ")
column = column + ntabs * 3
print(

Check warning on line 1011 in codespell_lib/_codespell.py

View check run for this annotation

Codecov / codecov/patch

codespell_lib/_codespell.py#L1009-L1011

Added lines #L1009 - L1011 were not covered by tests
f"{cfilename}:{cline}:{ccolumn}: {cwrongword} "
f"==> {crightword}{creason}"
)
print(f"{line}", end="")
print("{:>{width}}{}".format("", ccaret, width=column))

Check warning on line 1016 in codespell_lib/_codespell.py

View check run for this annotation

Codecov / codecov/patch

codespell_lib/_codespell.py#L1015-L1016

Added lines #L1015 - L1016 were not covered by tests
else:
print(
f"{cfilename}:{cline}: {cwrongword} "
f"==> {crightword}{creason}"
)
elif options.stdin_single_line:
print(f"{cline}: {cwrongword} ==> {crightword}{creason}")
else:
Expand Down Expand Up @@ -1139,6 +1161,18 @@
summary = None

context = None
if options.caret_diagnostic and (
(options.context is not None)
or (options.before_context is not None)
or (options.after_context is not None)
):
print(

Check warning on line 1169 in codespell_lib/_codespell.py

View check run for this annotation

Codecov / codecov/patch

codespell_lib/_codespell.py#L1169

Added line #L1169 was not covered by tests
"ERROR: --caret-diagnostic cannot be used together with "
"--context/-C --context-before/-B or --context-after/-A",
file=sys.stderr,
)
parser.print_help()
return EX_USAGE

Check warning on line 1175 in codespell_lib/_codespell.py

View check run for this annotation

Codecov / codecov/patch

codespell_lib/_codespell.py#L1174-L1175

Added lines #L1174 - L1175 were not covered by tests
if options.context is not None:
if (options.before_context is not None) or (options.after_context is not None):
print(
Expand Down
Loading