Skip to content

Commit

Permalink
Fixing "Automatic import placed after class Doc comment." (#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
m0rkeulv committed Aug 19, 2023
1 parent 3fcfe64 commit a84f06b
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.4.13
* Added: Quick fix for incorrect extends and implements (#940)
* Fixed: Resolving type from inline method calls (#868)
* Fixed: Automatic import was placed after class Doc comment (#842)

## 1.4.12
* Bugfix: "import class quickfix" missing in some cases (#1132)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,44 @@
import com.intellij.plugins.haxe.lang.psi.HaxePackageStatement;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiParserFacade;
import lombok.CustomLog;

import static com.intellij.plugins.haxe.util.UsefulPsiTreeUtil.isWhitespaceOrCommentButNotDocs;

/**
* @author: Fedor.Korotkov
*/
@CustomLog
public class HaxeAddImportHelper {
public static HaxeImportStatement addImport(String path, PsiFile file) {
int positionIndex = 0;
final PsiElement[] children = file.getChildren();

while ((children[positionIndex] instanceof HaxePackageStatement ||
UsefulPsiTreeUtil.isWhitespaceOrComment(children[positionIndex]) &&
positionIndex < children.length)) {
++positionIndex;
PsiElement child = children[positionIndex];
// find HaxePackageStatement position
while (!(child instanceof HaxePackageStatement)) {
if (++positionIndex < children.length) {
child = children[positionIndex];
}else {
log.warn("Unable to insert Import statement");
return null;
}
}
// find last whitespace line before docs or code and after last import
while (child instanceof HaxePackageStatement
|| child instanceof HaxeImportStatement
|| isWhitespaceOrCommentButNotDocs(child)) {
if (++positionIndex < children.length) {
child = children[positionIndex];
}else {
log.warn("Unable to insert Import statement");
return null;
}
}

assert positionIndex < children.length;
return insertImportBefore(path, file, children[positionIndex]);
assert child != null;
return insertImportBefore(path, file, child);
}

private static HaxeImportStatement insertImportBefore(String path, PsiFile file, PsiElement child) {
Expand All @@ -48,6 +69,8 @@ private static HaxeImportStatement insertImportBefore(String path, PsiFile file,
return null;
}

final PsiElement newLineElement = PsiParserFacade.getInstance(file.getProject()).createWhiteSpaceFromText("\n");
file.addBefore(newLineElement, child);
file.addBefore(importStatement, child);
return importStatement;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.plugins.haxe.HaxeFileType;
import com.intellij.plugins.haxe.lang.lexer.HaxeElementType;
import com.intellij.plugins.haxe.lang.lexer.HaxeTokenTypeSets;
import com.intellij.plugins.haxe.lang.psi.HaxeClass;
import com.intellij.plugins.haxe.lang.psi.HaxeParenthesizedExpression;
import com.intellij.plugins.haxe.lang.psi.HaxePsiCompositeElement;
import com.intellij.plugins.haxe.lang.psi.impl.HaxePsiDocComment;
import com.intellij.plugins.haxe.lang.util.HaxeAstUtil;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
Expand Down Expand Up @@ -79,7 +82,9 @@ public boolean value(PsiElement element) {
}

@Nullable
public static PsiElement getPrevSiblingSkippingCondition(@Nullable PsiElement sibling, Condition<PsiElement> condition, boolean strictly) {
public static PsiElement getPrevSiblingSkippingCondition(@Nullable PsiElement sibling,
Condition<PsiElement> condition,
boolean strictly) {
if (sibling == null) return null;
PsiElement result = strictly ? sibling.getPrevSibling() : sibling;
while (result != null && condition.value(result)) {
Expand All @@ -89,7 +94,9 @@ public static PsiElement getPrevSiblingSkippingCondition(@Nullable PsiElement si
}

@Nullable
public static PsiElement getNextSiblingSkippingCondition(@Nullable PsiElement sibling, Condition<PsiElement> condition, boolean strictly) {
public static PsiElement getNextSiblingSkippingCondition(@Nullable PsiElement sibling,
Condition<PsiElement> condition,
boolean strictly) {
if (sibling == null) return null;
PsiElement result = strictly ? sibling.getNextSibling() : sibling;
while (result != null && condition.value(result)) {
Expand Down Expand Up @@ -143,6 +150,10 @@ public static boolean isWhitespaceOrComment(ASTNode node) {
public static boolean isWhitespaceOrComment(PsiElement element) {
return element instanceof PsiWhiteSpace || element instanceof PsiComment;
}
public static boolean isWhitespaceOrCommentButNotDocs(PsiElement element) {
return element instanceof PsiWhiteSpace
|| (element instanceof PsiComment comment && comment.getTokenType() != HaxeTokenTypeSets.DOC_COMMENT);
}

private static void populateClassesList(List<HaxeClass> classList, Project project, VirtualFile file) {
VirtualFile[] files = file.getChildren();
Expand Down Expand Up @@ -170,8 +181,9 @@ public static List<HaxeClass> getClassesInDirectory(Project project, VirtualFile

@Nullable
public static <T> T getParentOfType(@Nullable PsiElement element, @NotNull Class<? extends T> aClass) {
if (element == null || element instanceof PsiFile)
if (element == null || element instanceof PsiFile) {
return null;
}

// Don't start with the passed element.
element = element.getParent();
Expand All @@ -187,8 +199,9 @@ public static <T> T getParentOfType(@Nullable PsiElement element, @NotNull Class

@Nullable
public static PsiElement getParent(@Nullable PsiElement element, @NotNull IElementType elementType) {
if (element == null || element instanceof PsiFile)
if (element == null || element instanceof PsiFile) {
return null;
}

element = element.getParent();
while (element != null && !(element instanceof PsiFile)) {
Expand Down Expand Up @@ -288,7 +301,7 @@ static public <T extends PsiElement> T getLastChild(PsiElement element, Class<T>

static public PsiElement getToken(PsiElement element, String token) {
for (ASTNode node : element.getNode().getChildren(null)) {
if (node.getText().equals(token)) return node.getPsi();
if (node.getText().equals(token)) return node.getPsi();
}
return null;
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/testData/addImportIntention/Helper.hx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package;

/**
helper class docs
**/
class Helper {
var bar:<caret>Baz;
}
5 changes: 5 additions & 0 deletions src/test/resources/testData/addImportIntention/Helper.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package;
import foo.Bar.Baz;

/**
helper class docs
**/
class Helper {
var bar:Baz;
}
2 changes: 2 additions & 0 deletions src/test/resources/testData/addImportIntention/Module.hx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package;

class Module {
var t:Ty<caret>pe1;
}
2 changes: 2 additions & 0 deletions src/test/resources/testData/addImportIntention/Module.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package;

import foo.Types.Type1;
class Module {
var t:Type1;
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/testData/addImportIntention/Simple.hx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package;

class Simple {
var bar:<caret>Bar;
}
2 changes: 2 additions & 0 deletions src/test/resources/testData/addImportIntention/Simple.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package;

import foo.Bar;
class Simple {
var bar:Bar;
Expand Down
1 change: 0 additions & 1 deletion src/test/resources/testData/move/moveFile1/after/Test.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ;

import bar.ArrayUtils;

class Test {
static function main() {
function is_c(val) { return val == "c"; }
Expand Down

0 comments on commit a84f06b

Please sign in to comment.