Skip to content

Commit eb6f618

Browse files
authored
Merge pull request #279 from fortran-lang/bug/lang-id-propagation
fix: evaluates langid only at LSP creation
2 parents 8918355 + 107c8e8 commit eb6f618

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

CHANGELOG.md

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

2727
### Fixed
2828

29+
- Fixed bug where the `langid` was not propagated correctly from the user
30+
settings to the LSP creation stage for all types of requests.
31+
([#257](https://github.com/fortran-lang/fortls/issues/257))
2932
- Fixed end of scope for `CRITICAL` keyword blocks
3033
([#255](https://github.com/fortran-lang/fortls/issues/255))
3134
- 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))

0 commit comments

Comments
 (0)