Skip to content

Commit

Permalink
Syntax: Fix conditions for syntax test handling
Browse files Browse the repository at this point in the history
Fixes #365
Closes #366
  • Loading branch information
FichteFoll committed Mar 6, 2022
1 parent cf8a301 commit 3aae6fd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
12 changes: 12 additions & 0 deletions plugins/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 13 additions & 2 deletions plugins/syntaxtest_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 3aae6fd

Please sign in to comment.