Swift plugin for CLion

Hi,

Recently we’ve opened the Early Access Program for CLion 1.5. In this post we highlight another new feature: Swift support in CLion!

Background

This summer Apple promised to open source Swift language before the end of 2015, and just recently they did it! Now developers can contribute to Swift compiler and standard library, SourceKit, core libraries like Foundation, libdispatch and XCTest, Swift package manager, LLDB for Swift, and more.

This work involved not only OS X, with many Linux developers joining in as well. This has created a growing need for a Swift IDE on several platforms, especially on Linux. We at JetBrains do support iOS/OS X development, including Objective-C and Swift languages, with a standalone IDE called AppCode. It’s compatible with Xcode and its main focus is iOS/OS X development (meaning toolchains, environment, etc.). Unfortunately, if you’d like to try Swift on Linux, AppCode is not a proper choice.

CLion, on the other hand, is cross-platform. And with Swift compiler and libraries open-sourced, we’ve managed to take the platform-independent part of Swift support and brought it to CLion via a separate plugin. That means you can use CLion as a Swift IDE on Linux!

Initial setup

Getting started with Swift in CLion takes a few simple steps:

  1. Download and install the plugin from disk.
  2. Download and setup the Swift toolchain.
  3. In CLion settings Build, Execution, Deployment | Swift specify a Swift SDK (on OS X the default is/Library/Developer/Toolchains/swift-latest.xctoolchain, and on Linux it’s empty):
    settings_norm
  4. If you are planning to use Swift debugger, enable Use Swift debugger.
    (Note: Since the debugger comes from the Swift SDK and is customized, avoid using it for debugging non-Swift programs.)
  5. CLion relies on CMake project model, so you need to start a CMake project for Swift. To simplify the configuration process, we’ve added a special CMake live template that adds a Swift build target:
    live_template
    SWIFT_SDK variable expands to the value specified in the settings.
  6. A sample CMake configuration file may look like this:
    sample_cmake
  7. After you build the project, .build directory will be created in your project folder. Now all you need to run the project is to add an executable that is located in .build directory to the configuration corresponding to your target name (in our case it’s HelloSwift).

And now you are ready for Swift!

Features

The set of features includes the most essential ones for you to try:

  • Parse/resolve classes in project and standard library (note that non-standard libraries are not resolved yet).
  • Code highlighting and completion:
    completion
  • Swift code formatter with a separate set of Swift code style settings:
    code_style
  • Navigation actions and File structure view.
  • Find usages.
  • Rename refactoring:
    refactoring
  • Override/Implement for functions and protocols.
  • And even a debugger, including proper rendering of variables and types:
    debug

    and expression evaluation:
    evaluate_code_fragment1

    Please note that on OS X the Swift debugger itself seems to have problems and fails to render classes correctly.

To see the above features in action, watch this short demo:
width="640" height="360" src="https://www.youtube.com/embed/PQ1dL47dPr0?feature=oembed" frameborder="0" allowfullscreen="" style="border-width: 0px; margin: 0px; padding: 0px; vertical-align: baseline; background: transparent;">

Share your feedback in the comments section below. We’ll be glad to hear what you think about the Swift plugin for CLion! And we’ll rely on your feedback in our decision where to move forward with the plugin.

Sincerely yours,
The CLion Team

This entry was posted in  Tips'n'Tricks and tagged  1.5EAPpluginSwift. Bookmark the  permalink.

