Skip to content

Fix: Return None in find_word_in_line when word is not found #467

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

Closed
Changes from 1 commit
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
Next Next commit
edited find_word_in_line function return type if the word is not found
islml committed Mar 6, 2025
commit 21fcd342ee0c89e64b436906a2e6e63bf09cb9d0
5 changes: 3 additions & 2 deletions fortls/helper_functions.py
Original file line number Diff line number Diff line change
@@ -204,7 +204,7 @@ def separate_def_list(test_str: str) -> list[str] | None:
return def_list


def find_word_in_line(line: str, word: str) -> Range:
def find_word_in_line(line: str, word: str) -> Range | None:
"""Find Fortran word in line
Parameters
@@ -228,7 +228,8 @@ def find_word_in_line(line: str, word: str) -> Range:
),
-1,
)
# TODO: if i == -1: return None makes more sense
if i == -1:
return None
return Range(i, i + len(word))


12 changes: 6 additions & 6 deletions fortls/parsers/internal/parser.py
Original file line number Diff line number Diff line change
@@ -1156,25 +1156,25 @@ def find_word_in_code_line(
forward: bool = True,
backward: bool = False,
pp_content: bool = False,
) -> tuple[int, Range]:
) -> tuple[int, Range | None]:
back_lines, curr_line, forward_lines = self.get_code_line(
line_no, forward=forward, backward=backward, pp_content=pp_content
)
word_range = Range(-1, -1)
word_range = None
if curr_line is not None:
find_word_lower = word.lower()
word_range = find_word_in_line(curr_line.lower(), find_word_lower)
if backward and (word_range.start < 0):
if backward and (word_range is None):
back_lines.reverse()
for i, line in enumerate(back_lines):
word_range = find_word_in_line(line.lower(), find_word_lower)
if word_range.start >= 0:
if word_range is not None:
line_no -= i + 1
return line_no, word_range
if forward and (word_range.start < 0):
if forward and (word_range is None):
for i, line in enumerate(forward_lines):
word_range = find_word_in_line(line.lower(), find_word_lower)
if word_range.start >= 0:
if word_range is not None:
line_no += i + 1
return line_no, word_range
return line_no, word_range