diff options
author | Beth Cutler <bcutler@google.com> | 2022-02-23 11:22:38 -0800 |
---|---|---|
committer | Facebook GitHub Bot <facebook-github-bot@users.noreply.github.com> | 2022-02-23 11:27:42 -0800 |
commit | b3cf13a7307fe568b45f0dca39b7ffe8b600de88 (patch) | |
tree | 4e947c425ca8b1f4db557494c571b33580818762 /core/src/main/java/com/facebook | |
parent | 93c7dd96c1e64363d964c036adf84fd6890f9ba4 (diff) | |
download | ktfmt-b3cf13a7307fe568b45f0dca39b7ffe8b600de88.tar.gz |
#291: move comments between imports above import list (#292)
Summary:
Fix for https://github.com/facebookincubator/ktfmt/issues/291
Pull Request resolved: https://github.com/facebookincubator/ktfmt/pull/292
Reviewed By: cgrushko
Differential Revision: D34424976
Pulled By: strulovich
fbshipit-source-id: 199b6d6644c4e51b08ed06fd3ab052f3b9339ba5
Diffstat (limited to 'core/src/main/java/com/facebook')
-rw-r--r-- | core/src/main/java/com/facebook/ktfmt/format/Formatter.kt | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/core/src/main/java/com/facebook/ktfmt/format/Formatter.kt b/core/src/main/java/com/facebook/ktfmt/format/Formatter.kt index d50c784..3bf2e2a 100644 --- a/core/src/main/java/com/facebook/ktfmt/format/Formatter.kt +++ b/core/src/main/java/com/facebook/ktfmt/format/Formatter.kt @@ -33,6 +33,7 @@ import com.google.googlejavaformat.java.FormatterException import com.google.googlejavaformat.java.JavaOutput import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtil import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtilRt +import org.jetbrains.kotlin.com.intellij.psi.PsiComment import org.jetbrains.kotlin.com.intellij.psi.PsiElement import org.jetbrains.kotlin.com.intellij.psi.PsiElementVisitor import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace @@ -144,22 +145,19 @@ object Formatter { return code } - fun findNonImportElement(): PsiElement? { - var element = importList.firstChild - while (element != null) { - if (element !is KtImportDirective && element !is PsiWhiteSpace) { - return element - } - element = element.nextSibling + val commentList = mutableListOf<PsiElement>() + // Find non-import elements; comments are moved, in order, to the top of the import list. Other + // non-import elements throw a ParseError. + var element = importList.firstChild + while (element != null) { + if (element is PsiComment) { + commentList.add(element) + } else if (element !is KtImportDirective && element !is PsiWhiteSpace) { + throw ParseError( + "Imports not contiguous: " + element.text, + StringUtil.offsetToLineColumn(code, element.startOffset)) } - return null - } - - val nonImportElement = findNonImportElement() - if (nonImportElement != null) { - throw ParseError( - "Imports not contiguous (perhaps a comment separates them?): " + nonImportElement.text, - StringUtil.offsetToLineColumn(code, nonImportElement.startOffset)) + element = element.nextSibling } fun canonicalText(importDirective: KtImportDirective) = importDirective.importedFqName?.asString() + @@ -169,10 +167,11 @@ object Formatter { if (importDirective.isAllUnder) "*" else "" val sortedImports = importList.imports.sortedBy(::canonicalText).distinctBy(::canonicalText) + val importsWithComments = commentList + sortedImports return code.replaceRange( importList.startOffset, importList.endOffset, - sortedImports.joinToString(separator = "\n") { imprt -> imprt.text } + "\n") + importsWithComments.joinToString(separator = "\n") { imprt -> imprt.text } + "\n") } } |