obiwan87 / odin-intellij

Odin Support plugin for JetBrains IDEs
https://plugins.jetbrains.com/plugin/22933-odin-support
MIT License
37 stars 3 forks source link

Is it possible to change the behavior of the 'Extend Selection' action to properly consider quotes? #46

Closed Dima-369 closed 2 months ago

Dima-369 commented 2 months ago

I am very used to the Extend Selection action in other programming languages, but I notice how it behaves a tad off for Odin.

If the cursor is on the space in the string a := "test string", expanding first selects test which is good, but the next invocation selects "test string" with the quotes. In pretty much all other languages, it first proceeds to select everything inside the string first. So, I would expect test string (without quotes).

This throws off my muscle memory a lot.

obiwan87 commented 2 months ago

Hi @Dima-369 Can you please check if the behaviour in the video matches your expectations? On Windows, I use Ctrl+W to expand selection and Ctrl+Shift+W to shrink the selection.

I've tested this on the upcoming version 0.5.0, which I hope to deploy today or in the coming days

https://github.com/user-attachments/assets/b3f91c36-89f1-4250-8d69-dda57ac6013f

Dima-369 commented 2 months ago

Can you please check if the behaviour in the video matches your expectations?

Yes, exactly! I am looking forward to 0.5.0 🙂

Dima-369 commented 2 months ago

I tested the latest develop and there this does not work?

https://github.com/user-attachments/assets/4c101eda-55b3-4b3c-b979-4e86b03bd93a

obiwan87 commented 2 months ago

Oh! Yes, you're right. The correct behaviour only works with strings that only consist of one "word". Tbh I have no clue how this comes about, as I haven't implemented the feature myself, but comes with the IDE. I merely have to tell the framework which tokens are the String tokens. Maybe I made a mistake there.

Dima-369 commented 2 months ago

Is it the same for all string literals?

Yeah, I tried with your example. Exactly, it is about the whitespace!

Dima-369 commented 2 months ago

I tested with the ZigBrains plugin, and it exhibits the same 'bug' also present here.

Edit: Same with the Nim IntelliJ plugin.

Dima-369 commented 2 months ago

It works in the Scala plugin for .scala files, but there is no .bnf file for their syntax to check against, apparently. Maybe this is a quirk with the lexer implementation in IntelliJ?

Dima-369 commented 2 months ago

I checked a Kotlin file (where this also works correctly) with Show Editor Highlighter Tokens enabled, and found that they treat the surrounding quotes as special tokens, which may help in fixing this?

CleanShot 2024-08-13 at 20 37 22@2x

I am very new to the .bnf files, I suppose DQ_STRING_LITERAL='regexp:"[^"]*"' may need changes for this?

obiwan87 commented 2 months ago

Oh cool, good to know. This is a tough one though. It has to be done on Lexer basis, which is defined in Odin.flex. The fact the strings would not contain quotes anymore would also have a series of consequences in the grammar, which is defined in Odin.bnf.

However, I was just looking at the token structure of Java. There, they also don't have any special tokens for opening or closing quotes, but it still works. Hmmm, this might be a question for JetBrains.

image

FYI: The token definitions in Odin.bnf are merely for the Preview-Tool that I used in the beginning. But the grammar grew a lot in complexity with custom rules and parsing states. What I want to say: the regexes in Odin.bnf are meaningless. They are not used to create the tokens. Rather the lexer is created from Odin.flex

obiwan87 commented 2 months ago

Anyway, ChatGPT knows it all. There is class called "ExtendWordSelectionHandler" which one can use to change the behaviour of extend/shrink selection.

https://github.com/JetBrains/intellij-community/blob/b7e62ca44afee874c9a404c66b1cd78ca308114e/plugins/kotlin/base/code-insight/minimal/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinStringLiteralSelectioner.kt#L13

Dima-369 commented 2 months ago

Anyway, ChatGPT knows it all.

Hah, I also feel that way for the IntelliJ Plugin SDK stuff - googling anything about it is such a pain 😄

And that's cool, so we can achieve that behavior!

obiwan87 commented 2 months ago

A pain indeed my friend! The nice thing is that the JetBrains guys really answer when posting a question on their issue tracker.

obiwan87 commented 2 months ago

Ok, I think we got it now. I added OdinStringSelectioner that should take care of selecting to just before the quotes. Just pushed :)

Dima-369 commented 2 months ago

I tested and indeed, it works perfectly for " now! Thanks.

I would love it if raw string literals via ` would also be supported 😉

https://github.com/user-attachments/assets/875ce5fe-2bf5-45a9-95fc-8697010bd8f8

obiwan87 commented 2 months ago

I tested and indeed, it works perfectly for " now! Thanks.

I would love it if raw string literals via ` would also be supported 😉

CleanShot.2024-08-14.at.13.25.04.mp4

Ah yes, adding it. :)

obiwan87 commented 2 months ago

Should be working now. I just publish the plugin to the JetBrains Marketplace. It will be accepted within 1-2 business days :) Thank you so much for helping with this release

FYI: I changed the Java version to 21, so you might have to change the project SDK to 21.

Dima-369 commented 2 months ago

Should be working now.

I tested and it works great, thanks!

Thank you so much for helping with this release

Thank you as well for the superfast fixes.

I am happy that this plugin has become even more useful now! 🙂

obiwan87 commented 2 months ago

I think on the next release I will focus on contextually correct code completion and refactorings. It is a pleasure to work with you. Feel free to join anytime you want