ktfmt icon indicating copy to clipboard operation
ktfmt copied to clipboard

IntelliJ plugin behaves weird when auto-completing Jetpack Compose code

Open ColtonIdle opened this issue 4 years ago • 16 comments

Jetpack compose is a declarative UI toolkit. If you have the ktfmt plugin + are working in compose code and hit "enter" to autocomplete something simple like a layout like a Box or a Column, everything just breaks when you hit return. The cursor is in the wrong spot. It replaces some code incorrectly, etc.

Attached is a video. Reproducing is easy just create a new compose project and try to add Box or Column. ktfmt plugin and compose used to work properly but as of maybe ~1 month ago things started freaking out. I just never reported it, but now it has messed up enough files that I'm reporting.

https://user-images.githubusercontent.com/31751141/128462736-85f1025c-d131-4e5a-ba2a-ccc3b13fb089.mp4

ColtonIdle avatar Aug 06 '21 05:08 ColtonIdle

Don't mean to be a bother about this, but it's really affecting the ability to use the plugin in day to day. Is anyone else experiencing this by chance?

ColtonIdle avatar Aug 12 '21 17:08 ColtonIdle

I am having the same experience.

ellykits avatar Aug 31 '21 10:08 ellykits

Since we share most of the plugin code with google-java-format, did you notice this behavior with their plugin as well? or is it irrelevant because Jetpack is Kotlin-only? Did they have anything interesting in their release notes lately, or relevant commits to the plugin directory?

cgrushko avatar Aug 31 '21 11:08 cgrushko

I am having the same experience.

It's not just me. Hooray!

Since we share most of the plugin code with google-java-format, did you notice this behavior with their plugin as well? or is it irrelevant because Jetpack is Kotlin-only?

I would think it's irrelevant because it's compose which is kotlin only.

Did they have anything interesting in their release notes lately, or relevant commits to the plugin directory?

Not that I know of. I've been using compose with ktfmt for a while and probably about 2 months ago it just started freaking out.

ColtonIdle avatar Aug 31 '21 14:08 ColtonIdle

@ellykits by anychance do you use dropbox configuration? (4 space instead of 2?)

ColtonIdle avatar Sep 25 '21 14:09 ColtonIdle

Auto completing a composable today hit me with this IDE error. I have ktfmt plugin enabled, not sure if it's related?

