remove duplicating code for variables name generate
authorSurkov Sergey <surkovsv93@gmail.com>
Fri, 02 Feb 2018 17:16:04 +0300
changeset 1969 0607c382d73f
parent 1968 64827679f3f1
child 1970 b524b356aa58
remove duplicating code for variables name generate
CodeFileTreeNode.py
controls/VariablePanel.py
editors/CodeFileEditor.py
--- a/CodeFileTreeNode.py	Wed Jan 31 16:31:01 2018 +0300
+++ b/CodeFileTreeNode.py	Fri Feb 02 17:16:04 2018 +0300
@@ -139,11 +139,11 @@
     def GetDataTypes(self, basetypes=False):
         return self.GetCTRoot().GetDataTypes(basetypes=basetypes)
 
-    def GenerateNewName(self, format, start_idx):
+    def GenerateNewName(self, name, format):
         return self.GetCTRoot().GenerateNewName(
-            None, None, format, start_idx,
-            dict([(var.getname().upper(), True)
-                  for var in self.CodeFile.variables.getvariable()]))
+            None, name, format,
+            exclude=dict([(var.getname().upper(), True)
+                          for var in self.CodeFile.variables.getvariable()]))
 
     def SetVariables(self, variables):
         self.CodeFile.variables.setvariable([])
--- a/controls/VariablePanel.py	Wed Jan 31 16:31:01 2018 +0300
+++ b/controls/VariablePanel.py	Fri Feb 02 17:16:04 2018 +0300
@@ -105,7 +105,6 @@
 }
 
 LOCATION_MODEL = re.compile("((?:%[IQM](?:\*|(?:[XBWLD]?[0-9]+(?:\.[0-9]+)*)))?)$")
-VARIABLE_NAME_SUFFIX_MODEL = re.compile("([0-9]*)$")
 
 
 # -------------------------------------------------------------------------------
@@ -514,7 +513,7 @@
         self.FilterChoices = []
         self.FilterChoiceTransfer = GetFilterChoiceTransfer()
 
-        self.DefaultValue = _VariableInfos("", "", "", "", "", True, "", DefaultType, ([], []), 0)
+        self.DefaultValue = _VariableInfos("LocalVar0", "", "", "", "", True, "", DefaultType, ([], []), 0)
 
         if element_type in ["config", "resource"]:
             self.DefaultTypes = {"All": "Global"}
@@ -590,36 +589,16 @@
         self.VariablesGrid.SetEditable(not self.Debug)
 
         def _AddVariable(new_row):
+            row_content = self.DefaultValue.copy()
             if new_row > 0:
-                row_content = self.Values[new_row - 1].copy()
-
-                result = VARIABLE_NAME_SUFFIX_MODEL.search(row_content.Name)
-                if result is not None:
-                    name = row_content.Name[:result.start(1)]
-                    suffix = result.group(1)
-                    if suffix != "":
-                        start_idx = int(suffix)
-                    else:
-                        start_idx = 0
-                else:
-                    name = row_content.Name
-                    start_idx = 0
-            else:
-                row_content = None
-                start_idx = 0
-                name = "LocalVar"
-
-            if row_content is not None and row_content.Edit:
-                row_content = self.Values[new_row - 1].copy()
-            else:
-                row_content = self.DefaultValue.copy()
-                if self.Filter in self.DefaultTypes:
-                    row_content.Class = self.DefaultTypes[self.Filter]
-                else:
-                    row_content.Class = self.Filter
-
-            row_content.Name = self.Controler.GenerateNewName(
-                self.TagName, None, name + "%d", start_idx)
+                # doesn't copy values of previous var if it's non-editable (like a FB)
+                if self.Values[new_row-1].Edit:
+                    row_content = self.Values[new_row-1].copy()
+                old_name = self.Values[new_row-1].Name
+                row_content.Name = self.Controler.GenerateNewName(
+                    self.TagName, old_name, old_name+'%d')
+            if not row_content.Class:
+                row_content.Class = self.DefaultTypes.get(self.Filter, self.Filter)
 
             if self.Filter == "All" and len(self.Values) > 0:
                 self.Values.insert(new_row, row_content)
--- a/editors/CodeFileEditor.py	Wed Jan 31 16:31:01 2018 +0300
+++ b/editors/CodeFileEditor.py	Fri Feb 02 17:16:04 2018 +0300
@@ -35,7 +35,6 @@
 from plcopen.structures import TestIdentifier, IEC_KEYWORDS, DefaultType
 from controls import CustomGrid, CustomTable
 from controls.CustomStyledTextCtrl import CustomStyledTextCtrl, faces, GetCursorPos, NAVIGATION_KEYS
-from controls.VariablePanel import VARIABLE_NAME_SUFFIX_MODEL
 from editors.ConfTreeNodeEditor import ConfTreeNodeEditor
 from util.BitmapLibrary import GetBitmap
 from util.TranslationCatalogs import NoTranslate
@@ -674,7 +673,7 @@
         self.Controler = controler
 
         self.VariablesDefaultValue = {
-            "Name":        "",
+            "Name":        "LocalVar0",
             "Type":        DefaultType,
             "Initial":     "",
             "Description": "",
@@ -694,19 +693,9 @@
         def _AddVariable(new_row):
             if new_row > 0:
                 row_content = self.Table.data[new_row - 1].copy()
-                result = VARIABLE_NAME_SUFFIX_MODEL.search(row_content["Name"])
-                if result is not None:
-                    name = row_content["Name"][:result.start(1)]
-                    suffix = result.group(1)
-                    if suffix != "":
-                        start_idx = int(suffix)
-                    else:
-                        start_idx = 0
-                else:
-                    name = row_content["Name"]
-                    start_idx = 0
-                row_content["Name"] = self.Controler.GenerateNewName(
-                    name + "%d", start_idx)
+                old_name = row_content['Name']
+                row_content['Name'] =\
+                    self.Controler.GenerateNewName(old_name, old_name+'%d')
             else:
                 row_content = self.VariablesDefaultValue.copy()
             self.Table.InsertRow(new_row, row_content)