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

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions fortls/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))


Expand Down
12 changes: 11 additions & 1 deletion fortls/langserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,17 @@
def _create_ref_link(self, obj) -> dict:
"""Create a link reference to an object"""
obj_file: FortranFile = obj.file_ast.file
sline, (schar, echar) = obj_file.find_word_in_code_line(obj.sline - 1, obj.name)
result = obj_file.find_word_in_code_line(obj.sline - 1, obj.name)
if result is None:
return uri_json(

Check warning on line 1776 in fortls/langserver.py

View check run for this annotation

Codecov / codecov/patch

fortls/langserver.py#L1776

Added line #L1776 was not covered by tests
path_to_uri(obj_file.path), obj.sline - 1, 0, obj.sline - 1, 0
)
sline, word_range = result
if word_range is None:
return uri_json(
path_to_uri(obj_file.path), obj.sline - 1, 0, obj.sline - 1, 0
)
schar, echar = word_range.start, word_range.end
if schar < 0:
schar = echar = 0
return uri_json(path_to_uri(obj_file.path), sline, schar, sline, echar)
Expand Down
2 changes: 1 addition & 1 deletion fortls/parsers/internal/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def build(self, file_obj):
self.sline, obj_range = file_obj.find_word_in_code_line(
self.sline, self.find_word
)
if obj_range.start >= 0:
if obj_range is not None:
schar = obj_range.start
echar = obj_range.end
diag = diagnostic_json(
Expand Down
12 changes: 6 additions & 6 deletions fortls/parsers/internal/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,25 +1156,25 @@
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:

Check warning on line 1171 in fortls/parsers/internal/parser.py

View check run for this annotation

Codecov / codecov/patch

fortls/parsers/internal/parser.py#L1171

Added line #L1171 was not covered by tests
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:

Check warning on line 1177 in fortls/parsers/internal/parser.py

View check run for this annotation

Codecov / codecov/patch

fortls/parsers/internal/parser.py#L1177

Added line #L1177 was not covered by tests
line_no += i + 1
return line_no, word_range
return line_no, word_range
Expand Down
7 changes: 5 additions & 2 deletions test/test_server_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ def validate_def(result_array, checks):
assert not checks[0]
return None
assert result_array["uri"] == path_to_uri(checks[2])
assert result_array["range"]["start"]["line"] == checks[0]
assert result_array["range"]["start"]["line"] == checks[1]
if result_array["range"] is not None:
assert result_array["range"]["start"]["line"] == checks[0]
assert result_array["range"]["start"]["line"] == checks[1]
else:
assert checks[0] is None


def def_request(uri: Path, line, char):
Expand Down