java.lang.IndexOutOfBoundsException: Wrong startOffset: 25821; documentLength: 25814
	at com.intellij.openapi.editor.impl.DocumentImpl.assertBounds(DocumentImpl.java:667)
	at com.intellij.openapi.editor.impl.DocumentImpl.deleteString(DocumentImpl.java:564)
	at com.intellij.codeInsight.template.impl.TemplateStateBase.lambda$restoreEmptyVariables$1(TemplateStateBase.java:118)
	at com.intellij.util.DocumentUtil.executeInBulk(DocumentUtil.java:49)
	at com.intellij.codeInsight.template.impl.TemplateStateBase.restoreEmptyVariables(TemplateStateBase.java:113)
	at com.intellij.codeInsight.template.impl.TemplateState.lambda$doReformat$2(TemplateState.java:401)
	at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:965)
	at com.intellij.codeInsight.template.impl.TemplateState.doReformat(TemplateState.java:403)
	at com.intellij.codeInsight.template.impl.TemplateState.lambda$processAllExpressions$1(TemplateState.java:360)
	at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:965)
	at com.intellij.codeInsight.template.impl.TemplateState.processAllExpressions(TemplateState.java:348)
	at com.intellij.codeInsight.template.impl.TemplateState.start(TemplateState.java:333)
	at com.intellij.codeInsight.template.impl.TemplateState.start(TemplateState.java:297)
	at com.intellij.codeInsight.template.impl.TemplateManagerImpl.lambda$startTemplate$2(TemplateManagerImpl.java:171)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:207)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:172)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:162)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:148)
	at com.intellij.codeInsight.template.impl.TemplateManagerImpl.startTemplate(TemplateManagerImpl.java:175)
	at com.intellij.codeInsight.template.impl.TemplateManagerImpl.startTemplate(TemplateManagerImpl.java:193)
	at com.intellij.codeInsight.template.impl.TemplateManagerImpl.startTemplate(TemplateManagerImpl.java:132)
	at com.intellij.codeInsight.template.impl.LiveTemplateLookupElementImpl.startTemplate(LiveTemplateLookupElementImpl.java:44)
	at com.intellij.codeInsight.template.impl.LiveTemplateLookupElementImpl.handleInsert(LiveTemplateLookupElementImpl.java:38)
	at com.intellij.codeInsight.completion.CodeCompletionHandlerBase.lambda$insertItem$14(CodeCompletionHandlerBase.java:690)
	at com.intellij.util.indexing.FileBasedIndex.lambda$ignoreDumbMode$0(FileBasedIndex.java:163)
	at com.intellij.openapi.util.RecursionManager$1.computePreventingRecursion(RecursionManager.java:111)
	at com.intellij.util.indexing.FileBasedIndexEx.ignoreDumbMode(FileBasedIndexEx.java:574)
	at com.intellij.util.indexing.FileBasedIndex.ignoreDumbMode(FileBasedIndex.java:162)
	at com.intellij.util.indexing.DumbModeAccessType.ignoreDumbMode(DumbModeAccessType.java:43)
	at com.intellij.codeInsight.completion.CodeCompletionHandlerBase.lambda$insertItem$15(CodeCompletionHandlerBase.java:689)
	at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:965)
	at com.intellij.codeInsight.completion.CodeCompletionHandlerBase.insertItem(CodeCompletionHandlerBase.java:673)
	at com.intellij.codeInsight.completion.CodeCompletionHandlerBase.lambda$doInsertItem$12(CodeCompletionHandlerBase.java:598)
	at com.intellij.openapi.editor.impl.CaretModelImpl.lambda$runForEachCaret$3(CaretModelImpl.java:312)
	at com.intellij.openapi.editor.impl.CaretModelImpl.doWithCaretMerging(CaretModelImpl.java:421)
	at com.intellij.openapi.editor.impl.CaretModelImpl.runForEachCaret(CaretModelImpl.java:321)
	at com.intellij.openapi.editor.impl.CaretModelImpl.runForEachCaret(CaretModelImpl.java:296)
	at com.intellij.codeInsight.completion.CodeCompletionHandlerBase.doInsertItem(CodeCompletionHandlerBase.java:588)
	at com.intellij.codeInsight.completion.CodeCompletionHandlerBase.insertItemHonorBlockSelection(CodeCompletionHandlerBase.java:548)
	at com.intellij.codeInsight.completion.CodeCompletionHandlerBase.lookupItemSelected(CodeCompletionHandlerBase.java:494)
	at com.intellij.codeInsight.completion.CompletionProgressIndicator.itemSelected(CompletionProgressIndicator.java:180)
	at com.intellij.codeInsight.completion.BaseCompletionLookupArranger.itemSelected(BaseCompletionLookupArranger.java:224)
	at com.intellij.codeInsight.lookup.impl.LookupImpl.fireItemSelected(LookupImpl.java:915)
	at com.intellij.codeInsight.lookup.impl.LookupImpl.finishLookupInWritableFile(LookupImpl.java:569)
	at com.intellij.codeInsight.lookup.impl.LookupImpl.lambda$finishLookup$5(LookupImpl.java:527)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:207)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:172)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:162)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:148)
	at com.intellij.codeInsight.lookup.impl.LookupImpl.finishLookup(LookupImpl.java:527)
	at com.intellij.codeInsight.lookup.impl.LookupImpl.finishLookup(LookupImpl.java:512)
	at com.intellij.codeInsight.lookup.impl.actions.ChooseItemAction$Handler.lambda$doExecute$0(ChooseItemAction.java:72)
	at com.intellij.util.SlowOperations.lambda$allowSlowOperations$0(SlowOperations.java:77)
	at com.intellij.util.SlowOperations.allowSlowOperations(SlowOperations.java:64)
	at com.intellij.util.SlowOperations.allowSlowOperations(SlowOperations.java:76)
	at com.intellij.codeInsight.lookup.impl.actions.ChooseItemAction$Handler.doExecute(ChooseItemAction.java:72)
	at com.intellij.openapi.editor.actionSystem.DynamicEditorActionHandler.doExecute(DynamicEditorActionHandler.java:52)
	at com.intellij.openapi.editor.actionSystem.EditorActionHandler.lambda$execute$4(EditorActionHandler.java:199)
	at com.intellij.openapi.editor.actionSystem.EditorActionHandler.doIfEnabled(EditorActionHandler.java:89)
	at com.intellij.openapi.editor.actionSystem.EditorActionHandler.execute(EditorActionHandler.java:198)
	at com.intellij.openapi.editor.actionSystem.EditorAction.lambda$actionPerformed$0(EditorAction.java:89)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:216)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:172)
	at com.intellij.openapi.editor.actionSystem.EditorAction.actionPerformed(EditorAction.java:98)
	at com.intellij.openapi.editor.actionSystem.EditorAction.actionPerformed(EditorAction.java:73)
	at com.intellij.openapi.actionSystem.ex.ActionUtil.lambda$performActionDumbAware$5(ActionUtil.java:273)
	at com.intellij.util.SlowOperations.lambda$allowSlowOperations$0(SlowOperations.java:77)
	at com.intellij.util.SlowOperations.allowSlowOperations(SlowOperations.java:64)
	at com.intellij.util.SlowOperations.allowSlowOperations(SlowOperations.java:76)
	at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:273)
	at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher$1.performAction(IdeKeyEventDispatcher.java:616)
	at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.lambda$processAction$3(IdeKeyEventDispatcher.java:676)
	at com.intellij.openapi.application.TransactionGuardImpl.performUserActivity(TransactionGuardImpl.java:95)
	at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.processAction(IdeKeyEventDispatcher.java:675)
	at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.processAction(IdeKeyEventDispatcher.java:626)
	at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.processActionOrWaitSecondStroke(IdeKeyEventDispatcher.java:486)
	at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.inInitState(IdeKeyEventDispatcher.java:481)
	at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.dispatchKeyEvent(IdeKeyEventDispatcher.java:232)
	at com.intellij.ide.IdeEventQueue.dispatchKeyEvent(IdeEventQueue.java:889)
	at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:833)
	at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$8(IdeEventQueue.java:449)
	at com.intellij.openapi.progress.impl.CoreProgressManager.computePrioritized(CoreProgressManager.java:808)
	at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$9(IdeEventQueue.java:448)
	at com.intellij.openapi.application.impl.ApplicationImpl.runIntendedWriteActionOnCurrentThread(ApplicationImpl.java:787)
	at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:502)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

