diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a478156..1b78a65e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/fortls/helper_functions.py b/fortls/helper_functions.py index b5e42167..fd1a9030 100644 --- a/fortls/helper_functions.py +++ b/fortls/helper_functions.py @@ -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)