Re-parse after lambda was converted to block (KT-17156)

#KT-17156 Fixed
This commit is contained in:
Nikolay Krasko
2017-03-30 19:45:48 +03:00
parent c740c0b177
commit 6201aa4fd9
2 changed files with 122 additions and 1 deletions
@@ -22,9 +22,12 @@ import com.intellij.lang.PsiBuilder;
import com.intellij.lang.PsiBuilderFactory;
import com.intellij.lexer.Lexer;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.IErrorCounterReparseableElementType;
import com.intellij.psi.tree.IFileElementType;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.idea.KotlinLanguage;
import org.jetbrains.kotlin.lexer.KotlinLexer;
import org.jetbrains.kotlin.lexer.KtTokens;
@@ -130,7 +133,8 @@ public interface KtNodeTypes {
@Override
public ASTNode parseContents(ASTNode chameleon) {
Project project = chameleon.getPsi().getProject();
PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(project, chameleon, null, KotlinLanguage.INSTANCE, chameleon.getChars());
PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(
project, chameleon, null, KotlinLanguage.INSTANCE, chameleon.getChars());
return KotlinParser.parseLambdaExpression(builder).getFirstChildNode();
}
@@ -139,6 +143,66 @@ public interface KtNodeTypes {
return new KtLambdaExpression(text);
}
@Override
public boolean isParsable(@Nullable ASTNode parent, CharSequence buffer, Language fileLanguage, Project project) {
return super.isParsable(parent, buffer, fileLanguage, project) &&
!wasArrowMovedOrDeleted(parent, buffer);
}
private boolean wasArrowMovedOrDeleted(@Nullable ASTNode parent, CharSequence buffer) {
if (parent == null) return false;
PsiElement parentPsi = parent.getPsi();
KtLambdaExpression[] lambdaExpressions = PsiTreeUtil.getChildrenOfType(parentPsi, KtLambdaExpression.class);
if (lambdaExpressions == null || lambdaExpressions.length != 1) return false;
// Now works only when actual node can be spotted ambiguously. Need change in API.
KtLambdaExpression lambdaExpression = lambdaExpressions[0];
KtFunctionLiteral literal = lambdaExpression.getFunctionLiteral();
PsiElement arrow = literal.getArrow();
// No arrow in original node
if (arrow == null) return false;
int arrowOffset = arrow.getStartOffsetInParent() + literal.getStartOffsetInParent();
Lexer oldLexer = new KotlinLexer();
oldLexer.start(lambdaExpression.getText());
Lexer newLexer = new KotlinLexer();
newLexer.start(buffer);
while (true) {
IElementType oldType = oldLexer.getTokenType();
if (oldType == null) break; // Didn't find an arrow token. Consider it as no arrow was present.
IElementType newType = newLexer.getTokenType();
if (newType == null) return true; // New text was finished before reaching arrow in old text
if (newType != oldType) {
if (newType == KtTokens.WHITE_SPACE) {
newLexer.advance();
continue;
}
else if (oldType == KtTokens.WHITE_SPACE) {
oldLexer.advance();
continue;
}
return true; // Arrow was moved or deleted
}
if (oldType == KtTokens.ARROW && oldLexer.getCurrentPosition().getOffset() == arrowOffset) {
break;
}
oldLexer.advance();
newLexer.advance();
}
return false;
}
@Override
public int getErrorsCount(CharSequence seq, Language fileLanguage, Project project) {
Lexer lexer = new KotlinLexer();