Swift code editor keyword autocomplete ideas

Hi folks!

I'm a brand new member here at these forums.
And am relatively new to the Swift programming language also.
Although I do have much coding experience with other languages.

After learning the basics of the language, and coding some practice projects.
I have started my first serious large Swift macOS application project.
Which is a small lightweight Swift code/script editor, much like the MacOS Script Editor application.
But instead of AppleScript, you can write and run Swift code, and get the returned results in an accessory view console.

I've implemented a keyword autocomplete mechanism into the editor, which offers code word suggestions in a pop up view, although at the moment it's only suggesting Swift language keywords, and Foundation and AppKit class type names.
But I would like to explore the options for a more comprehensive word autocomplete solution.
One that would offer property and function suggestions for variables or class types that are written in the code.

I'm aware of the SourceKitten project, but my understanding is that it relies on the SourceKit Framework which is part of the Xcode installation.
So I'm looking for solutions that don't rely on Xcode being installed, and instead can use the installed 'swift' or 'swiftc' command line installations, to offer autocomplete keyword suggestions.

So is there anyway to pass swift or the swift compiler, the code or a code file directly, so that it can analyse the code, and return the class type of any declared constants or variables ?

I'm open to any ideas or suggestions on how to go about tackling this problem.

Regards Mark

Hi Mark,

The language server protocol is the standard way to do this and the Apple implementation Sourcekit-LSP is part of the Swift installation and doesn't need Xcode. I have currently been playing with Visual Studio Code integration with SourceKit-LSP. You can find the out more about LSP and the Apple implementation from the GitHub repro GitHub - apple/sourcekit-lsp: Language Server Protocol implementation for Swift and C-based languages. The only thing I would say is I believe Sourcekit-LSP requires a Package.swift in the folder to work. I don't believe it'll work for stand alone swift scripts.

1 Like

Hi Adam

Thanks for that link to the SourceKit-LSP.
I shall look and try to understand how I could use it.
The whole developer toolchain of swift is a mystery to me at this point, as I'm very new to the language.
And my only real experience has been to install Xcode, and to code and learn swift from that environment.

The underlying toolchain is not something I know anything about at this stage.
My main goal was to find a way of offering an autocomplete solution, that did not require any dependencies, other than the standard non Xcode swift installation, or something that can be included in the application bundle package.

basically the goal is someone being able to install my little code/script editor, and not having to install anything else other than swift, or even maybe being able to include swift in that application bundle.

The swift REPL offers autocomplete suggestions, even with swift 4.0, so I was trying to find a simple way to tap into the same mechanism that he swift REPL uses.