50 Responses to Swift plugin for CLion

  1. Alex Polozov  says:

    Why just CLion and not a general IDEA Platform plugin?

    • Anastasia Kazakova  says:

      Actually it was much easier, since CLion and AppCode have a lot in common, and CMake support worked as a charmed (and it’s in CLion now).

  2. Edu Garcia  says:

    This is fantastic news!

    But I’ll ask a question that I’m sure I’m not alone wondering: Will this come to IDEA as well? I understand C/C++ not coming, because it’s the “core” of CLion (although you can say the same about HTML/JS and WebStorm, but I digress), but as Swift is not confined to a single IDE now, is there any reason why it won’t make it’s way to IDEA?

    Thank you in advance for your response

    • Anastasia Kazakova  says:

      Thanks!

      Swift plugin for CLion was now more essential since AppCode and CLion have a lot of things in common, and CMake in CLion worked well for Swift projects on Linux. However, none of these (AppCode, CLion) are currently included into IntelliJ IDEA. We could consider some of these later, but currently more tasks in the queue with much higher priority.

  3. Hisham  says:

    So is the plan to only have Swift support for Linux in CLion? What about AppCode?

    • Anastasia Kazakova  says:

      AppCode is an IDE for iOS/OS X dev, so it has Swift, but for development for Apple devices only. That’s by design. And we were not planning to change the thing. At least no such plans for now.

  4. Steve Miller  says:

    Great job!! I had been hoping for this, but assumed I’d have to wait for years… This is actually quite exciting.

    A quick question on the comment, “Swift SDK comes with its own LLDB library; avoid using it when debugging non-Swift apps”. Does that mean if I check that box that Swift’s LLDB will be used when debugging a C++ app?

    • Anastasia Kazakova  says:

      Yes, so for debugging C++ apps, better use either usual bundled LLDB (if you are on OS X) or bundled/selected GDB.

  5. Dev  says:

    This is awesome. I was using gedit for writing Swift on Linux, so this should be a big upgrade :-)

  6. Даниил Водопьян  says:

    Great news!
    A tech question, if i may:
    Does the plugin use clang internally for code parsing/resolving? If it doesn’t, what is it then and now is it better?

    • Anastasia Kazakova  says:

      Plugin uses the same parser AppCode does (that means our own), our IDE for iOS/OS X development – https://www.jetbrains.com/objc/. We have it for more than a year already and it works quite well. We are adding new Swift features there all the time.

      • Даниил Водопьян  says:

        But why can’t you use clang API? Can I read about the reasoning somewhere?

        Sorry for asking, I believe you have a good reason for anything you do. The thing is that I got an argument with a friend, would be great if you somehow help me out.

  7. User  says:

    The debugger isn’t working for me:

    Error running App: /home/ubuntu/Applications/clion-144.2151.8/CIDR/cidr-debugger/bin/LLDB/linux/LLDBFrontend not found

    I’m not sure if this is what you mean by ‘non-standard libraries’, but Clion couldn’t resolve a module I’m using. It’d be nice to have built-in support for module maps.

    The “swift build” command wouldn’t work for me, so I had to use “swift Sources/*.swift -I/all/the/dependencies”. Possibly because of that(?) Clion couldn’t find the executable, so I had to add the “-o” flag to specify the output and then manually configure Clion to use it.

    It’d be nice if at setup time the user chooses to include Swift support, for Clion to go download Swift and configure itself to use it.

    This is a great first step and I can’t wait to see where it goes.

    • Anastasia Kazakova  says:

      Have you set SDK path correctly? Have you switched on the usage of Swift debugger in settings? Does this /home/ubuntu/Applications/clion-144.2151.8/CIDR/cidr-debugger/bin/LLDB/linux/LLDBFrontend really exist?
      Do you know the reasons why “swift build” command is not working in our case? This could be the source of the problem…

      Yes, only standard library is resolved for now.

      Since you can build your own version of Swift compiler, not sure automatic download makes sense. But feel free to add all your requests/problems into our tracker: https://youtrack.jetbrains.com/issues/CPP

      • User  says:

        No, the directory doesn’t exist, starting with the “CIDR” directory. It wasn’t in the Clion file I downloaded, or the Swift plugin, so not sure where I’m supposed to get that, or if it’s just a bug that means it’s accidentally missing in the build.

        My CMake file is something like this:

        add_custom_target(HelloSwift
        COMMAND /Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swift Sources/*.swift -I/all/the/dependencies
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

        Yes, possibly I just need to figure out how to get “swift build” to work with my project. In any case, manually configuring the target executable worked fine.

        OK, thanks. I can’t imagine a large percentage of users would be building their own compiler, but fair point.

        • User  says:

          And yes, I had selected to use the Swift debugger in the settings, and I believe I had set the Swift SDK correctly as I could compile and run the app.

        • Anastasia Kazakova  says:

          Is it just a sample or do you really have “/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swift” in your CMake? Looks like you should have there: /home/ubuntu/swift/usr/bin/swift or similar. Is this the case?

          • User  says:

            Yes, just an example (I don’t have the project in front of me). That path is configured correctly for sure as the code compiles and runs.

          • Anastasia Kazakova  says:

            Ok, thanks. We’ll try to find out if non-working build command could be the source of the problem or not, and will let you know.

          • Anastasia Kazakova  says:

            One more question: could you please show the output for ls command for /home/ubuntu/Applications/clion-144.2151.8/ path?

          • Anastasia Kazakova  says:

            Looks like we’ve found the source of the problem. Ubuntu’s installer is missing the LLDBFrontend file accidentally. Sorry for that. We’ll put it shortly to the confluence page and will update the post here with the instruction on how to set it up. Next EAP builds will include the files by themselves. Sorry once again. And thanks for helping us investigate this!

          • User  says:

            Thanks

          • Anastasia Kazakova  says:

            Done. Please, check the note in number 4 in the instruction in this blog post.

    • Anastasia Kazakova  says:

      By the way, have you added the custom target in your CMakeLists.txt. Something like:
      add_custom_target(HelloSwift
      COMMAND /Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swift build
      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
      SOURCES Package.swift main.swift MyClass.swift)

  8. Chris Hatton  says:

    Great news! JetBrain’s support for Linux in Swift will be a huge boost to the language and ecosystem. This really strengthens the cross-platform roadmap for our product.

    I’m having some trouble with the instructions above, however, which seem to be incomplete; I’ve added the live template, set-up the SDK and my test code builds – but does not run. If I point ‘Executable’ parameter of the Run Configuration to a previously built executable inside ‘.debug’, an LLDB error is thrown.

    How should the run configuration be set up?

  9. Tim  says:

    Could you pls. explain how to set up Executable for swift project?
    https://i.imgur.com/CRqznMd.jpg

    • Anastasia Kazakova  says:

      If you’d added a custom target, smth like:
      add_custom_target(HelloSwift
      COMMAND /usr/bin/swift build
      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
      SOURCES Package.swift main.swift MyClass.swift)
      Then go to HelloSwift configuration for this target and after building select an executable from the .build directory.

    • Anastasia Kazakova  says:

      let me know if it helps

      • Tim  says:

        I added custom target then build it but still can’t run project see screenshot:

        http://i.imgur.com/bHKNASN.jpg

        What I should select as Executable? It seems Clion can’t build swift project properly or I’m doing something wrong.

        • Anastasia Kazakova  says:

          First, you are missing the custom target name in your CMake command. In my sample it’s HelloSwift.
          Then, let’s check what directories do you have in your project after you successfully build the project. You should have .build in there.
          Then, you need to Edit configuration HelloSwift (Build All is cmake special configuration for building all the project), and set an executable there. Press “Select other” in the drop down menu and if needed select “Show hidden files and directories” while searching for the .build dir in the list.

          • Tim  says:

            Sorry Anastasia you’re right it was my fault I forgot to add custom target name.

          • Anastasia Kazakova  says:

            Do ou have .build directory created after project building now?

          • Tim  says:

            “Do ou have .build directory created after project building now?”

            No I don’t have .build directory.https://i.imgur.com/YLJQgBr.jpg
            I don’t know what I’m doing wrong. But at least in terminal using swift build command it works.

          • Anastasia Kazakova  says:

            What I see on the screenshot is a Build all configuration, since it’s All Targets, not the swift_hello target.
            Then, have you built this configuration from CLion? Run | Build (or Ctrl+F9)

          • Tim  says:

            “Then, have you built this configuration from CLion? Run | Build (or Ctrl+F9)”

            Yes I used Clean then Build (Alt+Shift+D/Ctrl+F9) then tried Shift+F10 (Run Build All) and got Error: Executable is not specified.

          • Anastasia Kazakova  says:

            Ok, could you please build the project, then compress the project directory (the whole one, including .idea and others) and send it to clion-support at jetbrains.com? We’ll what’s going on there.

          • Tim  says:

            I sent project with logs.

          • Anastasia Kazakova  says:

            Got it, thanks. I’ll have a look shortly and reply via email.

  10. Tim  says:

    By the way after building there’s no .build directory.

  11. Swift  says:

    Hello

    Can you please paste the code of the CMakeList instead of a screenshot

    thanks

    • Anastasia Kazakova  says:

      You can easily generate this CMake with add_swift live template. It will take proper swift SDK path from the settings automatically, you’ll just need to write target name.

  12. wu  says:

    error: no Package.swift file found

    • Anastasia Kazakova  says:

      Do you actually have this file in our project? Could you please share the project you are testing on?

  13. wu  says:

    15:37:20 Build finished in 183ms
    15:37:20 Error running s04: File not found: /home/wz/.CLion15/system/cmake/generated/s04-de9964d6/de9964d6/Debug/s04

    • wu  says:

      Hello

      That’s the project logs on Ubuntu.

      CMake file like this :
      add_custom_target(s04
      COMMAND /home/wz/swift/swift-2.2-SNAPSHOT-2016-01-11-a-ubuntu15.10/usr/bin/swift build
      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
      SOURCES Package.swift main.swift)

    • wu  says:

      /home/wz/.local/share/Trash/files/clion-144.2608.6/bin/cmake/bin/cmake –build /home/wz/.CLion15/system/cmake/generated/s04-de9964d6/de9964d6/Debug –target s04 — -j 2
      error: Your source structure is not supported due to invalid sources layout: /home/wz/ClionProjects/s04. There should be no source files under: /home/wz/ClionProjects/s04.
      That’s the bulid message:

      CMakeFiles/s04.dir/build.make:57: recipe for target ‘CMakeFiles/s04’ failed
      make[3]: *** [CMakeFiles/s04] Error 1
      CMakeFiles/Makefile2:67: recipe for target ‘CMakeFiles/s04.dir/all’ failed
      make[2]: *** [CMakeFiles/s04.dir/all] Error 2
      CMakeFiles/Makefile2:74: recipe for target ‘CMakeFiles/s04.dir/rule’ failed
      make[1]: *** [CMakeFiles/s04.dir/rule] Error 2
      Makefile:118: recipe for target ‘s04’ failed
      make: *** [s04] Error 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值