Skip to content

Commit

Permalink
fix: properly locates closing parenthesis in the presence of quotes
Browse files Browse the repository at this point in the history
Fixes #250
  • Loading branch information
gnikit committed May 19, 2024
1 parent f29b02d commit c71e8a8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
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

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

0 comments on commit c71e8a8

Please sign in to comment.