PLAY: Fat removal game

Hi List,

I’ve played recently with the StyledTextCtrl and I’ve tried to implement a very small editor.
The code is very small and I would like to get it even smaller…
Any ideas are welcomed. :slight_smile:

This is not Yet Another Python Editor… just a play with the code to see how much functionality could be implemented with the minimum amount of code.

The approach I used is to capture the keypress and convert it to a command string like “CTRL+F4” and to keep a map of commands strings to methods with a very simple command processor calling those methods with the source of the keypress.

Right now the editor can:
CTRL+N - New File
CTRL+O - Open File
CTRL_S - Save File
CTRL+F4 - Close the App making sure the dirty files get a chance to being saved :slight_smile:
CTRL+Q - python comment/uncomment
plus the default StyledTextCtrl commands

The code is split in 3 parts:
customSTC.py handles the mapping of the default StyledTextCtrl commands to command strings
skin.py contains the python skin
minied.py
implement the notebook and the notebook commands

So… if you think you can slim the current code… please take a look…

As I said… any ideas are welcomed!

Thank you for reading this far.

Peter

minied.zip (6.21 KB)

···


There is NO FATE, we are the creators.

Remove the static definition of customSTC.wxk_map and generate it by
doing...

wxk_map = {}
for i in dir(wx):
    if i.startswith('WXK_'):
        wxk_map[getattr(wx, i)] = i[4:]

If you want to make writing arbitrary content manipulation commands
easier (and shorter), I would suggest taking a look at
plugins/lineabstraction.py from PyPE. It's currently pretty large
because it includes as much functionality as I thought would be useful,
but you could trim it for your own uses. Examples of its use are
available in PyPE's documentation and in the sample macros
macros/samples/ .

- Josiah

···

"Peter Damoc" <pdamoc@gmail.com> wrote:

Hi List,

I've played recently with the StyledTextCtrl and I've tried to implement a
very small editor.
The code is very small and I would like to get it even smaller...
Any ideas are welcomed. :slight_smile:

Peter Damoc:

Hi List,

I've played recently with the StyledTextCtrl and I've tried to implement a very small editor.
The code is very small and I would like to get it even smaller...
Any ideas are welcomed. :slight_smile:

Nice idea. I looked at customSTC.py and there is room for improvement, besides Josiah's suggestion. For example, this is the last method:

     def Expand(self, line, doExpand, force=False, visLevels=0,
                level=-1):
         lastChild = self.GetLastChild(line, level)
         line = line + 1

         while line <= lastChild:
             if force:
                 if visLevels > 0:
                     self.ShowLines(line, line)
                 else:
                     self.HideLines(line, line)
             else:
                 if doExpand:
                     self.ShowLines(line, line)

             if level == -1:
                 level = self.GetFoldLevel(line)

             if level & stc.STC_FOLDLEVELHEADERFLAG:
                 if force:
                     if visLevels > 1:
                         self.SetFoldExpanded(line, True)
                     else:
                         self.SetFoldExpanded(line, False)

                     line = self.Expand(line, doExpand, force,
                                        visLevels-1)

                 else:
                     if doExpand and self.GetFoldExpanded(line):
                         line = self.Expand(line, True, force,
                                            visLevels-1)
                     else:
                         line = self.Expand(line, False, force,
                                            visLevels-1)
             else:
                 line = line + 1

         return line

If I was to review this code, I would say it is too long, too deeply nested and suffers from duplication. In addition, there are no tests, so we would have to be careful refactoring this.

My first change would be to call self.SetFoldExpanded only once:
                     self.SetFoldExpanded(line, visLevels > 1)

My next step would be to remove the duplicated calls to self.Expand:
                     expandLine = doExpand
                 else:
                     expandLine = doExpand and self.GetFoldExpanded(line)
                 line = self.Expand(line, expandLine, force,

                                    visLevels-1)

I would replace "line = line + 1" with "line += 1".

Further refactoring then gets me this version (untested):

     def Expand(self, line, doExpand, force=False, visLevels=0,
                level=-1):
         lastChild = self.GetLastChild(line, level)
         line += 1
         while line <= lastChild:
             if (force and visLevels > 0) or (not force and doExpand):
                 self.ShowLines(line, line)
             elif force and visLevels == 0:
                 self.HideLines(line, line)
             if level == -1:
                 level = self.GetFoldLevel(line)
             if level & stc.STC_FOLDLEVELHEADERFLAG:
                 if force:
                     self.SetFoldExpanded(line, visLevels > 1)
                     expandLine = doExpand
                 else:
                     expandLine = doExpand and self.GetFoldExpanded(line)
                 line = self.Expand(line, expandLine, force, visLevels-1)
             else:
                 line += 1
         return line

To refactor this further I need to know more about how this works. E.g. does it make sense to hide a line and then call expand on it? What is force actually meant to force? Why does the recursive call to Expand not pass the level argument? Etc. If I understand the code better I could maybe extract a few extra methods, or reorder to make it show its intention better.

Other methods have similar issues, OnUpdateUI for example. Are you sure that method is correct? styleBefore is uninitialized if caretPos == 0. I noticed this while refactoring it...

Cheers, Frank

Hi Frank,
Thank you for your contribution. I will check it in the morning and maybe post a new version of the archive.
The methods you were looking at (OnUpdateUI, OnMarginClick, FoldAll and Expand) are a verbatim copy&paste from the wxPython Demo. I’ve looked at them but they looked pretty much Ok.
As I said… first thing in the morning… I will look again :slight_smile:
Thank you again for joining in :wink:
Peter.

···

On 10/30/06, Frank Niessink < frank@niessink.com> wrote:

Peter Damoc:

Hi List,

I’ve played recently with the StyledTextCtrl and I’ve tried to implement
a very small editor.
The code is very small and I would like to get it even smaller…
Any ideas are welcomed. :slight_smile:

Nice idea. I looked at customSTC.py and there is room for improvement,
besides Josiah’s suggestion. For example, this is the last method:

To refactor this further I need to know more about how this works. E.g.
does it make sense to hide a line and then call expand on it? What is
force actually meant to force? Why does the recursive call to Expand not
pass the level argument? Etc. If I understand the code better I could

maybe extract a few extra methods, or reorder to make it show its
intention better.

Other methods have similar issues, OnUpdateUI for example. Are you sure
that method is correct? styleBefore is uninitialized if caretPos == 0. I

noticed this while refactoring it…

Cheers, Frank


There is NO FATE, we are the creators.

Hi list,

Here is the new archive with Josiah and Frank’s contributions.

Peter.

minied.02.zip (5.66 KB)

···


There is NO FATE, we are the creators.

Peter Damoc:

Hi list,

Here is the new archive with Josiah and Frank's contributions.

Hi Peter,

Here's a challenge: add a menubar and menu's that contain the commands already defined. Please tell us what happened.

Cheers, Frank