diff --git a/SedRegex/__init__.py b/SedRegex/__init__.py index 46bc021..8d6cc02 100644 --- a/SedRegex/__init__.py +++ b/SedRegex/__init__.py @@ -50,8 +50,10 @@ __url__ = 'https://github.com/jlu5/SupyPlugins' from . import config from . import plugin -from imp import reload +from . import constants +from importlib import reload +reload(constants) reload(config) reload(plugin) diff --git a/SedRegex/constants.py b/SedRegex/constants.py new file mode 100755 index 0000000..3610e40 --- /dev/null +++ b/SedRegex/constants.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import re + +SED_REGEX = re.compile( + # This part matches an optional nick followed by ":" or ",", used to direct replacement + # at a particular user. + r"^(?:(?P.+?)[:,] )?" + + # Match and save the delimiter (any one symbol) as a named group + r"s(?P[^\w\s])" + + # Match the pattern to replace, which can be any string up to the first instance of the delimiter + r"(?P(?:(?!(?P=delim)).)*)(?P=delim)" + + # Ditto with the replacement + r"(?P(?:(?!(?P=delim)).)*)" + + # Optional final delimiter plus flags at the end + r"(?:(?P=delim)(?P[a-z]*))?" +) + +if __name__ == '__main__': + print("This is the full regex used by the plugin; paste it into your favourite regex tester " + "for debugging:") + print(SED_REGEX) diff --git a/SedRegex/plugin.py b/SedRegex/plugin.py index 233429b..9cbee97 100644 --- a/SedRegex/plugin.py +++ b/SedRegex/plugin.py @@ -1,6 +1,6 @@ ### # Copyright (c) 2015, Michael Daniel Telatynski -# Copyright (c) 2015-2019, James Lu +# Copyright (c) 2015-2020, James Lu # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -52,8 +52,7 @@ if sys.version_info[0] < 3: 'supports Python 2, consult the python2-legacy branch at ' 'https://github.com/jlu5/SupyPlugins/tree/python2-legacy') -SED_REGEX = re.compile(r"^(?:(?P.+?)[:,] )?s(?P[^\w\s])(?P.*?)(?P=delim)" - r"(?P.*?)(?P=delim)(?P[a-z]*)$") +from .constants import * # Replace newlines and friends with things like literal "\n" (backslash and "n") axe_spaces = utils.str.MultipleReplacer({'\n': '\\n', '\t': '\\t', '\r': '\\r'}) diff --git a/SedRegex/test.py b/SedRegex/test.py index 9300ebb..d45c8c6 100644 --- a/SedRegex/test.py +++ b/SedRegex/test.py @@ -1,5 +1,5 @@ ### -# Copyright (c) 2017-2019, James Lu +# Copyright (c) 2017-2020, James Lu # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -181,6 +181,36 @@ class SedRegexTestCase(ChannelPluginTestCase): m = self.getMsg(' ', timeout=1) self.assertIn('timed out', str(m)) + def testMissingTrailingSeparator(self): + # Allow the plugin to work if you miss the trailing / + self.feedMsg('hello world') + self.feedMsg('s/world/everyone') + m = self.getMsg(' ') + self.assertIn('hello everyone', str(m)) + + # Make sure it works if there's a space in the replacement + self.feedMsg('hello world') + self.feedMsg('s@world@how are you') + m = self.getMsg(' ') + self.assertIn('hello how are you', str(m)) + + # Ditto with a space in the original text + self.feedMsg("foo bar @ baz") + self.feedMsg('s/bar @/and') + m = self.getMsg(' ') + self.assertIn('foo and baz', str(m)) + + def testIgnoreTextAfterTrailingSeparator(self): + # https://github.com/jlu5/SupyPlugins/issues/59 + self.feedMsg('see you ltaer') + self.feedMsg('s/ltaer/later/ this text will be ignored') + m = self.getMsg(' ') + self.assertIn('see you later', str(m)) + + self.feedMsg('s/LTAER/later, bye/i ') + m = self.getMsg(' ') + self.assertIn('see you later, bye', str(m)) + # TODO: test ignores # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: