Skip to content

Major-mode for Apple's Swift programming language.

License

Notifications You must be signed in to change notification settings

taku0/swift3-mode

Repository files navigation

License GPL 3 MELPA MELPA

swift3-mode

This project has been merged into swift-emacs/swift-mode. Please use it.

Major-mode for Apple's Swift programming language.

This is a fork of chrisbarrett/swift-mode with a new indentation engine, supporting Swift 3.

Installation

Install swift3-mode package from MELPA.

To install without MELPA, download latest release and execute M-x package-install-file.

Features

  • Font Lock

  • Indentation

    switch foo {
    case let .P1(x)
           where
             x > 0,
         let .P2(x)
           where
             x > 0:
        bar()
          .then { x in
              return baz(x)
          }
          .then {(
                   x,
                   y
                 ) in
              return moo(x + y)
          }
    }
    
    // Hanging brace
    let x = [
      1,
      2,
      3
    ]
    
    // Brace on its own line
    let y =
      [
        1,
        2,
        3
      ]
    
    // Utrecht style
    let z =
      [ 1
      , 2
      , 3
      ]
  • forward-sexp

  • beginning-of-defun and end-of-defun

  • indent-new-comment-line

  • Imenu

  • Running Swift REPL in a buffer (M-x run-swift)

This package does not provide flycheck. See flycheck-swift.

Limitations

Some syntax constructs removed from Swift 3.0 are not supported:

  • C-style for-loop: for var i = 1; i < 10; i++ { }

  • Multiple assignments in single if let:

    if let x = x,
           y = y {
    }

    Use multiple let instead:

    if let x = x,
       let y = y {
    }

Indentation may not accurate. For example, foo(Bar < A, B > (c)) is ambiguous. It is indented like either

foo(Bar < A,
    B > (c)) // Passing two Boolean arguments to foo

or

foo(Bar < A,
          B > (c)) // constructing Bar with two type arguments and a value

The Swift compiler disambiguates this case using tokens after >, but those tokens may not available in editing time. We use some heuristic for this.

Another example is difficulty of handling of colons. We have to pair all ? and : of conditional operators to decide indentation. This is a future work.

switch foo {
  case let P(x) where x is Foo? ? a ? b : c ?? d : e ? f : g :
    h ? i?.j() : k()
}

switch foo {
  case let P(x) where (x is Foo?) ? (a ? b : c ?? d) : (e ? f : g) :
    h ? i?.j() : k()
}

Yet another difficult case is consistency of blocks. We want to indent method chains like this:

var x = foo
  .then { x in
      aaa
  }
  .then { x in
      aaa
  }

while we also want to indent if body like this:

if anotherVeryLongVariableName
     .veryLongPropertyName {
    aaa
}

Then, how should we indent this when the cursor is before @?

var x = foo
  .bar {
    @

This could be

var x = foo
  .bar {
    @abc willSet {
        aaa
    }
}

or

var x = foo
  .bar {
      @abc var x = 1
      x
  }

Both are syntactically correct code. We cannot handle this case properly. This is also a future work.

Hacking

To build the package locally, run make package. You need Cask.

To install the built package, run make install.

To run tests, run make check.

For other commands, run make help.

Related projects

Acknowledgements

The REPL code is based on js-comint.

Thanks to the following original developer and users for their contributions:

You can find a full list of those people here.

Thanks to @purcell (Steve Purcell) for advices on the code and arrangement for merging swift3-mode and swift-mode.

License

GPLv3. See COPYING for details. Copyright (C) 2014-2016 taku0, Chris Barrett, Bozhidar Batsov, Arthur Evstifeev.