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

Don't use "match" as a variable #2043

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions novelwriter/core/coretools.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,9 @@ def searchText(self, text: str) -> tuple[list[tuple[int, int, str]], bool]:
count = 0
capped = False
results = []
for match in self._regEx.finditer(text):
pos = match.start(0)
num = len(match.group(0))
for res in self._regEx.finditer(text):
pos = res.start(0)
num = len(res.group(0))
lim = text[:pos].rfind("\n") + 1
cut = text[lim:pos].rfind(" ") + lim + 1
context = text[cut:cut+100].partition("\n")[0]
Expand Down
24 changes: 12 additions & 12 deletions novelwriter/core/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,36 +1109,36 @@ def _extractFormats(

# Match Markdown
for regEx, fmts in self._rxMarkdown:
for match in regEx.finditer(text):
for res in regEx.finditer(text):
temp.extend(
(match.start(n), match.end(n), fmt, "")
(res.start(n), res.end(n), fmt, "")
for n, fmt in enumerate(fmts) if fmt > 0
)

# Match Shortcodes
for match in REGEX_PATTERNS.shortcodePlain.finditer(text):
for res in REGEX_PATTERNS.shortcodePlain.finditer(text):
temp.append((
match.start(1), match.end(1),
self._shortCodeFmt.get(match.group(1).lower(), 0),
res.start(1), res.end(1),
self._shortCodeFmt.get(res.group(1).lower(), 0),
"",
))

# Match Shortcode w/Values
tHandle = self._handle or ""
for match in REGEX_PATTERNS.shortcodeValue.finditer(text):
kind = self._shortCodeVals.get(match.group(1).lower(), 0)
for res in REGEX_PATTERNS.shortcodeValue.finditer(text):
kind = self._shortCodeVals.get(res.group(1).lower(), 0)
temp.append((
match.start(0), match.end(0),
res.start(0), res.end(0),
self.FMT_STRIP if kind == skip else kind,
f"{tHandle}:{match.group(2)}",
f"{tHandle}:{res.group(2)}",
))

# Match Dialogue
if self._rxDialogue and hDialog:
for regEx, fmtB, fmtE in self._rxDialogue:
for match in regEx.finditer(text):
temp.append((match.start(0), 0, fmtB, ""))
temp.append((match.end(0), 0, fmtE, ""))
for res in regEx.finditer(text):
temp.append((res.start(0), 0, fmtB, ""))
temp.append((res.end(0), 0, fmtE, ""))

# Post-process text and format
result = text
Expand Down
16 changes: 8 additions & 8 deletions novelwriter/gui/dochighlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,10 @@ def highlightBlock(self, text: str) -> None:

if hRules:
for rX, hRule in hRules:
for match in re.finditer(rX, text[xOff:]):
for res in re.finditer(rX, text[xOff:]):
for xM, hFmt in hRule.items():
xPos = match.start(xM) + xOff
xEnd = match.end(xM) + xOff
xPos = res.start(xM) + xOff
xEnd = res.end(xM) + xOff
for x in range(xPos, xEnd):
cFmt = self.format(x)
if cFmt.fontStyleName() != "markup":
Expand Down Expand Up @@ -484,18 +484,18 @@ def spellCheck(self, text: str, offset: int) -> list[tuple[int, int]]:
if "[" in text:
# Strip shortcodes
for regEx in [RX_FMT_SC, RX_FMT_SV]:
for match in regEx.finditer(text, offset):
if (s := match.start(0)) >= 0 and (e := match.end(0)) >= 0:
for res in regEx.finditer(text, offset):
if (s := res.start(0)) >= 0 and (e := res.end(0)) >= 0:
pad = " "*(e - s)
text = f"{text[:s]}{pad}{text[e:]}"

self._spellErrors = []
checker = SHARED.spelling
for match in RX_WORDS.finditer(text.replace("_", " "), offset):
for res in RX_WORDS.finditer(text.replace("_", " "), offset):
if (
(word := match.group(0))
(word := res.group(0))
and not (word.isnumeric() or word.isupper() or checker.checkWord(word))
):
self._spellErrors.append((match.start(0), match.end(0)))
self._spellErrors.append((res.start(0), res.end(0)))

return self._spellErrors
6 changes: 3 additions & 3 deletions tests/test_text/test_text_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
def allMatches(regEx: re.Pattern, text: str) -> list[list[str]]:
"""Get all matches for a regex."""
result = []
for match in regEx.finditer(text):
for res in regEx.finditer(text):
result.append([
(match.group(n), match.start(n), match.end(n))
for n in range((match.lastindex or 0) + 1)
(res.group(n), res.start(n), res.end(n))
for n in range((res.lastindex or 0) + 1)
])
return result

Expand Down