Skip to content

Commit f118412

Browse files
albertziegenhagelgnikit
authored andcommitted
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 65d0ca8 commit f118412

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

fortls/helper_functions.py

Lines changed: 20 additions & 7 deletions
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/parsers/internal/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ def load_from_disk(self) -> tuple[str | None, bool | None]:
902902

903903
self.hash = hash
904904
self.contents_split = contents.splitlines()
905-
self.fixed = detect_fixed_format(self.contents_split, self.preproc)
905+
self.fixed = detect_fixed_format(self.contents_split)
906906
self.contents_pp = self.contents_split
907907
self.nLines = len(self.contents_split)
908908
return None, True
@@ -1011,7 +1011,7 @@ def set_contents(self, contents_split: list, detect_format: bool = True):
10111011
self.contents_pp = self.contents_split
10121012
self.nLines = len(self.contents_split)
10131013
if detect_format:
1014-
self.fixed = detect_fixed_format(self.contents_split, self.preproc)
1014+
self.fixed = detect_fixed_format(self.contents_split)
10151015

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

test/test_helper.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)