Skip to content

Commit 33988ac

Browse files
cpovirkcushon
authored andcommitted
Promote NestingCounter to a top-level type
and use it from JavadocWriter. MOE_MIGRATED_REVID=133601793
1 parent d360f2b commit 33988ac

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -545,29 +545,5 @@ private static Pattern closeTagPattern(String namePattern) {
545545
return compile(format("^</(?:%s)\\b[^>]*>", namePattern), CASE_INSENSITIVE);
546546
}
547547

548-
private static final class NestingCounter {
549-
private int value;
550-
551-
void increment() {
552-
value++;
553-
}
554-
555-
void incrementIfPositive() {
556-
if (value > 0) {
557-
value++;
558-
}
559-
}
560-
561-
void decrementIfPositive() {
562-
if (value > 0) {
563-
value--;
564-
}
565-
}
566-
567-
boolean isPositive() {
568-
return value > 0;
569-
}
570-
}
571-
572548
static class LexException extends Exception {}
573549
}

core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ final class JavadocWriter {
5050
*/
5151
private boolean continuingListItemOfInnermostList;
5252
private boolean continuingFooterTag;
53-
private int continuingListItemCount;
53+
private final NestingCounter continuingListItemCount = new NestingCounter();
5454
private int remainingOnLine;
5555
private boolean atStartOfLine;
5656
private RequestedWhitespace requestedWhitespace = NONE;
@@ -102,7 +102,7 @@ void writeFooterJavadocTagStart(Token token) {
102102
* currently know which of those tags are open.
103103
*/
104104
continuingListItemOfInnermostList = false;
105-
continuingListItemCount = 0;
105+
continuingListItemCount.reset();
106106

107107
if (!wroteAnythingSignificant) {
108108
// Javadoc consists solely of tags. This is frowned upon in general but OK for @Overrides.
@@ -130,9 +130,7 @@ void writeListOpen(Token token) {
130130
void writeListClose(Token token) {
131131
requestNewline();
132132

133-
if (continuingListItemCount > 0) {
134-
continuingListItemCount--;
135-
}
133+
continuingListItemCount.decrementIfPositive();
136134
writeToken(token);
137135

138136
// TODO(cushon): only if continuingListItemCount == 0?
@@ -144,13 +142,11 @@ void writeListItemOpen(Token token) {
144142

145143
if (continuingListItemOfInnermostList) {
146144
continuingListItemOfInnermostList = false;
147-
if (continuingListItemCount > 0) {
148-
continuingListItemCount--;
149-
}
145+
continuingListItemCount.decrementIfPositive();
150146
}
151147
writeToken(token);
152148
continuingListItemOfInnermostList = true;
153-
continuingListItemCount++;
149+
continuingListItemCount.increment();
154150
}
155151

156152
void writeHeaderOpen(Token token) {
@@ -288,7 +284,8 @@ private void writeToken(Token token) {
288284
requestNewline();
289285
}
290286

291-
if (requestedWhitespace == BLANK_LINE && (continuingListItemCount > 0 || continuingFooterTag)) {
287+
if (requestedWhitespace == BLANK_LINE
288+
&& (continuingListItemCount.isPositive() || continuingFooterTag)) {
292289
/*
293290
* We don't write blank lines inside lists or footer tags, even in cases where we otherwise
294291
* would (e.g., before a <p> tag). Justification: We don't write blank lines _between_ list
@@ -381,7 +378,7 @@ enum AutoIndent {
381378
}
382379

383380
private int innerIndent() {
384-
int innerIndent = continuingListItemCount * 4;
381+
int innerIndent = continuingListItemCount.value() * 4;
385382
if (continuingFooterTag) {
386383
innerIndent += 4;
387384
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2016 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.googlejavaformat.java.javadoc;
16+
17+
/** Mutable integer for tracking the level of nesting. */
18+
final class NestingCounter {
19+
private int value;
20+
21+
int value() {
22+
return value;
23+
}
24+
25+
void increment() {
26+
value++;
27+
}
28+
29+
void incrementIfPositive() {
30+
if (value > 0) {
31+
value++;
32+
}
33+
}
34+
35+
void decrementIfPositive() {
36+
if (value > 0) {
37+
value--;
38+
}
39+
}
40+
41+
boolean isPositive() {
42+
return value > 0;
43+
}
44+
45+
void reset() {
46+
value = 0;
47+
}
48+
}

0 commit comments

Comments
 (0)