Skip to content

Commit 06b9321

Browse files
committed
Unifies glob pattern resolution for json and cmd inputs
Fixes #142 1. Changes the container of `incl_suffixes` from a list to a set 2. Also, now the following options defined in the cmd interface will not be initialised to empty sets when a json config file is present. - `excl_paths` - `source_dirs` - `incl_suffixes` - `excl_suffixes` - `include_dirs` Fixes #143
1 parent 6b47337 commit 06b9321

File tree

4 files changed

+58
-26
lines changed

4 files changed

+58
-26
lines changed

CHANGELOG.md

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
## Unreleased
44

5+
## 2.9.0
6+
7+
### Fixed
8+
9+
- Fixed glob pattern resolution for command line arguments
10+
([#142](https://github.com/gnikit/fortls/issues/142))
11+
12+
### Changed
13+
14+
- Changed the default value of the following options when a mix the command line
15+
interface and the `json` interface are used. Instead of having the `json` interface
16+
default the values to an empty set it now defaults to the values of the
17+
command line: `excl_paths`, `source_dirs`,
18+
- `incl_suffixes`
19+
- `excl_suffixes`
20+
- `include_dirs`
21+
([#143](https://github.com/gnikit/fortls/issues/143))
22+
523
## 2.8.0
624

725
### Added

fortls/interface.py

+2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ def commandline_args(name: str = "fortls") -> argparse.ArgumentParser:
114114
"--incl_suffixes",
115115
type=str,
116116
nargs="*",
117+
default=set(),
118+
action=SetAction,
117119
metavar="SUFFIXES",
118120
help=(
119121
"Consider additional file extensions to the default (default: "

fortls/langserver.py

+33-21
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ def serve_initialize(self, request: dict):
191191
self.source_dirs.add(self.root_path)
192192

193193
self._load_config_file()
194+
self._resolve_globs_in_paths()
194195
self._config_logger(request)
195196
self._load_intrinsics()
196197
self._add_source_dirs()
@@ -1529,26 +1530,12 @@ def _load_config_file(self) -> None:
15291530
self.post_message(msg)
15301531

15311532
def _load_config_file_dirs(self, config_dict: dict) -> None:
1532-
# Exclude paths (directories & files)
1533-
# with glob resolution
1534-
for path in config_dict.get("excl_paths", []):
1535-
self.excl_paths.update(set(resolve_globs(path, self.root_path)))
1536-
1537-
# Source directory paths (directories)
1538-
# with glob resolution
1539-
source_dirs = config_dict.get("source_dirs", [])
1540-
for path in source_dirs:
1541-
# resolve_globs filters any nonexisting directories so FileNotFoundError
1542-
# found inside only_dirs can never be raised
1543-
dirs = only_dirs(resolve_globs(path, self.root_path))
1544-
self.source_dirs.update(set(dirs))
1545-
1546-
# Keep all directories present in source_dirs but not excl_paths
1547-
self.source_dirs = {i for i in self.source_dirs if i not in self.excl_paths}
1548-
self.incl_suffixes = config_dict.get("incl_suffixes", [])
1533+
self.excl_paths = set(config_dict.get("excl_paths", self.excl_paths))
1534+
self.source_dirs = set(config_dict.get("source_dirs", self.source_dirs))
1535+
self.incl_suffixes = set(config_dict.get("incl_suffixes", self.incl_suffixes))
15491536
# Update the source file REGEX
15501537
self.FORTRAN_SRC_EXT_REGEX = src_file_exts(self.incl_suffixes)
1551-
self.excl_suffixes = set(config_dict.get("excl_suffixes", []))
1538+
self.excl_suffixes = set(config_dict.get("excl_suffixes", self.excl_suffixes))
15521539

15531540
def _load_config_file_general(self, config_dict: dict) -> None:
15541541
# General options ------------------------------------------------------
@@ -1607,11 +1594,36 @@ def _load_config_file_preproc(self, config_dict: dict) -> None:
16071594
if isinstance(self.pp_defs, list):
16081595
self.pp_defs = {key: "" for key in self.pp_defs}
16091596

1610-
for path in config_dict.get("include_dirs", set()):
1597+
self.include_dirs = set(config_dict.get("include_dirs", self.include_dirs))
1598+
1599+
def _resolve_globs_in_paths(self) -> None:
1600+
"""Resolves glob patterns in `excl_paths`, `source_dirs` and `include_dirs`.
1601+
Also performs the exclusion of `excl_paths` from `source_dirs`.
1602+
"""
1603+
# Exclude paths (directories & files) with glob resolution
1604+
excl_paths = set()
1605+
for path in self.excl_paths:
1606+
excl_paths.update(set(resolve_globs(path, self.root_path)))
1607+
self.excl_paths = excl_paths.copy()
1608+
1609+
# Source directory paths (directories) with glob resolution
1610+
source_dirs = set()
1611+
for path in self.source_dirs:
1612+
# resolve_globs filters any nonexisting directories so FileNotFoundError
1613+
# found inside only_dirs can never be raised
1614+
source_dirs.update(set(only_dirs(resolve_globs(path, self.root_path))))
1615+
self.source_dirs = source_dirs.copy()
1616+
1617+
# Keep all directories present in source_dirs but not excl_paths
1618+
self.source_dirs = {i for i in self.source_dirs if i not in self.excl_paths}
1619+
1620+
# Preprocessor includes
1621+
include_dirs = set()
1622+
for path in self.include_dirs:
16111623
# resolve_globs filters any nonexisting directories so FileNotFoundError
16121624
# found inside only_dirs can never be raised
1613-
dirs = only_dirs(resolve_globs(path, self.root_path))
1614-
self.include_dirs.update(set(dirs))
1625+
include_dirs.update(set(only_dirs(resolve_globs(path, self.root_path))))
1626+
self.include_dirs = include_dirs.copy()
16151627

16161628
def _add_source_dirs(self) -> None:
16171629
"""Will recursively add all subdirectories that contain Fortran

test/test_interface.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_command_line_file_parsing_options():
2929
" --excl_suffixes _tmp.f90 _h5hut_tests.F90 --excl_paths exclude tests".split()
3030
)
3131
assert args.source_dirs == {"tmp", "./local", "/usr/include/**"}
32-
assert args.incl_suffixes == [".FF", ".fpc", ".h", "f20"]
32+
assert args.incl_suffixes == {".FF", ".fpc", ".h", "f20"}
3333
assert args.excl_suffixes == {"_tmp.f90", "_h5hut_tests.F90"}
3434
assert args.excl_paths == {"exclude", "tests"}
3535

@@ -109,10 +109,10 @@ def test_config_file_general_options():
109109
def test_config_file_dir_parsing_options():
110110
server, r = unittest_server_init()
111111
# File parsing
112-
assert server.source_dirs == {f'{r/"subdir"}', f'{r/"pp"}', f'{r/"pp"/"include"}'}
113-
assert server.incl_suffixes == [".FF", ".fpc", ".h", "f20"]
112+
assert server.source_dirs == {"pp/**", "subdir"}
113+
assert server.incl_suffixes == {".FF", ".fpc", ".h", "f20"}
114114
assert server.excl_suffixes == {"_tmp.f90", "_h5hut_tests.F90"}
115-
assert server.excl_paths == {f'{r/"excldir"}', f'{r/"hover"}'}
115+
assert server.excl_paths == {"excldir", "hover/**"}
116116

117117

118118
def test_config_file_autocomplete_options():
@@ -144,7 +144,7 @@ def test_config_file_preprocessor_options():
144144
server, root = unittest_server_init()
145145
# Preprocessor options
146146
assert server.pp_suffixes == [".h", ".fh"]
147-
assert server.include_dirs == {f'{root/"include"}'}
147+
assert server.include_dirs == {"./include/**"}
148148
assert server.pp_defs == {
149149
"HAVE_PETSC": "",
150150
"HAVE_ZOLTAN": "",

0 commit comments

Comments
 (0)