Corrected comments binding so that comments before local functions and classes are bound to them

This commit is contained in:
Valentin Kipyatkov
2014-10-03 18:32:12 +04:00
parent 9a8aa0f71a
commit b811a6cab4
5 changed files with 77 additions and 9 deletions
@@ -18,10 +18,12 @@ package org.jetbrains.jet.lang.parsing;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.intellij.lang.PsiBuilder; import com.intellij.lang.PsiBuilder;
import com.intellij.lang.WhitespacesAndCommentsBinder;
import com.intellij.psi.tree.IElementType; import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet; import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetNodeType; import org.jetbrains.jet.JetNodeType;
import org.jetbrains.jet.JetNodeTypes;
import org.jetbrains.jet.lexer.JetToken; import org.jetbrains.jet.lexer.JetToken;
import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.lexer.JetTokens;
@@ -959,7 +961,11 @@ public class JetExpressionParsing extends AbstractJetParsing {
if (declType != null) { if (declType != null) {
decl.done(declType); 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$); TrailingWhitespacesAndCommentsBinder.INSTANCE$);
return true; return true;
} }
+8
View File
@@ -31,6 +31,14 @@ class D {
// Function foo() // Function foo()
fun foo(/* parameters */ p1: Int/* p1 */, p2: Int /* p2 */) { 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 } // end of foo
// class object // class object
+54
View File
@@ -176,6 +176,60 @@ JetFile: CommentsBinding.kt
BLOCK BLOCK
PsiElement(LBRACE)('{') PsiElement(LBRACE)('{')
PsiWhiteSpace('\n ') 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)('}') PsiElement(RBRACE)('}')
PsiWhiteSpace(' ') PsiWhiteSpace(' ')
PsiComment(EOL_COMMENT)('// end of foo') PsiComment(EOL_COMMENT)('// end of foo')
+1 -1
View File
@@ -58,9 +58,9 @@ JetFile: When.kt
PsiElement(RBRACE)('}') PsiElement(RBRACE)('}')
PsiElement(RBRACE)('}') PsiElement(RBRACE)('}')
PsiWhiteSpace('\n\n ') PsiWhiteSpace('\n\n ')
FUN
PsiComment(EOL_COMMENT)('// foo') PsiComment(EOL_COMMENT)('// foo')
PsiWhiteSpace('\n ') PsiWhiteSpace('\n ')
FUN
PsiElement(fun)('fun') PsiElement(fun)('fun')
PsiWhiteSpace(' ') PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('b') PsiElement(IDENTIFIER)('b')
@@ -602,11 +602,11 @@ JetFile: BinaryTree.kt
BOOLEAN_CONSTANT BOOLEAN_CONSTANT
PsiElement(false)('false') PsiElement(false)('false')
PsiWhiteSpace('\n\n ') PsiWhiteSpace('\n\n ')
FUN
PsiComment(EOL_COMMENT)('// In principle, this has access to item anyway, but then it's unreachable code') PsiComment(EOL_COMMENT)('// In principle, this has access to item anyway, but then it's unreachable code')
PsiWhiteSpace('\n ') 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') 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 ') PsiWhiteSpace('\n ')
FUN
PsiElement(fun)('fun') PsiElement(fun)('fun')
PsiWhiteSpace(' ') PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('add') PsiElement(IDENTIFIER)('add')
@@ -832,9 +832,9 @@ JetFile: BinaryTree.kt
PsiWhiteSpace('\n ') PsiWhiteSpace('\n ')
PsiElement(RBRACE)('}') PsiElement(RBRACE)('}')
PsiWhiteSpace('\n\n ') PsiWhiteSpace('\n\n ')
FUN
PsiComment(EOL_COMMENT)('// In principle, this has access to item anyway') PsiComment(EOL_COMMENT)('// In principle, this has access to item anyway')
PsiWhiteSpace('\n ') PsiWhiteSpace('\n ')
FUN
PsiElement(fun)('fun') PsiElement(fun)('fun')
PsiWhiteSpace(' ') PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('addNoRef') PsiElement(IDENTIFIER)('addNoRef')