TextViewer.py
changeset 295 c6ef6d92ce16
parent 249 d8425712acef
child 297 e837b67cb184
equal deleted inserted replaced
294:4a36f2ec8967 295:c6ef6d92ce16
   139         # Indentation size
   139         # Indentation size
   140         self.SetTabWidth(2)
   140         self.SetTabWidth(2)
   141         self.SetUseTabs(0)
   141         self.SetUseTabs(0)
   142         
   142         
   143         self.Keywords = []
   143         self.Keywords = []
   144         self.Variables = []
   144         self.Variables = {}
   145         self.Functions = []
   145         self.Functions = []
   146         self.Jumps = []
   146         self.Jumps = []
   147         self.EnumeratedValues = []
   147         self.EnumeratedValues = []
   148         self.DisableEvents = True
   148         self.DisableEvents = True
   149         self.TextSyntax = "ST"
   149         self.TextSyntax = "ST"
   150         self.CurrentAction = None
   150         self.CurrentAction = None
   151         self.TagName = tagname
   151         self.TagName = tagname
   152         self.Errors = []
   152         self.Errors = []
   153         self.Debug = debug
   153         self.Debug = debug
   154         self.InstancePath = instancepath
   154         self.InstancePath = instancepath
       
   155         self.StructElementsStack = []
   155         
   156         
   156         self.ParentWindow = window
   157         self.ParentWindow = window
   157         self.Controler = controler
   158         self.Controler = controler
   158 
   159 
   159         self.SetModEventMask(wx.stc.STC_MOD_BEFOREINSERT|wx.stc.STC_MOD_BEFOREDELETE)
   160         self.SetModEventMask(wx.stc.STC_MOD_BEFOREINSERT|wx.stc.STC_MOD_BEFOREDELETE)
   270         self.RefreshJumpList()
   271         self.RefreshJumpList()
   271         self.EmptyUndoBuffer()
   272         self.EmptyUndoBuffer()
   272         self.DisableEvents = False
   273         self.DisableEvents = False
   273         
   274         
   274         words = self.TagName.split("::")
   275         words = self.TagName.split("::")
   275         self.Variables = [variable["Name"].upper() for variable in self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)]
   276         
       
   277         self.Variables = dict([(variable["Name"], variable["Tree"]) for variable in self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)])
   276         if self.Controler.GetEditedElementType(self.TagName, self.Debug)[1] == "function" or words[0] == "T" and self.TextSyntax == "IL":
   278         if self.Controler.GetEditedElementType(self.TagName, self.Debug)[1] == "function" or words[0] == "T" and self.TextSyntax == "IL":
   277             self.Variables.append(words[-1].upper())
   279             self.Variables[words[-1]] = {}
   278         
   280         
   279         self.Functions = []
   281         self.Functions = []
   280         for category in self.Controler.GetBlockTypes(self.TagName, self.Debug):
   282         for category in self.Controler.GetBlockTypes(self.TagName, self.Debug):
   281             for blocktype in category["list"]:
   283             for blocktype in category["list"]:
   282                 if blocktype["type"] == "function" and blocktype["name"] not in self.Keywords and blocktype["name"] not in self.Variables:
   284                 if blocktype["type"] == "function" and blocktype["name"] not in self.Keywords and blocktype["name"] not in self.Variables.keys():
   283                     self.Functions.append(blocktype["name"].upper())
   285                     self.Functions.append(blocktype["name"].upper())
   284         
   286         
   285         self.EnumeratedValues = []
   287         self.EnumeratedValues = []
   286         for value in self.Controler.GetEnumeratedDataValues():
   288         for value in self.Controler.GetEnumeratedDataValues():
   287             self.EnumeratedValues.append(value.upper())
   289             self.EnumeratedValues.append(value.upper())
   288         
   290         
   289         self.Colourise(0, -1)
   291         self.Colourise(0, -1)
   290         
   292         
   291     def RefreshScaling(self, refresh=True):
   293     def RefreshScaling(self, refresh=True):
   292         pass
   294         pass
       
   295     
       
   296     def IsValidVariable(self, name_list, var_tree):
       
   297         if len(name_list) == 0:
       
   298             return True
       
   299         else:
       
   300             sub_tree = var_tree.get(name_list[0].upper(), None)
       
   301             if sub_tree is not None:
       
   302                 return self.IsValidVariable(name_list[1:], sub_tree)
       
   303         return False
   293     
   304     
   294     def OnStyleNeeded(self, event):
   305     def OnStyleNeeded(self, event):
   295         self.TextChanged = True
   306         self.TextChanged = True
   296         line = self.LineFromPosition(self.GetEndStyled())
   307         line = self.LineFromPosition(self.GetEndStyled())
   297         if line == 0:
   308         if line == 0:
   298             start_pos = last_styled_pos = 0
   309             start_pos = last_styled_pos = 0
   299         else:
   310         else:
   300             start_pos = last_styled_pos = self.GetLineEndPosition(line - 1) + 1
   311             start_pos = last_styled_pos = self.GetLineEndPosition(line - 1) + 1
   301         end_pos = event.GetPosition()
   312         end_pos = event.GetPosition()
   302         self.StartStyling(start_pos, 0xff)
   313         self.StartStyling(start_pos, 0xff)
       
   314         
       
   315         struct_elements = []
   303         
   316         
   304         current_pos = last_styled_pos
   317         current_pos = last_styled_pos
   305         state = SPACE
   318         state = SPACE
   306         line = ""
   319         line = ""
   307         word = ""
   320         word = ""
   314                 elif state == NUMBER:
   327                 elif state == NUMBER:
   315                     self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   328                     self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   316                 elif state == WORD:
   329                 elif state == WORD:
   317                     if word in self.Keywords:
   330                     if word in self.Keywords:
   318                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD)
   331                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD)
   319                     elif word in self.Variables:
   332                     elif self.IsValidVariable(struct_elements + [word], self.Variables):
   320                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE)
   333                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE)
   321                     elif word in self.Functions:
   334                     elif word in self.Functions:
   322                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION)
   335                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION)
   323                     elif word in self.Jumps:
   336                     elif word in self.Jumps:
   324                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP)
   337                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP)
   325                     elif word in self.EnumeratedValues:
   338                     elif word in self.EnumeratedValues:
   326                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   339                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   327                     else:
   340                     else:
   328                         self.SetStyling(current_pos - last_styled_pos, 31)
   341                         self.SetStyling(current_pos - last_styled_pos, 31)
   329                         if self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos:
   342                         if word != "]" and (self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos):
   330                             self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK)
   343                             self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK)
   331                             self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK)
   344                             self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK)
   332                             self.StartStyling(current_pos, 0xff)
   345                             self.StartStyling(current_pos, 0xff)
       
   346                     struct_elements = []
   333                 else:
   347                 else:
   334                     self.SetStyling(current_pos - last_styled_pos, 31)
   348                     self.SetStyling(current_pos - last_styled_pos, 31)
   335                 last_styled_pos = current_pos
   349                 last_styled_pos = current_pos
   336                 state = SPACE
   350                 state = SPACE
   337                 line = ""
   351                 line = ""
   338             elif line.endswith("(*") and state != COMMENT:
   352             elif line.endswith("(*") and state != COMMENT:
   339                 self.SetStyling(current_pos - last_styled_pos - 1, 31)
   353                 self.SetStyling(current_pos - last_styled_pos - 1, 31)
   340                 last_styled_pos = current_pos
   354                 last_styled_pos = current_pos
       
   355                 if state == WORD:
       
   356                     struct_elements = []
   341                 state = COMMENT
   357                 state = COMMENT
   342             elif state == COMMENT:
   358             elif state == COMMENT:
   343                 if line.endswith("*)"):
   359                 if line.endswith("*)"):
   344                     self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT)
   360                     self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT)
   345                     last_styled_pos = current_pos + 1
   361                     last_styled_pos = current_pos + 1
   364                     word += char
   380                     word += char
   365             else:
   381             else:
   366                 if state == WORD:
   382                 if state == WORD:
   367                     if word in self.Keywords:
   383                     if word in self.Keywords:
   368                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD)
   384                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD)
   369                     elif word in self.Variables:
   385                     elif self.IsValidVariable(struct_elements + [word], self.Variables):
   370                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE)
   386                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE)
   371                     elif word in self.Functions:
   387                     elif word in self.Functions:
   372                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION)
   388                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION)
   373                     elif word in self.Jumps:
   389                     elif word in self.Jumps:
   374                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP)
   390                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP)
   375                     elif word in self.EnumeratedValues:
   391                     elif word in self.EnumeratedValues:
   376                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   392                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   377                     else:
   393                     else:
   378                         self.SetStyling(current_pos - last_styled_pos, 31)
   394                         self.SetStyling(current_pos - last_styled_pos, 31)
   379                         if self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos:
   395                         if word != "]" and (self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos):
   380                             self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK)
   396                             self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK)
   381                             self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK)
   397                             self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK)
   382                             self.StartStyling(current_pos, 0xff)
   398                             self.StartStyling(current_pos, 0xff)
       
   399                     if char == '.':
       
   400                         if word != "]":
       
   401                             struct_elements.append(word)
       
   402                     else:
       
   403                         if char == '[':
       
   404                             self.StructElementsStack.append(struct_elements + [word])
       
   405                         struct_elements = []
   383                     word = ""
   406                     word = ""
   384                     last_styled_pos = current_pos
   407                     last_styled_pos = current_pos
   385                     state = SPACE
   408                     state = SPACE
   386                 elif state == NUMBER:
   409                 elif state == NUMBER:
   387                     self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   410                     self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   388                     last_styled_pos = current_pos
   411                     last_styled_pos = current_pos
   389                     state = SPACE
   412                     state = SPACE
       
   413                 if char == ']':
       
   414                     struct_elements = self.StructElementsStack.pop(-1)
       
   415                     word = char
       
   416                     state = WORD
   390             current_pos += 1
   417             current_pos += 1
   391         if state == COMMENT:
   418         if state == COMMENT:
   392             self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT)
   419             self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT)
   393         elif state == NUMBER:
   420         elif state == NUMBER:
   394             self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   421             self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   395         elif state == WORD:
   422         elif state == WORD:
   396             if word in self.Keywords:
   423             if word in self.Keywords:
   397                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD)
   424                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD)
   398             elif word in self.Variables:
   425             elif self.IsValidVariable(struct_elements + [word], self.Variables):
   399                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE)
   426                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE)
   400             elif word in self.Functions:
   427             elif word in self.Functions:
   401                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION)
   428                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION)
   402             elif word in self.Jumps:
   429             elif word in self.Jumps:
   403                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP)
   430                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP)
   457                     if words[0].upper() in ["CAL", "CALC", "CALNC"]:
   484                     if words[0].upper() in ["CAL", "CALC", "CALNC"]:
   458                         kw = self.Functions
   485                         kw = self.Functions
   459                     elif words[0].upper() in ["JMP", "JMPC", "JMPNC"]:
   486                     elif words[0].upper() in ["JMP", "JMPC", "JMPNC"]:
   460                         kw = self.Jumps
   487                         kw = self.Jumps
   461                     else:
   488                     else:
   462                         kw = self.Variables
   489                         kw = self.Variables.keys()
   463             else:
   490             else:
   464                 kw = self.Keywords + self.Variables + self.Functions
   491                 kw = self.Keywords + self.Variables.keys() + self.Functions
   465             if len(kw) > 0:
   492             if len(kw) > 0:
   466                 kw.sort()
   493                 kw.sort()
   467                 self.AutoCompSetIgnoreCase(True)
   494                 self.AutoCompSetIgnoreCase(True)
   468                 self.AutoCompShow(len(words[-1]), " ".join(kw))
   495                 self.AutoCompShow(len(words[-1]), " ".join(kw))
   469         else:
   496         else: