@@ -238,6 +238,46 @@ final class ConfigurateScanner implements Scanner { // Configurate: rename + pac
238238 */
239239 private Map <Integer , SimpleKey > possibleSimpleKeys ;
240240
241+ // Configurate start -- comment capture
242+ private boolean captureComments = true ;
243+ private final StringBuilder comments = new StringBuilder ();
244+
245+ public String popComments () {
246+ if (this .comments .length () == 0 ) {
247+ return null ;
248+ } else {
249+ final String ret = this .comments .toString ();
250+ this .comments .delete (0 , Integer .MAX_VALUE );
251+ return ret ;
252+ }
253+ }
254+
255+ private void pushComment (int length ) {
256+ if (!this .captureComments ) {
257+ this .reader .forward (length );
258+ return ;
259+ }
260+
261+ if (this .comments .length () > 0 ) {
262+ this .comments .append (org .spongepowered .configurate .loader .AbstractConfigurationLoader .CONFIGURATE_LINE_SEPARATOR );
263+ }
264+ final String comment = this .reader .prefixForward (length );
265+ if (comment .startsWith (" " )) {
266+ this .comments .append (comment , 1 , comment .length ());
267+ } else {
268+ this .comments .append (comment );
269+ }
270+ }
271+
272+ void setCaptureComments (boolean capture ) {
273+ this .captureComments = capture ;
274+ if (!capture && this .comments .length () > 0 ) {
275+ this .comments .delete (0 , Integer .MAX_VALUE );
276+ }
277+ }
278+
279+ // Configurate end -- comment capture
280+
241281 public ConfigurateScanner (StreamReader reader ) { // Configurate: rename
242282 this .reader = reader ;
243283 this .tokens = new ArrayList <Token >(100 );
@@ -1222,11 +1262,12 @@ private void scanToNextToken() {
12221262 // past the comment.
12231263 if (reader .peek () == '#' ) {
12241264 ff = 0 ;
1265+ reader .forward (); // skip comment character
12251266 while (Constant .NULL_OR_LINEBR .hasNo (reader .peek (ff ))) {
12261267 ff ++;
12271268 }
12281269 if (ff > 0 ) {
1229- reader . forward (ff );
1270+ this . pushComment (ff ); // Configurate: capture comment
12301271 }
12311272 }
12321273 // If we scanned a line break, then (depending on flow level),
@@ -1428,9 +1469,14 @@ private void scanDirectiveIgnoredLine(Mark startMark) {
14281469 reader .forward ();
14291470 }
14301471 if (reader .peek () == '#' ) {
1472+ // Configurate start -- comments
1473+ reader .forward (); // skip '#'
1474+ int ff = 0 ;
14311475 while (Constant .NULL_OR_LINEBR .hasNo (reader .peek ())) {
1432- reader . forward () ;
1476+ ff ++ ;
14331477 }
1478+ if (ff > 0 ) pushComment (ff );
1479+ // Configurate end
14341480 }
14351481 int c = reader .peek ();
14361482 String lineBreak = scanLineBreak ();
@@ -1743,9 +1789,15 @@ private String scanBlockScalarIgnoredLine(Mark startMark) {
17431789
17441790 // If a comment occurs, scan to just before the end of line.
17451791 if (reader .peek () == '#' ) {
1792+ // Configurate start -- comments
1793+ reader .forward ();
1794+
1795+ int ff = 0 ;
17461796 while (Constant .NULL_OR_LINEBR .hasNo (reader .peek ())) {
1747- reader . forward () ;
1797+ ff ++ ;
17481798 }
1799+ if (ff > 0 ) pushComment (ff );
1800+ // Configurate end
17491801 }
17501802 // If the next character is not a null or line break, an error has
17511803 // occurred.
@@ -2008,7 +2060,7 @@ private Token scanPlain() {
20082060 int c ;
20092061 int length = 0 ;
20102062 // A comment indicates the end of the scalar.
2011- if (reader .peek () == '#' ) {
2063+ if (reader .peek () == '#' ) { // Configurate: comment captured in scanToNextToken
20122064 break ;
20132065 }
20142066 while (true ) {
@@ -2029,7 +2081,7 @@ private Token scanPlain() {
20292081 endMark = reader .getMark ();
20302082 spaces = scanPlainSpaces ();
20312083 // System.out.printf("spaces[%s]\n", spaces);
2032- if (spaces .length () == 0 || reader .peek () == '#'
2084+ if (spaces .length () == 0 || reader .peek () == '#' // Configurate: comment captured in next token
20332085 || (this .flowLevel == 0 && this .reader .getColumn () < indent )) {
20342086 break ;
20352087 }
0 commit comments