ColtonIdle avatar Sep 26 '21 00:09 ColtonIdle

No I use the Google format

On Sat, Sep 25, 2021, 5:04 PM ColtonIdle @.***> wrote:

@ellykits https://github.com/ellykits by anychance do you use dropbox configuration? (4 space instead of 2?)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/facebookincubator/ktfmt/issues/249#issuecomment-927126268, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGM667DBIODZLYLAV3ULISDUDXJITANCNFSM5BVHJM7A .

ellykits avatar Sep 26 '21 11:09 ellykits

I'm using latest arctic fox on a brand new project. It's 2 minutes of me just playing around with compose and you can see everywhere where ktfmt just breaks my code when auto completing + auto-formatting. How can I help debug this issue?

https://user-images.githubusercontent.com/31751141/146634786-2e71e768-8858-4547-8763-38db17865d3c.mp4

ColtonIdle avatar Dec 18 '21 08:12 ColtonIdle

@cgrushko apologies for the ping, but I've been trying to debug this the past few days and I'm right about at the point of ripping my hair out. Before I do so, do you noticed anything suspicious? Is there anyway I can help with how the plugin works with AS and auto-complete? It makes the plugin useless for me.

ColtonIdle avatar Dec 21 '21 17:12 ColtonIdle

I am experiencing this as well. I don't think there can be done a lot as a user. The issue is obviously due to a misbehaviour in the code commit - formatting cycle. So I assume Android Studio plugin is also doing some code formatting and one of them reads an earlier state of the file and formats it based on that.

I'd have to dig deeper into the source code, but this and the WriteAction need to be double-checked. It seems that the document.replaceString may operate on outdated data.

andrasferenczi avatar Jan 04 '22 07:01 andrasferenczi

I'm glad someone else is having the same issue. Hopefully one of the maintainers comes across this with some more direction on how we can help.

ColtonIdle avatar Jan 05 '22 05:01 ColtonIdle

It looks like to me that the ktfmt maintainers are busy and it is not likely to get fixed.

It is at least a day's work for me to understand what causes it in details and I cannot put in that much time into it either.

But in case you have observed that only the Android Studio @Composable -related code causes it, you could also try posting a ticket on their repository (I am not sure where exactly that is). Maybe its their code that requires fixing.

andrasferenczi avatar Jan 05 '22 07:01 andrasferenczi

Any update on this? Still seeing this as well.

fabiohzgusto avatar Nov 10 '22 04:11 fabiohzgusto

@fabiohzgusto thank god its not just me 😄

ColtonIdle avatar Nov 10 '22 19:11 ColtonIdle

I didn't know this issue was so old lol. Still there, though.

For the time being, I've uninstalled the Android Studio plugin. Instead, I'm relying on the gradle plugin along with a pre-commit hook.

addy-zero avatar Jul 29 '23 21:07 addy-zero

Disable the enhanced auto-completion when using Jetpack Compose in Android Studio

CleanShot 2024-04-12 at 18 25 01

blipinsk avatar Apr 12 '24 16:04 blipinsk

@blipinsk, does that mean we still have issues with this here?

hick209 avatar May 03 '24 13:05 hick209

No longer experiencing this issue. I'm using Iguana version of Android studio.

ellykits avatar May 03 '24 13:05 ellykits

Closing for now, but feel free to reopen if the issue is still prevalent on AS >= Iguana

hick209 avatar May 03 '24 13:05 hick209

@blipinsk, does that mean we still have issues with this here?

@hick209 I mean... This feels more like an issue related to the "enhanced auto-completion compose formatting", but I didn't do any deep-down investigation to be certain 🤷‍♂️

blipinsk avatar May 03 '24 13:05 blipinsk

Filed a bug on issue tracker here: https://issuetracker.google.com/issues/338842142

ColtonIdle avatar May 06 '24 15:05 ColtonIdle

apparently "fixed" on the AS side? https://issuetracker.google.com/issues/338842142#comment5

ColtonIdle avatar May 19 '24 03:05 ColtonIdle

I'll try it on my end and let you all know

blipinsk avatar May 19 '24 15:05 blipinsk

Looks like it's fixed btw 😉

blipinsk avatar Aug 07 '24 21:08 blipinsk