Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 1e07da7

Browse files
committed
fix(javac): add Java compiler flag to override -XDkeepComments flag
When the 'keepComments' flag is set, the compiler uses JavadocTokenizer for tokenizing the source code. While creating the LineMap instance, the JavadocTokenizer sets the 'expandTabs' flag to true which expands '\t' characters to spaces. This is needed while generating HTML pages for the Javadoc. However, if the tabs are expanded to spaces, then getPosition(...) and getColumnNumber(...) methods in the LineMap class return indices which cannot be directly used to insert text in the source code. This results in StringIndexOutOfBoundsException when trying to insert text in the editor (#1127, and probably #916 as well). This commit adds the 'keepCommentsOverride' option to the compiler which is used by NBParserFactory to override the 'keepComments' flag. The CompileBatch class sets this option to 'ignore' which ignores the 'keepComments' flag so that JavadocTokenizer is not used for tokenization.
1 parent 52d401d commit 1e07da7

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

lsp/java/src/main/java/com/itsaky/androidide/lsp/java/compiler/CompileBatch.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ private List<String> options() {
168168
"-XDcompilePolicy=byfile",
169169
"-XD-Xprefer=source",
170170
"-XDide",
171+
"-XDkeepCommentsOverride=ignore",
171172
"-XDsuppressAbortOnBadClassFile",
172173
"-XDshould-stop.at=GENERATE",
173174
"-XDdiags.formatterOptions=-source",

subprojects/javac-services/src/main/java/com/itsaky/androidide/javac/services/NBParserFactory.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
*/
3636
package com.itsaky.androidide.javac.services;
3737

38+
import com.itsaky.androidide.utils.ILogger;
3839
import openjdk.tools.javac.parser.JavacParser;
3940
import openjdk.tools.javac.parser.Lexer;
4041
import openjdk.tools.javac.parser.ParserFactory;
@@ -52,17 +53,26 @@
5253
import openjdk.tools.javac.util.List;
5354
import openjdk.tools.javac.util.Name;
5455
import openjdk.tools.javac.util.Names;
56+
import openjdk.tools.javac.util.Options;
5557
import openjdk.tools.javac.util.Position;
5658

5759
/**
5860
* @author lahvac
5961
*/
6062
public class NBParserFactory extends ParserFactory {
6163

64+
public static final String KEEP_COMMENTS_OVERRIDE = "keepCommentsOverride";
65+
public static final String KEEP_COMMENTS_OVERRIDE_KEEP = "keep";
66+
public static final String KEEP_COMMENTS_OVERRIDE_IGNORE = "ignore";
67+
68+
private static final ILogger LOG = ILogger.newInstance("NBParserFactory");
69+
6270
protected final ScannerFactory scannerFactory;
6371
protected final Names names;
6472
protected final CancelService cancelService;
6573

74+
protected final String keepCommentsOverride;
75+
6676
public static void preRegister(Context context) {
6777
context.put(parserFactoryKey, (Context.Factory<ParserFactory>) NBParserFactory::new);
6878
}
@@ -72,6 +82,8 @@ protected NBParserFactory(Context context) {
7282
this.scannerFactory = ScannerFactory.instance(context);
7383
this.names = Names.instance(context);
7484
this.cancelService = CancelService.instance(context);
85+
this.keepCommentsOverride = Options.instance(context).get(KEEP_COMMENTS_OVERRIDE);
86+
7587
}
7688

7789
@Override
@@ -81,9 +93,20 @@ public JavacParser newParser(
8193
boolean keepEndPos,
8294
boolean keepLineMap,
8395
boolean parseModuleInfo) {
84-
Lexer lexer = scannerFactory.newScanner(input, keepDocComments);
96+
var keepDocCommentsOverride = keepDocComments;
97+
if (this.keepCommentsOverride != null) {
98+
if (KEEP_COMMENTS_OVERRIDE_IGNORE.equals(this.keepCommentsOverride)) {
99+
keepDocCommentsOverride = false;
100+
} else if (KEEP_COMMENTS_OVERRIDE_KEEP.equals(this.keepCommentsOverride)) {
101+
keepDocCommentsOverride = true;
102+
}
103+
LOG.debug("'keepComments' overridden to ", this.keepCommentsOverride);
104+
}
105+
106+
Lexer lexer = scannerFactory.newScanner(input, keepDocCommentsOverride);
85107
return new NBJavacParser(
86-
this, lexer, keepDocComments, keepLineMap, keepEndPos, parseModuleInfo, cancelService);
108+
this, lexer, keepDocCommentsOverride, keepLineMap, keepEndPos, parseModuleInfo,
109+
cancelService);
87110
}
88111

89112
public static class NBJavacParser extends JavacParser {
@@ -201,7 +224,9 @@ public int getEndPos(JCTree jctree) {
201224

202225
@Override
203226
public void storeEnd(JCTree tree, int endpos) {
204-
if (endpos >= 0) delegate.storeEnd(tree, endpos);
227+
if (endpos >= 0) {
228+
delegate.storeEnd(tree, endpos);
229+
}
205230
}
206231

207232
@Override

0 commit comments

Comments
 (0)