Skip to content

Commit b042ea6

Browse files
committed
Merge pull request #19 from antlr/emit-comments
inject comments
2 parents 6d8d6dd + 4753621 commit b042ea6

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

java/codebuff.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" />
1919
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
2020
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.4" level="project" />
21+
<orderEntry type="library" name="Maven: com.google.guava:guava:19.0" level="project" />
2122
</component>
2223
</module>

java/codebuff.ipr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,17 @@
220220
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
221221
</component>
222222
<component name="libraryTable">
223+
<library name="Maven: com.google.guava:guava:19.0">
224+
<CLASSES>
225+
<root url="jar://$MAVEN_REPOSITORY$/com/google/guava/guava/19.0/guava-19.0.jar!/" />
226+
</CLASSES>
227+
<JAVADOC>
228+
<root url="jar://$MAVEN_REPOSITORY$/com/google/guava/guava/19.0/guava-19.0-javadoc.jar!/" />
229+
</JAVADOC>
230+
<SOURCES>
231+
<root url="jar://$MAVEN_REPOSITORY$/com/google/guava/guava/19.0/guava-19.0-sources.jar!/" />
232+
</SOURCES>
233+
</library>
223234
<library name="Maven: junit:junit:4.11">
224235
<CLASSES>
225236
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.11/junit-4.11.jar!/" />

java/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<artifactId>commons-lang3</artifactId>
4848
<version>3.4</version>
4949
</dependency>
50+
<dependency>
51+
<groupId>com.google.guava</groupId>
52+
<artifactId>guava</artifactId>
53+
<version>19.0</version>
54+
</dependency>
5055
</dependencies>
5156

5257
<build>

java/src/org/antlr/codebuff/Formatter.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.antlr.codebuff;
22

3+
import com.google.common.base.CharMatcher;
34
import org.antlr.v4.runtime.CommonToken;
45
import org.antlr.v4.runtime.CommonTokenStream;
56
import org.antlr.v4.runtime.ParserRuleContext;
@@ -112,6 +113,8 @@ public void processToken(int indexIntoRealTokens, int tokenIndexInStream) {
112113
CommonToken curToken = (CommonToken)tokens.get(tokenIndexInStream);
113114
String tokText = curToken.getText();
114115

116+
emitCommentsToTheLeft(tokenIndexInStream);
117+
115118
int[] features = getNodeFeatures(tokenToNodeMap, doc, tokenIndexInStream, line, tabSize);
116119
// must set "prev end column" value as token stream doesn't have it;
117120
// we're tracking it as we emit tokens
@@ -215,6 +218,50 @@ else if ( (align&0xFF)==CAT_INDENT_FROM_ANCESTOR_FIRST_TOKEN ) {
215218
charPosInLine += n;
216219
}
217220

221+
/** Look into the token stream to get the comments to the left of current
222+
* token. Emit all whitespace and comments except for whitespace at the
223+
* end as we'll inject that per newline prediction.
224+
*/
225+
public void emitCommentsToTheLeft(int tokenIndexInStream) {
226+
List<Token> hiddenTokensToLeft = tokens.getHiddenTokensToLeft(tokenIndexInStream);
227+
if ( hiddenTokensToLeft!=null ) {
228+
// if at least one is not whitespace, assume it's a comment and print all hidden stuff including whitespace
229+
boolean hasComment = false;
230+
for (Token hidden : hiddenTokensToLeft) {
231+
String hiddenText = hidden.getText();
232+
if ( !hiddenText.matches("\\s+") ) {
233+
hasComment = true;
234+
break;
235+
}
236+
}
237+
if ( hasComment ) {
238+
// avoid whitespace at end of sequence as we'll inject that
239+
int last = -1;
240+
for (int i=hiddenTokensToLeft.size()-1; i>=0; i--) {
241+
Token hidden = hiddenTokensToLeft.get(i);
242+
String hiddenText = hidden.getText();
243+
if ( !hiddenText.matches("\\s+") ) {
244+
last = i;
245+
break;
246+
}
247+
}
248+
List<Token> stripped = hiddenTokensToLeft.subList(0, last+1);
249+
for (Token hidden : stripped) {
250+
String hiddenText = hidden.getText();
251+
output.append(hiddenText);
252+
if ( hiddenText.matches("\\n+") ) {
253+
line += CharMatcher.is('\n').countIn(hiddenText);
254+
charPosInLine = 0;
255+
}
256+
else {
257+
// if a comment or plain ' ', must count char position
258+
charPosInLine += hiddenText.length();
259+
}
260+
}
261+
}
262+
}
263+
}
264+
218265
public TokenPositionAnalysis getTokenAnalysis(int[] features, int indexIntoRealTokens, int tokenIndexInStream,
219266
int injectNewline,
220267
int alignWithPrevious,

0 commit comments

Comments
 (0)