Skip to content
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

fix: properly locates closing parenthesis in the presence of quotes #400

Merged
merged 1 commit into from
May 19, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- Fixed bug where parser would crash when trying to retrieve an invalid line no.
([#398](https://github.com/fortran-lang/fortls/issues/398))
- Fixed bug with string quotes not being escaped when looking for parenthesis
([#250](https://github.com/fortran-lang/fortls/issues/250))

## 3.0.0

Expand Down
12 changes: 12 additions & 0 deletions fortls/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,21 @@ def find_paren_match(string: str) -> int:

>>> find_paren_match('a, (b, (c, d)')
-1

>>> find_paren_match('nt(sin(0.5))+8+len("ab((c")-3) :: y')
29

>>> find_paren_match("nt(sin(0.5))+8+len('ab))c')-3) :: y")
29
"""
paren_count = 1
quote_state = {"'": False, '"': False}
for i, char in enumerate(string):
if char in quote_state:
quote_state[char] = not quote_state[char]
if any(quote_state.values()):
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this logic work with code like len('ab"c)')?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it doesn't.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In LFortran we parse the string (handling the inner " and such correctly), and then you can locate the parentheses on top (when you see ", just parse the string).


if char == "(":
paren_count += 1
elif char == ")":
Expand Down
14 changes: 14 additions & 0 deletions test/test_server_hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,3 +666,17 @@ def test_types():
"```fortran90\nTYPE, EXTENDS(extends_t) :: a_t\n```",
]
validate_hover(results, ref_results)


def test_complicated_kind_spec():
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir / "hover")})
file_path = test_dir / "hover" / "complicated_kind_spec.f90"
string += hover_req(file_path, 1, 40)
string += hover_req(file_path, 2, 40)
errcode, results = run_request(string, fortls_args=["-n", "1"])
assert errcode == 0
ref_results = [
'```fortran90\nREAL(int(sin(0.5))+8+len("ab((c")-3) :: y\n```',
'```fortran90\nREAL(int(sin(0.5))+8+len("ab))c")-3) :: z\n```',
]
validate_hover(results, ref_results)
4 changes: 4 additions & 0 deletions test/test_source/hover/complicated_kind_spec.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
program complicated_kind_spec
real(int(sin(0.5))+8+len("ab((c")-3) :: y
real(int(sin(0.5))+8+len("ab))c")-3) :: z
end program