Skip to content

Commit

Permalink
adding minimum lines after package, import and using
Browse files Browse the repository at this point in the history
  • Loading branch information
m0rkeulv committed Feb 10, 2024
1 parent 716a427 commit 27f1155
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import lombok.CustomLog;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.List;

import static com.intellij.plugins.haxe.lang.lexer.HaxeTokenTypeSets.*;
Expand Down Expand Up @@ -121,6 +123,7 @@ public Spacing getSpacingInternal(Block child1, Block child2) {

final IElementType elementType = myNode.getElementType();
final IElementType parentType = myNode.getTreeParent() == null ? null : myNode.getTreeParent().getElementType();
final IElementType typeNext = getNextElementType();
final ASTNode node1 = ((AbstractBlock)child1).getNode();
final IElementType type1 = node1.getElementType();
final ASTNode node2 = ((AbstractBlock)child2).getNode();
Expand All @@ -142,10 +145,22 @@ public Spacing getSpacingInternal(Block child1, Block child2) {
}
}

if (type1 == IMPORT_STATEMENT ||
type1 == PACKAGE_STATEMENT ||
type1 == USING_STATEMENT) {
return addSingleSpaceIf(false, true);
//if (
// type1 == IMPORT_STATEMENT ||
// //type1 == PACKAGE_STATEMENT ||
// type1 == USING_STATEMENT) {
// return addSingleSpaceIf(false, true);
//}

if (type1.equals(PACKAGE_STATEMENT)) {
return Spacing.createSpacing(0, 0, mySettings.BLANK_LINES_AFTER_PACKAGE, true, mySettings.KEEP_BLANK_LINES_IN_CODE);
}

if (type1 == IMPORT_STATEMENT && type2 != IMPORT_STATEMENT ) {
return Spacing.createSpacing(0, 0, mySettings.BLANK_LINES_AFTER_IMPORTS, true, mySettings.KEEP_BLANK_LINES_IN_CODE);
}
if (type1 == USING_STATEMENT && type2 != USING_STATEMENT ) {
return Spacing.createSpacing(0, 0, myHaxeCodeStyleSettings.MINIMUM_BLANK_LINES_AFTER_USING, true, mySettings.KEEP_BLANK_LINES_IN_CODE);
}

if (elementType.equals(IMPORT_WILDCARD)) {
Expand Down Expand Up @@ -444,6 +459,26 @@ else if (typeType1 == OCOLON) {
return Spacing.createSpacing(0, 1, 0, true, mySettings.KEEP_BLANK_LINES_IN_CODE);
}

@Nullable
private IElementType getNextElementType() {
if (myNode.getTreeParent() == null) return null;

ASTNode parent = myNode.getTreeParent();
ASTNode[] parentChildren = parent.getChildren(null);
List<ASTNode> list = Arrays.asList(parentChildren);
int myNodeIndex = list.indexOf(myNode);
for (int i = myNodeIndex+1; i < list.size(); i++) {
ASTNode node = list.get(i);
IElementType type = node.getElementType();
if (!WHITESPACES.contains(type)) {
return type;
}
}


return null;
}

private Spacing addSingleSpaceIf(boolean condition) {
return addSingleSpaceIf(condition, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class HaxeCodeStyleSettings extends CustomCodeStyleSettings {
public boolean SPACE_BEFORE_TYPE_REFERENCE_COLON = false;
public boolean SPACE_AFTER_TYPE_REFERENCE_COLON = false;

public int MINIMUM_BLANK_LINES_AFTER_USING = 2;

protected HaxeCodeStyleSettings(CodeStyleSettings container) {
super("HaxeCodeStyleSettings", container);
}
Expand Down
Loading

0 comments on commit 27f1155

Please sign in to comment.