Skip to content

Commit a5b3468

Browse files
committed
Adds literal hover support for standalone vars
Hover info will now be served on not only variables that are part of a scope e.g. a function or a subroutine.
1 parent 0e9348a commit a5b3468

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

CHANGELOG.md

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

3+
## 1.15.2
4+
5+
### Fixes
6+
7+
- Further improves the literal variable hover added in v1.14.0
8+
39
## 1.15.1
410

511
### Fixes

fortls/constants.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
import sys
22

33
PY3K = sys.version_info >= (3, 0)
4+
5+
# A string used to mark literals e.g. 10, 3.14, "words", etc.
6+
# The description name chosen is non-ambiguous and cannot naturally
7+
# occur in Fortran (with/out C preproc) code
8+
# It is invalid syntax to define a type starting with numerics
9+
# it cannot also be a comment that requires !, c, d
10+
# and ^= (xor_eq) operator is invalid in Fortran C++ preproc
11+
FORTRAN_LITERAL = "0^=__LITERAL_INTERNAL_DUMMY_VAR_"

fortls/langserver.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pathlib import Path
1313

1414
# Local modules
15+
from fortls.constants import FORTRAN_LITERAL
1516
from fortls.helper_functions import expand_name
1617
from fortls.intrinsics import (
1718
get_intrinsic_keywords,
@@ -809,29 +810,26 @@ def get_definition(self, def_file, def_line, def_char, hover_req=False):
809810

810811
# If we have a Fortran literal constant e.g. 100, .false., etc.
811812
# Return a dummy object with the correct type & position in the doc
812-
if (
813-
hover_req
814-
and curr_scope
815-
and (
816-
NUMBER_REGEX.match(def_name)
817-
or LOGICAL_REGEX.match(def_name)
818-
or SQ_STRING_REGEX.match(def_name)
819-
or DQ_STRING_REGEX.match(def_name)
820-
)
821-
):
822-
# The description name chosen is non-ambiguous and cannot naturally
823-
# occur in Fortran (with/out C preproc) code
824-
# It is invalid syntax to define a type starting with numerics
825-
# it cannot also be a comment that requires !, c, d
826-
# and ^= (xor_eq) operator is invalid in Fortran C++ preproc
827-
var_obj = fortran_var(
828-
curr_scope.file_ast,
829-
def_line + 1,
830-
def_name,
831-
"0^=__LITERAL_INTERNAL_DUMMY_VAR_",
832-
curr_scope.keywords,
833-
)
834-
return var_obj
813+
if hover_req and curr_scope:
814+
var_type = None
815+
if NUMBER_REGEX.match(def_name):
816+
if any(s in def_name for s in [".", "e", "d"]):
817+
var_type = f"{FORTRAN_LITERAL}REAL"
818+
else:
819+
var_type = f"{FORTRAN_LITERAL}INTEGER"
820+
elif LOGICAL_REGEX.match(def_name):
821+
var_type = f"{FORTRAN_LITERAL}LOGICAL"
822+
elif SQ_STRING_REGEX.match(def_name) or DQ_STRING_REGEX.match(def_name):
823+
var_type = f"{FORTRAN_LITERAL}STRING"
824+
if var_type:
825+
return fortran_var(
826+
curr_scope.file_ast,
827+
def_line + 1,
828+
def_name,
829+
var_type,
830+
curr_scope.keywords,
831+
)
832+
835833
else:
836834
return var_obj
837835
return None
@@ -1119,9 +1117,20 @@ def create_signature_hover():
11191117
elif self.variable_hover and (var_type == VAR_TYPE_ID):
11201118
# Unless we have a Fortran literal include the desc in the hover msg
11211119
# See get_definition for an explanaiton about this default name
1122-
if var_obj.desc != "0^=__LITERAL_INTERNAL_DUMMY_VAR_":
1120+
if not var_obj.desc.startswith(FORTRAN_LITERAL):
11231121
hover_str, highlight = var_obj.get_hover()
11241122
hover_array.append(create_hover(hover_str, highlight))
1123+
# Hover for Literal variables
1124+
elif var_obj.desc.endswith("REAL"):
1125+
hover_array.append(create_hover("REAL", True))
1126+
elif var_obj.desc.endswith("INTEGER"):
1127+
hover_array.append(create_hover("INTEGER", True))
1128+
elif var_obj.desc.endswith("LOGICAL"):
1129+
hover_array.append(create_hover("LOGICAL", True))
1130+
elif var_obj.desc.endswith("STRING"):
1131+
hover_str = f"CHARACTER(LEN={len(var_obj.name)})"
1132+
hover_array.append(create_hover(hover_str, True))
1133+
11251134
# Include the signature if one is present e.g. if in an argument list
11261135
if self.hover_signature:
11271136
hover_str = create_signature_hover()

0 commit comments

Comments
 (0)