reddit / emacs

My first major mode: `kotlin-ts-mode`

alebrick · 2022-12-28 18:00
alebrick · 2022-12-28 18:02
I've been getting into Kotlin recently, and I find the \`tree-sitter\` integration in Emacs 29 pretty cool, so I decided to try to improve the Kotlin editing experience :). I'd love feedback about general things a major mode should do, as well as Kotlin-specific things that I could consider adding.
ieoa · 2022-12-28 19:49
Whilst I don't use Kotlin, it has inspired me about how accessible it is to create a new `foo-ts-mode`.
alebrick · 2022-12-28 20:30
I've been playing around with `tree-sitter` for a while, so I didn't have that learning curve, but it ended up being quite easy. I found this doubly true for the indentation logic: I had an MR up for the non-`tree-sitter` `kotlin-mode` to improve the indentation, and the logic gets super complex super quickly.
canatella · 2022-12-28 22:31
Care to detail your Emacs Kotlin setup? I resorted to use IntelliJ because of the lack of good LSP support and stuff like auto includes. Also the code formatter is slow, it's really a painful experience to me. I've been under Emacs for 25 years know and having to use something else to code is so difficult...
leungbk · 2022-12-29 02:09
Nice! Have you considered sending this as a patch to upstream Emacs?
wunderspud7575 · 2022-12-29 08:59
Yes, OP, please consider this - I believe the Emacs 29 branch is still open to submissions of treesitter mode additions.
Qkesslerr · 2022-12-29 09:05
Hi there! Thank you so much for working on this, really good job. I love people starting on the emacs package ecosystem with something that could eventually be commited to core! Couple of points, when trying the mode out on a project at work. I'll compare some of the highlights with the package kotlin-mode, which was the one I was previously using to edit .kt files. ([https://github.com/Emacs-Kotlin-Mode-Maintainers/kotlin-mode](https://github.com/Emacs-Kotlin-Mode-Maintainers/kotlin-mode)) 1. String interpolation is lacking on kotlin-ts-mode. Example: `Regex("${CompositionKeys.ASIN}(.\*)")`. On kotlin-mode, there is highlighting for `CompositionKeys.ASIN`. Also happens for variables directly added to strings: `log.warn("Filtering $request")`, request not highlighted here. 2. We are lacking highlighting support for functions, and properties when calling either an instance or an static object. Also happens when accessing properties for the active class, using `this`. `this` is highlighted, but none of the methods / properties after it are: `this.property.functionOnProperty()`. Examples: - `bookCacheDao.get(bookKey)` -> none of this is highlighted, all white.- `StaticDestinationHelper.isValid(destination)` -> same as above. 3. Constants declared on companion are not highlighted, at all. Neither are the methods called on constants. Example:- `private const val SOFTWARE_VERSION_UNAVAILABLE = ""`. Highlighted correctly on the companion when declared, but not at all when used.- `SOFTWARE_VERSION_UNAVAILABLE.isEmpty()` would not be highlighted either. 4. Imports are not highlighted. Yes the `import` keyword, but nothing on the right side. Don't know if we want to keep it that way, but there are common cases where we could highlight, i.e: importing a constant vs importing classes vs importing from a companion elsewhere. Thank you again for working on this!
[deleted] · 2022-12-29 13:49
[deleted]
alebrick · 2022-12-29 14:08
I've just pushed an update for highlighting property access and function calls. Could you have a look if that makes your #2 look better?
alebrick · 2022-12-29 14:09
I didn't consider it, but I'm not opposed. I guess my main concern at this point is that it's still pretty "raw", and I'm not sure if it's ready for the normal Emacs release cadence at this point. I've never contributed anything to upstream before: is there any way to have a base version in the distribution, and still release improvements independently? I guess that Org does that, so it must be somehow possible.
alebrick · 2022-12-29 14:12
I don't have anything too magical to share, sadly. I'm just using `lsp-mode` with `kotlin-ts-mode`. I agree that the Kotlin Language Server is behind the state of a lot of the others, so I still have it on my list to learn more about LSP, so that I can contribute there :)
alebrick · 2022-12-29 15:16
And now I've pushed an update that adds syntax highlighting for interpolated strings. That should make your #1 look better.
Qkesslerr · 2022-12-29 15:57
Hi again! Thanks for being so quick with the changes! I'll take a look now. Also, something to note is that using the HEAD on emacs master (with commit "8c84ef128f32b227c7a46cd1da8221b41bd0a370"), we don't have the `c-ts-mode-comment-setup` function defined. Whenever I pull your changes, I always comment out that function.
Qkesslerr · 2022-12-29 16:43
Update on this! Really makes an improvement, just working on #1 and #2, it's great. Adding to that list: * `asinSuffix to functionNameThatReturnsAString()` inside a `map` function call on an iterator doesn't have the `to` keyword highlighted and `asinSuffix` on the example is white, as are usages of variables like `request.personalizationDetails`. EDIT(2): With your last commits, personalizationDetails is now highlighted, but request is white, while when declared it was correctly highlighted (blue in my case, using gruvbox).
alebrick · 2022-12-29 19:25
Interesting. That function exists in the `emacs-29` branch...maybe it hasn't been merged back to `master` yet? Edit: Formatting
leungbk · 2022-12-30 00:12
I guess you could release it as an ELPA core package if you want to release improvements independently. I wouldn't worry about it being unpolished, since the reviewers should have some suggestions to tidy everything up.