Worksheet functionality

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Worksheet functionality

Stefan Schmiedl
Does anybody out there have some magic (or even better: code) for
getting the a worksheet to evaluate/print the current chunk of code?

I have a worksheet full of snippets like

MyPresenter show.
!
MyPresenter allInstances do: [:each | each view close]
!
Presenter subclass: #ANewPresenter
        instanceVariableNames: 'table'
        classVariableNames: ''
        poolDictionaries: ''
        classInstanceVariableNames: ''
!
Model subclass: #ANewModel
        instanceVariableNames: 'table'
        classVariableNames: ''
        poolDictionaries: ''
        classInstanceVariableNames: ''
!
|r img|
[r := IStatConnector createObject: 'StatConnectorSrv.StatConnector'.
r init: 'R'.
r evaluateNoReturn: 'png("e:\demo.png", 400, 300)';
        evaluateNoReturn: 'plot(((-30:30)/10)**3)';
        evaluateNoReturn: 'dev.off()'.
] ensure: [r close].
img := (GdiplusImage fromFile: 'e:\demo.png').
(ImagePresenter show: 'Basic image' on: img) topShell extent: img
extent;
        caption: 'CFaR Prototyp 01/2006'

While Ctrl+e/Ctrl+d works good enough for the one-liners, I get slowed
down while selecting the longer expressions like the last one. Ideally
Ctrl+Shift+e would evaluate the chunk containing the text cursor.

Happy new year, fellow Dolphineers.

s.


Reply | Threaded
Open this post in threaded view
|

Re: Worksheet functionality

Ian Bartholomew-21
Stefan,

> Does anybody out there have some magic (or even better: code) for
> getting the a worksheet to evaluate/print the current chunk of code?

I can't say it's something I ever felt the need for, but it's an easy
enough change to implement...

SmalltalkWorkspace>>selectEvaluationRange
        "Private - Answer the current evaluation range, selecting the current
line and answering its
        entire range if there is no selection."

        | te range |
        te := self view.
        range := te selectionRange.
        range isEmpty
                ifTrue:
                        [| previous next |
                        previous := (1 to: te selectionRange start) select: [:index | (te
text at: index) = $!].
                        next := (te selectionRange stop to: te text size) select: [:index |
(te text at: index) = $!].
                        previous isEmpty & next notEmpty ifTrue: [previous :=
OrderedCollection with: -1].
                        previous notEmpty & next isEmpty ifTrue: [next := OrderedCollection
with: te text size + 1].
                        previous notEmpty & next notEmpty
                                ifTrue:
                                        [range := previous last + 2 to: next first - 2.
                                        te selectionRange: range]
                                ifFalse: [range := te selectCurrentLine]].
        ^range




- If there is a selection made then that is evaluated.
- If there are no bangs then the line is evaluated (as normal).
- If there are two or more bangs then the chunk around the cursor
position is evaluated.
- If there is only one bang then all the code before or after the bang
(depending on the cursor location) is evaluated.

That's the way it is supposed to work anyway :-)

NB. If there embedded bangs (in comments, strings etc) then it won't
work as expected.

--
Ian

Use the Reply-To address to contact me (limited validity).
Mail sent to the From address is ignored.


Reply | Threaded
Open this post in threaded view
|

Re: Worksheet functionality

Stefan Schmiedl
Hi Ian,

thanks for providing code that I could work on.

On Sun, 01 Jan 2006 13:31:32 +0000, Ian Bartholomew wrote:

>> Does anybody out there have some magic (or even better: code) for
>> getting the a worksheet to evaluate/print the current chunk of code?
>
> I can't say it's something I ever felt the need for, but it's an easy
> enough change to implement...

I tend to try things out in a workspace, and after a while a
screenful of "few-liners" emerges at the top of the workspace
which make my life easier. Not to mention them being an important
aid for a failing memory ...

For example, I enjoy working with *few* things. So my browser
shows the flat list of classes in the current package. How many
mouse clicks would I need to create a new class possibly from a
superclass currently not visible?

Now, thanks to your sample code (which I messed with, of course),
I can Alt+Left to the Worksheet besides the Browser, substitute
the class names, Ctrl+e and Alt+Right and I'm done. That's *much*
faster for me.

And yes, vim is my favourite text editor.

>
> - If there is a selection made then that is evaluated.
> - If there are no bangs then the line is evaluated (as normal).
> - If there are two or more bangs then the chunk around the
>   cursor position is evaluated.
> - If there is only one bang then all the code before or after
>   the bang (depending on the cursor location) is evaluated.
>
> That's the way it is supposed to work anyway :-)

I think that you were off by one with an index or two, especially
when sitting at the end of the text. I guess mine will fail too,
if you evaluate an empty workspace, but that's a case of "don't
do it, then" for me.

>
> NB. If there embedded bangs (in comments, strings etc) then it won't work
> as expected.

The following code behaves a little bit different, but is just what I need:
- A selection is evaluated.
- If there are no bangs, the complete text is evaluated.
- If there are bangs, the "current" chunk is evaluated.
- Detection of chunk delimiters is more robust, as it checks for
  lineDelimiter on the "inside".
- And, very important (not), it is *much* faster for *huge*
  Workspaces, since I'm using detect instead of building an
  OrderedCollection just to throw it away :-)

selectEvaluationRange
 "Private - Answer the current evaluation range, selecting the
 current chunk and answering its entire range if there is no
 selection."

 | te range |
 te := self view.
 range := te selectionRange.
 range isEmpty
  ifTrue:
   [| previous next |
   previous := (te selectionRange start - 1 to: 1 by: -1) detect:
       [:index |
       (te text at: index) = $! and: [
        (te text midString: 2 from: index + 1) =
         ##(String lineDelimiter)]]
      ifNone: [-1].
   next := (te selectionRange stop + 1 to: te text size) detect:
       [:index |
       (te text at: index) = $! and: [
        (te text midString: 2 from: index - 2) =
         ##(String lineDelimiter)]]
      ifNone: [te text size + 2].
   range := previous + 2 to: next - 2.
   te selectionRange: range].
 ^range

Hacking your development tools, what a great way to start the new
year.

s.