Skip to content

Commit

Permalink
Fix cursor position in PHP test methods.
Browse files Browse the repository at this point in the history
@todo fix cursor position for Jasmine tests.
@todo replace string replace with run_command (#10)
  • Loading branch information
bogdananton committed Jul 15, 2015
1 parent 1b83e98 commit 75d23ec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ If the original phrase is inside a dockblock then it updates the method using th

Install
-----
Install by downloading the [1.0.1 release zip](https://github.com/testNameGenerator/SublimeText-plugin/releases/download/1.0.1/testNameGenerator.zip) and unpacking it into the \Packages folder (SublimeText Menu \ Preferences \ Browse Packages).
Install by downloading the [1.0.4 release zip](https://github.com/testNameGenerator/SublimeText-plugin/releases/download/1.0.4/testNameGenerator.zip) and unpacking it into the \Packages folder (SublimeText Menu \ Preferences \ Browse Packages).

Usage
-----
Expand Down
36 changes: 31 additions & 5 deletions TestNameGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ def run(self, edit):
SublimeConnect.insertMethodName(edit, cursorLine, TextHelper.prepareTestBlockPHP(phrase, methodName))
else:
SublimeConnect.updateMethodNamePHP(edit, cursorLine, methodName, existingMethod)

else:
# JS will add a new test method because the text is inside the test method
SublimeConnect.insertMethodName(edit, cursorLine, TextHelper.prepareTestBlockJS(phrase))

SublimeConnect.close()

class TextHelper():
def patternExistingMethodPHP(lineContents):
# complete block: return r'\/\*([\*\n\s]+)' + lineContents + '([a-zA-Z0-9\n\s\*@]+)\*\/([\s]+)(public)?(\s)?function(\s)?test([a-zA-Z0-9_]+)(\s\n)?\('
Expand All @@ -73,7 +74,7 @@ def patternMethodName():

def prepareTestBlockPHP(phrase, methodName):
tab = SublimeConnect.getWhitespaceTab()
return tab + "/**\n" + tab + " * " + phrase + "\n" + tab + " */\n" + tab + "public function test" + methodName + "()\n" + tab + "{\n\n" + tab + "}\n\n" + tab
return tab + "/**\n" + tab + " * " + phrase + "\n" + tab + " */\n" + tab + "public function test" + methodName + "()\n" + tab + "{\n" + tab + tab + "\n" + tab + "}\n" + tab

# will generate Jasmine blocks
def prepareTestBlockJS(phrase):
Expand All @@ -82,21 +83,46 @@ def prepareTestBlockJS(phrase):

if (examineNameTypeParts[0].lower() == "describe"):
phrase = " ".join(examineNameTypeParts.pop(0)) # remove the "describe" prefix
return self.getJasmineDescribeBlock(phrase, tab)
return TextHelper.getJasmineDescribeBlock(phrase, tab)

else:
return self.getJasmineItBlock(phrase, tab)
return TextHelper.getJasmineItBlock(phrase, tab)

def getJasmineDescribeBlock(phrase, tab):
return tab + "describe('" + phrase + "', function () {\n\n" + tab + "});\n\n" + tab

def getJasmineItBlock(phrase, tab):
return tab + tab + "it('" + phrase + "', function () {\n\n" + tab + "" + tab + "});\n\n" + tab
return tab + tab + "it('" + phrase + "', function () {\n" + tab + tab + tab + "\n" + tab + "" + tab + "});\n\n" + tab

# connector to the sublime api
class SublimeConnect():
context = False

@classmethod
def close(self):
view = self.context.view
queueMoveCursors = []
deltaCol = 1 if self.getWhitespaceTab() == "\t" else 4

for cursor in view.sel():
target = view.text_point(self.getCursorRow(cursor) - 2, self.getCursorCol(cursor) + deltaCol)
queueMoveCursors.append(sublime.Region(target))
pass

view.sel().clear()

for cursor in queueMoveCursors:
view.sel().add(cursor)
pass

@classmethod
def getCursorRow(self, cursor):
return self.context.view.rowcol(cursor.begin())[0]

@classmethod
def getCursorCol(self, cursor):
return self.context.view.rowcol(cursor.begin())[1]

@classmethod
def find_all(self, a_str, sub):
start = 0
Expand Down
2 changes: 1 addition & 1 deletion package-metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"description": "PHP-PHPUnit / JS-Jasmine Syntax: Will convert plain text task/message into a test method, using the plain text as a comment for readability.", "version": "1.0.3", "url": "https://github.com/testNameGenerator/SublimeText-plugin"}
{"description": "PHP-PHPUnit / JS-Jasmine Syntax: Will convert plain text task/message into a test method, using the plain text as a comment for readability.", "version": "1.0.4", "url": "https://github.com/testNameGenerator/SublimeText-plugin"}

0 comments on commit 75d23ec

Please sign in to comment.