Skip to content

Commit

Permalink
Update version to 0.1.8; improve Fold
Browse files Browse the repository at this point in the history
  • Loading branch information
Saigut committed Jun 8, 2022
1 parent 8c1d42a commit ca9b6a0
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 19 deletions.
13 changes: 3 additions & 10 deletions src/main/fold/SchemeFoldingBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import com.intellij.lang.folding.FoldingBuilder;
import com.intellij.lang.folding.FoldingDescriptor;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import main.psi.impl.SchemeFormDefineBase;
import main.psi.util.SchemePsiUtil;
import main.psi.impl.SchemeBodyOfForm;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
Expand Down Expand Up @@ -37,12 +35,7 @@ public boolean isCollapsedByDefault(ASTNode node)
private void appendDescriptors(ASTNode node, List<FoldingDescriptor> descriptors)
{
if (isFoldableNode(node)) {
ASTNode bodyNode = SchemePsiUtil.getNodeNormalChildAt(node, 2);
if (bodyNode != null) {
descriptors.add(new FoldingDescriptor(bodyNode,
new TextRange(bodyNode.getTextRange().getStartOffset(),
node.getLastChildNode().getTreePrev().getTextRange().getEndOffset())));
}
descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
}
ASTNode child = node.getFirstChildNode();
while (child != null)
Expand All @@ -55,6 +48,6 @@ private void appendDescriptors(ASTNode node, List<FoldingDescriptor> descriptors
private boolean isFoldableNode(ASTNode node)
{
PsiElement element = node.getPsi();
return element instanceof SchemeFormDefineBase;
return element instanceof SchemeBodyOfForm;
}
}
1 change: 1 addition & 0 deletions src/main/parser/AST.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public interface AST
IElementType AST_ELE_DATUM_COMMENT = new SchemeElementType("ast datum comment");

// Forms
IElementType AST_BODY_OF_FORM = new SchemeElementType("ast body of form");
IElementType AST_FORM_DEFINE = new SchemeElementType("ast define");
IElementType AST_FORM_DEFINE_RECORD_TYPE = new SchemeElementType("ast define-record-type");
IElementType AST_FORM_DEFINE_SYNTAX = new SchemeElementType("ast define-syntax");
Expand Down
95 changes: 86 additions & 9 deletions src/main/parser/SchemeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,46 @@ IElementType eatRemainList(PsiBuilder builder, IElementType close, IElementType
return mark_type;
}

// ret: true, have body; false, have no body.
boolean helperAdvanceForBody(PsiBuilder builder, IElementType close, int i)
{
int j = 0;
IElementType token_type = builder.getTokenType();
while (j < i && token_type != close && token_type != null)
{
parseSexp(token_type, builder);
token_type = builder.getTokenType();
j++;
}
return (token_type != null) && (token_type != close);
}

void helperEatFormBody(PsiBuilder builder, IElementType close,
PsiBuilder.Marker bodyMarker)
{
IElementType token_type = builder.getTokenType();
while (token_type != close && token_type != null)
{
parseSexp(token_type, builder);
token_type = builder.getTokenType();
}
bodyMarker.done(AST.AST_BODY_OF_FORM);
}

IElementType parseFormLet(PsiBuilder builder, IElementType close)
{
return eatRemainList(builder, close, AST.AST_FORM_LET);
boolean haveBody;
IElementType form_mark_type = AST.AST_FORM_LET;
if (builder.getTokenType() == NAME_LITERAL) {
haveBody = helperAdvanceForBody(builder, close, 2);
} else {
haveBody = helperAdvanceForBody(builder, close, 1);
}
if (haveBody) {
PsiBuilder.Marker bodyMarker = builder.mark();
helperEatFormBody(builder, close, bodyMarker);
}
return eatRemainList(builder, close, form_mark_type);
}

IElementType parseFormSet(PsiBuilder builder, IElementType close)
Expand All @@ -164,7 +201,12 @@ IElementType parseFormSet(PsiBuilder builder, IElementType close)

IElementType parseFormLambda(PsiBuilder builder, IElementType close)
{
return eatRemainList(builder, close, AST.AST_FORM_PROCEDURE);
IElementType form_mark_type = AST.AST_FORM_PROCEDURE;
if (helperAdvanceForBody(builder, close, 1)) {
PsiBuilder.Marker bodyMarker = builder.mark();
helperEatFormBody(builder, close, bodyMarker);
}
return eatRemainList(builder, close, form_mark_type);
}

IElementType parseFormCons(PsiBuilder builder, IElementType close)
Expand All @@ -174,27 +216,52 @@ IElementType parseFormCons(PsiBuilder builder, IElementType close)

IElementType parseFormDefine(PsiBuilder builder, IElementType close)
{
return eatRemainList(builder, close, AST.AST_FORM_DEFINE);
IElementType form_mark_type = AST.AST_FORM_DEFINE;
if (helperAdvanceForBody(builder, close, 1)) {
PsiBuilder.Marker bodyMarker = builder.mark();
helperEatFormBody(builder, close, bodyMarker);
}
return eatRemainList(builder, close, form_mark_type);
}

IElementType parseFormDefineRecordType(PsiBuilder builder, IElementType close)
{
return eatRemainList(builder, close, AST.AST_FORM_DEFINE_RECORD_TYPE);
IElementType form_mark_type = AST.AST_FORM_DEFINE_RECORD_TYPE;
if (helperAdvanceForBody(builder, close, 1)) {
PsiBuilder.Marker bodyMarker = builder.mark();
helperEatFormBody(builder, close, bodyMarker);
}
return eatRemainList(builder, close, form_mark_type);
}

IElementType parseFormDefineSyntax(PsiBuilder builder, IElementType close)
{
return eatRemainList(builder, close, AST.AST_FORM_DEFINE_SYNTAX);
IElementType form_mark_type = AST.AST_FORM_DEFINE_SYNTAX;
if (helperAdvanceForBody(builder, close, 1)) {
PsiBuilder.Marker bodyMarker = builder.mark();
helperEatFormBody(builder, close, bodyMarker);
}
return eatRemainList(builder, close, form_mark_type);
}

IElementType parseFormDo(PsiBuilder builder, IElementType close)
{
return eatRemainList(builder, close, AST.AST_FORM_DO);
IElementType form_mark_type = AST.AST_FORM_DO;
if (helperAdvanceForBody(builder, close, 2)) {
PsiBuilder.Marker bodyMarker = builder.mark();
helperEatFormBody(builder, close, bodyMarker);
}
return eatRemainList(builder, close, form_mark_type);
}

IElementType parseFormList(PsiBuilder builder, IElementType close)
{
return eatRemainList(builder, close, AST.AST_FORM_LIST);
IElementType form_mark_type = AST.AST_FORM_LIST;
if (helperAdvanceForBody(builder, close, 0)) {
PsiBuilder.Marker bodyMarker = builder.mark();
helperEatFormBody(builder, close, bodyMarker);
}
return eatRemainList(builder, close, form_mark_type);
}

IElementType parseFormLibrary(PsiBuilder builder, IElementType close)
Expand All @@ -214,12 +281,22 @@ IElementType parseFormCdr(PsiBuilder builder, IElementType close)

IElementType parseFormBegin(PsiBuilder builder, IElementType close)
{
return eatRemainList(builder, close, AST.AST_FORM_BEGIN);
IElementType form_mark_type = AST.AST_FORM_BEGIN;
if (helperAdvanceForBody(builder, close, 0)) {
PsiBuilder.Marker bodyMarker = builder.mark();
helperEatFormBody(builder, close, bodyMarker);
}
return eatRemainList(builder, close, form_mark_type);
}

IElementType parseFormIf(PsiBuilder builder, IElementType close)
{
return eatRemainList(builder, close, AST.AST_FORM_IF);
IElementType form_mark_type = AST.AST_FORM_IF;
if (helperAdvanceForBody(builder, close, 1)) {
PsiBuilder.Marker bodyMarker = builder.mark();
helperEatFormBody(builder, close, bodyMarker);
}
return eatRemainList(builder, close, form_mark_type);
}

IElementType parseFormCond(PsiBuilder builder, IElementType close)
Expand Down
4 changes: 4 additions & 0 deletions src/main/parser/SchemePsiCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ else if (elementType == AST.AST_BASIC_ELE_SYMBOL)
{
return new SchemeSymbol(node);
}
else if (elementType == AST.AST_BODY_OF_FORM)
{
return new SchemeBodyOfForm(node);
}
else if (elementType == AST.AST_FORM_AND)
{
return new SchemeFormAnd(node);
Expand Down
11 changes: 11 additions & 0 deletions src/main/psi/impl/SchemeBodyOfForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main.psi.impl;

import com.intellij.lang.ASTNode;

public class SchemeBodyOfForm extends SchemePsiElementBase
{
public SchemeBodyOfForm(ASTNode node)
{
super(node, "SchemeFormBody");
}
}

0 comments on commit ca9b6a0

Please sign in to comment.