Skip to content

Commit 4a14d5e

Browse files
Remove preproc fromdetect_fixed_format and handle line continuation
Additionally, `test_helper.py` has been removed since all the test cases in there are in the examples of the docstring and hence they are covered by doctest already.
1 parent 0efb6e5 commit 4a14d5e

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

fortls/helper_functions.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def expand_name(line: str, char_pos: int) -> str:
3838
return ""
3939

4040

41-
def detect_fixed_format(file_lines: list[str], preproc: bool = False) -> bool:
41+
def detect_fixed_format(file_lines: list[str]) -> bool:
4242
"""Detect fixed/free format by looking for characters in label columns
4343
and variable declarations before column 6. Treat intersection format
4444
files as free format.
@@ -47,9 +47,6 @@ def detect_fixed_format(file_lines: list[str], preproc: bool = False) -> bool:
4747
----------
4848
file_lines : list[str]
4949
List of consecutive file lines
50-
preproc : bool
51-
If true, preprocessor directives (lines starting with '#') will be
52-
ignored
5350
5451
Returns
5552
-------
@@ -72,13 +69,29 @@ def detect_fixed_format(file_lines: list[str], preproc: bool = False) -> bool:
7269
>>> detect_fixed_format(['trailing line & ! comment'])
7370
False
7471
75-
But preprocessor lines might be ignored
76-
>>> detect_fixed_format(['#if defined(A) && !defined(B)'], preproc=True)
72+
But preprocessor lines will be ignored
73+
>>> detect_fixed_format(
74+
... ['#if defined(A) && !defined(B)', 'C Fixed format', '#endif'])
7775
True
76+
77+
>>> detect_fixed_format(
78+
... ['#if defined(A) && !defined(B)', ' free format', '#endif'])
79+
False
80+
81+
And preprocessor line-continuation is taken into account
82+
>>> detect_fixed_format(
83+
... ['#if defined(A) \\\\ ', ' && !defined(B)', 'C Fixed format', '#endif'])
84+
True
85+
86+
>>> detect_fixed_format(
87+
... ['#if defined(A) \\\\', '&& \\\\', '!defined(B)', ' free format', '#endif'])
88+
False
7889
"""
90+
pp_continue = False
7991
for line in file_lines:
8092
# Ignore preprocessor lines
81-
if preproc and line.startswith("#"):
93+
if line.startswith("#") or pp_continue:
94+
pp_continue = line.rstrip().endswith("\\")
8295
continue
8396
if FRegex.FREE_FORMAT_TEST.match(line):
8497
return False

fortls/parse_fortran.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ def load_from_disk(self) -> tuple[str | None, bool | None]:
904904

905905
self.hash = hash
906906
self.contents_split = contents.splitlines()
907-
self.fixed = detect_fixed_format(self.contents_split, self.preproc)
907+
self.fixed = detect_fixed_format(self.contents_split)
908908
self.contents_pp = self.contents_split
909909
self.nLines = len(self.contents_split)
910910
return None, True
@@ -1013,7 +1013,7 @@ def set_contents(self, contents_split: list, detect_format: bool = True):
10131013
self.contents_pp = self.contents_split
10141014
self.nLines = len(self.contents_split)
10151015
if detect_format:
1016-
self.fixed = detect_fixed_format(self.contents_split, self.preproc)
1016+
self.fixed = detect_fixed_format(self.contents_split)
10171017

10181018
def get_line(self, line_no: int, pp_content: bool = False) -> str:
10191019
"""Get single line from file"""

test/test_helper.py

-20
This file was deleted.

0 commit comments

Comments
 (0)