From 53b8d6f8e615c8e3a0801b6432a1e50ea1217ad7 Mon Sep 17 00:00:00 2001 From: Kelly Kiernan Date: Fri, 4 Mar 2016 14:39:27 -0500 Subject: [PATCH] updated insert constructor property. idea for issue #64 --- .../commands/insert_constructor_prop.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/php_companion/commands/insert_constructor_prop.py b/php_companion/commands/insert_constructor_prop.py index 87a46a9..0cdcfec 100644 --- a/php_companion/commands/insert_constructor_prop.py +++ b/php_companion/commands/insert_constructor_prop.py @@ -135,9 +135,22 @@ def add_constructor(self, prop_name): self.add_region(cursor_start, cursor_end) def find_class_opening_bracket(self): - 'Find the position of the opening bracket of the class block.' - pos = self.view.find(r'class\s+[0-9A-Za-z_]+', 0).end() - return self.view.find(r'\{', pos).end() + 'Get current cursor position' + cursorRegion = self.view.sel()[0] + + 'Find all classes in this file' + classRegions = self.view.find_all(r'class\s+[0-9A-Za-z_]+', 0); + + 'Loop through the class regions and save their distances from the cursor' + distances = []; + for classRegion in classRegions: + distances.append(abs(cursorRegion.end() - classRegion.end())) + + 'Get the class region that is closest to the cursor' + closestClassRegion = classRegions[distances.index(min(distances))] + + 'Return the opening bracket position' + return self.view.find(r'\{', closestClassRegion.end()).end() def find_properties(self): 'Find all the class properties defined in the current view.'