reddit / emacs

How to develop Xcode project in emacs?

Tak222 · 2022-11-12 16:50
How do you develop Xcode projects using Swift/SwiftUI in Emacs? **EDIT3:** I go for `lsp-mode` and `lsp-sourcekit` as it works now (See **EDIT2**). I want to use Emacs as alternative to Xcode since text editting capability of Emacs is so powerfull and I got used to it. It seems there are several ways to do that but each one has some issues. * lsp-mode is broken for swift over a year. * [lsp-sourcekit ;; completion stops working after lsp server returns an error #3028](https://github.com/emacs-lsp/lsp-mode/issues/3028) * **EDIT2:** **This issue has solved** in [Fix usage of ...trigger-for-incomplete-completions #3815](https://github.com/emacs-lsp/lsp-mode/pull/3815) thanks to `crabbkw` and `yyoncho`! * **EDIT:** One of the main contributor `yyoncho` taking look at to fix and ***needs some help***. [https://github.com/emacs-lsp/lsp-mode/issues/3028#issuecomment-1312535123](https://github.com/emacs-lsp/lsp-mode/issues/3028#issuecomment-1312535123) * [company-sourcekit](https://github.com/nathankot/company-sourcekit) and [SourceKittenDaemon](https://github.com/terhechte/SourceKittenDaemon) doesn't work for `.xcworkspace` * [Support for xcworkspace #73](https://github.com/terhechte/SourceKittenDaemon/issues/73) * [eglot](https://github.com/joaotavora/eglot) doesn't officially support Swift and doesn't have instruction to get it working. * [konrad1977's comment on Swift support? #825](https://github.com/joaotavora/eglot/issues/825#issuecomment-1024267560) seems promissing though.
eleven_cupfuls · 2022-11-12 18:39
You can get pretty far with just swift-mode and dabbrev. That's the simplest option, with minimal effort. But of course you won't get full autocompletion. Eglot works very well with sourcekit-lsp, which is Apple's LSP server. (lsp-mode may also work; I haven't tried it in a long time and don't know one way or the other.) The problem is sourcekit-lsp. The server requires information about the files in the project and how they are built -- an "index". And sourcekit-lsp itself simply does not recognize Xcode projects. There are suggestions scattered around the web as to how you can generate a 'compile\_commands.json' file from an Xcode project. sourcekit-lsp is _supposed to_ be able to consume this, but I've never been able to get it to work. The only reliable way that I have found is having a Package.swift manifest file for the project, and running a `swift build` for the project with that. (If you have a pure SPM project, Eglot+sourcekit-lsp works pretty much flawlessly.) For an Xcode project, this actually means creating a dummy Package.swift, that is _only_ used for indexing. Quite annoyingly manual, and if you have third-party dependencies it's likely there will always be un-resolvable build errors. But even with those errors a partial build index will be created and usable for completion. And once you have it set up it should just continue to work -- maybe re-running `swift build` every so often as the project changes. For some of the details you are welcome to look at and pick from [my Swift/Eglot config](https://gitlab.com/woolsweater/dotemacs.d/-/blob/82505b9b9e2cabf9c960a94c7be13f97dabbdf7c/modules/my-swift-mode.el). I use it on a daily basis and it is generally functional. Although you notice I'm not distributing it as its own package: I don't really warrant it works without tweaking anywhere but on my machines. ;) I also have an [assortment of helpers for Xcode documents](https://gitlab.com/woolsweater/dotemacs.d/-/blob/82505b9b9e2cabf9c960a94c7be13f97dabbdf7c/modules/convexed.el). You should also look at u/xenodium's [config](https://github.com/xenodium/dotsies) and other repos; I know there are some interesting SwiftUI helpers in there.
Tak222 · 2022-11-13 07:25
Thank you for sharing your knowledge and config! I will look into and try your config. It would be great if there is out-of-box solution for this. (I'm willing to contribute but I would be a spare tire on Elisp and LSP...) As for lsp-mode, it seems that one of the main contributor \`yyoncho\` taking look at to fix and needs some help. (I will add this to original post.) Can anyone help him out? https://github.com/emacs-lsp/lsp-mode/issues/3028#issuecomment-1312535123
konrad1977 · 2022-11-13 09:51
I have a few packages to make my life easier, using Emacs. swift-additions.el (has a command swift-additions:compile-and-run-silent) This is exactly what Cmd-R does for you in Xcode. It will auto-detect (or let you choose) your build configuration, target (using completing-read instead of widgets). It will also launch the simulator, install it and parse its outputs (some output as in xcode terminal). Its heavily based on the swift-mode:repl stuff. But I wanted to try to do it myself, to learn elisp. I have taken u/eleven_cupfuls setup for eglot to add support for UIKit completions, without any success though. You need to do the package.swift in your root trick to make it work, but I havent figured it out. If a build error occur, i have another package called "Periphery.el" that parses the compilation and shows the errors in the same manner as flycheck.el [https://imgur.com/a/Bxiq9JP](https://imgur.com/a/Bxiq9JP) Periphery-search.el can grep symbol at point or the current selection. [https://imgur.com/a/YAgezbz](https://imgur.com/a/YAgezbz) ​ **Periphery:** periphery-search (ag/rg interface that presents the result as flycheck) periphery-loco: Run my localization parser periphery-swiftlint: Run swift lint and show the result directly. ​ I also have a localization-mode.el that highlight localization files. Most of my elisp code is quite badly written though. I hack around and take a lot of stuff on the internet. [https://github.com/konrad1977/emacs/tree/main/localpackages](https://github.com/konrad1977/emacs/tree/main/localpackages) Use it on your own risk. company-sourcekitten.el is a proof of concept based on [https://github.com/nathankot/company-sourcekit](https://github.com/nathankot/company-sourcekit) but I invoke the binary directly without using the http proxy. It kind of works :) But it's just an unfinished hack.
Tak222 · 2022-11-13 15:55
Thank you for the details of your config that I actually took a look yesterday. In short, completion for UIKit doesn’t work unless we use package.swift but there is no known way to how to do that properly… I will try to use your code though!
konrad1977 · 2022-11-13 17:21
It has bugs, I am aware of that. If you fix any. Please report them =) Hacking coding!
eleven_cupfuls · 2022-11-13 18:12
Yeah, it's a frustrating situation. I wish I could make this more concrete and maybe put together some kind of package, but as of now the core piece, that Package.swift, is still just manual hacks. Good luck, though!
eleven_cupfuls · 2022-11-13 18:24
Good stuff u/konrad1977! You might consider contributing some of your build and simulator commands to [swift-mode's repl module](https://github.com/swift-emacs/swift-mode/blob/master/swift-mode-repl.el). I have found the maintainer pretty easy to work with.
eleven_cupfuls · 2022-11-13 18:41
Oh, I forgot to mention, I have also found [dumb-jump](https://github.com/jacktasia/dumb-jump) to work pretty well for Xcode projects, with no configuration.
konrad1977 · 2022-11-13 20:05
I am not sure it belong there. But they are free to take whatever they want. I started swift-additions.el because swift-mode didn't work the way I wanted so it might not be the perfect match.
Tak222 · 2022-11-22 13:40
I look forward to giving it a try if you would make a package! And good to know dump-jump works well for Xcode projects. Thanks, u/eleven_cupfuls!
Tak222 · 2022-11-22 13:47
Either contributing to swift-mode or making swift-additions.el as GitHub repo It would be great as anyone can contribute and use same code-base so we don't have to reinvent the wheel. Maintaining open source project takes hassle so I respect your decision to make and appreciate making public your config!