Skip to content

Commit cd63540

Browse files
committed
Fixes the hover of preprocessor functions
This is still not complete and ideally we should parser everything as a fortran object.
1 parent 55a43d0 commit cd63540

File tree

6 files changed

+36
-6
lines changed

6 files changed

+36
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
- Update constant parameters for `omp_lib` and `omp_lib_kinds` Interface v5.0
1616
- Format json files with `prettier`
1717

18+
### Fixes
19+
20+
- Fixes the hover of preprocessor functions. It now displays the function name
21+
witout the argument list and the function body. The argument list cannot be
22+
multiline but the function body can.
23+
1824
## 1.16.0
1925

2026
### Adds

fortls/parse_fortran.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,14 @@ def replace_vars(line: str):
12031203
output_file.append(line)
12041204
pp_defines.append(i + 1)
12051205
def_name = match.group(2)
1206+
# If this is an argument list of a function add them to the name
1207+
# get_definition will only return the function name upon hover
1208+
# hence if the argument list is appended in the def_name then
1209+
# querying the dictionary will not yield a result.
1210+
# Need to properly parse the preprocessor files instead of this.
1211+
# This also does not allow for multiline argument list definitions.
1212+
# if match.group(3):
1213+
# def_name += match.group(3)
12061214
if (match.group(1) == "define") and (def_name not in defs_tmp):
12071215
eq_ind = line[match.end(0) :].find(" ")
12081216
if eq_ind >= 0:

fortls/regex_patterns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
# Preprocessor mathching rules
106106
DEFINED_REGEX = re.compile(r"defined[ ]*\([ ]*([a-z_][a-z0-9_]*)[ ]*\)", re.I)
107107
PP_REGEX = re.compile(r"#(if |ifdef|ifndef|else|elif|endif)")
108-
PP_DEF_REGEX = re.compile(r"#(define|undef)[ ]*([a-z0-9_]+)", re.I)
108+
PP_DEF_REGEX = re.compile(r"#(define|undef)[ ]*([\w]+)(\((\w+(,[ ]*)?)+\))?", re.I)
109109
PP_DEF_TEST_REGEX = re.compile(r"(![ ]*)?defined[ ]*\([ ]*([a-z0-9_]*)[ ]*\)$", re.I)
110110
PP_INCLUDE_REGEX = re.compile(r"#include[ ]*([\"a-z0-9_\.]*)", re.I)
111111
# Context matching rules

test/test_preproc.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,21 @@ def check_return(result_array, checks):
2929
root_dir = os.path.join(test_dir, "pp")
3030
string = write_rpc_request(1, "initialize", {"rootPath": root_dir})
3131
file_path = os.path.join(test_dir, "pp", "preproc.F90")
32-
string += hover_req(file_path, 5, 8)
33-
string += hover_req(file_path, 7, 30)
32+
string += hover_req(file_path, 5, 8) # user defined type
33+
string += hover_req(file_path, 7, 30) # variable
34+
string += hover_req(file_path, 7, 40) # multi-lin variable
35+
string += hover_req(file_path, 8, 7) # function with if conditional
36+
string += hover_req(file_path, 9, 7) # multiline function with if conditional
3437
errcode, results = run_request(string, f" --config={root_dir}/.pp_conf.json")
3538
assert errcode == 0
3639

3740
# Reference solution
38-
ref_results = ("#define PCType character*(80)", "#define PETSC_ERR_INT_OVERFLOW 84")
41+
ref_results = (
42+
"#define PCType character*(80)",
43+
"#define PETSC_ERR_INT_OVERFLOW 84",
44+
"#define varVar 55",
45+
"#define ewrite if (priority <= 3) write((priority), format)",
46+
"#define ewrite2 if (priority <= 3) write((priority), format)",
47+
)
3948
assert len(ref_results) == len(results) - 1
4049
check_return(results[1:], ref_results)

test/test_source/pp/include/petscpc.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@
55

66
#define PC type(tPC)
77
#define PCType character*(80)
8-
8+
#define ewrite(priority, format) if (priority <= 3) write((priority), format)
9+
#define ewrite2(priority, format) \
10+
if (priority <= 3) write((priority), format)
11+
#define varVar \
12+
55
913
#endif

test/test_source/pp/preproc.F90

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ program preprocessor
55
integer, parameter :: var = 1000
66
PCType :: tmp
77
print*, 999, 3.14, "some", var, PETSC_ERR_MEM
8-
print*, PETSC_ERR_INT_OVERFLOW
8+
print*, PETSC_ERR_INT_OVERFLOW, varVar
9+
ewrite(1,*) 'Assemble EP P1 matrix and rhs sytem'
10+
ewrite2(1,*) 'Assemble EP P1 matrix and rhs sytem'
11+
912
#endif
1013
end program preprocessor

0 commit comments

Comments
 (0)