Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!howland.reston.ans.net!ix.netcom.com!netcom.com!kcooper
From: kcooper@netcom.com (Ken Cooper)
Subject: Nifty edIt Extension
Message-ID: <kcooperD5I872.Gqz@netcom.com>
Keywords: edIt extension accessors get set cooper peters
Organization: NETCOM On-line Communication Services (408 261-4700 guest)
Date: Wed, 15 Mar 1995 23:06:38 GMT
Lines: 137
Sender: kcooper@netcom8.netcom.com


For those of you out there who have picked up edIt, either
in demo or release form, here's a goody for you.

A common issue for Smalltalk programmers is having to add 
accessors (get and set) methods for the variables you declare.
Wouldn't it be nice if you could simply click on the reference
to a variable in your code, and have it generate get/set methods 
for you?  Well, now you can, using this edIt extension.  If there
is a variable reference under the cursor when you execute this,
accessors will automatically be generated.  If no variable is
under the cursor, a QuickList will appear with all the possible
variables you'd add accessors for.

This extension works in standard VST Win32, VSE Win32, and VST +
Xoteryx browsers.

Enjoy!

Ken Cooper
Cooper & Peters, Inc.

P.S. This is a good example of the kind of thing we're looking
for in the contest we're holding for edIt extensions.

=============== CUT HERE ==================

"This filein contains an edIt extension that automatically
generates get and set methods for the variable under the
cursor. If the variable is a class or class instance variable, 
the extension adds the methods as class methods.

Category: Smart Editing
Key Binding: Ctrl-Y+A
Browser Support: Team/V, Base Image, Xoteryx
Author: Ken Cooper (71571.407@compuserve.com)
Methods:
  Behavior>>addAccessorsFor: aVariableName
  CPEdIt>>addAccessorsForSelectedVariable
  CPEdIt>>changedSelectors
"!

!Behavior methods!
  
addAccessorsFor: aVariableName

    "Private - Adds get and set methods for aVariableName.
    Copyright 1995, Cooper & Peters, Inc."
    | s |

    #addedByCnP.
    s := ReadWriteStream on: String new.
    s nextPutAll: '!!', self name, ' methods!!'; cr.
    s nextPutAll: aVariableName; cr; cr.
    s nextPutAll: '    ^', aVariableName, '!! !!'.
    s fileIn.

    s := ReadWriteStream on: String new.
    s nextPutAll: '!!', self name, ' methods!!'; cr.
    s nextPutAll: aVariableName, ': anObject'; cr; cr.
    s nextPutAll: '    ', aVariableName, ' := anObject.!! !!'.
    s fileIn.! !

!CPEdIt methods!
  
addAccessorsForSelectedVariable

    "Add methods to the selected class accessing the variable under the cursor.
     If the variable is a class or class instance variable, add the accessors
     as class methods. If no variable is under the cursor, bring up a list."

    | variableName classForDeclaration list |

    variableName := self wordAtCursor.
    classForDeclaration := self editedClass.
    
    classForDeclaration isNil ifTrue: [ ^self ].

    variableName isEmpty ifTrue: [
        list := classForDeclaration instVarNames asOrderedCollection.
        list addAll: classForDeclaration classVarNames.
        list addAll: classForDeclaration class instVarNames.
        variableName := (CPSelectStringDlg new 
            openOn: list asSortedCollection asOrderedCollection
            selecting: nil 
            windowTitle: 'Select a Variable to Add Get/set Methods For') selection.
        variableName isNil ifTrue: [
                ^self
        ].
    ].

    variableName first isUpperCase ifTrue: [
        (classForDeclaration classVarNames includes: variableName) ifFalse: [
            ^self
        ].
        classForDeclaration := classForDeclaration class.
    ] ifFalse: [
        (classForDeclaration class instVarNames includes: variableName) ifTrue: [
            classForDeclaration := classForDeclaration class.
        ] ifFalse: [
            (classForDeclaration instVarNames includes: variableName) ifFalse: [
                 ^self
            ].
        ].
    ].
    classForDeclaration addAccessorsFor: variableName.
    self changedSelectors.
! !

!CPEdIt methods!
 
changedSelectors

    "Private - A selector has changed for the selected class.     Notify
    the browser we're in to update its method list, if it has one"

    | methodList |

    "Notifies any viewmanagers under CodeBrowser"
    self outmostManager changed: #selectors:.

    "Notifies the Team/V SubsystemBrowser. Ugly, but simplest solution."
    [
        methodList := self outmostManager classEditor methodList.
        methodList flushMethodOrganizer.
        methodList container categoryList updateRebuildList.
        methodList updateList.
    ] on: MessageNotUnderstood do: [ ].
    
    "Notifies Xoteryx browsers. Ugly, but simplest solution."
    [
        self outmostManager methodManager subPane update.
    ] on: MessageNotUnderstood do: [ ].! !

CPCategoryManager current addMethod: #addAccessorsForSelectedVariable inClass: #CPEdIt toCategory: 'Smart Editing'.
CPKeyBindingsSet current bindKey: 'Ctrl-Q+A' toSelector: #addAccessorsForSelectedVariable inClass: #CPEdIt.
!
