Hacking Atom to create a Swift IDE that runs on Linux and Mac

Ankit Agarwal
5 min readDec 12, 2015

--

As of now theres no IDE available for swift that runs on linux which makes it kind of annoying to develop packages due to constant switching between text editor and terminal. I had never used Atom before but I wanted to see if it’ll live upto its name and let me hack it to integrate the Swift Package Manager and LLDB into it.

Well, end result:

Atom with SPM and LLDB

Atom Swift Debugger

Atom is a text editor by Github which is marketed as “A hackable text editor for the 21st Century”. It is written in CoffeeScript which is pretty looking Javascript. Writing packages for Atom is easy and encouraged to customise the text editor to suit your needs. Your packages can have node dependencies and the entire project is OpenSourced.

Swift Debugger is a small atom package I wrote which will run the swift build command then start lldb with the executable as argument bringing compiling and debugging of swift packages right inside Atom’s UI. You can toggle breakpoints in your files visually with a shortcut and the lldb will apply those breakpoints and break at those lines.

Since Atom works on Mac and Linux, the swift-debugger package also works on both.

Tutorial

Lets create a small swift project and try the debugger on a ubuntu machine. First install Atom if you don’t already have it. Then type this command on terminal :

$ apm install swift-debugger language-swift
Installing swift-debugger to /home/aciid/.atom/packages ✓
Installing language-swift to /home/aciid/.atom/packages ✓

apm is Atom Package Manager which will fetch the latest version of the swift-debugger and install it. (language-swift is a package to provide syntax highlighting)

Now enter this to create a simple swift package

$ mkdir MySwiftProject && touch MySwiftProject/main.swift && touch MySwiftProject/Package.swift

Now open this folder in atom using command line or open atom and browse to the folder you created.

$ atom MySwiftProject

Now lets give name to this package. Enter this in Package.swift and save it.

import PackageDescriptionlet package = Package(
name: "MySwiftProject"
)

Open main.swift and enter some sample code and save the file:

let myAwesomeString = "hey I am an awesome string"
print(myAwesomeString)
let awesomeInt = 500
print(awesomeInt)

Press alt-r to activate the swift-debugger package. You should see a screen like this :

You will see the debugger attached to bottom of the text editor. You can toggle it using alt-r

For now you need to manually enter the executable name and swift compiler path into the debugger’s input bar to get started. Executable name is the package name you gave in Package.swift (MySwiftProject) and swift path is the place where swift tool chain is. Mine is at /home/aciid/swift/swift-2.2-SNAPSHOT-2015–12–01-b-ubuntu15.10/usr/bin

enter these two things in the input box of the debugger (Place where it says po foo) no spaces before or after =

e=MySwiftProject (press enter)
p=/home/aciid/swift/swift-2.2-SNAPSHOT-2015–12–01-b-ubuntu15.10/usr/bin (press enter)

The debugger will output “swift path set” and “executable path set” respectively.

All set. Time to build and run the project. Just press run button in debugger. The debugger will compile and run the executable and show you the output.

Neat right?

Time to set a breakpoint, go to line number 3 and press alt-shift-r and it should show you a blue bar in gutter which means breakpoint is on for that line. You can toggle it on or off.

Now press run again and the execution will stop at line number 3. Enter “p myAwesomeString” in the input box to print the object.

You can go to next line using “next line” button, and “resume” to go to the next breakpoint. If there is no more breakpoints, it’ll continue the execution. You can press “stop” to kill the execution and “clear” to clear the output.

The inputbox sends exact commands to lldb debugger so every feature of lldb debugger is available right there itself!

One more Example

Now lets try building and debugging project I created for the last post.

Go to terminal and type :

$ git clone https://github.com/aciidb0mb3r/SwiftGetClientDemo
$ atom SwiftGetClientDemo

Seeing Package.swift executable name is GetClientUser so enter e=GetClientUser and p=path/to/swift/toolchain

Press run and you’ll see SPM at work:

Now on left in file explorer you’ll see the folder Packages created, go to Packages/SimpleGetClient-1.0.0/GetClient.swift and toggle a breakpoint at line number 17 using alt-shift-r and press run, when execution stops, enter “p explodedStrings” to inspect that variable.

You can play around with the debugger using “next line” and more breakpoints.

Report any bugs you find at : https://github.com/aciidb0mb3r/atom-swift-debugger/issues by creating an issue.

--

--