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

Ignore preprocessor lines when detecting fixed form fortran #302

Merged
merged 3 commits into from
Jan 21, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
([#219](https://github.com/fortran-lang/fortls/issues/219))
- Changed hover messages and signature help to use Markdown
([#45](https://github.com/fortran-lang/fortls/issues/45))
- Changed automatic detection of fixed/free-form of files to ignore
preprocessor lines.
([#302](https://github.com/fortran-lang/fortls/pull/302))

### Fixed

Expand Down
23 changes: 23 additions & 0 deletions fortls/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,31 @@ def detect_fixed_format(file_lines: list[str]) -> bool:
Lines wih ampersands are not fixed format
>>> detect_fixed_format(['trailing line & ! comment'])
False

But preprocessor lines will be ignored
>>> detect_fixed_format(
... ['#if defined(A) && !defined(B)', 'C Fixed format', '#endif'])
True

>>> detect_fixed_format(
... ['#if defined(A) && !defined(B)', ' free format', '#endif'])
False

And preprocessor line-continuation is taken into account
>>> detect_fixed_format(
... ['#if defined(A) \\\\ ', ' && !defined(B)', 'C Fixed format', '#endif'])
True

>>> detect_fixed_format(
... ['#if defined(A) \\\\', '&& \\\\', '!defined(B)', ' free format', '#endif'])
False
"""
pp_continue = False
for line in file_lines:
# Ignore preprocessor lines
if line.startswith("#") or pp_continue:
pp_continue = line.rstrip().endswith("\\")
continue
if FRegex.FREE_FORMAT_TEST.match(line):
return False
tmp_match = FRegex.VAR.match(line)
Expand Down