Skip to content

fix: evaluates langid only at LSP creation #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

### Fixed

- Fixed bug where the `langid` was not propagated correctly from the user
settings to the LSP creation stage for all types of requests.
([#257](https://github.com/fortran-lang/fortls/issues/257))
- Fixed end of scope for `CRITICAL` keyword blocks
([#255](https://github.com/fortran-lang/fortls/issues/255))
- Fixed bug where completion of interfaces in USE ONLY would produce the snippet
Expand Down
8 changes: 3 additions & 5 deletions fortls/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ def get_var_stack(line: str) -> list[str]:
return None


def fortran_md(code: str, docs: str | None, langid: str = "fortran90"):
def fortran_md(code: str, docs: str | None):
"""Convert Fortran code to markdown

Parameters
Expand All @@ -591,17 +591,15 @@ def fortran_md(code: str, docs: str | None, langid: str = "fortran90"):
Fortran code
docs : str | None
Documentation string
langid : str, optional
Language ID, by default 'fortran90'

Returns
-------
str
Markdown string
"""
msg = ""
if code:
msg = f"```{langid}\n{code}\n```"
msg = "```{langid}\n" # This gets inserted later
msg += f"{code}\n```"
# Add documentation
if docs: # if docs is not None or ""
msg += f"\n-----\n{docs}"
Expand Down
38 changes: 33 additions & 5 deletions fortls/langserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,14 @@ def build_comp(
if call_sig is not None:
comp_obj["detail"] += " " + call_sig
# Use the full markdown documentation
hover_msg = candidate.get_hover_md(long=True)
hover_msg: str = candidate.get_hover_md(long=True)
if hover_msg:
hover_msg = {"kind": "markdown", "value": hover_msg}
hover_msg: dict = {
"kind": "markdown",
"value": hover_msg.replace(
"```{langid}", f"```{self.hover_language}", 1
),
}
comp_obj["documentation"] = hover_msg
return comp_obj

Expand Down Expand Up @@ -837,6 +842,17 @@ def check_optional(arg, params: dict):
return i
return None

def replace_langid(params: list[dict]) -> list[dict]:
new_params = params[:]
for param in new_params:
if "documentation" not in param:
continue
# Replace the first value of langid, when starting a code block
param["documentation"]["value"] = param["documentation"][
"value"
].replace("```{langid}", f"```{self.hover_language}", 1)
return params

# Get parameters from request
params: dict = request["params"]
uri: str = params["textDocument"]["uri"]
Expand Down Expand Up @@ -904,6 +920,9 @@ def check_optional(arg, params: dict):
label, doc_str, params = var_obj.get_signature()
if label is None:
return None
# Replace placeholder language id with Fortran ID
params = replace_langid(params)

# Find current parameter by index or by
# looking at last arg with optional name
param_num = len(arg_strings) - 1
Expand All @@ -917,6 +936,7 @@ def check_optional(arg, params: dict):
param_num = opt_num
signature = {"label": label, "parameters": params}
if doc_str is not None:
doc_str = doc_str.format(langid=self.hover_language)
signature["documentation"] = {"kind": "markdown", "value": doc_str}
req_dict = {"signatures": [signature], "activeParameter": param_num}
return req_dict
Expand Down Expand Up @@ -1063,7 +1083,7 @@ def serve_hover(self, request: dict):
def create_hover(string: str, docs: str | None):
# This does not account for Fixed Form Fortran, but it should be
# okay for 99% of cases
return fortran_md(string, docs, self.hover_language)
return fortran_md(string, docs).format(langid=self.hover_language)

# Get parameters from request
params: dict = request["params"]
Expand All @@ -1087,7 +1107,11 @@ def create_hover(string: str, docs: str | None):
MODULE_TYPE_ID,
CLASS_TYPE_ID,
):
hover_array.append(var_obj.get_hover_md(long=True))
hover_array.append(
var_obj.get_hover_md(long=True).replace(
"```{langid}", f"```{self.hover_language}", 1
)
)
elif var_type == INTERFACE_TYPE_ID:
for member in var_obj.mems:
hover_str, docs = member.get_hover(long=True)
Expand All @@ -1097,7 +1121,11 @@ def create_hover(string: str, docs: str | None):
# Unless we have a Fortran literal include the desc in the hover msg
# See get_definition for an explanation about this default name
if not var_obj.desc.startswith(FORTRAN_LITERAL):
hover_array.append(var_obj.get_hover_md(long=True))
hover_array.append(
var_obj.get_hover_md(long=True).replace(
"```{langid}", f"```{self.hover_language}", 1
)
)
# Hover for Literal variables
elif var_obj.desc.endswith("REAL"):
hover_array.append(create_hover("REAL", None))
Expand Down