From 3aae6fdcb76009e95ed73f65f82017fbb01d13d3 Mon Sep 17 00:00:00 2001 From: FichteFoll Date: Sun, 6 Mar 2022 14:35:19 +0100 Subject: [PATCH] Syntax: Fix conditions for syntax test handling Fixes #365 Closes #366 --- plugins/lib/__init__.py | 12 ++++++++++++ plugins/syntaxtest_dev.py | 15 +++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/plugins/lib/__init__.py b/plugins/lib/__init__.py index cfcfcae9..351fd2f3 100644 --- a/plugins/lib/__init__.py +++ b/plugins/lib/__init__.py @@ -30,3 +30,15 @@ def wrapper(*args, **kwargs): return (ret, sublime.INHIBIT_WORD_COMPLETIONS) if ret is not None else None return wrapper + + +def path_is_relative_to(path, *other): + """Check whether a `pathlib.Path` is relative to another Path-like. + + Backport of Python 3.9's `pathlib.PurePath.is_relative_to`. + """ + try: + path.relative_to(*other) + return True + except ValueError: + return False diff --git a/plugins/syntaxtest_dev.py b/plugins/syntaxtest_dev.py index 6cef16b4..7eceb2b6 100644 --- a/plugins/syntaxtest_dev.py +++ b/plugins/syntaxtest_dev.py @@ -2,13 +2,14 @@ import logging import re from os import path +from pathlib import Path import sublime import sublime_plugin from sublime_lib.flags import RegionOption -from .lib import get_setting +from .lib import get_setting, path_is_relative_to __all__ = ( 'SyntaxTestHighlighterListener', @@ -435,7 +436,17 @@ class AssignSyntaxTestSyntaxListener(sublime_plugin.EventListener): PLAIN_TEXT = "Packages/Text/Plain text.tmLanguage" def on_load(self, view): - if view.size() == 0 and view.file_name().startswith(sublime.packages_path() + '/'): + file_name = view.file_name() + if not file_name: + return + file_path = Path(file_name) + if ( + not file_path.name.startswith("syntax_test_") + or not path_is_relative_to(file_path, sublime.packages_path()) + ): + return + + if view.size() == 0: logger.debug("Delaying on_load because view was empty") sublime.set_timeout(lambda: self._on_load(view), 100) else: