Hi all,
I'm relatively new to swift but wanted to document how I set up VSCode as a fairly good IDE. I don't have anything particularly AGAINST XCode (except that, for me, it's the IDE equivalent of iTunes), but I use VSCode as my primary editor for my other languages and wanted to see whether I could get it working with Swift. This is on my Mac, btw. YMMV for other OSes.
TLDR: it works, mostly well.
Extensions I installed:
- CodeLLDB for debugging
- Run On Save for formatting
- SwiftLint for linting
- Maintained Swift Development Environment (MSDE) for syntax highlighting and hovering
- Dash for offline documentation
Utilities I installed:
- swiftformat
- swiftlint
- sourcekite (instructions at the MSDE plugin repo)
First off: I disabled format on save in VSCode, and autosave as well. I then added the following:
"emeraldwalk.runonsave": {
"commands": [
{
"match": "\\.swift$",
"isAsync": false,
"cmd": "cd ${fileDirname} && swiftformat ${fileBasename}"
}
]
}
This will autoformat all files ending in .swift
whenever they're saved.
Next, I created a tasks.json
as follows:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "swiftc with debug",
"type": "shell",
"command": "swiftc -g ${file}",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"panel": "dedicated"
}
}
]
}
This can be used by itself, but it's better with a launch.json
:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=8303
87
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"preLaunchTask": "swiftc with debug",
"name": "Debug",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"sourceLanguages": [
"swift"
]
}
]
}
With these two files, LLDB debugging works - you can set breakpoints, inspect variables, etc... all from within VSCode.
It'd be nice if we had a "one-stop shop" for turning VSCode into a Swift IDE, but until then, I hope this helps.
If there are VSCode experts out there who want to give suggestions as to how to make my environment even better, I'm all ears.