Skip to content

Commit 891ae75

Browse files
authored
Merge pull request #2978 from Rawi01/language-server
Add syntax highlighting and javadoc to language server (VSCode)
2 parents ba2b47b + 07cf64e commit 891ae75

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
lines changed

src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
9696
import org.eclipse.jdt.internal.compiler.ast.Wildcard;
9797
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
98+
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
9899
import org.eclipse.jdt.internal.compiler.lookup.Binding;
99100
import org.eclipse.jdt.internal.compiler.lookup.CaptureBinding;
100101
import org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
@@ -337,6 +338,8 @@ static class EclipseReflectiveMembers {
337338
public static final Class<?> INTERSECTION_BINDING1, INTERSECTION_BINDING2;
338339
public static final Field INTERSECTION_BINDING_TYPES1, INTERSECTION_BINDING_TYPES2;
339340
public static final Field TYPE_DECLARATION_RECORD_COMPONENTS;
341+
public static final Class<?> COMPILATION_UNIT;
342+
public static final Method COMPILATION_UNIT_ORIGINAL_FROM_CLONE;
340343
static {
341344
STRING_LITERAL__LINE_NUMBER = getField(StringLiteral.class, "lineNumber");
342345
ANNOTATION__MEMBER_VALUE_PAIR_NAME = getField(Annotation.class, "memberValuePairName");
@@ -346,6 +349,8 @@ static class EclipseReflectiveMembers {
346349
INTERSECTION_BINDING_TYPES1 = INTERSECTION_BINDING1 == null ? null : getField(INTERSECTION_BINDING1, "intersectingTypes");
347350
INTERSECTION_BINDING_TYPES2 = INTERSECTION_BINDING2 == null ? null : getField(INTERSECTION_BINDING2, "intersectingTypes");
348351
TYPE_DECLARATION_RECORD_COMPONENTS = getField(TypeDeclaration.class, "recordComponents");
352+
COMPILATION_UNIT = getClass("org.eclipse.jdt.internal.core.CompilationUnit");
353+
COMPILATION_UNIT_ORIGINAL_FROM_CLONE = COMPILATION_UNIT == null ? null : Permit.permissiveGetMethod(COMPILATION_UNIT, "originalFromClone");
349354
}
350355

351356
public static int reflectInt(Field f, Object o) {
@@ -2731,7 +2736,14 @@ public static void setDocComment(CompilationUnitDeclaration cud, EclipseNode ecl
27312736
public static void setDocComment(CompilationUnitDeclaration cud, TypeDeclaration type, ASTNode node, String doc) {
27322737
if (doc == null) return;
27332738

2734-
Map<String, String> docs = EcjAugments.CompilationUnit_javadoc.setIfAbsent(cud.compilationResult.compilationUnit, new HashMap<String, String>());
2739+
ICompilationUnit compilationUnit = cud.compilationResult.compilationUnit;
2740+
if (compilationUnit.getClass().equals(COMPILATION_UNIT)) {
2741+
try {
2742+
compilationUnit = (ICompilationUnit) Permit.invoke(COMPILATION_UNIT_ORIGINAL_FROM_CLONE, compilationUnit);
2743+
} catch (Throwable t) { }
2744+
}
2745+
2746+
Map<String, String> docs = EcjAugments.CompilationUnit_javadoc.setIfAbsent(compilationUnit, new HashMap<String, String>());
27352747
if (node instanceof AbstractMethodDeclaration) {
27362748
AbstractMethodDeclaration methodDeclaration = (AbstractMethodDeclaration) node;
27372749
String signature = getSignature(type, methodDeclaration);

src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public String mapResourceName(int classFileFormatVersion, String resourceName) {
8787
patchHideGeneratedNodes(sm);
8888
patchPostCompileHookEclipse(sm);
8989
patchFixSourceTypeConverter(sm);
90-
patchDisableLombokForCodeCleanup(sm);
9190
patchListRewriteHandleGeneratedMethods(sm);
9291
patchSyntaxAndOccurrencesHighlighting(sm);
9392
patchSortMembersOperation(sm);
@@ -206,14 +205,6 @@ private static void patchSyntaxAndOccurrencesHighlighting(ScriptManager sm) {
206205
.build());
207206
}
208207

209-
private static void patchDisableLombokForCodeCleanup(ScriptManager sm) {
210-
sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.exitEarly()
211-
.target(new MethodTarget("org.eclipse.jdt.core.dom.ASTNode", "accept", "void", "org.eclipse.jdt.core.dom.ASTVisitor"))
212-
.decisionMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "isRefactoringVisitorAndGenerated", "boolean", "org.eclipse.jdt.core.dom.ASTNode", "org.eclipse.jdt.core.dom.ASTVisitor"))
213-
.request(StackRequest.THIS, StackRequest.PARAM1)
214-
.build());
215-
}
216-
217208
private static void patchListRewriteHandleGeneratedMethods(ScriptManager sm) {
218209
sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.replaceMethodCall()
219210
.target(new MethodTarget("org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteAnalyzer$ListRewriter", "rewriteList"))
@@ -308,6 +299,12 @@ private static void patchHideGeneratedNodes(ScriptManager sm) {
308299
"org.eclipse.jdt.core.dom.SimpleName[]"))
309300
.request(StackRequest.RETURN_VALUE).build());
310301

302+
sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.exitEarly()
303+
.target(new MethodTarget("org.eclipse.jdt.core.dom.ASTNode", "accept", "void", "org.eclipse.jdt.core.dom.ASTVisitor"))
304+
.decisionMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "isBlockedVisitorAndGenerated", "boolean", "org.eclipse.jdt.core.dom.ASTNode", "org.eclipse.jdt.core.dom.ASTVisitor"))
305+
.request(StackRequest.THIS, StackRequest.PARAM1)
306+
.build());
307+
311308
patchRefactorScripts(sm);
312309
patchFormatters(sm);
313310
}
@@ -895,6 +892,14 @@ private static void patchJavadoc(ScriptManager sm) {
895892
.requestExtra(StackRequest.PARAM1)
896893
.build());
897894

