📄 phpeditor.py
字号:
#----------------------------------------------------------------------------# Name: PHPEditor.py# Purpose: PHP Script Editor for pydocview tbat uses the Styled Text Control## Author: Morgan Hua## Created: 1/4/04# CVS-ID: $Id: PHPEditor.py,v 1.6 2006/04/20 06:25:59 RD Exp $# Copyright: (c) 2005 ActiveGrid, Inc.# License: wxWindows License#----------------------------------------------------------------------------import wximport stringimport STCTextEditorimport CodeEditorimport OutlineServiceimport osimport reimport FindInDirServiceimport activegrid.util.appdirs as appdirsimport activegrid.util.sysutils as sysutils_ = wx.GetTranslationclass PHPDocument(CodeEditor.CodeDocument): passclass PHPView(CodeEditor.CodeView): def GetCtrlClass(self): """ Used in split window to instantiate new instances """ return PHPCtrl def GetAutoCompleteHint(self): pos = self.GetCtrl().GetCurrentPos() if pos == 0: return None, None validLetters = string.letters + string.digits + '_$' word = '' while (True): pos = pos - 1 if pos < 0: break char = chr(self.GetCtrl().GetCharAt(pos)) if char not in validLetters: break word = char + word return None, word def GetAutoCompleteDefaultKeywords(self): return PHPKEYWORDS #---------------------------------------------------------------------------- # Methods for OutlineService #---------------------------------------------------------------------------- def DoLoadOutlineCallback(self, force=False): outlineService = wx.GetApp().GetService(OutlineService.OutlineService) if not outlineService: return False outlineView = outlineService.GetView() if not outlineView: return False treeCtrl = outlineView.GetTreeCtrl() if not treeCtrl: return False view = treeCtrl.GetCallbackView() newCheckSum = self.GenCheckSum() if not force: if view and view is self: if self._checkSum == newCheckSum: return False self._checkSum = newCheckSum treeCtrl.DeleteAllItems() document = self.GetDocument() if not document: return True filename = document.GetFilename() if filename: rootItem = treeCtrl.AddRoot(os.path.basename(filename)) treeCtrl.SetDoSelectCallback(rootItem, self, None) else: return True text = self.GetValue() if not text: return True INTERFACE_PATTERN = 'interface[ \t]+\w+' CLASS_PATTERN = '((final|abstract)[ \t]+)?((public|private|protected)[ \t]+)?(static[ \t]+)?class[ \t]+\w+((implements|extends)\w+)?' FUNCTION_PATTERN = '(abstract[ \t]+)?((public|private|protected)[ \t]+)?(static[ \t]+)?function[ \t]+?\w+\(.*?\)' interfacePat = re.compile(INTERFACE_PATTERN, re.M|re.S) classPat = re.compile(CLASS_PATTERN, re.M|re.S) funcPat= re.compile(FUNCTION_PATTERN, re.M|re.S) pattern = re.compile('^[ \t]*('+ CLASS_PATTERN + '.*?{|' + FUNCTION_PATTERN + '|' + INTERFACE_PATTERN +'\s*?{).*?$', re.M|re.S) iter = pattern.finditer(text) indentStack = [(0, rootItem)] for pattern in iter: line = pattern.string[pattern.start(0):pattern.end(0)] foundLine = classPat.search(line) if foundLine: indent = foundLine.start(0) itemStr = foundLine.string[foundLine.start(0):foundLine.end(0)] else: foundLine = funcPat.search(line) if foundLine: indent = foundLine.start(0) itemStr = foundLine.string[foundLine.start(0):foundLine.end(0)] else: foundLine = interfacePat.search(line) if foundLine: indent = foundLine.start(0) itemStr = foundLine.string[foundLine.start(0):foundLine.end(0)] if indent == 0: parentItem = rootItem else: lastItem = indentStack.pop() while lastItem[0] >= indent: lastItem = indentStack.pop() indentStack.append(lastItem) parentItem = lastItem[1] item = treeCtrl.AppendItem(parentItem, itemStr) treeCtrl.SetDoSelectCallback(item, self, (pattern.end(0), pattern.start(0) + indent)) # select in reverse order because we want the cursor to be at the start of the line so it wouldn't scroll to the right indentStack.append((indent, item)) treeCtrl.Expand(rootItem) return Trueclass PHPService(CodeEditor.CodeService): def __init__(self): CodeEditor.CodeService.__init__(self)class PHPCtrl(CodeEditor.CodeCtrl): def __init__(self, parent, id=-1, style=wx.NO_FULL_REPAINT_ON_RESIZE): CodeEditor.CodeCtrl.__init__(self, parent, id, style) self.SetLexer(wx.stc.STC_LEX_HTML) self.SetStyleBits(7) self.SetKeyWords(4, string.join(PHPKEYWORDS)) self.SetProperty("fold.html", "1") def CreatePopupMenu(self): FINDCLASS_ID = wx.NewId() FINDDEF_ID = wx.NewId() menu = CodeEditor.CodeCtrl.CreatePopupMenu(self) self.Bind(wx.EVT_MENU, self.OnPopFindDefinition, id=FINDDEF_ID) menu.Insert(1, FINDDEF_ID, _("Find 'function'")) self.Bind(wx.EVT_MENU, self.OnPopFindClass, id=FINDCLASS_ID) menu.Insert(2, FINDCLASS_ID, _("Find 'class'")) return menu def OnPopFindDefinition(self, event): view = wx.GetApp().GetDocumentManager().GetCurrentView() if hasattr(view, "GetCtrl") and view.GetCtrl() and hasattr(view.GetCtrl(), "GetSelectedText"): pattern = view.GetCtrl().GetSelectedText().strip() if pattern: searchPattern = "function\s+%s" % pattern wx.GetApp().GetService(FindInDirService.FindInDirService).FindInProject(searchPattern) def OnPopFindClass(self, event): view = wx.GetApp().GetDocumentManager().GetCurrentView() if hasattr(view, "GetCtrl") and view.GetCtrl() and hasattr(view.GetCtrl(), "GetSelectedText"): definition = "class\s+%s" pattern = view.GetCtrl().GetSelectedText().strip() if pattern: searchPattern = definition % pattern wx.GetApp().GetService(FindInDirService.FindInDirService).FindInProject(searchPattern) def CanWordWrap(self): return True def SetViewDefaults(self): CodeEditor.CodeCtrl.SetViewDefaults(self, configPrefix = "PHP", hasWordWrap = True, hasTabs = True, hasFolding=True) def GetFontAndColorFromConfig(self): return CodeEditor.CodeCtrl.GetFontAndColorFromConfig(self, configPrefix = "PHP") def UpdateStyles(self): CodeEditor.CodeCtrl.UpdateStyles(self) if not self.GetFont(): return faces = { 'font' : self.GetFont().GetFaceName(), 'size' : self.GetFont().GetPointSize(), 'size2': self.GetFont().GetPointSize() - 2, 'color' : "%02x%02x%02x" % (self.GetFontColor().Red(), self.GetFontColor().Green(), self.GetFontColor().Blue()) }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -