Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to intention & Support non-empty constructor #6

Merged
merged 5 commits into from
Dec 30, 2018

Conversation

shiraji
Copy link
Contributor

@shiraji shiraji commented Aug 26, 2018

Here is the screenshot

fill-in

@shiraji shiraji force-pushed the convert_to_intention branch 2 times, most recently from cbc04b4 to 2e488a2 Compare August 26, 2018 16:00
result = "$result$parameterString"
private fun createParameterSetterExpression(element: KtValueArgumentList, parameters: List<ValueParameterDescriptor>) {
val arguments = element.arguments
val argumentNames = arguments.map { it.getArgumentName()?.asName?.identifier }.filterNotNull()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simplify this by mapNotNull 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

if (arguments.size > index && !arguments[index].isNamed()) return@forEachIndexed
if (argumentNames.contains(parameter.name.identifier)) return@forEachIndexed
val newArgument = factory.createArgument(
expression = factory.createExpression(createDefaultValueFromParameter(parameter)),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

factory.createExpression throws java.lang.IllegalStateException when createDefaultValueFromParameter returns empty string.

java.lang.IllegalStateException: Failed to create expression from text: ''
	at org.jetbrains.kotlin.psi.KtPsiFactory.createExpression(KtPsiFactory.kt:59)
	at com.github.suusan2go.kotlinfillclass.intentions.FillClassIntention.createParameterSetterExpression(FillClassIntention.kt:50)
	at com.github.suusan2go.kotlinfillclass.intentions.FillClassIntention.applyTo(FillClassIntention.kt:25)
	at com.github.suusan2go.kotlinfillclass.intentions.FillClassIntention.applyTo(FillClassIntention.kt:17)
	at org.jetbrains.kotlin.idea.intentions.SelfTargetingIntention.invoke(SelfTargetingIntention.kt:107)
	at com.intellij.codeInsight.intention.impl.config.IntentionActionWrapper.invoke(IntentionActionWrapper.java:67)
	at com.intellij.codeInsight.intention.impl.IntentionActionWithTextCaching$MyIntentionAction.invoke(IntentionActionWithTextCaching.java:173)
	at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.lambda$invokeIntention$3(ShowIntentionActionsHandler.java:200)
	at com.intellij.openapi.application.WriteAction.run(WriteAction.java:105)
	at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.invokeIntention(ShowIntentionActionsHandler.java:202)
	at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.lambda$null$1(ShowIntentionActionsHandler.java:177)
	at com.intellij.openapi.application.TransactionGuardImpl.runSyncTransaction(TransactionGuardImpl.java:88)
	at com.intellij.openapi.application.TransactionGuardImpl.submitTransactionAndWait(TransactionGuardImpl.java:153)
	at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.lambda$chooseActionAndInvoke$2(ShowIntentionActionsHandler.java:176)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:139)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:97)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:87)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:73)
	at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.chooseActionAndInvoke(ShowIntentionActionsHandler.java:175)
	at com.intellij.codeInsight.intention.impl.IntentionListStep.lambda$applyAction$0(IntentionListStep.java:118)
	at com.intellij.openapi.application.TransactionGuardImpl.performUserActivity(TransactionGuardImpl.java:195)
	at com.intellij.ui.popup.AbstractPopup.lambda$dispose$8(AbstractPopup.java:1405)
	at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:3078)
	at com.intellij.ide.IdeEventQueue.ifFocusEventsInTheQueue(IdeEventQueue.java:176)
	at com.intellij.ide.IdeEventQueue.executeWhenAllFocusEventsLeftTheQueue(IdeEventQueue.java:132)
	at com.intellij.openapi.wm.impl.FocusManagerImpl.doWhenFocusSettlesDown(FocusManagerImpl.java:190)
	at com.intellij.openapi.wm.impl.IdeFocusManagerImpl.doWhenFocusSettlesDown(IdeFocusManagerImpl.java:58)
	at com.intellij.ui.popup.AbstractPopup.dispose(AbstractPopup.java:1399)
	at com.intellij.ui.popup.WizardPopup.dispose(WizardPopup.java:160)
	at com.intellij.ui.popup.list.ListPopupImpl.dispose(ListPopupImpl.java:307)
	at com.intellij.openapi.util.Disposer$1.execute(Disposer.java:48)
	at com.intellij.openapi.util.Disposer$1.execute(Disposer.java:44)
	at com.intellij.openapi.util.objectTree.ObjectNode$1.execute(ObjectNode.java:138)
	at com.intellij.openapi.util.objectTree.ObjectNode$1.execute(ObjectNode.java:107)
	at com.intellij.openapi.util.objectTree.ObjectTree.executeActionWithRecursiveGuard(ObjectTree.java:182)
	at com.intellij.openapi.util.objectTree.ObjectNode.execute(ObjectNode.java:107)
	at com.intellij.openapi.util.objectTree.ObjectTree.executeAll(ObjectTree.java:151)
	at com.intellij.openapi.util.Disposer.dispose(Disposer.java:129)
	at com.intellij.openapi.util.Disposer.dispose(Disposer.java:125)
	at com.intellij.ui.popup.WizardPopup.disposeAllParents(WizardPopup.java:263)
	at com.intellij.ui.popup.list.ListPopupImpl.handleNextStep(ListPopupImpl.java:442)
	at com.intellij.ui.popup.list.ListPopupImpl._handleSelect(ListPopupImpl.java:396)
	at com.intellij.ui.popup.list.ListPopupImpl.handleSelect(ListPopupImpl.java:337)
	at com.intellij.ui.popup.list.ListPopupImpl$1.actionPerformed(ListPopupImpl.java:250)
	at com.intellij.ui.popup.WizardPopup.proceedKeyEvent(WizardPopup.java:378)
	at com.intellij.ui.popup.WizardPopup.dispatch(WizardPopup.java:358)
	at com.intellij.ui.popup.PopupDispatcher.dispatchKeyEvent(PopupDispatcher.java:126)
	at com.intellij.ui.popup.PopupDispatcher.dispatch(PopupDispatcher.java:160)
	at com.intellij.ide.IdePopupManager.dispatch(IdePopupManager.java:86)
	at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:673)
	at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:382)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

So I think we have to pass null to expression if createDefaultValueFromParameter returns empty string.

Copy link
Owner

@suusan2go suusan2go Sep 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing createDefaultValueFromParameter to return null when not match KotlinBuiltins and handling like this would be better?

            val defaultValue = createDefaultValueFromParameter(parameter)
            val newArgument = factory.createArgument(
                    expression = defaultValue?.let { factory.createExpression(it) },
                    name = parameter.name
            )

Copy link
Contributor Author

@shiraji shiraji Sep 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to change the return value in this PR? I prefer not to do that, thou.

The things is I'm not quite sure what do you want to do when createDefaultValueFromParameter returns null.

data class Bar(val value: String)
data class Foo(val value: Bar)
fun foo() {
    Foo(value) // compile error
}

Instead, I like this one.

data class Bar(val value: String)
data class Foo(val value: Bar)
fun foo() {
    Foo(value = 
       Bar(
           value = ""
       )
   )
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point is that this pull-request changes the current filling behavior.

data class UserName(
        val value: String
)

data class User(
        val id: Int,
        val name: UserName,
        val age: Int
)
// fill class target
val user = User()

// expected
val user = User(id = 0, name =,  age = 0)

// got
val user = User(id = 0)

I think this is because this exception commented here.
#6 (comment)

I agree with your comment that filling class like this is better and I want to support it in the future, but I have no idea how to implement it now 😅 Do you have any ideas ?

data class Bar(val value: String)
data class Foo(val value: Bar)
fun foo() {
    Foo(value = 
       Bar(
           value = ""
       )
   )
}

@suusan2go
Copy link
Owner

Sorry for late reply. Thanks for your great pull request 🍺

@shiraji
Copy link
Contributor Author

shiraji commented Sep 2, 2018

Do you have any ideas ?

I'm not sure...

@suusan2go
Copy link
Owner

@shiraji Thank you for your pull request and I'm sorry for late reply 😓 LGTM !

@suusan2go suusan2go merged commit 7a43c65 into suusan2go:master Dec 30, 2018
@shiraji shiraji deleted the convert_to_intention branch December 30, 2018 08:31
@suusan2go suusan2go mentioned this pull request Jun 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants