Skip to content

Commit 942d9a8

Browse files
authored
Merge branch 'master' into pre-commit-ci-update-config
2 parents 57be310 + 6a82432 commit 942d9a8

File tree

7 files changed

+73
-11
lines changed

7 files changed

+73
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626

2727
### Fixed
2828

29+
- Fixed bug where `associate` blocks for variables pointing to function results
30+
where not properly resolved
31+
([#269](https://github.com/fortran-lang/fortls/issues/269))
32+
- Fixed bug where the `langid` was not propagated correctly from the user
33+
settings to the LSP creation stage for all types of requests.
34+
([#257](https://github.com/fortran-lang/fortls/issues/257))
2935
- Fixed end of scope for `CRITICAL` keyword blocks
3036
([#255](https://github.com/fortran-lang/fortls/issues/255))
3137
- Fixed bug where completion of interfaces in USE ONLY would produce the snippet

fortls/helper_functions.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ def get_var_stack(line: str) -> list[str]:
582582
return None
583583

584584

585-
def fortran_md(code: str, docs: str | None, langid: str = "fortran90"):
585+
def fortran_md(code: str, docs: str | None):
586586
"""Convert Fortran code to markdown
587587
588588
Parameters
@@ -591,17 +591,15 @@ def fortran_md(code: str, docs: str | None, langid: str = "fortran90"):
591591
Fortran code
592592
docs : str | None
593593
Documentation string
594-
langid : str, optional
595-
Language ID, by default 'fortran90'
596-
597594
Returns
598595
-------
599596
str
600597
Markdown string
601598
"""
602599
msg = ""
603600
if code:
604-
msg = f"```{langid}\n{code}\n```"
601+
msg = "```{langid}\n" # This gets inserted later
602+
msg += f"{code}\n```"
605603
# Add documentation
606604
if docs: # if docs is not None or ""
607605
msg += f"\n-----\n{docs}"

fortls/langserver.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,14 @@ def build_comp(
514514
if call_sig is not None:
515515
comp_obj["detail"] += " " + call_sig
516516
# Use the full markdown documentation
517-
hover_msg = candidate.get_hover_md(long=True)
517+
hover_msg: str = candidate.get_hover_md(long=True)
518518
if hover_msg:
519-
hover_msg = {"kind": "markdown", "value": hover_msg}
519+
hover_msg: dict = {
520+
"kind": "markdown",
521+
"value": hover_msg.replace(
522+
"```{langid}", f"```{self.hover_language}", 1
523+
),
524+
}
520525
comp_obj["documentation"] = hover_msg
521526
return comp_obj
522527

@@ -837,6 +842,17 @@ def check_optional(arg, params: dict):
837842
return i
838843
return None
839844

845+
def replace_langid(params: list[dict]) -> list[dict]:
846+
new_params = params[:]
847+
for param in new_params:
848+
if "documentation" not in param:
849+
continue
850+
# Replace the first value of langid, when starting a code block
851+
param["documentation"]["value"] = param["documentation"][
852+
"value"
853+
].replace("```{langid}", f"```{self.hover_language}", 1)
854+
return params
855+
840856
# Get parameters from request
841857
params: dict = request["params"]
842858
uri: str = params["textDocument"]["uri"]
@@ -904,6 +920,9 @@ def check_optional(arg, params: dict):
904920
label, doc_str, params = var_obj.get_signature()
905921
if label is None:
906922
return None
923+
# Replace placeholder language id with Fortran ID
924+
params = replace_langid(params)
925+
907926
# Find current parameter by index or by
908927
# looking at last arg with optional name
909928
param_num = len(arg_strings) - 1
@@ -917,6 +936,7 @@ def check_optional(arg, params: dict):
917936
param_num = opt_num
918937
signature = {"label": label, "parameters": params}
919938
if doc_str is not None:
939+
doc_str = doc_str.format(langid=self.hover_language)
920940
signature["documentation"] = {"kind": "markdown", "value": doc_str}
921941
req_dict = {"signatures": [signature], "activeParameter": param_num}
922942
return req_dict
@@ -1063,7 +1083,7 @@ def serve_hover(self, request: dict):
10631083
def create_hover(string: str, docs: str | None):
10641084
# This does not account for Fixed Form Fortran, but it should be
10651085
# okay for 99% of cases
1066-
return fortran_md(string, docs, self.hover_language)
1086+
return fortran_md(string, docs).format(langid=self.hover_language)
10671087

10681088
# Get parameters from request
10691089
params: dict = request["params"]
@@ -1087,7 +1107,11 @@ def create_hover(string: str, docs: str | None):
10871107
MODULE_TYPE_ID,
10881108
CLASS_TYPE_ID,
10891109
):
1090-
hover_array.append(var_obj.get_hover_md(long=True))
1110+
hover_array.append(
1111+
var_obj.get_hover_md(long=True).replace(
1112+
"```{langid}", f"```{self.hover_language}", 1
1113+
)
1114+
)
10911115
elif var_type == INTERFACE_TYPE_ID:
10921116
for member in var_obj.mems:
10931117
hover_str, docs = member.get_hover(long=True)
@@ -1097,7 +1121,11 @@ def create_hover(string: str, docs: str | None):
10971121
# Unless we have a Fortran literal include the desc in the hover msg
10981122
# See get_definition for an explanation about this default name
10991123
if not var_obj.desc.startswith(FORTRAN_LITERAL):
1100-
hover_array.append(var_obj.get_hover_md(long=True))
1124+
hover_array.append(
1125+
var_obj.get_hover_md(long=True).replace(
1126+
"```{langid}", f"```{self.hover_language}", 1
1127+
)
1128+
)
11011129
# Hover for Literal variables
11021130
elif var_obj.desc.endswith("REAL"):
11031131
hover_array.append(create_hover("REAL", None))

fortls/objects.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,7 @@ def __init__(
11601160
args: str = "",
11611161
mod_flag: bool = False,
11621162
keywords: list = None,
1163+
keyword_info: dict = None,
11631164
result_type: str = None,
11641165
result_name: str = None,
11651166
):
@@ -1173,9 +1174,13 @@ def __init__(
11731174
self.result_name: str = result_name
11741175
self.result_type: str = result_type
11751176
self.result_obj: Variable = None
1177+
self.keyword_info: dict = keyword_info
11761178
# Set the implicit result() name to be the function name
11771179
if self.result_name is None:
11781180
self.result_name = self.name
1181+
# Used in Associated blocks
1182+
if self.keyword_info is None:
1183+
self.keyword_info = {}
11791184

11801185
def copy_interface(self, copy_source: Function):
11811186
# Call the parent class method

fortls/parse_fortran.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1493,14 +1493,15 @@ def parse(
14931493
log.debug("%s !!! SUBROUTINE - Ln:%d", line, line_no)
14941494

14951495
elif obj_type == "fun":
1496-
keywords, _ = map_keywords(obj_info.keywords)
1496+
keywords, keyword_info = map_keywords(obj_info.keywords)
14971497
new_fun = Function(
14981498
file_ast,
14991499
line_no,
15001500
obj_info.name,
15011501
args=obj_info.args,
15021502
mod_flag=obj_info.mod_flag,
15031503
keywords=keywords,
1504+
keyword_info=keyword_info,
15041505
result_type=obj_info.result.type,
15051506
result_name=obj_info.result.name,
15061507
)

test/test_server_hover.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,20 @@ def test_hover_block():
366366
validate_hover(results, ref_results)
367367

368368

369+
def test_associate_block_func_result():
370+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir / "hover")})
371+
file_path = test_dir / "hover" / "associate_block_2.f90"
372+
string += hover_req(file_path, 2, 14)
373+
string += hover_req(file_path, 3, 9)
374+
errorcode, results = run_request(string, fortls_args=["--sort_keywords", "-n", "1"])
375+
assert errorcode == 0
376+
ref_results = [
377+
"```fortran90\nLOGICAL FUNCTION :: hi\n```",
378+
"```fortran90\nLOGICAL FUNCTION :: hi\n```",
379+
]
380+
validate_hover(results, ref_results)
381+
382+
369383
def test_hover_submodule_procedure():
370384
"""Test that submodule procedures and functions with modifier keywords
371385
are correctly displayed when hovering.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
program associate_block_2
2+
implicit none
3+
associate (hi => say_hi())
4+
if (hi) print *, 'Bye'
5+
end associate
6+
contains
7+
logical function say_hi()
8+
say_hi = .true.
9+
end
10+
end program

0 commit comments

Comments
 (0)