Skip to content

Commit 3b71755

Browse files
committed
Fixed function hovering showing up as result type
Closes Type prefixed functions display wrong signature upon hover #22
1 parent b0987da commit 3b71755

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# CHANGELONG
22

3+
## 2.2.2
4+
5+
### Fixed
6+
7+
- Fixed hovering over functions displaying as theire result types
8+
([gnikit/fortls#22](https://github.com/gnikit/fortls/issues/22))
9+
310
## 2.2.1
411

512
### Changed

fortls/langserver.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,9 @@ def get_definition(
740740
)
741741
):
742742
curr_scope = curr_scope.parent
743-
var_obj = find_in_scope(curr_scope, def_name, self.obj_tree)
743+
var_obj = find_in_scope(
744+
curr_scope, def_name, self.obj_tree, var_line_number=def_line + 1
745+
)
744746
# Search in global scope
745747
if var_obj is None:
746748
if is_member:

fortls/objects.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,13 @@ def find_in_scope(
165165
obj_tree: dict,
166166
interface: bool = False,
167167
local_only: bool = False,
168+
var_line_number: int = None,
168169
):
169170
def check_scope(
170-
local_scope: fortran_scope, var_name_lower: str, filter_public: bool = False
171+
local_scope: fortran_scope,
172+
var_name_lower: str,
173+
filter_public: bool = False,
174+
var_line_number: int = None,
171175
):
172176
for child in local_scope.get_children():
173177
if child.name.startswith("#GEN_INT"):
@@ -178,6 +182,19 @@ def check_scope(
178182
if (child.vis < 0) or ((local_scope.def_vis < 0) and (child.vis <= 0)):
179183
continue
180184
if child.name.lower() == var_name_lower:
185+
# For functions with an implicit result() variable the name
186+
# of the function is used. If we are hovering over the function
187+
# definition, we do not want the implicit result() to be returned.
188+
# If scope is from a function and child's name is same as functions name
189+
# and start of scope i.e. function definition is equal to the request ln
190+
# then we are need to skip this child
191+
if (
192+
isinstance(local_scope, fortran_function)
193+
and local_scope.name.lower() == child.name.lower()
194+
and local_scope.sline == var_line_number
195+
):
196+
return None
197+
181198
return child
182199
return None
183200

@@ -186,7 +203,7 @@ def check_scope(
186203
# Check local scope
187204
if scope is None:
188205
return None
189-
tmp_var = check_scope(scope, var_name_lower)
206+
tmp_var = check_scope(scope, var_name_lower, var_line_number=var_line_number)
190207
if local_only or (tmp_var is not None):
191208
return tmp_var
192209
# Check INCLUDE statements

test/test_server.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,10 @@ def check_return(result_array, checks):
573573
string += hover_req(file_path, 7, 55)
574574
file_path = test_dir / "hover" / "pointers.f90"
575575
string += hover_req(file_path, 1, 26)
576+
file_path = test_dir / "hover" / "functions.f90"
577+
string += hover_req(file_path, 1, 11)
578+
string += hover_req(file_path, 7, 19)
579+
576580
errcode, results = run_request(
577581
string, fortls_args=["--variable_hover", "--sort_keywords"]
578582
)
@@ -590,6 +594,10 @@ def check_return(result_array, checks):
590594
"DOUBLE PRECISION, PARAMETER :: somevar = 23.12",
591595
"DOUBLE PRECISION, PARAMETER :: some = 1e-19",
592596
"INTEGER, POINTER",
597+
"""FUNCTION fun1(arg)
598+
INTEGER, INTENT(IN) :: arg""",
599+
"""INTEGER FUNCTION fun2(arg)
600+
INTEGER, INTENT(IN) :: arg""",
593601
)
594602
assert len(ref_results) == len(results) - 1
595603
check_return(results[1:], ref_results)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
! simple function
2+
function fun1(arg)
3+
integer, intent(in) :: arg
4+
integer :: fun1
5+
end function fun1
6+
7+
! function with type on definition, implied result
8+
integer function fun2(arg)
9+
integer, intent(in) :: arg
10+
end function fun2

0 commit comments

Comments
 (0)