From 05269cea2f57723e7d1ad76b8c5b58fa1547c388 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Thu, 6 Dec 2018 18:17:07 +0300 Subject: [PATCH] Reformat in idea.editor package --- .../kotlin/idea/editor/BatchTemplateRunner.kt | 14 ++-- .../idea/editor/KotlinBackspaceHandler.java | 2 +- .../idea/editor/KotlinEditorOptions.java | 3 +- .../kotlin/idea/editor/KotlinEnterHandler.kt | 27 +++---- .../editor/KotlinLiteralCopyPasteProcessor.kt | 39 ++++------ .../KotlinMultilineStringEnterHandler.kt | 78 ++++++++++--------- .../kotlin/idea/editor/KotlinQuoteHandler.kt | 13 ++-- .../idea/editor/KotlinSmartEnterHandler.kt | 3 +- .../idea/editor/KotlinTypedHandler.java | 14 +++- .../fixers/KotlinCatchParameterFixer.kt | 3 +- .../idea/editor/fixers/KotlinDoWhileFixer.kt | 10 +-- .../editor/fixers/KotlinForConditionFixer.kt | 5 +- .../fixers/KotlinFunctionParametersFixer.kt | 3 +- .../fixers/KotlinMissingIfBranchFixer.kt | 3 +- .../fixers/KotlinMissingWhenBodyFixer.kt | 2 +- .../KotlinPropertySetterParametersFixer.kt | 8 +- .../fixers/KotlinWhenSubjectCaretFixer.kt | 4 +- .../fixers/KotlinWhileConditionFixer.kt | 2 +- .../editor/fixers/MissingConditionFixer.kt | 8 +- .../kotlin/idea/editor/fixers/fixersUtil.kt | 4 +- ...tlinCallExpressionWithLambdaSelectioner.kt | 6 +- .../KotlinDeclarationSelectioner.kt | 29 +++---- .../KotlinDocCommentSelectioner.kt | 6 +- .../wordSelection/KotlinListSelectioner.kt | 14 ++-- .../KotlinStatementGroupSelectioner.kt | 45 ++++++----- .../KotlinStringLiteralSelectioner.kt | 2 +- .../wordSelection/KotlinTypeSelectioner.kt | 4 +- .../KotlinWordSelectionFilter.kt | 6 +- 28 files changed, 180 insertions(+), 177 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/BatchTemplateRunner.kt b/idea/src/org/jetbrains/kotlin/idea/editor/BatchTemplateRunner.kt index c757cf36444..e25ed8f234a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/BatchTemplateRunner.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/BatchTemplateRunner.kt @@ -60,14 +60,14 @@ class BatchTemplateRunner(private val project: Project) { PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.document) val template = factory() ?: return@executeWriteCommand manager.startTemplate( - editor, - template, - object : TemplateEditingAdapter() { - override fun templateFinished(template: Template, brokenOff: Boolean) { - if (brokenOff) return - ApplicationManager.getApplication().invokeLater { runTemplates(iterator) } - } + editor, + template, + object : TemplateEditingAdapter() { + override fun templateFinished(template: Template, brokenOff: Boolean) { + if (brokenOff) return + ApplicationManager.getApplication().invokeLater { runTemplates(iterator) } } + } ) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinBackspaceHandler.java b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinBackspaceHandler.java index eaab16194f8..73037ffa1ac 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinBackspaceHandler.java +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinBackspaceHandler.java @@ -30,7 +30,7 @@ public class KotlinBackspaceHandler extends BackspaceHandlerDelegate { @Override public void beforeCharDeleted(char c, PsiFile file, Editor editor) { int offset = editor.getCaretModel().getOffset() - 1; - deleteGt = c =='<' && file instanceof KtFile && + deleteGt = c == '<' && file instanceof KtFile && (isAfterToken(offset, editor, KtTokens.FUN_KEYWORD) || isAfterToken(offset, editor, KtTokens.IDENTIFIER)); } diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinEditorOptions.java b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinEditorOptions.java index e012cad6b5a..e63ec555ea8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinEditorOptions.java +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinEditorOptions.java @@ -70,7 +70,8 @@ public class KotlinEditorOptions implements PersistentStateComponent, - caretAdvance: Ref, - dataContext: DataContext, - originalHandler: EditorActionHandler? + file: PsiFile, + editor: Editor, + caretOffsetRef: Ref, + caretAdvance: Ref, + dataContext: DataContext, + originalHandler: EditorActionHandler? ): EnterHandlerDelegate.Result? { if (file !is KtFile) return EnterHandlerDelegate.Result.Continue @@ -74,8 +74,7 @@ class KotlinEnterHandler: EnterHandlerDelegateAdapter() { try { CodeStyleManager.getInstance(file.getProject())!!.adjustLineIndent(file, editor.caretModel.offset) - } - catch (e: IncorrectOperationException) { + } catch (e: IncorrectOperationException) { LOG.error(e) } @@ -88,10 +87,12 @@ class KotlinEnterHandler: EnterHandlerDelegateAdapter() { // We can't use the core platform logic (EnterInStringLiteralHandler) because it assumes that the string // is a single token and the first character of the token is an opening quote. In the case of Kotlin, // the opening quote is a separate token and the first character of the string token is just a random letter. - private fun preprocessEnterInStringLiteral(psiFile: PsiFile, - editor: Editor, - caretOffsetRef: Ref, - caretAdvanceRef: Ref): Boolean { + private fun preprocessEnterInStringLiteral( + psiFile: PsiFile, + editor: Editor, + caretOffsetRef: Ref, + caretAdvanceRef: Ref + ): Boolean { var caretOffset = caretOffsetRef.get() val psiAtOffset = psiFile.findElementAt(caretOffset) ?: return false val stringTemplate = psiAtOffset.getStrictParentOfType() ?: return false diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt index 21fe8c8507b..8f12e76fe88 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt @@ -36,7 +36,7 @@ import org.jetbrains.kotlin.psi.KtStringTemplateExpression import org.jetbrains.kotlin.psi.psiUtil.* private val PsiElement.templateContentRange: TextRange? - get() = this.getParentOfType(false)?.let{ + get() = this.getParentOfType(false)?.let { it.textRange.cutOut(it.getContentRange()) } @@ -61,8 +61,7 @@ private fun deduceBlockSelectionWidth(startOffsets: IntArray, endOffsets: IntArr } return if (totalLength < text.length && (text.length + 1) % fragmentCount == 0) { (text.length + 1) / fragmentCount - 1 - } - else { + } else { -1 } } @@ -91,20 +90,19 @@ class KotlinLiteralCopyPasteProcessor : CopyPastePreProcessor { } val elTp = element.node.elementType if (elTp == KtTokens.ESCAPE_SEQUENCE && fileRange.contains(element.range) && - element.templateContentRange?.contains(fileRange) == true) { + element.templateContentRange?.contains(fileRange) == true + ) { val tpEntry = element.parent as KtEscapeStringTemplateEntry changed = true buffer.append(tpEntry.unescapedValue) givenTextOffset = element.endOffset - } - else if (elTp == KtTokens.SHORT_TEMPLATE_ENTRY_START || elTp == KtTokens.LONG_TEMPLATE_ENTRY_START) { + } else if (elTp == KtTokens.SHORT_TEMPLATE_ENTRY_START || elTp == KtTokens.LONG_TEMPLATE_ENTRY_START) { //Process inner templates without escaping val tpEntry = element.parent val inter = fileRange.intersection(tpEntry.range)!! buffer.append(fileText.substring(inter.startOffset, inter.endOffset)) givenTextOffset = inter.endOffset - } - else { + } else { val inter = fileRange.intersection(element.range)!! buffer.append(fileText.substring(inter.startOffset, inter.endOffset)) givenTextOffset = inter.endOffset @@ -143,13 +141,12 @@ class KotlinLiteralCopyPasteProcessor : CopyPastePreProcessor { } endsInLineBreak = it is NewLineChunk } - return if (endsInLineBreak){ + return if (endsInLineBreak) { res.removeSuffix(lineBreak).toString() + "\\n" - } else{ + } else { res.toString() } - } - else { + } else { val tripleQuoteRe = Regex("[\"]{3,}") TemplateTokenSequence(text).map { chunk -> when (chunk) { @@ -172,13 +169,11 @@ private object NewLineChunk : TemplateChunk() private class TemplateTokenSequence(private val inputString: String) : Sequence { private fun String.guessIsTemplateEntryStart(): Boolean = if (this.startsWith("\${")) { true - } - else if (this.length > 1 && this[0] == '$') { + } else if (this.length > 1 && this[0] == '$') { val guessedIdentifier = substring(1) val tokenType = KotlinLexer().apply { start(guessedIdentifier) }.tokenType tokenType == KtTokens.IDENTIFIER || tokenType == KtTokens.THIS_KEYWORD - } - else { + } else { false } @@ -192,8 +187,7 @@ private class TemplateTokenSequence(private val inputString: String) : Sequence< val tokenType = lexer.tokenType return if (tokenType == KtTokens.IDENTIFIER || tokenType == KtTokens.THIS_KEYWORD) { from + lexer.tokenEnd - 1 - } - else { + } else { -1 } } @@ -202,8 +196,7 @@ private class TemplateTokenSequence(private val inputString: String) : Sequence< while (lexer.tokenType != null) { if (lexer.tokenType == KtTokens.LONG_TEMPLATE_ENTRY_START) { depth++ - } - else if (lexer.tokenType == KtTokens.LONG_TEMPLATE_ENTRY_END) { + } else if (lexer.tokenType == KtTokens.LONG_TEMPLATE_ENTRY_END) { depth-- if (depth == 0) { return from + lexer.currentPosition.offset @@ -240,16 +233,14 @@ private class TemplateTokenSequence(private val inputString: String) : Sequence< to += 1 if (to < inputString.length) to += 1 continue - } - else if (c == '$') { + } else if (c == '$') { if (inputString.substring(to).guessIsTemplateEntryStart()) { if (from < to) yieldLiteral(inputString.substring(from until to)) from = to to = findTemplateEntryEnd(inputString, from) if (to != -1) { yield(EntryChunk(inputString.substring(from until to))) - } - else { + } else { to = inputString.length yieldLiteral(inputString.substring(from until to)) } diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinMultilineStringEnterHandler.kt b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinMultilineStringEnterHandler.kt index aaa37edf347..5de961059e9 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinMultilineStringEnterHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinMultilineStringEnterHandler.kt @@ -53,8 +53,9 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() { private var isInBrace = false override fun preprocessEnter( - file: PsiFile, editor: Editor, caretOffset: Ref, caretAdvance: Ref, dataContext: DataContext, - originalHandler: EditorActionHandler?): EnterHandlerDelegate.Result { + file: PsiFile, editor: Editor, caretOffset: Ref, caretAdvance: Ref, dataContext: DataContext, + originalHandler: EditorActionHandler? + ): EnterHandlerDelegate.Result { val offset = caretOffset.get().toInt() if (editor !is EditorWindow) { return preprocessEnter(file, editor, offset, originalHandler, dataContext) @@ -69,7 +70,13 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() { return preprocessEnter(file, editor, offset, originalHandler, dataContext) } - private fun preprocessEnter(file: PsiFile, editor: Editor, offset: Int, originalHandler: EditorActionHandler?, dataContext: DataContext): Result { + private fun preprocessEnter( + file: PsiFile, + editor: Editor, + offset: Int, + originalHandler: EditorActionHandler?, + dataContext: DataContext + ): Result { if (file !is KtFile) return Result.Continue val document = editor.document @@ -80,8 +87,7 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() { val element = file.findElementAt(offset) if (!inMultilineString(element, offset)) { return Result.Continue - } - else { + } else { wasInMultilineString = true } @@ -171,8 +177,7 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() { // Move closing bracket under same indent forceIndent(caretOffset() + 1, indentSize, newMarginChar, document, settings) } - } - else { + } else { val isPrevLineFirst = document.getLineNumber(literalOffset) == prevLineNumber val indentInPreviousLine = when { @@ -190,12 +195,12 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() { val marginCharToInsert = if (marginChar != null && !prefixStripped.startsWith(marginChar) && !nonBlankNotFirstLines.isEmpty() && - nonBlankNotFirstLines.none { it.trimStart().startsWith(marginChar) }) { + nonBlankNotFirstLines.none { it.trimStart().startsWith(marginChar) } + ) { // We have margin char but decide not to insert it null - } - else { + } else { marginChar } @@ -245,7 +250,7 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() { class MultilineSettings(project: Project) { private val kotlinIndentOptions = - CodeStyleSettingsManager.getInstance(project).currentSettings.getIndentOptions(KotlinFileType.INSTANCE) + CodeStyleSettingsManager.getInstance(project).currentSettings.getIndentOptions(KotlinFileType.INSTANCE) private val useTabs = kotlinIndentOptions.USE_TAB_CHARACTER private val tabSize = kotlinIndentOptions.TAB_SIZE @@ -285,7 +290,7 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() { } fun inMultilineString(element: PsiElement?, offset: Int) = - !(findString(element, offset)?.isSingleQuoted() ?: true) + !(findString(element, offset)?.isSingleQuoted() ?: true) fun getMarginCharFromLiteral(str: KtStringTemplateExpression, marginChar: Char = DEFAULT_TRIM_MARGIN_CHAR): Char? { val lines = str.text.lines() @@ -304,18 +309,17 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() { private fun getLiteralCalls(str: KtStringTemplateExpression): Sequence { var previous: PsiElement = str return str.parents - .takeWhile { parent -> - if (parent is KtQualifiedExpression && parent.receiverExpression == previous) { - previous = parent - true - } - else { - false - } - } - .mapNotNull { qualified -> - (qualified as KtQualifiedExpression).selectorExpression as? KtCallExpression + .takeWhile { parent -> + if (parent is KtQualifiedExpression && parent.receiverExpression == previous) { + previous = parent + true + } else { + false } + } + .mapNotNull { qualified -> + (qualified as KtQualifiedExpression).selectorExpression as? KtCallExpression + } } fun getMarginCharFromTrimMarginCallsInChain(str: KtStringTemplateExpression): Char? { @@ -335,7 +339,7 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() { } fun getLineByNumber(number: Int, document: Document): String = - document.getText(TextRange(document.getLineStartOffset(number), document.getLineEndOffset(number))) + document.getText(TextRange(document.getLineStartOffset(number), document.getLineEndOffset(number))) fun insertNewLine(nlOffset: Int, indent: Int, document: Document, settings: MultilineSettings) { document.insertString(nlOffset, "\n") @@ -347,9 +351,11 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() { val lineStart = document.getLineStartOffset(lineNumber) val line = getLineByNumber(lineNumber, document) val wsPrefix = line.takeWhile { c -> c == ' ' || c == '\t' } - document.replaceString(lineStart, - lineStart + wsPrefix.length, - settings.getSmartSpaces(indent) + (marginChar?.toString() ?: "")) + document.replaceString( + lineStart, + lineStart + wsPrefix.length, + settings.getSmartSpaces(indent) + (marginChar?.toString() ?: "") + ) } fun String.prefixLength(f: (Char) -> Boolean) = takeWhile(f).count() @@ -360,20 +366,20 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() { if (marginChar == null) { document.insertString(literal.textRange.endOffset, ".$TRIM_INDENT_CALL()") - } - else { + } else { document.insertString( - literal.textRange.endOffset, - if (marginChar == DEFAULT_TRIM_MARGIN_CHAR) { - ".$TRIM_MARGIN_CALL()" - } - else { - ".$TRIM_MARGIN_CALL(\"$marginChar\")" - }) + literal.textRange.endOffset, + if (marginChar == DEFAULT_TRIM_MARGIN_CHAR) { + ".$TRIM_MARGIN_CALL()" + } else { + ".$TRIM_MARGIN_CALL(\"$marginChar\")" + } + ) } } private data class HostPosition(val file: PsiFile, val editor: Editor, val offset: Int) + private fun getHostPosition(dataContext: DataContext): HostPosition? { val editor = dataContext.hostEditor as? EditorEx ?: return null val project = dataContext.project diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinQuoteHandler.kt b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinQuoteHandler.kt index 377ca2ccf22..5a84e5a12a3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinQuoteHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinQuoteHandler.kt @@ -29,8 +29,7 @@ class KotlinQuoteHandler : QuoteHandler { val start = iterator.start val end = iterator.end return end - start >= 1 && offset == end - 1 - } - else if (tokenType == KtTokens.CLOSING_QUOTE) { + } else if (tokenType == KtTokens.CLOSING_QUOTE) { return true } return false @@ -53,10 +52,10 @@ class KotlinQuoteHandler : QuoteHandler { override fun isInsideLiteral(iterator: HighlighterIterator): Boolean { val tokenType = iterator.tokenType return tokenType == KtTokens.REGULAR_STRING_PART || - tokenType == KtTokens.OPEN_QUOTE || - tokenType == KtTokens.CLOSING_QUOTE || - tokenType == KtTokens.SHORT_TEMPLATE_ENTRY_START || - tokenType == KtTokens.LONG_TEMPLATE_ENTRY_END || - tokenType == KtTokens.LONG_TEMPLATE_ENTRY_START + tokenType == KtTokens.OPEN_QUOTE || + tokenType == KtTokens.CLOSING_QUOTE || + tokenType == KtTokens.SHORT_TEMPLATE_ENTRY_START || + tokenType == KtTokens.LONG_TEMPLATE_ENTRY_END || + tokenType == KtTokens.LONG_TEMPLATE_ENTRY_START } } diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinSmartEnterHandler.kt b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinSmartEnterHandler.kt index 8e3b070a430..5fdf1135d2b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinSmartEnterHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinSmartEnterHandler.kt @@ -100,7 +100,8 @@ class KotlinSmartEnterHandler : SmartEnterProcessorWithFixers() { caretOffset = CharArrayUtil.shiftBackward(chars, caretOffset - 1, " \t") + 1 if (CharArrayUtil.regionMatches(chars, caretOffset - "{}".length, "{}") || - CharArrayUtil.regionMatches(chars, caretOffset - "{\n}".length, "{\n}")) { + CharArrayUtil.regionMatches(chars, caretOffset - "{\n}".length, "{\n}") + ) { commit(editor) val settings = CodeStyleSettingsManager.getSettings(file.project) val old = settings.KEEP_SIMPLE_BLOCKS_IN_ONE_LINE diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java index 1500514f191..26a9b20e435 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java @@ -150,7 +150,9 @@ public class KotlinTypedHandler extends TypedHandlerDelegate { if (KtTokens.COMMENTS.contains(tokenType) || tokenType == KtTokens.REGULAR_STRING_PART || tokenType == KtTokens.OPEN_QUOTE - || tokenType == KtTokens.CHARACTER_LITERAL) return; + || tokenType == KtTokens.CHARACTER_LITERAL) { + return; + } AutoPopupController.getInstance(project).autoPopupParameterInfo(editor, null); } @@ -187,7 +189,9 @@ public class KotlinTypedHandler extends TypedHandlerDelegate { if (!endsWith(chars, offset, "this@") && !endsWith(chars, offset, "return@") && !endsWith(chars, offset, "break@") - && !endsWith(chars, offset, "continue@")) return false; + && !endsWith(chars, offset, "continue@")) { + return false; + } PsiElement lastElement = file.findElementAt(offset - 1); if (lastElement == null) return false; @@ -233,7 +237,7 @@ public class KotlinTypedHandler extends TypedHandlerDelegate { int offset = editor.getCaretModel().getOffset(); PsiElement previousElement = file.findElementAt(offset - 1); if (previousElement instanceof LeafPsiElement - && ((LeafPsiElement) previousElement).getElementType() == KtTokens.LONG_TEMPLATE_ENTRY_START) { + && ((LeafPsiElement) previousElement).getElementType() == KtTokens.LONG_TEMPLATE_ENTRY_START) { editor.getDocument().insertString(offset, "}"); return Result.STOP; } @@ -253,7 +257,8 @@ public class KotlinTypedHandler extends TypedHandlerDelegate { if (autoIndentCase(editor, project, file, KtOperationReferenceExpression.class)) { return Result.STOP; } - } else if (c == '&') { + } + else if (c == '&') { if (autoIndentCase(editor, project, file, KtOperationReferenceExpression.class)) { return Result.STOP; } @@ -264,6 +269,7 @@ public class KotlinTypedHandler extends TypedHandlerDelegate { /** * Copied from + * * @see com.intellij.codeInsight.editorActions.TypedHandler#indentBrace(Project, Editor, char) */ private static void indentBrace(@NotNull final Project project, @NotNull final Editor editor, char braceChar) { diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinCatchParameterFixer.kt b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinCatchParameterFixer.kt index f2f50d840ba..53ed0db17bf 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinCatchParameterFixer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinCatchParameterFixer.kt @@ -37,8 +37,7 @@ class KotlinCatchParameterFixer : SmartEnterProcessorWithFixers.Fixer() { override fun apply(editor: Editor, processor: KotlinSmartEnterHandler, psiElement: PsiElement) { @@ -35,13 +35,11 @@ class KotlinDoWhileFixer : SmartEnterProcessorWithFixers.Fixer psiElement.startLine(doc)) { + } else if (whileKeyword != null && body !is KtBlockExpression && body.startLine(doc) > psiElement.startLine(doc)) { doc.insertString(whileKeyword.range.start, "}") doc.insertString(start + "do".length, "{") diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinForConditionFixer.kt b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinForConditionFixer.kt index 15c4f6e704a..02365e44926 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinForConditionFixer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinForConditionFixer.kt @@ -19,11 +19,12 @@ package org.jetbrains.kotlin.idea.editor.fixers import com.intellij.psi.PsiElement import org.jetbrains.kotlin.psi.KtForExpression -class KotlinForConditionFixer: MissingConditionFixer() { +class KotlinForConditionFixer : MissingConditionFixer() { override val keyword = "for" override fun getElement(element: PsiElement?) = element as? KtForExpression override fun getCondition(element: KtForExpression) = - element.loopRange ?: element.loopParameter ?: element.destructuringDeclaration + element.loopRange ?: element.loopParameter ?: element.destructuringDeclaration + override fun getLeftParenthesis(element: KtForExpression) = element.leftParenthesis override fun getRightParenthesis(element: KtForExpression) = element.rightParenthesis override fun getBody(element: KtForExpression) = element.body diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinFunctionParametersFixer.kt b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinFunctionParametersFixer.kt index 973ab40cbd5..d05578b5beb 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinFunctionParametersFixer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinFunctionParametersFixer.kt @@ -35,8 +35,7 @@ class KotlinFunctionParametersFixer : SmartEnterProcessorWithFixers.Fixer() { diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinPropertySetterParametersFixer.kt b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinPropertySetterParametersFixer.kt index ffc4c9d8b04..108c72c17cc 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinPropertySetterParametersFixer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinPropertySetterParametersFixer.kt @@ -46,17 +46,15 @@ class KotlinPropertySetterParametersFixer : SmartEnterProcessorWithFixers.Fixer< if (parameter?.text.isNullOrBlank()) { if (psiElement.rightParenthesis == null) { doc.insertString(parameterOffset, "value)") - } - else { + } else { doc.insertString(parameterOffset, "value") } - } - else if (psiElement.rightParenthesis == null) { + } else if (psiElement.rightParenthesis == null) { doc.insertString(parameterOffset + parameter!!.text.length, ")") } } - private val KtPropertyAccessor.leftParenthesis : ASTNode? + private val KtPropertyAccessor.leftParenthesis: ASTNode? get() = node.findChildByType(KtTokens.LPAR) } diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinWhenSubjectCaretFixer.kt b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinWhenSubjectCaretFixer.kt index 8a694d53bb7..ae8dd90398d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinWhenSubjectCaretFixer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinWhenSubjectCaretFixer.kt @@ -16,10 +16,10 @@ package org.jetbrains.kotlin.idea.editor.fixers -import com.intellij.psi.PsiElement import com.intellij.lang.SmartEnterProcessorWithFixers -import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler import com.intellij.openapi.editor.Editor +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler import org.jetbrains.kotlin.psi.KtWhenExpression class KotlinWhenSubjectCaretFixer : SmartEnterProcessorWithFixers.Fixer() { diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinWhileConditionFixer.kt b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinWhileConditionFixer.kt index 8d59d69bb56..48bc383935e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinWhileConditionFixer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinWhileConditionFixer.kt @@ -19,7 +19,7 @@ package org.jetbrains.kotlin.idea.editor.fixers import com.intellij.psi.PsiElement import org.jetbrains.kotlin.psi.KtWhileExpression -class KotlinWhileConditionFixer: MissingConditionFixer() { +class KotlinWhileConditionFixer : MissingConditionFixer() { override val keyword = "while" override fun getElement(element: PsiElement?) = element as? KtWhileExpression override fun getCondition(element: KtWhileExpression) = element.condition diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/MissingConditionFixer.kt b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/MissingConditionFixer.kt index 4fee2eb1d38..9450ed9f126 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/MissingConditionFixer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/MissingConditionFixer.kt @@ -21,7 +21,7 @@ import com.intellij.openapi.editor.Editor import com.intellij.psi.PsiElement import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler -abstract class MissingConditionFixer : SmartEnterProcessorWithFixers.Fixer() { +abstract class MissingConditionFixer : SmartEnterProcessorWithFixers.Fixer() { override fun apply(editor: Editor, processor: KotlinSmartEnterHandler, element: PsiElement) { val workElement = getElement(element) ?: return @@ -42,12 +42,10 @@ abstract class MissingConditionFixer : SmartEnterProcessorWithFix doc.replaceString(workElement.range.start, stopOffset, "$keyword ()") processor.registerUnresolvedError(workElement.range.start + "$keyword (".length) - } - else { + } else { processor.registerUnresolvedError(lParen.range.end) } - } - else { + } else { if (rParen == null) { doc.insertString(condition.range.end, ")") } diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/fixersUtil.kt b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/fixersUtil.kt index e780a41e4bf..307633a71ba 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/fixersUtil.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/fixersUtil.kt @@ -16,9 +16,9 @@ package org.jetbrains.kotlin.idea.editor.fixers -import com.intellij.psi.PsiElement -import com.intellij.openapi.util.TextRange import com.intellij.openapi.editor.Document +import com.intellij.openapi.util.TextRange +import com.intellij.psi.PsiElement val PsiElement.range: TextRange get() = textRange!! val TextRange.start: Int get() = startOffset diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinCallExpressionWithLambdaSelectioner.kt b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinCallExpressionWithLambdaSelectioner.kt index 0c139ba5dfe..ebf4abc56ef 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinCallExpressionWithLambdaSelectioner.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinCallExpressionWithLambdaSelectioner.kt @@ -26,8 +26,7 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset class KotlinCallExpressionWithLambdaSelectioner : ExtendWordSelectionHandlerBase() { - override fun canSelect(e: PsiElement): Boolean - = e is KtCallExpression && e.hasLambda() + override fun canSelect(e: PsiElement): Boolean = e is KtCallExpression && e.hasLambda() override fun select(e: PsiElement, editorText: CharSequence, cursorOffset: Int, editor: Editor): List? { if (e !is KtCallExpression) return null @@ -36,7 +35,6 @@ class KotlinCallExpressionWithLambdaSelectioner : ExtendWordSelectionHandlerBase return listOf(TextRange(e.startOffset, endOffset)) } - private fun KtCallExpression.hasLambda(): Boolean - = lambdaArguments.isNotEmpty() + private fun KtCallExpression.hasLambda(): Boolean = lambdaArguments.isNotEmpty() } \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinDeclarationSelectioner.kt b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinDeclarationSelectioner.kt index f1615c79f5f..158ef158abd 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinDeclarationSelectioner.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinDeclarationSelectioner.kt @@ -30,8 +30,7 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset import java.util.* class KotlinDeclarationSelectioner : ExtendWordSelectionHandlerBase() { - override fun canSelect(e: PsiElement) - = e is KtDeclaration + override fun canSelect(e: PsiElement) = e is KtDeclaration override fun select(e: PsiElement, editorText: CharSequence, cursorOffset: Int, editor: Editor): List? { if (e is KtDestructuringDeclaration) { @@ -42,27 +41,29 @@ class KotlinDeclarationSelectioner : ExtendWordSelectionHandlerBase() { val firstChild = e.firstChild val firstComment = firstChild - .siblings(forward = true, withItself = true) - .firstOrNull { it is PsiComment } + .siblings(forward = true, withItself = true) + .firstOrNull { it is PsiComment } val firstNonComment = firstChild - .siblings(forward = true, withItself = true) - .first { it !is PsiComment && it !is PsiWhiteSpace } + .siblings(forward = true, withItself = true) + .first { it !is PsiComment && it !is PsiWhiteSpace } val lastChild = e.lastChild val lastComment = lastChild - .siblings(forward = false, withItself = true) - .firstOrNull { it is PsiComment } + .siblings(forward = false, withItself = true) + .firstOrNull { it is PsiComment } val lastNonComment = lastChild - .siblings(forward = false, withItself = true) - .first { it !is PsiComment && it !is PsiWhiteSpace } + .siblings(forward = false, withItself = true) + .first { it !is PsiComment && it !is PsiWhiteSpace } if (firstComment != null || lastComment != null) { val startOffset = minOf( - firstComment?.startOffset ?: Int.MAX_VALUE, - lastNonComment.startOffset) + firstComment?.startOffset ?: Int.MAX_VALUE, + lastNonComment.startOffset + ) val endOffset = maxOf( - lastComment?.endOffset ?: Int.MIN_VALUE, - lastNonComment.endOffset) + lastComment?.endOffset ?: Int.MIN_VALUE, + lastNonComment.endOffset + ) result.addRange(editorText, TextRange(startOffset, endOffset)) } diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinDocCommentSelectioner.kt b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinDocCommentSelectioner.kt index a4241f7c4eb..671fd6170ce 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinDocCommentSelectioner.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinDocCommentSelectioner.kt @@ -16,16 +16,14 @@ package org.jetbrains.kotlin.idea.editor.wordSelection +import com.intellij.codeInsight.editorActions.ExtendWordSelectionHandlerBase import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiElement - -import com.intellij.codeInsight.editorActions.ExtendWordSelectionHandlerBase import org.jetbrains.kotlin.kdoc.psi.api.KDoc class KotlinDocCommentSelectioner : ExtendWordSelectionHandlerBase() { - override fun canSelect(e: PsiElement) - = e is KDoc + override fun canSelect(e: PsiElement) = e is KDoc override fun select(e: PsiElement, editorText: CharSequence, cursorOffset: Int, editor: Editor): List? { return ExtendWordSelectionHandlerBase.expandToWholeLine(editorText, e.textRange) diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinListSelectioner.kt b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinListSelectioner.kt index 6de0a1c8102..5a7d07e4035 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinListSelectioner.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinListSelectioner.kt @@ -16,21 +16,21 @@ package org.jetbrains.kotlin.idea.editor.wordSelection +import com.intellij.codeInsight.editorActions.ExtendWordSelectionHandlerBase import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiElement import com.intellij.psi.tree.TokenSet +import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.KtParameterList import org.jetbrains.kotlin.psi.KtTypeArgumentList import org.jetbrains.kotlin.psi.KtTypeParameterList import org.jetbrains.kotlin.psi.KtValueArgumentList -import org.jetbrains.kotlin.lexer.KtTokens -import com.intellij.codeInsight.editorActions.ExtendWordSelectionHandlerBase class KotlinListSelectioner : ExtendWordSelectionHandlerBase() { companion object { - fun canSelect(e: PsiElement) - = e is KtParameterList || e is KtValueArgumentList || e is KtTypeParameterList || e is KtTypeArgumentList + fun canSelect(e: PsiElement) = + e is KtParameterList || e is KtValueArgumentList || e is KtTypeParameterList || e is KtTypeArgumentList } override fun canSelect(e: PsiElement) = KotlinListSelectioner.canSelect(e) @@ -41,8 +41,10 @@ class KotlinListSelectioner : ExtendWordSelectionHandlerBase() { val endNode = node.findChildByType(TokenSet.create(KtTokens.RPAR, KtTokens.GT)) ?: return null val innerRange = TextRange(startNode.startOffset + 1, endNode.startOffset) if (e is KtTypeArgumentList || e is KtTypeParameterList) { - return listOf(innerRange, - TextRange(startNode.startOffset, endNode.startOffset + endNode.textLength)) + return listOf( + innerRange, + TextRange(startNode.startOffset, endNode.startOffset + endNode.textLength) + ) } return listOf(innerRange) } diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinStatementGroupSelectioner.kt b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinStatementGroupSelectioner.kt index c8dace7d6bc..7dd6f60d724 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinStatementGroupSelectioner.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinStatementGroupSelectioner.kt @@ -16,17 +16,16 @@ package org.jetbrains.kotlin.idea.editor.wordSelection +import com.intellij.codeInsight.editorActions.ExtendWordSelectionHandlerBase import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange +import com.intellij.psi.PsiComment import com.intellij.psi.PsiElement import com.intellij.psi.PsiWhiteSpace import com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.lexer.KtTokens - -import com.intellij.codeInsight.editorActions.ExtendWordSelectionHandlerBase -import org.jetbrains.kotlin.psi.psiUtil.siblings -import com.intellij.psi.PsiComment import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.siblings /** * Originally from IDEA platform: StatementGroupSelectioner @@ -42,23 +41,31 @@ class KotlinStatementGroupSelectioner : ExtendWordSelectionHandlerBase() { val parent = e.parent val startElement = e.siblings(forward = false, withItself = false) - .firstOrNull { // find preceding '{' or blank line - it is LeafPsiElement && it.elementType == KtTokens.LBRACE || - it is PsiWhiteSpace && it.getText()!!.count { it == '\n' } > 1 - } - ?.siblings(forward = true, withItself = false) - ?.dropWhile { it is PsiWhiteSpace } // and take first non-whitespace element after it - ?.firstOrNull() ?: parent.firstChild!! + .firstOrNull { + // find preceding '{' or blank line + it is LeafPsiElement && it.elementType == KtTokens.LBRACE || + it is PsiWhiteSpace && it.getText()!!.count { it == '\n' } > 1 + } + ?.siblings(forward = true, withItself = false) + ?.dropWhile { it is PsiWhiteSpace } // and take first non-whitespace element after it + ?.firstOrNull() ?: parent.firstChild!! val endElement = e.siblings(forward = true, withItself = false) - .firstOrNull { // find next '}' or blank line - it is LeafPsiElement && it.elementType == KtTokens.RBRACE || - it is PsiWhiteSpace && it.getText()!!.count { it == '\n' } > 1 - } - ?.siblings(forward = false, withItself = false) - ?.dropWhile { it is PsiWhiteSpace } // and take first non-whitespace element before it - ?.firstOrNull() ?: parent.lastChild!! + .firstOrNull { + // find next '}' or blank line + it is LeafPsiElement && it.elementType == KtTokens.RBRACE || + it is PsiWhiteSpace && it.getText()!!.count { it == '\n' } > 1 + } + ?.siblings(forward = false, withItself = false) + ?.dropWhile { it is PsiWhiteSpace } // and take first non-whitespace element before it + ?.firstOrNull() ?: parent.lastChild!! - return ExtendWordSelectionHandlerBase.expandToWholeLine(editorText, TextRange(startElement.textRange!!.startOffset, endElement.textRange!!.endOffset)) + return ExtendWordSelectionHandlerBase.expandToWholeLine( + editorText, + TextRange( + startElement.textRange!!.startOffset, + endElement.textRange!!.endOffset + ) + ) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinStringLiteralSelectioner.kt b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinStringLiteralSelectioner.kt index e9eed759d78..ae0af92bfc1 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinStringLiteralSelectioner.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinStringLiteralSelectioner.kt @@ -17,9 +17,9 @@ package org.jetbrains.kotlin.idea.editor.wordSelection import com.intellij.codeInsight.editorActions.ExtendWordSelectionHandlerBase -import com.intellij.psi.PsiElement import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange +import com.intellij.psi.PsiElement import org.jetbrains.kotlin.psi.KtStringTemplateExpression import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.startOffset diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinTypeSelectioner.kt b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinTypeSelectioner.kt index 3f526c2472c..da6e95b822e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinTypeSelectioner.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinTypeSelectioner.kt @@ -32,8 +32,8 @@ class KotlinTypeSelectioner : ExtendWordSelectionHandlerBase() { override fun canSelect(e: PsiElement): Boolean { return e is KtTypeReference - && e.getStrictParentOfType() == null - && e.getStrictParentOfType() == null + && e.getStrictParentOfType() == null + && e.getStrictParentOfType() == null } override fun select(e: PsiElement, editorText: CharSequence, cursorOffset: Int, editor: Editor): List? { diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinWordSelectionFilter.kt b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinWordSelectionFilter.kt index 2d92647d05b..bf57e61fd9e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinWordSelectionFilter.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinWordSelectionFilter.kt @@ -18,13 +18,13 @@ package org.jetbrains.kotlin.idea.editor.wordSelection import com.intellij.openapi.util.Condition import com.intellij.psi.PsiElement -import org.jetbrains.kotlin.KtNodeTypes.* -import org.jetbrains.kotlin.psi.KtContainerNode +import org.jetbrains.kotlin.KtNodeTypes.BLOCK import org.jetbrains.kotlin.idea.KotlinLanguage import org.jetbrains.kotlin.kdoc.parser.KDocElementTypes import org.jetbrains.kotlin.lexer.KtTokens +import org.jetbrains.kotlin.psi.KtContainerNode -class KotlinWordSelectionFilter : Condition{ +class KotlinWordSelectionFilter : Condition { override fun value(e: PsiElement): Boolean { if (e.language != KotlinLanguage.INSTANCE) return true