24
24
import java .util .ArrayList ;
25
25
import java .util .Collection ;
26
26
import java .util .List ;
27
+ import java .util .Map ;
27
28
import java .util .stream .Collectors ;
28
29
import java .util .stream .Stream ;
30
+ import javax .annotation .Nullable ;
29
31
import org .sonar .plugins .python .api .tree .CellMagicStatement ;
30
32
import org .sonar .plugins .python .api .tree .DynamicObjectInfoStatement ;
31
33
import org .sonar .plugins .python .api .tree .Expression ;
37
39
import org .sonar .plugins .python .api .tree .Token ;
38
40
import org .sonar .plugins .python .api .tree .Tree ;
39
41
import org .sonar .python .DocstringExtractor ;
42
+ import org .sonar .python .IPythonLocation ;
40
43
import org .sonar .python .api .IPythonGrammar ;
41
44
import org .sonar .python .api .PythonGrammar ;
42
45
43
46
public class IPythonTreeMaker extends PythonTreeMaker {
44
47
48
+ private Map <Integer , IPythonLocation > offsetMap ;
49
+
50
+ public IPythonTreeMaker () {
51
+ this (null );
52
+ }
53
+ public IPythonTreeMaker (@ Nullable Map <Integer , IPythonLocation > offsetMap ) {
54
+ this .offsetMap = offsetMap ;
55
+ }
56
+
57
+ @ Override
58
+ protected Token toPyToken (@ Nullable com .sonar .sslr .api .Token token ) {
59
+ if (token == null ) {
60
+ return null ;
61
+ }
62
+ if (this .offsetMap != null ) {
63
+ return TokenEnricher .enrichToken (token , offsetMap );
64
+ }
65
+ return new TokenImpl (token );
66
+ }
67
+
45
68
@ Override
46
69
public FileInput fileInput (AstNode astNode ) {
47
70
StatementList statementList = astNode .getChildren (IPythonGrammar .CELL , IPythonGrammar .MAGIC_CELL )
48
71
.stream ()
49
72
.flatMap (this ::getStatementsFromCell )
50
- .collect (Collectors .collectingAndThen (Collectors .toList (), l -> l .isEmpty ()? null : new StatementListImpl (l )));
73
+ .collect (Collectors .collectingAndThen (Collectors .toList (), l -> l .isEmpty () ? null : new StatementListImpl (l )));
51
74
Token endOfFile = toPyToken (astNode .getFirstChild (GenericTokenType .EOF ).getToken ());
52
75
FileInputImpl pyFileInputTree = new FileInputImpl (statementList , endOfFile , DocstringExtractor .extractDocstring (statementList ));
53
76
setParents (pyFileInputTree );
@@ -59,7 +82,7 @@ private Stream<Statement> getStatementsFromCell(AstNode cell) {
59
82
if (cell .is (IPythonGrammar .CELL )) {
60
83
return getStatements (cell ).stream ().map (this ::statement );
61
84
} else {
62
- return Stream .of (cell .getFirstChild (IPythonGrammar .CELL_MAGIC_STATEMENT )).map (IPythonTreeMaker ::cellMagicStatement );
85
+ return Stream .of (cell .getFirstChild (IPythonGrammar .CELL_MAGIC_STATEMENT )).map (this ::cellMagicStatement );
63
86
}
64
87
}
65
88
@@ -85,12 +108,12 @@ protected Expression annotatedRhs(AstNode annotatedRhs) {
85
108
return super .annotatedRhs (annotatedRhs );
86
109
}
87
110
88
- private static CellMagicStatement cellMagicStatement (AstNode astNode ) {
111
+ private CellMagicStatement cellMagicStatement (AstNode astNode ) {
89
112
var tokens = astNode .getChildren ()
90
113
.stream ()
91
114
.map (AstNode ::getTokens )
92
115
.flatMap (Collection ::stream )
93
- .map (IPythonTreeMaker ::toPyToken )
116
+ .map (this ::toPyToken )
94
117
.toList ();
95
118
return new CellMagicStatementImpl (tokens );
96
119
}
@@ -121,7 +144,7 @@ protected DynamicObjectInfoStatement dynamicObjectInfoStatement(AstNode astNode)
121
144
isQuestionMarksBefore = false ;
122
145
child .getTokens ()
123
146
.stream ()
124
- .map (PythonTreeMaker ::toPyToken )
147
+ .map (this ::toPyToken )
125
148
.forEach (children ::add );
126
149
}
127
150
}
@@ -137,9 +160,8 @@ protected LineMagic lineMagic(AstNode astNode) {
137
160
.skip (2 )
138
161
.map (AstNode ::getTokens )
139
162
.flatMap (Collection ::stream )
140
- .map (IPythonTreeMaker ::toPyToken )
163
+ .map (this ::toPyToken )
141
164
.toList ();
142
165
return new LineMagicImpl (percent , name , tokens );
143
166
}
144
-
145
167
}
0 commit comments