- Swift Development with Visual Studio Code
- Step 0: Install Xcode
- Step 1: Install Visual Studio Code
- Step 2: Install Node and NPM
- Step 3: Build and Install SourceKit-LSP Extension for Visual Studio Code
- simctl
- Swift coding in visual studio code
- 3 Answers 3
- Swift for Visual Studio Code
- Kiad Studios LLC
- Swift for Visual Studio Code
- Installation
- macOS
- Linux (in progress)
- Vs code swift windows
- Vs code swift windows
- About
Swift Development with Visual Studio Code
Visual Studio Code (VSCode) is a cross-platform text and source code editor from Microsoft. It’s one of the most exciting open source projects today, with regular updates from hundreds of contributors. VSCode was among the first tools to support Language Server Protocol (LSP), which has played a large part in providing a great developer experience, in a variety of languages and technologies.
With the previously announced now shipping in Xcode, it’s a great time to see how this integration works for yourself.
This week, we’ll walk through the process of how to get started with Swift’s new Language Server Protocol support in Visual Studio Code on macOS. If you haven’t tried writing Swift outside Xcode, or are already a VSCode user and new to the language entirely, this article will tell you everything you need to know.
Step 0: Install Xcode
If you don’t already have Xcode installed on your machine, open the Terminal app and run the following command:
Running this command presents a system prompt.
Click the “Get Xcode” button and continue installation on the App Store.
You can verify that everything is working as expected by running the sourcekit-lsp command:
This command launches a new language server process, but don’t worry if it doesn’t provide any feedback to STDOUT — that means it’s working as intended. Exit the process with an ETX signal ( ^ C ).
Step 1: Install Visual Studio Code
Download Visual Studio Code and install it to your system Applications folder. Open the app and follow the instructions for launching from the command line. You’ll need to have the code command accessible from $PATH in order to install the SourceKit-LSP extension later on.
Electron apps have a reputation for being big and slow, but don’t let that stop you from giving VSCode a try — its performance and memory footprint are comparable to a native app.
Step 2: Install Node and NPM
VSCode extensions are written in JavaScript / TypeScript. If you’re not already set up for JS development, you can download Node (a JavaScript run-time for outside the browser)
and npm (a package manager for Node) with Homebrew using the following commands or manually by following these instructions:
To verify that you have a working installation, run the following command:
Step 3: Build and Install SourceKit-LSP Extension for Visual Studio Code
From the command line, clone the sourcekit-lsp repository and navigate to Editors/vscode in the resulting directory. Use npm to build the extension and then use the code command to install it:
Now launch (or relaunch) VSCode and open a Swift project, such as this one, and test out Language Server Protocol support for Swift.
To get the full experience of working with Swift from VSCode, we recommend that you also check out the Swift Lint extension (for real-time style and convention diagnostics).
So there you have it — the makings of a first-class Swift development experience outside of Xcode.
And with GitHub’s recent announcement of Codespaces, that future may be coming sooner than we once thought. Thanks to Swift’s support for Language Server Protocol, we’ll soon be able to edit Swift code — syntax highlighting, autocompletion, and all — directly from the browser.
Questions? Corrections? Issues and pull requests are always welcome.
This article uses Swift version 5.2. Find status information for all articles on the status page.
Mattt (@mattt) is a writer and developer in Portland, Oregon. He is the founder of NSHipster and Flight School, and the creator of several open source libraries, including AFNetworking and Alamofire.
simctl
With a mouse, anyone can use a computer, even if they aren’t a “computer person”. …though, if you are a “computer person”, you use keyboard shortcuts for most operations, and automate the rest from the Terminal.
Swift coding in visual studio code
Is there any possibility to write swift code and preview in Visual Studio Code (windows coding app)? I see in the guide for swift coding that requires ios system but i have just windows for the moment.
Can I code swift in Windows?
3 Answers 3
Yes, you can code Swift on Windows. Check out RemObjects Silver which gives you a Swift compiler in Visual Studio 2015. Visual Studio Express is free, but I don’t know if it works with Silver.
Swift itself is just a language. In order to build an iOS app, you need frameworks and libraries like Foundation and UITouch (which can be written in any language, in this case: C, Objective-C and C++). Apple has open sourced many Foundation classes and ported them to Linux but there are lot of stuffs that are not fully implemented or there are bugs in Linux that don’t exist in OS X. There’s no official Windows port. I doubt Apple will ever open source UITouch at all.
Silver can target .NET (and I guess by extension its cross-platform cousin Mono), Java or Sugar (vendor’s proprietary framework). If you want to use Swift on Windows, yes, you can, but don’t expect to build an iOS app on Windows. If you are serious about iOS development, buy a Mac, even the $500 Mac Mini can go a long way. If you only want to play with Xcode, assemble a Hackintosh or a virtual machine so that you can run OS X.
Update June 2020: at WWDC 2020, Apple announced Swift 5.3 will expand to include support of Windows and more Linux platforms:
Swift 5.3 is a release meant to include significant quality and performance enhancements. In addition, this release will expand the number of platforms where Swift is available and supported, notably adding support for Windows and additional Linux distributions.
There is official support for Swift on Windows 10. It works with Visual Studio 2019 (not VS Code). Install Swift on Windows
Swift for Visual Studio Code
Kiad Studios LLC
Swift for Visual Studio Code
An extension for VS Code which provides support for the Swift language, providing the following:
- Completion Lists
- Signature Help
- Snippets
- Quick Info
- Goto Definition
Please note that this extension will ONLY work with Swift Package Manager projects for the time being. If a Package.swift file is not in the root of your project, this extension will fail.
Also note, this is an early beta release. There will be bugs. Please file any issues you run into over here: https://github.com/owensd/vscode-swift.
Installation
Obviously, you’ll need to have Visual Studio Code installed. Then search for the Swift for Visual Studio Code extension from the command palette ( cmd+shift+p | ctrl+shift+p ).
You’ll need to ensure that the swift tool is on your command path is one of the supported versions. As of now, only Swift 3.1 is supported. If you are using development snapshots, you can configure your project to use the correct version of Swift with the `swift-langsrv.
Note: It is entirely possible to use development snapshots. However, they are untested and unsupported. The problem is that the SourceKit binary may simply not load for mis-matched versions of Swift.
swift.toolchainPath — the path to the root of the Swift toolchain, for example: /Library/Developer/Toolchains/swift-3.1.1-RELEASE.xctoolchain
macOS
If you have the correct version of Swift installed, there is no other action necessary as all of the dependencies are installed for you.
Linux (in progress)
The fundamental problem is Swift 3.1 does not build with SourceKit. this is being addressed in Swift 4. There are workarounds that I will published within the next few releases.
Vs code swift windows
Swift for Visual Studio Code
An extension for VS Code which provides support for the Swift language, providing the following:
- Completion Lists
- Signature Help
- Snippets
- Quick Info
- Goto Definition
Please note that this extension will ONLY work with Swift Package Manager projects for the time being. If a Package.swift file is not in the root of your project, this extension will fail.
Also note, this is an early beta release. There will be bugs. Please file any issues you run into over here: https://github.com/owensd/vscode-swift.
Obviously, you’ll need to have Visual Studio Code installed. Then search for the Swift for Visual Studio Code extension from the command palette ( cmd+shift+p | ctrl+shift+p ).
You’ll need to ensure that the swift tool is on your command path is one of the supported versions. As of now, only Swift 3.1 is supported. If you are using development snapshots, you can configure your project to use the correct version of Swift with the `swift-langsrv.
Note: It is entirely possible to use development snapshots. However, they are untested and unsupported. The problem is that the SourceKit binary may simply not load for mis-matched versions of Swift.
swift.toolchainPath — the path to the root of the Swift toolchain, for example: /Library/Developer/Toolchains/swift-3.1.1-RELEASE.xctoolchain
If you have the correct version of Swift installed, there is no other action necessary as all of the dependencies are installed for you.
Linux (in progress)
The fundamental problem is Swift 3.1 does not build with SourceKit. this is being addressed in Swift 4. There are workarounds that I will published within the next few releases.
Windows Linux Subsystem (experimental)
Vs code swift windows
Swift for Visual Studio Code
This extension adds rich language support for the Swift language to VS Code. These features are provided by the Swift framework itself through SourceKit and a Swift Language Server implementation.
Currently the extension provides rudimentary support for:
- Completion lists
- Symbol resolution
Future support for:
- Document Highlights: highlights all ‘equal’ symbols in a Swift document.
- Hover: provides hover information for a symbol selected in a Swift document.
- Signature Help: provides signature help for a symbol selected in a Swift document.
- Find References: find all project-wide references for a symbol selected in a Swift document.
- List Workspace Symbols: list all project-wide symbols.
- CodeLens: compute CodeLens statistics for a given Swift document.
- Rename: project-wide rename of a symbol.
- Debugger
- Swift Package Manger
Use completion lists to find out about available standard library types and function signatures.
Do the same for your own custom types. Including documentation comments.
This extension requires Swift to be installed on your system. It also requires a Swift language server be installed as well. By default the extension looks for the language server to be installed at /usr/local/bin/LanguageServer (though this behavior is configurable).
- Swift Open Source swift-DEVELOPMENT-SNAPSHOT-2016-12-01-a toolchain (Minimum REQUIRED for latest release)
- macOS 10.11.6 (El Capitan) or higher
- Xcode Version 8.2 beta (8C30a) or higher using one of the above toolchains (Recommended)
A few remarks about Linux support
The language server that drives this extension depends on SourceKit to be available with the Swift toolchain. Unfortunately, at this time that means that Linux support is not really possible because SourceKit is not built by default on Linux.
All of the dependencies of the language server, at least ostensibly, support Linux. So there should be little preventing Linux support beyond SourceKit being available on the platform.
I want to stress: I hope this will not be the long-term answer/solution. In fact running on Linux was the whole reason why I started this extension.
Debug and Development
The extension itself relies on a language server to interact with it. This server has been developed to work with Visual Studio Code.
An example workflow for interactively debugging the language server while using it with the Visual Stuio Code client is provided in this section. The instructions are devided into two sections. The first section explains how to generate and configure an Xcode project for debugging. The second section explains how to configure the Visual Studio Code plugin to use the debug executable.
In the directory containing the clone of the language server use SwiftPM to generate an Xcode project.
Since the language server client, e.g., VSCode, will actually launch the language server LLDB needs to be told to wait for the application to launch. This can be configured in Xcode after opening the generated project in Xcode. See the screenshot below.
The next step is to build the executable and launch LLDB. Both of these steps can be performed by going to «Product > Run» or the keyboard shortcut ⌘R. After building completes, Xcode should report something like «Waiting to attach to LanguageServer : LanguageServer».
One final step is to determine the TARGET_BUILD_DIR . This is used to tell the VSCode extension in the next section where the debug language server is located.
From a terminal whose current working directory contains the Xcode project previously generated by SwiftPM you can get this information from xcodebuild .
Take note of this value it will be used later.
Open the directory containing the clone of the Visual Studio Code extension in Visual Studio Code.
Start the TypeScript compiler or the build task (e.g., ⇧⌘B or Tasks: Run Build Task).
Now open src/extension.ts and provide the value of TARGET_BUILD_DIR for the debug executable. The change should be similar to the patch that follows.
NOTE: Make sure the $
Once this is complete you should be able to open the VSCode debugger and and select Launch Extension . This should start both the language server (Xcode/Swift) and the extension (VScode/TypeScript) in debug mode.
- As noted above you might not be able to capture all the commands upon the language server initially starting up. The current hypothesis is that it takes a little bit of time for LLDB (the Swift debugger) to actually attach to the running process so a few instructions are missed.
One recommendation is to put a break-point in handle.swift as this is likely where the server is getting into to trouble.
- Messages are logged to the Console.app using the me.lovelett.langserver-swift sub-system. One place to look the raw language server JSON-RPC messages is there.
For detailed release notes see: Releases
- Completions use snippet formatting for placeholders, allows the cursor to tab between placeholders
- Completions index against all swift files in the workspace
- Bug fixes
- Allow configuring sourceKitten install location
Preview release of the extension
About
An extension for VS Code which provides support for the Swift language.