From b811a6cab4feaefa3369fc346915b468011a0ffd Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Fri, 3 Oct 2014 18:32:12 +0400 Subject: [PATCH] Corrected comments binding so that comments before local functions and classes are bound to them --- .../lang/parsing/JetExpressionParsing.java | 8 ++- compiler/testData/psi/CommentsBinding.kt | 8 +++ compiler/testData/psi/CommentsBinding.txt | 54 +++++++++++++++++++ compiler/testData/psi/When.txt | 4 +- compiler/testData/psi/examples/BinaryTree.txt | 12 ++--- 5 files changed, 77 insertions(+), 9 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java b/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java index e5559e9e998..07a4dbf617b 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java @@ -18,10 +18,12 @@ package org.jetbrains.jet.lang.parsing; import com.google.common.collect.ImmutableMap; import com.intellij.lang.PsiBuilder; +import com.intellij.lang.WhitespacesAndCommentsBinder; import com.intellij.psi.tree.IElementType; import com.intellij.psi.tree.TokenSet; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.JetNodeType; +import org.jetbrains.jet.JetNodeTypes; import org.jetbrains.jet.lexer.JetToken; import org.jetbrains.jet.lexer.JetTokens; @@ -959,7 +961,11 @@ public class JetExpressionParsing extends AbstractJetParsing { if (declType != null) { decl.done(declType); - decl.setCustomEdgeTokenBinders(null/* for local declaration we do not take preceding comments*/, + // we do not attach preceding comments to local variables because they are likely commenting a few statements below + WhitespacesAndCommentsBinder leftBinder = declType == JetNodeTypes.PROPERTY || declType == JetNodeTypes.MULTI_VARIABLE_DECLARATION + ? null + : PrecedingWhitespacesAndCommentsBinder.INSTANCE$; + decl.setCustomEdgeTokenBinders(leftBinder, TrailingWhitespacesAndCommentsBinder.INSTANCE$); return true; } diff --git a/compiler/testData/psi/CommentsBinding.kt b/compiler/testData/psi/CommentsBinding.kt index 41636e10c88..27546cd6bc0 100644 --- a/compiler/testData/psi/CommentsBinding.kt +++ b/compiler/testData/psi/CommentsBinding.kt @@ -31,6 +31,14 @@ class D { // Function foo() fun foo(/* parameters */ p1: Int/* p1 */, p2: Int /* p2 */) { + // before local var + val local = 1 // local var + // before local fun + fun localFun() = 1 // local fun + // before local class + class Local{} // local class + // before statement + foo() // statement } // end of foo // class object diff --git a/compiler/testData/psi/CommentsBinding.txt b/compiler/testData/psi/CommentsBinding.txt index b342b681413..9afef7a861a 100644 --- a/compiler/testData/psi/CommentsBinding.txt +++ b/compiler/testData/psi/CommentsBinding.txt @@ -175,6 +175,60 @@ JetFile: CommentsBinding.kt PsiWhiteSpace(' ') BLOCK PsiElement(LBRACE)('{') + PsiWhiteSpace('\n ') + PsiComment(EOL_COMMENT)('// before local var') + PsiWhiteSpace('\n ') + PROPERTY + PsiElement(val)('val') + PsiWhiteSpace(' ') + PsiElement(IDENTIFIER)('local') + PsiWhiteSpace(' ') + PsiElement(EQ)('=') + PsiWhiteSpace(' ') + INTEGER_CONSTANT + PsiElement(INTEGER_LITERAL)('1') + PsiWhiteSpace(' ') + PsiComment(EOL_COMMENT)('// local var') + PsiWhiteSpace('\n ') + FUN + PsiComment(EOL_COMMENT)('// before local fun') + PsiWhiteSpace('\n ') + PsiElement(fun)('fun') + PsiWhiteSpace(' ') + PsiElement(IDENTIFIER)('localFun') + VALUE_PARAMETER_LIST + PsiElement(LPAR)('(') + PsiElement(RPAR)(')') + PsiWhiteSpace(' ') + PsiElement(EQ)('=') + PsiWhiteSpace(' ') + INTEGER_CONSTANT + PsiElement(INTEGER_LITERAL)('1') + PsiWhiteSpace(' ') + PsiComment(EOL_COMMENT)('// local fun') + PsiWhiteSpace('\n ') + CLASS + PsiComment(EOL_COMMENT)('// before local class') + PsiWhiteSpace('\n ') + PsiElement(class)('class') + PsiWhiteSpace(' ') + PsiElement(IDENTIFIER)('Local') + CLASS_BODY + PsiElement(LBRACE)('{') + PsiElement(RBRACE)('}') + PsiWhiteSpace(' ') + PsiComment(EOL_COMMENT)('// local class') + PsiWhiteSpace('\n ') + PsiComment(EOL_COMMENT)('// before statement') + PsiWhiteSpace('\n ') + CALL_EXPRESSION + REFERENCE_EXPRESSION + PsiElement(IDENTIFIER)('foo') + VALUE_ARGUMENT_LIST + PsiElement(LPAR)('(') + PsiElement(RPAR)(')') + PsiWhiteSpace(' ') + PsiComment(EOL_COMMENT)('// statement') PsiWhiteSpace('\n ') PsiElement(RBRACE)('}') PsiWhiteSpace(' ') diff --git a/compiler/testData/psi/When.txt b/compiler/testData/psi/When.txt index d0ced58445a..a0c571ac02f 100644 --- a/compiler/testData/psi/When.txt +++ b/compiler/testData/psi/When.txt @@ -58,9 +58,9 @@ JetFile: When.kt PsiElement(RBRACE)('}') PsiElement(RBRACE)('}') PsiWhiteSpace('\n\n ') - PsiComment(EOL_COMMENT)('// foo') - PsiWhiteSpace('\n ') FUN + PsiComment(EOL_COMMENT)('// foo') + PsiWhiteSpace('\n ') PsiElement(fun)('fun') PsiWhiteSpace(' ') PsiElement(IDENTIFIER)('b') diff --git a/compiler/testData/psi/examples/BinaryTree.txt b/compiler/testData/psi/examples/BinaryTree.txt index 4257ceb13ae..0d2b4b8c79d 100644 --- a/compiler/testData/psi/examples/BinaryTree.txt +++ b/compiler/testData/psi/examples/BinaryTree.txt @@ -602,11 +602,11 @@ JetFile: BinaryTree.kt BOOLEAN_CONSTANT PsiElement(false)('false') PsiWhiteSpace('\n\n ') - PsiComment(EOL_COMMENT)('// In principle, this has access to item anyway, but then it's unreachable code') - PsiWhiteSpace('\n ') - PsiComment(EOL_COMMENT)('// BAD: the naive implementation of ref will create H(T) ref objects, but can be optimized to create only one') - PsiWhiteSpace('\n ') FUN + PsiComment(EOL_COMMENT)('// In principle, this has access to item anyway, but then it's unreachable code') + PsiWhiteSpace('\n ') + PsiComment(EOL_COMMENT)('// BAD: the naive implementation of ref will create H(T) ref objects, but can be optimized to create only one') + PsiWhiteSpace('\n ') PsiElement(fun)('fun') PsiWhiteSpace(' ') PsiElement(IDENTIFIER)('add') @@ -832,9 +832,9 @@ JetFile: BinaryTree.kt PsiWhiteSpace('\n ') PsiElement(RBRACE)('}') PsiWhiteSpace('\n\n ') - PsiComment(EOL_COMMENT)('// In principle, this has access to item anyway') - PsiWhiteSpace('\n ') FUN + PsiComment(EOL_COMMENT)('// In principle, this has access to item anyway') + PsiWhiteSpace('\n ') PsiElement(fun)('fun') PsiWhiteSpace(' ') PsiElement(IDENTIFIER)('addNoRef')