TextViewer.py
changeset 295 c6ef6d92ce16
parent 249 d8425712acef
child 297 e837b67cb184
--- a/TextViewer.py	Mon Dec 15 09:45:16 2008 +0100
+++ b/TextViewer.py	Fri Dec 19 15:07:54 2008 +0100
@@ -141,7 +141,7 @@
         self.SetUseTabs(0)
         
         self.Keywords = []
-        self.Variables = []
+        self.Variables = {}
         self.Functions = []
         self.Jumps = []
         self.EnumeratedValues = []
@@ -152,6 +152,7 @@
         self.Errors = []
         self.Debug = debug
         self.InstancePath = instancepath
+        self.StructElementsStack = []
         
         self.ParentWindow = window
         self.Controler = controler
@@ -272,14 +273,15 @@
         self.DisableEvents = False
         
         words = self.TagName.split("::")
-        self.Variables = [variable["Name"].upper() for variable in self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)]
+        
+        self.Variables = dict([(variable["Name"], variable["Tree"]) for variable in self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)])
         if self.Controler.GetEditedElementType(self.TagName, self.Debug)[1] == "function" or words[0] == "T" and self.TextSyntax == "IL":
-            self.Variables.append(words[-1].upper())
+            self.Variables[words[-1]] = {}
         
         self.Functions = []
         for category in self.Controler.GetBlockTypes(self.TagName, self.Debug):
             for blocktype in category["list"]:
-                if blocktype["type"] == "function" and blocktype["name"] not in self.Keywords and blocktype["name"] not in self.Variables:
+                if blocktype["type"] == "function" and blocktype["name"] not in self.Keywords and blocktype["name"] not in self.Variables.keys():
                     self.Functions.append(blocktype["name"].upper())
         
         self.EnumeratedValues = []
@@ -291,6 +293,15 @@
     def RefreshScaling(self, refresh=True):
         pass
     
+    def IsValidVariable(self, name_list, var_tree):
+        if len(name_list) == 0:
+            return True
+        else:
+            sub_tree = var_tree.get(name_list[0].upper(), None)
+            if sub_tree is not None:
+                return self.IsValidVariable(name_list[1:], sub_tree)
+        return False
+    
     def OnStyleNeeded(self, event):
         self.TextChanged = True
         line = self.LineFromPosition(self.GetEndStyled())
@@ -301,6 +312,8 @@
         end_pos = event.GetPosition()
         self.StartStyling(start_pos, 0xff)
         
+        struct_elements = []
+        
         current_pos = last_styled_pos
         state = SPACE
         line = ""
@@ -316,7 +329,7 @@
                 elif state == WORD:
                     if word in self.Keywords:
                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD)
-                    elif word in self.Variables:
+                    elif self.IsValidVariable(struct_elements + [word], self.Variables):
                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE)
                     elif word in self.Functions:
                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION)
@@ -326,10 +339,11 @@
                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
                     else:
                         self.SetStyling(current_pos - last_styled_pos, 31)
-                        if self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos:
+                        if word != "]" and (self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos):
                             self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK)
                             self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK)
                             self.StartStyling(current_pos, 0xff)
+                    struct_elements = []
                 else:
                     self.SetStyling(current_pos - last_styled_pos, 31)
                 last_styled_pos = current_pos
@@ -338,6 +352,8 @@
             elif line.endswith("(*") and state != COMMENT:
                 self.SetStyling(current_pos - last_styled_pos - 1, 31)
                 last_styled_pos = current_pos
+                if state == WORD:
+                    struct_elements = []
                 state = COMMENT
             elif state == COMMENT:
                 if line.endswith("*)"):
@@ -366,7 +382,7 @@
                 if state == WORD:
                     if word in self.Keywords:
                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD)
-                    elif word in self.Variables:
+                    elif self.IsValidVariable(struct_elements + [word], self.Variables):
                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE)
                     elif word in self.Functions:
                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION)
@@ -376,10 +392,17 @@
                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
                     else:
                         self.SetStyling(current_pos - last_styled_pos, 31)
-                        if self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos:
+                        if word != "]" and (self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos):
                             self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK)
                             self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK)
                             self.StartStyling(current_pos, 0xff)
+                    if char == '.':
+                        if word != "]":
+                            struct_elements.append(word)
+                    else:
+                        if char == '[':
+                            self.StructElementsStack.append(struct_elements + [word])
+                        struct_elements = []
                     word = ""
                     last_styled_pos = current_pos
                     state = SPACE
@@ -387,6 +410,10 @@
                     self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
                     last_styled_pos = current_pos
                     state = SPACE
+                if char == ']':
+                    struct_elements = self.StructElementsStack.pop(-1)
+                    word = char
+                    state = WORD
             current_pos += 1
         if state == COMMENT:
             self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT)
@@ -395,7 +422,7 @@
         elif state == WORD:
             if word in self.Keywords:
                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD)
-            elif word in self.Variables:
+            elif self.IsValidVariable(struct_elements + [word], self.Variables):
                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE)
             elif word in self.Functions:
                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION)
@@ -459,9 +486,9 @@
                     elif words[0].upper() in ["JMP", "JMPC", "JMPNC"]:
                         kw = self.Jumps
                     else:
-                        kw = self.Variables
-            else:
-                kw = self.Keywords + self.Variables + self.Functions
+                        kw = self.Variables.keys()
+            else:
+                kw = self.Keywords + self.Variables.keys() + self.Functions
             if len(kw) > 0:
                 kw.sort()
                 self.AutoCompSetIgnoreCase(True)