Method source editor automatic indenting?

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

Method source editor automatic indenting?

Griff-2
When you are working within the method source editor, is there a way to
enable automatic indenting?

Right now when I type each line, the cursor goes back to the far left
side of the control so I've got to hit <tab> on each new line before I
start typing. For example,

printOn: aStream
        "Append, to aStream, a String whose characters are a description
         of the receiver as a developer would want to see it"

        self basicPrintOn: aStream.
        aStream nextPut: $(.
        self owner printOn: aStream.
        aStream nextPut: $).

required 6 tab presses.

Thanks for your answers!


Reply | Threaded
Open this post in threaded view
|

Re: Method source editor automatic indenting?

Andy Bower
Griff,

> When you are working within the method source editor, is there a way
> to enable automatic indenting?
>
> Right now when I type each line, the cursor goes back to the far left
> side of the control so I've got to hit <tab> on each new line before I
> start typing. For example,
>
> printOn: aStream
> "Append, to aStream, a String whose characters are a description
> of the receiver as a developer would want to see it"
>
> self basicPrintOn: aStream.
> aStream nextPut: $(.
> self owner printOn: aStream.
> aStream nextPut: $).
>
> required 6 tab presses.

No, there isn't any way to easily set this up. Automatic indenting
wouldn't be quite as simple as it sounds because the actual behaviour
would have to depend on how you want the code to be formatted and there
could be may different schemes.

What Dolphin does have, however, is an automatic formatter that can be
applied when your method is compiled. You can invoke this by typing
Ctrl-Shift-S to save your changes instead of just Ctrl+S. While this
doesn't sound like what you're requesting, in reality it can be rather
useful.

Personally, I never try to format source as I type it.  Instead I just
enter the code, perhaps using just newlines (and sometimes not even
using these, just spaces). At the end of the method I just hit
Ctrl-Shift-S and the source is compiled and formatted.  This doesn't
sound like much but it frees you from the mental and physical effort of
trying to lay out the source in a particular way. Since Smalltalk
methods are generally so short, I don't find the problem to understand
the unformatted source while I'm typing it.

There are problems with this scheme, however.  In some situations the
automatic formatter doesn't necessarily do what you want; in particular
it is a bit odd when it comes to embedded comments in the method
source.  Also, some users have complained that they don't like the
formatting algorithm used.  Although this can be changed, I'd recommend
just sticking with the default formatting - my experience was that it
was surprisingly easy to get used to even though it wasn't my original
style.

I hope this helps.

--
Best regards,

Andy Bower
Dolphin Support
www.object-arts.com


Reply | Threaded
Open this post in threaded view
|

Re: Method source editor automatic indenting?

Jeff M.
Andy Bower wrote:
>
> What Dolphin does have, however, is an automatic formatter that can be
> applied when your method is compiled. You can invoke this by typing
> Ctrl-Shift-S to save your changes instead of just Ctrl+S. While this
> doesn't sound like what you're requesting, in reality it can be rather
> useful.

Let me confirm this. I have used many, many IDEs for various languages,
and I have grown to completely love this feature. It didn't take long
for me to just ignore making the code look pretty en lieu of just
getting it to work and thinking it through. Once it works, then I just
sprinkle some comments to help "force" the formatting the way I'd like
it.

The only (let me stress that... the ONLY) complaint I have about it is
when I'm sending messages that have more than one argument, they will
be automatically formatted to multiple lines. The same goes for calling
multiple methods with the same object. For example:

 someVector x: 10; y: 20; z: 30.

would get formatted:

 someVector
    x: 10;
    y: 20;
    z: 30.

This isn't bad, but if I'm setting 20 vectors, it's easier to have them
formatted horizontally instead of vertically. In this case, I just
format them myself. But there are also many instances where I want it
done the latter way - and even trying to come up with a methodology
that I follow to dicern which is better is difficult.

It is an absolute blessing to just type code w/o having to worry about
how it looks and just get it working first. Then have the whole thing
formatted nicely for me.

Jeff M.


Reply | Threaded
Open this post in threaded view
|

Re: Method source editor automatic indenting?

Tim M
>Jeff M. wrote:
> Andy Bower wrote:

> Let me confirm this. I have used many, many IDEs for various languages,
> and I have grown to completely love this feature. It didn't take long
> for me to just ignore making the code look pretty en lieu of just
> getting it to work and thinking it through.

