Skip to content

Commit db5d01b

Browse files
committed
Tweaked trigger to suggest using a config file (fixes #77)
1 parent 3b7b973 commit db5d01b

File tree

5 files changed

+71
-22
lines changed

5 files changed

+71
-22
lines changed

hdl_checker/base_server.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from hdl_checker.static_check import getStaticMessages
5656
from hdl_checker.types import (
5757
BuildFlagScope,
58+
ConfigFileOrigin,
5859
RebuildInfo,
5960
RebuildLibraryUnit,
6061
RebuildPath,
@@ -91,7 +92,9 @@
9192
else:
9293
JSONDecodeError = json.decoder.JSONDecodeError
9394

94-
WatchedFile = NamedTuple("WatchedFile", (("path", Path), ("last_read", float)))
95+
WatchedFile = NamedTuple(
96+
"WatchedFile", (("path", Path), ("last_read", float), ("origin", ConfigFileOrigin))
97+
)
9598

9699

97100
class BaseServer(object): # pylint: disable=useless-object-inheritance
@@ -159,8 +162,8 @@ def _clearLruCaches(self):
159162
for meth in self._cached_methods:
160163
meth.cache_clear()
161164

162-
def setConfig(self, filename):
163-
# type: (Union[Path, str]) -> None
165+
def setConfig(self, filename, origin):
166+
# type: (Union[Path, str], ConfigFileOrigin) -> None
164167
"""
165168
Sets the configuration file. Calling this method will only trigger a
166169
configuration update if the given file name is different what was
@@ -177,7 +180,7 @@ def setConfig(self, filename):
177180
else:
178181
return
179182

180-
self.config_file = WatchedFile(path, mtime)
183+
self.config_file = WatchedFile(path, mtime, origin)
181184
_logger.debug("Set config to %s", self.config_file)
182185

183186
def _updateConfigIfNeeded(self):
@@ -199,14 +202,17 @@ def _updateConfigIfNeeded(self):
199202
if self.config_file.last_read >= file_mtime:
200203
return
201204

202-
# Don't let the user hanging if adding sources is taking too long. Also
203-
# need to notify when adding is done.
205+
# Don't let the user hanging if adding sources is taking too long
206+
# when there's no config file. Also need to notify when adding is
207+
# done.
204208
timer = Timer(
205209
_HOW_LONG_IS_TOO_LONG,
206210
self._handleUiInfo,
207211
args=(_HOW_LONG_IS_TOO_LONG_MSG,),
208212
)
209-
timer.start()
213+
214+
if self.config_file.origin is ConfigFileOrigin.generated:
215+
timer.start()
210216

211217
try:
212218
config = json.load(open(str(self.config_file.path)))
@@ -215,7 +221,9 @@ def _updateConfigIfNeeded(self):
215221

216222
self.configure(config)
217223

218-
self.config_file = WatchedFile(self.config_file.path, file_mtime)
224+
self.config_file = WatchedFile(
225+
self.config_file.path, file_mtime, self.config_file.origin
226+
)
219227
_logger.debug("Updated config file to %s", self.config_file)
220228
timer.cancel()
221229

@@ -362,7 +370,9 @@ def _setupIfNeeded(self):
362370
return
363371

364372
# Force config file out of to date to trigger reparsing
365-
self.config_file = WatchedFile(self.config_file.path, 0)
373+
self.config_file = WatchedFile(
374+
self.config_file.path, 0, self.config_file.origin
375+
)
366376

367377
def clean(self):
368378
# type: (...) -> Any

hdl_checker/handlers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
import bottle # type: ignore
2828

2929
from hdl_checker import __version__ as version
30-
from hdl_checker import types as t # pylint: disable=unused-import
3130
from hdl_checker.base_server import BaseServer
3231
from hdl_checker.builders.fallback import Fallback
3332
from hdl_checker.path import Path
33+
from hdl_checker.types import ConfigFileOrigin
3434
from hdl_checker.utils import terminateProcess
3535

3636
_logger = logging.getLogger(__name__)
@@ -88,7 +88,7 @@ def _getServerByProjectFile(project_file):
8888
try:
8989
project = Server(root_dir=root_dir)
9090
if project_file is not None:
91-
project.setConfig(project_file)
91+
project.setConfig(project_file, origin=ConfigFileOrigin.user)
9292
_logger.debug("Created new project server for '%s'", project_file)
9393
except (IOError, OSError):
9494
_logger.info("Failed to create checker, reverting to fallback")

hdl_checker/lsp.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from typing import Any, Callable, Dict, Iterable, List, Optional, Set, Tuple, Union
2525

2626
import six
27-
2827
from pyls import lsp as defines # type: ignore
2928
from pyls._utils import debounce # type: ignore
3029
from pyls.python_ls import PythonLanguageServer # type: ignore
@@ -44,7 +43,7 @@
4443
tAnyDesignUnit,
4544
)
4645
from hdl_checker.path import Path, TemporaryPath
47-
from hdl_checker.types import Location, MarkupKind
46+
from hdl_checker.types import ConfigFileOrigin, Location, MarkupKind
4847
from hdl_checker.utils import getTemporaryFilename, logCalls, onNewReleaseFound
4948

5049
_logger = logging.getLogger(__name__)
@@ -251,7 +250,7 @@ def _onConfigUpdate(self, options):
251250
path = self._getProjectFilePath(options)
252251

253252
try:
254-
self.checker.setConfig(path)
253+
self.checker.setConfig(path, origin=ConfigFileOrigin.user)
255254
return
256255
except UnknownParameterError as exc:
257256
_logger.info("Failed to read config from %s: %s", path, exc)
@@ -274,7 +273,7 @@ def _onConfigUpdate(self, options):
274273
# Write this to a file and tell the server to use it
275274
auto_project_file = getTemporaryFilename(AUTO_PROJECT_FILE_NAME)
276275
json.dump(config, open(auto_project_file, "w"))
277-
self.checker.setConfig(auto_project_file)
276+
self.checker.setConfig(auto_project_file, origin=ConfigFileOrigin.generated)
278277

279278
def _getProjectFilePath(self, options=None):
280279
# type: (...) -> str

hdl_checker/tests/test_base_server.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
from hdl_checker.path import Path
6666
from hdl_checker.types import (
6767
BuildFlagScope,
68+
ConfigFileOrigin,
6869
FileType,
6970
Location,
7071
RebuildLibraryUnit,
@@ -141,7 +142,7 @@ def test(handle_ui_info):
141142
open(source, "w").write("")
142143

143144
project = DummyServer(_Path(path))
144-
project.setConfig(Path(config))
145+
project.setConfig(_Path(config), origin=ConfigFileOrigin.generated)
145146
# Get messages of anything to trigger reading the config
146147
project.getMessagesByPath(Path(source))
147148

@@ -151,6 +152,34 @@ def test(handle_ui_info):
151152

152153
removeIfExists(path)
153154

155+
@it.should( # type: ignore
156+
"not warn when setup is taking too long if the user provides the config file"
157+
)
158+
@patch("hdl_checker.base_server._HOW_LONG_IS_TOO_LONG", 0.1)
159+
@patch.object(
160+
hdl_checker.base_server.BaseServer, "configure", lambda *_: time.sleep(0.5)
161+
)
162+
@patch("hdl_checker.tests.DummyServer._handleUiInfo")
163+
def test(handle_ui_info):
164+
165+
path = tempfile.mkdtemp()
166+
167+
config = p.join(path, "config.json")
168+
source = p.join(path, "source.vhd")
169+
170+
# Make sure the files exist
171+
open(config, "w").write("")
172+
open(source, "w").write("")
173+
174+
project = DummyServer(_Path(path))
175+
project.setConfig(Path(config), origin=ConfigFileOrigin.user)
176+
# Get messages of anything to trigger reading the config
177+
project.getMessagesByPath(Path(source))
178+
179+
handle_ui_info.assert_not_called()
180+
181+
removeIfExists(path)
182+
154183
@it.should( # type: ignore
155184
"not warn when setup takes less than _HOW_LONG_IS_TOO_LONG"
156185
)
@@ -166,7 +195,7 @@ def test(handle_ui_info):
166195
open(source, "w").write("")
167196

168197
project = DummyServer(_Path(path))
169-
project.setConfig(Path(config))
198+
project.setConfig(Path(config), origin=ConfigFileOrigin.user)
170199
# Get messages of anything to trigger reading the config
171200
project.getMessagesByPath(Path(source))
172201

@@ -220,7 +249,7 @@ def setup():
220249
def test():
221250
project = DummyServer(_Path("nonexisting"))
222251
with it.assertRaises(FileNotFoundError):
223-
project.setConfig(str(it.project_file))
252+
project.setConfig(str(it.project_file), origin=ConfigFileOrigin.user)
224253

225254
with it.having("no project file at all"):
226255

@@ -308,7 +337,7 @@ def setup():
308337
}
309338
)
310339

311-
it.project.setConfig(it.config_file)
340+
it.project.setConfig(it.config_file, origin=ConfigFileOrigin.user)
312341

313342
@it.should("use MockBuilder builder") # type: ignore
314343
def test():
@@ -344,7 +373,7 @@ def test():
344373
"hdl_checker.base_server.WatchedFile.__init__", side_effect=[None]
345374
) as watched_file:
346375
old = it.project.config_file
347-
it.project.setConfig(it.config_file)
376+
it.project.setConfig(it.config_file, origin=ConfigFileOrigin.user)
348377
it.project._updateConfigIfNeeded()
349378
it.assertEqual(it.project.config_file, old)
350379
watched_file.assert_not_called()
@@ -430,7 +459,10 @@ def test(handle_ui_warning):
430459
@patch("hdl_checker.base_server.json.dump")
431460
def test(_):
432461
with PatchBuilder():
433-
it.project.setConfig(Path(p.join(TEST_PROJECT, "vimhdl.prj")))
462+
it.project.setConfig(
463+
Path(p.join(TEST_PROJECT, "vimhdl.prj")),
464+
origin=ConfigFileOrigin.user,
465+
)
434466
it.project._updateConfigIfNeeded()
435467

436468
entity_a = _SourceMock(
@@ -614,7 +646,10 @@ def setup(handle_ui_info):
614646

615647
with PatchBuilder():
616648
it.project = DummyServer(_Path(TEST_TEMP_PATH))
617-
it.project.setConfig(Path(p.join(TEST_PROJECT, "vimhdl.prj")))
649+
it.project.setConfig(
650+
Path(p.join(TEST_PROJECT, "vimhdl.prj")),
651+
origin=ConfigFileOrigin.user,
652+
)
618653
it.project._updateConfigIfNeeded()
619654
handle_ui_info.assert_called_once_with("Added 10 sources")
620655

hdl_checker/types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,8 @@ class MarkupKind(Enum):
9898

9999
# A location range within a source file
100100
Range = NamedTuple("Range", (("start", Location), ("end", Optional[Location])))
101+
102+
class ConfigFileOrigin(str, Enum):
103+
"Specifies tracked design unit types"
104+
user = "user"
105+
generated = "generated"

0 commit comments

Comments
 (0)