895+
/* This is a copy for the language server implementation that also supports markdown */
896+
sm.addScript(ScriptBuilder.wrapMethodCall()
897+
.target(new MethodTarget("org.eclipse.jdt.ls.core.internal.javadoc.JavadocContentAccess2", "getHTMLContent", "java.lang.String", "org.eclipse.jdt.core.IJavaElement", "boolean"))
898+
.methodToWrap(new Hook("org.eclipse.jdt.ls.core.internal.javadoc.JavadocContentAccess2", "getHTMLContentFromSource", "java.lang.String", "org.eclipse.jdt.core.IJavaElement"))
899+
.wrapMethod(new Hook("lombok.launch.PatchFixesHider$Javadoc", "getHTMLContentFromSource", "java.lang.String", "java.lang.String", "org.eclipse.jdt.core.IJavaElement"))
900+
.requestExtra(StackRequest.PARAM1)
901+
.build());
902+
898903
/* This is an older version that uses IMember instead of IJavaElement */
899904
sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.wrapMethodCall()
900905
.target(new MethodTarget("org.eclipse.jdt.internal.ui.text.javadoc.JavadocContentAccess2", "getHTMLContent", "java.lang.String", "org.eclipse.jdt.core.IMember", "boolean"))

src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 The Project Lombok Authors.
2+
* Copyright (C) 2020-2021 The Project Lombok Authors.
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a copy
55
* of this software and associated documentation files (the "Software"), to deal
@@ -103,18 +103,23 @@ static final String getSignature(SourceMethod sourceMethod) {
103103
private static class Reflection {
104104
private static final Method javadoc2HTML;
105105
private static final Method oldJavadoc2HTML;
106+
private static final Method lsJavadoc2HTML;
106107
static {
107-
Method a = null, b = null;
108+
Method a = null, b = null, c = null;
108109

109110
try {
110111
a = Permit.getMethod(JavadocContentAccess2.class, "javadoc2HTML", IMember.class, IJavaElement.class, String.class);
111112
} catch (Throwable t) {}
112113
try {
113114
b = Permit.getMethod(JavadocContentAccess2.class, "javadoc2HTML", IMember.class, String.class);
114115
} catch (Throwable t) {}
116+
try {
117+
c = Permit.getMethod(Class.forName("org.eclipse.jdt.ls.core.internal.javadoc.JavadocContentAccess2"), "javadoc2HTML", IMember.class, IJavaElement.class, String.class);
118+
} catch (Throwable t) {}
115119

116120
javadoc2HTML = a;
117121
oldJavadoc2HTML = b;
122+
lsJavadoc2HTML = c;
118123
}
119124

120125
private static String javadoc2HTML(IMember member, IJavaElement element, String rawJavadoc) {
@@ -125,6 +130,13 @@ private static String javadoc2HTML(IMember member, IJavaElement element, String
125130
return null;
126131
}
127132
}
133+
if (lsJavadoc2HTML != null) {
134+
try {
135+
return (String) lsJavadoc2HTML.invoke(null, member, element, rawJavadoc);
136+
} catch (Throwable t) {
137+
return null;
138+
}
139+
}
128140
if (oldJavadoc2HTML != null) {
129141
try {
130142
return (String) oldJavadoc2HTML.invoke(null, member, rawJavadoc);

src/eclipseAgent/lombok/launch/PatchFixesHider.java

+3-12
Original file line numberDiff line numberDiff line change
@@ -414,23 +414,14 @@ public static boolean isGenerated(org.eclipse.jdt.internal.compiler.ast.ASTNode
414414
return result;
415415
}
416416

417-
public static boolean isRefactoringVisitorAndGenerated(org.eclipse.jdt.core.dom.ASTNode node, org.eclipse.jdt.core.dom.ASTVisitor visitor) {
417+
public static boolean isBlockedVisitorAndGenerated(org.eclipse.jdt.core.dom.ASTNode node, org.eclipse.jdt.core.dom.ASTVisitor visitor) {
418418
if (visitor == null) return false;
419419

420420
String className = visitor.getClass().getName();
421-
if (!(className.startsWith("org.eclipse.jdt.internal.corext.fix") || className.startsWith("org.eclipse.jdt.internal.ui.fix"))) return false;
421+
if (!(className.startsWith("org.eclipse.jdt.internal.corext.fix") || className.startsWith("org.eclipse.jdt.internal.ui.fix") || className.startsWith("org.eclipse.jdt.ls.core.internal.semantictokens.SemanticTokensVisitor"))) return false;
422422
if (className.equals("org.eclipse.jdt.internal.corext.fix.VariableDeclarationFixCore$WrittenNamesFinder")) return false;
423423

424-
boolean result = false;
425-
try {
426-
result = ((Boolean)node.getClass().getField("$isGenerated").get(node)).booleanValue();
427-
if (!result && node.getParent() != null && node.getParent() instanceof org.eclipse.jdt.core.dom.QualifiedName) {
428-
result = isGenerated(node.getParent());
429-
}
430-
} catch (Exception e) {
431-
// better to assume it isn't generated
432-
}
433-
return result;
424+
return isGenerated(node);
434425
}
435426

436427
public static boolean isListRewriteOnGeneratedNode(org.eclipse.jdt.core.dom.rewrite.ListRewrite rewrite) {

0 commit comments

Comments
 (0)