While I mostly agree with you guys, I think the original request of
doing at least something with doing auto indenting is still useful.
This is 2006 after all and it feels a bit lame to just type on new
lines all the time... and then press format at the end (yes I do it too
- but I do grit my teeth a bit).

I certainly would like something that did a bess guess indenting while
I was typing. It wouldn't have to be perfect, and I would still use the
auto formatter, but I think it would help you think a bit as you type.
It would also feel nicer - just as the Tab browsers help keep me
organized.

So I would certainly like to see something done with this.

Tim


Reply | Threaded
Open this post in threaded view
|

Re: Method source editor automatic indenting?

Joseph Pelrine-7
Hey Tim,

TimM wrote:

>>Jeff M. wrote:
>>Andy Bower wrote:
> <snip>
> I certainly would like something that did a bess guess indenting while
> I was typing. It wouldn't have to be perfect, and I would still use the
> auto formatter, but I think it would help you think a bit as you type.
> It would also feel nicer - just as the Tab browsers help keep me
> organized.
>
> So I would certainly like to see something done with this.

Here's my version. I've added comments so you can tweak it to match your
coding preferences.

SmalltalkWorkspace>>#autoIndentText
        | text insertionPosition char position crSize numTabs replaceRange |
        (text := self view plainText) isEmpty ifTrue: [^self].
        crSize := 2.
        insertionPosition := view caretPosition.
        position := (insertionPosition - 3) max: 1.
        numTabs := 0.
        char := text at: position.
       
        "Tab in if we're opening a Block"
        (char == $[ or: [char == $(]) ifTrue: [numTabs := 1].
       
        "Tab out if we're closing a Block"
        char == $] ifTrue: [numTabs := -1].
        (char == $. and: [(text at: (position - 1 max: 1)) == $]]) ifTrue:
[numTabs := -1].
       
        "Roll back and find out how many tabs the previous line has"
        [position > 0 and: [(char := text at: position) ~~ Character cr]]
whileTrue:
                        [char == $[ ifTrue: [numTabs := numTabs max: 0].
                        position := position - 1].
        position := position + crSize min: text size.
        [(text at: position ifAbsent: []) == Character tab] whileTrue:
                        [numTabs := numTabs + 1.
                        position := position + 1].

        replaceRange := insertionPosition to: insertionPosition - 1.
        view
                selectionRange: replaceRange;
                replaceSelection: ((String new: (numTabs max: 0)) atAllPut: Character tab)

To use it, though, you have to change the system method below :-( Sorry,
Blair, you know I hate doing that, but it was the simplest thing that
could possibly work. As always, feel free to roll it into the base.


SmalltalkWorkspace>>#onCharAdded: aCharacter
        "add this line"
        (aCharacter == Character cr)
                ifTrue: [self autoIndentText].
        (self isAutoCompletionEnabled and: [self isSyntaxColoringEnabled])
ifFalse: [^self].
        self startAutocompleteTimer

Hope that helps!
Joseph


Reply | Threaded
Open this post in threaded view
|

Re: Method source editor automatic indenting?

Tim M
Hi Joseph,

> Here's my version. I've added comments so you can tweak it to match your
coding preferences.

Thats a nice simple solution, it works pretty well in my image, and like
I said - I just do a final reformat when I've worked out my solution.

If you don't mind, I'll plug it into the next release of Intelli-Dolphin
(which handles patching system methods, and recovering them if you unload).

Tim


Reply | Threaded
Open this post in threaded view
|

Re: Method source editor automatic indenting?

Joseph Pelrine-7
Tim M wrote:
> Hi Joseph,
>
>> Here's my version. I've added comments so you can tweak it to match your
>
> coding preferences.
>
> Thats a nice simple solution, it works pretty well in my image, and like
> I said - I just do a final reformat when I've worked out my solution.
I agree, there is a difference between indenting and formatting...
>
> If you don't mind, I'll plug it into the next release of Intelli-Dolphin
> (which handles patching system methods, and recovering them if you unload).
Please do. Glad to know I've helped someone.

Cheers
Joseph


Reply | Threaded
Open this post in threaded view
|

Re: Method source editor automatic indenting?

Griff-2
That is a nice feature, thank you.