Skip to content
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
4 changes: 2 additions & 2 deletions fortls/parsers/internal/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import contextlib
from typing import TYPE_CHECKING

from fortls.constants import MODULE_TYPE_ID
from fortls.constants import ENUM_TYPE_ID, MODULE_TYPE_ID

from .imports import Import, ImportTypes
from .use import Use
Expand Down Expand Up @@ -141,7 +141,7 @@ def check_scope(
from .function import Function

for child in local_scope.get_children():
if child.name.startswith("#GEN_INT"):
if child.name.startswith("#GEN_INT") or child.get_type() == ENUM_TYPE_ID:
tmp_var = check_scope(child, var_name_lower, filter_public)
if tmp_var is not None:
return tmp_var
Expand Down
2 changes: 1 addition & 1 deletion fortls/regex_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class FortranRegularExpressions:
VAR: Pattern = compile(
r"[ ]*(INTEGER|REAL|DOUBLE[ ]*PRECISION|COMPLEX"
r"|DOUBLE[ ]*COMPLEX|CHARACTER|LOGICAL|PROCEDURE"
r"|EXTERNAL|CLASS|TYPE)", # external :: variable is handled by this
r"|EXTERNAL|CLASS|TYPE|ENUMERATOR)", # external :: variable is handled by this
I,
)
KIND_SPEC: Pattern = compile(r"[ ]*([*]?\([ ]*[\w*:]|\*[ ]*[0-9:]*)", I)
Expand Down
16 changes: 16 additions & 0 deletions test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from setup_tests import test_dir

from fortls.parsers.internal.parser import FortranFile
from fortls.parsers.internal.utilities import find_in_scope


def test_line_continuations():
Expand Down Expand Up @@ -60,6 +61,21 @@ def test_weird_parser_bug():
assert not ast.end_errors


def test_find_in_scope_recurses_into_enum_block():
file_path = test_dir / "test_enum_ref.f90"
file = FortranFile(str(file_path))
err_str, _ = file.load_from_disk()
assert err_str is None

ast = file.parse()
scope = ast.get_inner_scope(9)
assert scope is not None
result = find_in_scope(scope, "blue", {})

assert result is not None
assert result.name == "blue"


@pytest.mark.parametrize(
"ln_no, pp_defs, reference",
[
Expand Down
13 changes: 13 additions & 0 deletions test/test_server_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,16 @@ def test_def_function_implicit_result_variable():
assert len(ref_res) == len(results) - 1
for i, res in enumerate(ref_res):
validate_def(results[i + 1], res)


def test_def_enumerator():
"""Test that going to definition on an enum member works."""
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
file_path = test_dir / "test_enum_ref.f90"
string += def_request(file_path, 9, 11)
errcode, results = run_request(string)
assert errcode == 0
ref_res = [[4, 4, str(file_path)]]
assert len(ref_res) == len(results) - 1
for i, res in enumerate(ref_res):
validate_def(results[i + 1], res)
16 changes: 16 additions & 0 deletions test/test_server_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,19 @@ def test_references_ignore_comments_on_use_import():
[str(file_path), 5, 23, 35],
),
)


def test_references_enumerator():
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
file_path = test_dir / "test_enum_ref.f90"
string += ref_req(file_path, 9, 11)
errcode, results = run_request(string)
assert errcode == 0
validate_refs(
results[1],
(
[str(file_path), 4, 27, 31],
[str(file_path), 8, 10, 14],
[str(file_path), 9, 15, 19],
),
)
11 changes: 11 additions & 0 deletions test/test_source/test_enum_ref.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
program enum_refs
implicit none

enum, bind(c)
enumerator :: red = 1, blue = 2, green = 3
end enum

integer :: color
color = blue
if (color == blue) color = red
end program enum_refs
Loading