Skip to content

Commit

Permalink
#114 preview put argument on separate lines correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
oboenikui committed Jul 20, 2023
1 parent f1bbee3 commit becde2e
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.impl.ImaginaryEditor
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.popup.JBPopupFactory
import com.intellij.openapi.ui.popup.PopupStep
Expand Down Expand Up @@ -152,7 +153,8 @@ open class FillClassFix(

val lambdaArgument = call.lambdaArguments.singleOrNull()
val editor = argumentList.findExistingEditor()
if (descriptors.size == 1 || editor == null) {
?: ImaginaryEditor(project, argumentList.containingFile.viewProvider.document)
if (descriptors.size == 1 || editor is ImaginaryEditor) {
argumentList.fillArgumentsAndFormat(
descriptor = descriptors.first().second,
editor = editor,
Expand All @@ -168,7 +170,7 @@ open class FillClassFix(
argumentList: KtValueArgumentList,
lambdaArgument: KtLambdaArgument?,
descriptors: List<Pair<KtFunction, FunctionDescriptor>>,
editor: Editor?,
editor: Editor,
): BaseListPopupStep<String> {
val functionName = descriptors.first().let { (_, descriptor) ->
if (descriptor is ClassConstructorDescriptor) {
Expand Down Expand Up @@ -207,15 +209,15 @@ open class FillClassFix(

private fun KtValueArgumentList.fillArgumentsAndFormat(
descriptor: FunctionDescriptor,
editor: Editor?,
editor: Editor,
lambdaArgument: KtLambdaArgument?,
) {
fillArgumentsAndFormat(descriptor.valueParameters, editor, lambdaArgument)
}

private fun KtValueArgumentList.fillArgumentsAndFormat(
parameters: List<ValueParameterDescriptor>,
editor: Editor?,
editor: Editor,
lambdaArgument: KtLambdaArgument? = null,
) {
val argumentSize = arguments.size
Expand All @@ -225,7 +227,7 @@ open class FillClassFix(
// post-fill process

// 1. Put arguments on separate lines
if (editor != null && putArgumentsOnSeparateLines) {
if (putArgumentsOnSeparateLines) {
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.document)
if (this.arguments.isNotEmpty()) {
PutArgumentOnSeparateLineHelper.applyTo(this, editor)
Expand All @@ -251,7 +253,7 @@ open class FillClassFix(

// 4. Set argument placeholders
// This should be run on final state
if (editor != null && movePointerToEveryArgument) {
if (editor !is ImaginaryEditor && movePointerToEveryArgument) {
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.document)
startToReplaceArguments(argumentSize, editor)
}
Expand All @@ -260,7 +262,7 @@ open class FillClassFix(
private fun KtValueArgumentList.fillArguments(
factory: KtPsiFactory,
parameters: List<ValueParameterDescriptor>,
editor: Editor?,
editor: Editor,
lambdaArgument: KtLambdaArgument? = null,
) {
val arguments = this.arguments
Expand All @@ -280,7 +282,7 @@ open class FillClassFix(
private fun createDefaultValueArgument(
parameter: ValueParameterDescriptor,
factory: KtPsiFactory,
editor: Editor?,
editor: Editor,
): KtValueArgument {
if (withoutDefaultValues) {
return factory.createArgument(null, parameter.name)
Expand Down

0 comments on commit becde2e

Please sign in to comment.