swift-emacs / swift-mode

Emacs support for Apple's Swift programming language.
GNU General Public License v3.0
363 stars 47 forks source link

Indentation for generic parameter lists #164

Closed dabrahams closed 4 years ago

dabrahams commented 4 years ago

The automatic indentation for cases like this is somewhat suboptimal:

struct NonuniformTrainingBatches<
  BaseSamples: Collection, 
  BaseIndices: MutableCollection, BatchSample: Equatable
> {
  func foo() {}
}

I get:

struct NonuniformTrainingBatches<
BaseSamples: Collection, 
BaseIndices: MutableCollection, BatchSample: Equatable
                                  > {
  func foo() {}
}

The original indentation is what I'd have wanted. Generic parameter lists should probably be treated as "parenthesized expressions".

taku0 commented 4 years ago

Short answer: it could be improved. I will try it.

Discussion:

< and > are used as either binary operators or angle brackets. Treating them as a bracket unconditionally would break indentation of expressions. So we must distinguish them.

There are two cases where < ... > is used other than binary operator:

generic-argument-clause is ambiguous: foo(A < B, C > (d)) could be parsed as foo((A < B), (C > (d))) or foo(A<B, C>(d)) (invocation of A with two type arguments B, C and one value d). On the other hand, generic-parameter-clause seems to be much simpler; looking back one or two tokens is sufficient to recognize the open bracket.

Summary of grammers of generic-parameter-clause:

taku0 commented 4 years ago

Fixed by https://github.com/swift-emacs/swift-mode/commit/29759f529032fff9b88a77318a10b2a1ec8ef5c7. Thank you for reporting.

dabrahams commented 4 years ago

Thank you!

On Fri, Apr 17, 2020 at 11:19 PM taku0 notifications@github.com wrote:

Fixed by 29759f5 https://github.com/swift-emacs/swift-mode/commit/29759f529032fff9b88a77318a10b2a1ec8ef5c7 .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/swift-emacs/swift-mode/issues/164#issuecomment-615582464, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAKYINTKITSBWAVRZOOMS3RNFA6XANCNFSM4MJ5Q7RQ .

-- Dave