-
Notifications
You must be signed in to change notification settings - Fork 43
Description
Specific builds: ST2 2221, ST3 3065 (both on Linux 64 bit).
Re: Class sublime.View.find(pattern, fromPosition, <flags>)
I am unsure which of the following are intended behaviour and which are bugs.
Problem One
Both the ST2 and ST3 API documentation for view.find() specify that it returns None if pattern is not found. That is what happens in ST2, but in ST3 view.find() returns the Region (-1, -1) if pattern is not found.
Problem Two
In ST2 view.find() throws a RuntimeError exception if pattern contains an invalid regular expression (such as a partially formed one) or if pattern is an empty string and the sublime.LITERAL flag has been set. It does NOT throw a RuntimeError error if pattern is an empty string and the sublime.LITERAL flag has NOT been set. In ST3 view.find() does not throw an exception in these cases.
Below is some test code and its output to demonstrate.
Hope this helps.
# ViewFindBugDemo.py
import sublime
import sublime_plugin
# ViewFindBugDemoCommand so ST command will be: view_find_bug_demo
# { "keys": ["ctrl+t", "ctrl+p"], "command": "view_find_bug_demo" },
# Specific builds: ST2 2221, ST3 3065 (both on Linux 64 bit).
class ViewFindBugDemoCommand(sublime_plugin.TextCommand):
"""
The ViewFindBugDemoCommand class demonstrates inconsistencies / bugs in the
Sublime Text API View class method find(pattern, fromPosition, <flags>).
"""
def run(self, edit):
print("ST Version: " + sublime.version())
# -------------------------------------------------------------------- #
# Find with a valid pattern that does not exist in the document.
pattern = "EXP_DOES_NOT_EXIST_IN_DOC"
print("Find with a valid pattern that does not exist:")
find_region = self.view.find(pattern, 0)
print("find_region: " + str(find_region))
# -------------------------------------------------------------------- #
# Find with a malformed regex pattern.
# Note: In ST2 tests this section must be commented out to run the
# final two tests below because find() will throw an exception.
pattern = "[0-9"
print("Find with a malformed regex pattern:")
find_region = self.view.find(pattern, 0)
print("find_region: " + str(find_region))
# -------------------------------------------------------------------- #
# Find with an empty string pattern - no flags set.
pattern = ""
print("Find with empty string and no flags set:")
find_region = self.view.find(pattern, 0)
print("find_region: " + str(find_region))
# -------------------------------------------------------------------- #
# Find with an empty string pattern - sublime.LITERAL flag set.
pattern = ""
print("Find with empty string and sublime.LITERAL flag set:")
find_region = self.view.find(pattern, 0, sublime.LITERAL)
print("find_region: " + str(find_region))
# End of def run()
# End of class ViewFindBugDemoCommand()
# ---------------------------------------------------------------------------- #
# Sublime Text v.2 Output.
# ST Version: 2221
# Find with a valid pattern that does not exist:
# find_region: None
# Find with a malformed regex pattern:
# Traceback (most recent call last):
# File "./sublime_plugin.py", line 362, in run_
# File "./ViewFindBugDemo.py", line 44, in run
# RuntimeError: Found a closing ) with no corresponding openening parenthesis.
# The error occured while parsing the regular expression: '[0-9>>>HERE>>>'.
# in regular expression [0-9
# After commenting out the first 2 tests:
# ST Version: 2221
# Find with empty string and no flags set:
# find_region: (0, 0)
# Find with empty string and sublime.LITERAL flag set:
# Traceback (most recent call last):
# File "./sublime_plugin.py", line 362, in run_
# File "./ViewFindBugDemo.py", line 68, in run
# RuntimeError: Empty regular expression. in regular expression
# ---------------------------------------------------------------------------- #
# Sublime Text v.3 Output.
# ST Version: 3065
# Find with a valid pattern that does not exist:
# find_region: (-1, -1)
# Find with a malformed regex pattern:
# find_region: (-1, -1)
# Find with empty string and no flags set:
# find_region: (0, 0)
# Find with empty string and sublime.LITERAL flag set:
# find_region: (-1, -1)
# ---------------------------------------------------------------------------- #