File: drSourceBrowserGoTo.py

package info (click to toggle)
drpython 1%3A3.11.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,572 kB
  • ctags: 1,504
  • sloc: python: 16,099; sh: 319; makefile: 39
file content (125 lines) | stat: -rw-r--r-- 4,474 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#   Programmer: Daniel Pozmanter
#   E-mail:     drpython@bluebottle.com
#   Note:       You must reply to the verification e-mail to get through.
#
#   Copyright 2003-2007 Daniel Pozmanter
#
#   Distributed under the terms of the GPL (GNU Public License)
#
#   DrPython is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#Go To Source Browser

import re
import wx
from drSingleChoiceDialog import drSingleChoiceDialog

respacecount = re.compile(r'\S')

def GetMatches(text, resourcebrowser):
    matches = []
    positions = []

    matcher = resourcebrowser.finditer(text)

    try:
        match = matcher.next()
    except:
        match = None
    while match is not None:
        matches.append(match.group().replace('\t', '    '))
        positions.append(match.start())
        try:
            match = matcher.next()
        except:
            match = None

    return matches, positions

class drSourceBrowserGoToDialog(drSingleChoiceDialog):

    def __init__(self, parent, Document, matches, positions):
        drSingleChoiceDialog.__init__(self, parent, 'Source Browser Go To', matches, False, SetSizer=False)

        self.Document = Document

        self.positions = positions

        self.txtLine = wx.TextCtrl(self, -1, '', (0, 0), (350, -1), style=wx.TE_READONLY)
        self.txtDefinedIn = wx.TextCtrl(self, -1, '', (0, 0), (350, -1), style=wx.TE_READONLY)

        self.lineSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.lineSizer.Add(wx.StaticText(self, -1, '  '), 0, wx.SHAPED)
        self.lineSizer.Add(self.txtLine, 1, wx.EXPAND)
        self.lineSizer.Add(wx.StaticText(self, -1, '  '), 0, wx.SHAPED)

        self.defSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.defSizer.Add(wx.StaticText(self, -1, '  '), 0, wx.SHAPED)
        self.defSizer.Add(self.txtDefinedIn, 1, wx.EXPAND)
        self.defSizer.Add(wx.StaticText(self, -1, '  '), 0, wx.SHAPED)

        self.theSizer.Insert(2, wx.StaticText(self, -1, '  '), 0, wx.SHAPED)
        self.theSizer.Insert(3, self.lineSizer, 0, wx.EXPAND)
        self.theSizer.Insert(4, self.defSizer, 0, wx.EXPAND)

        self.SetSizerAndFit(self.theSizer)

        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelected, id=self.ID_CHOICES)

        self.OnSelected(None)

    def OnSelected(self, event):
        sel = self.listChoices.GetFirstSelected()
        if sel > -1:
            pos = self.listChoices.GetItemData(sel)

            spaces = respacecount.search(self.choices[pos]).start() - 1

            definedin = self.Document.GetFilenameTitle()

            if spaces > 0:
                x = pos
                while x > -1:
                    if (respacecount.search(self.choices[x]).start() - 1) < spaces:
                        definedin = self.choices[x].rstrip()
                        break
                    x -= 1

            linenumber = str(self.Document.LineFromPosition(self.positions[pos]) + 1)

            self.txtLine.SetValue('Line Number:      ' + linenumber)
            self.txtDefinedIn.SetValue('Defined In:    ' + definedin)

def SourceBrowserGoTo(frame, Document):
    compchar = Document.GetIndentationCharacter()
    resourcebrowser = re.compile(r'(^'+compchar+'*?class\s.*[(:])|(^'+compchar+'*?def\s.*[(:])|(^'+compchar+'*?import\s.*$)|(^'+compchar+'*?from\s.*$)', re.MULTILINE)
    text = Document.GetText()

    matches, positions = GetMatches(text, resourcebrowser)

    d = drSourceBrowserGoToDialog(frame, Document, matches, positions)
    answer = d.ShowModal()
    d.Destroy()

    if answer == wx.ID_OK:
        i = d.GetSelection()
        line = Document.LineFromPosition(positions[i])
        Document.EnsureVisible(line)
        Document.ScrollToLine(line)
        Document.GotoLine(line)
        Document.GotoPos(positions[i])

    Document.SetFocus()
    Document.SetSTCFocus(True)