Skip to content

Commit

Permalink
Switch back to using Spotless for code formatting (#4496)
Browse files Browse the repository at this point in the history
To better align with `azure-sdk-for-java` code formatting, Java code
generation is switching back to using Spotless to format code after
generation. This aligns with what `azure-sdk-for-java` is using, and
will result in fewer code formatting conflicts.
  • Loading branch information
alzimmermsft authored Sep 24, 2024
1 parent a9029f3 commit 08384aa
Show file tree
Hide file tree
Showing 1,360 changed files with 33,503 additions and 26,293 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,33 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<spotless.config.path>../</spotless.config.path>
</properties>

<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.52.0</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.azure</groupId>
Expand Down Expand Up @@ -59,17 +79,12 @@
<artifactId>org.eclipse.lsp4j</artifactId>
<version>0.21.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.core</artifactId>
<version>3.33.0</version>
</dependency>
<dependency>
<groupId>org.atteo</groupId>
<artifactId>evo-inflector</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
package com.microsoft.typespec.http.client.generator.core.customization;

import com.microsoft.typespec.http.client.generator.core.customization.implementation.ls.EclipseLanguageClient;
import org.eclipse.lsp4j.SymbolInformation;

import java.lang.annotation.Annotation;
import org.eclipse.lsp4j.SymbolInformation;

public final class AnnotationCustomization<A extends Annotation> extends CodeCustomization {
private final String packageName;
Expand All @@ -16,8 +15,8 @@ public final class AnnotationCustomization<A extends Annotation> extends CodeCus
private final A annotation;

static <A extends Annotation> AnnotationCustomization<A> createClassAnnotationCustomization(Editor editor,
EclipseLanguageClient languageClient, SymbolInformation symbol, String packageName, String className,
A annotation) {
EclipseLanguageClient languageClient, SymbolInformation symbol, String packageName, String className,
A annotation) {
return new AnnotationCustomization<>(editor, languageClient, symbol, packageName, className, null, null,
annotation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@

package com.microsoft.typespec.http.client.generator.core.customization;

import com.microsoft.typespec.http.client.generator.core.customization.implementation.Utils;
import com.microsoft.typespec.http.client.generator.core.customization.implementation.ls.EclipseLanguageClient;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import org.eclipse.lsp4j.FileChangeType;
import org.eclipse.lsp4j.FileEvent;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.SymbolKind;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WorkspaceEdit;

import com.microsoft.typespec.http.client.generator.core.customization.implementation.Utils;
import com.microsoft.typespec.http.client.generator.core.customization.implementation.ls.EclipseLanguageClient;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -26,6 +18,13 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.eclipse.lsp4j.FileChangeType;
import org.eclipse.lsp4j.FileEvent;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.SymbolKind;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WorkspaceEdit;

/**
* The class level customization for an AutoRest generated class.
Expand All @@ -38,16 +37,16 @@ public final class ClassCustomization extends CodeCustomization {
* '*' or '/'. From there it captures all word and space characters before and inside '( )' ignoring any trailing
* spaces and an opening '{'.
*/
private static final Pattern METHOD_SIGNATURE_PATTERN =
Pattern.compile("^\\s*([^/*][\\w\\s]+\\([\\w\\s<>,\\.]*\\))\\s*\\{?$", Pattern.MULTILINE);
private static final Pattern METHOD_SIGNATURE_PATTERN
= Pattern.compile("^\\s*([^/*][\\w\\s]+\\([\\w\\s<>,\\.]*\\))\\s*\\{?$", Pattern.MULTILINE);

/*
* This pattern attempts to find the first line of a constructor string that doesn't have a first non-space
* character of '*' or '/', effectively the first non-Javadoc line. From there it captures all word and space
* characters before and inside '( )' ignoring any trailing spaces and an opening '{'.
*/
private static final Pattern CONSTRUCTOR_SIGNATURE_PATTERN =
Pattern.compile("^\\s*([^/*][\\w\\s]+\\([\\w\\s<>,\\.]*\\))\\s*\\{?$", Pattern.MULTILINE);
private static final Pattern CONSTRUCTOR_SIGNATURE_PATTERN
= Pattern.compile("^\\s*([^/*][\\w\\s]+\\([\\w\\s<>,\\.]*\\))\\s*\\{?$", Pattern.MULTILINE);

private static final Pattern BLOCK_OPEN = Pattern.compile("\\) *\\{");
private static final Pattern PUBLIC_MODIFIER = Pattern.compile(" *public ");
Expand All @@ -58,7 +57,7 @@ public final class ClassCustomization extends CodeCustomization {
private final String className;

ClassCustomization(Editor editor, EclipseLanguageClient languageClient, String packageName, String className,
SymbolInformation classSymbol) {
SymbolInformation classSymbol) {
super(editor, languageClient, classSymbol);

this.packageName = packageName;
Expand Down Expand Up @@ -91,6 +90,7 @@ public ClassCustomization addImports(String... imports) {
/**
* Adds a static block to the class. The {@code staticCodeBlock} should include the static keyword followed by
* the static code.
*
* @param staticCodeBlock The static code block including the static keyword.
* @return The updated {@link ClassCustomization}.
*/
Expand All @@ -116,9 +116,10 @@ public ClassCustomization addStaticBlock(String staticCodeBlock, List<String> im
int lastSymbolLine = symbol.getLocation().getRange().getStart().getLine();

// Find the last field symbol.
Optional<SymbolInformation> lastSymbol = languageClient.listDocumentSymbols(fileUri).stream()
.filter(symbol -> symbol.getKind() == SymbolKind.Field)
.reduce((first, second) -> second);
Optional<SymbolInformation> lastSymbol = languageClient.listDocumentSymbols(fileUri)
.stream()
.filter(symbol -> symbol.getKind() == SymbolKind.Field)
.reduce((first, second) -> second);

int indentAmount = INDENT_LENGTH;
if (lastSymbol.isPresent()) {
Expand All @@ -132,12 +133,12 @@ public ClassCustomization addStaticBlock(String staticCodeBlock, List<String> im
int staticBlockStartLine = lastSymbolLine + 1;
editor.insertBlankLine(fileName, staticBlockStartLine, false);
Position staticBlockPosition = editor.insertBlankLineWithIndent(fileName, staticBlockStartLine, indentAmount);
if(!staticCodeBlock.trim().startsWith("static")) {
staticCodeBlock = "static { " + System.lineSeparator() + staticCodeBlock + System.lineSeparator() + "}";
if (!staticCodeBlock.trim().startsWith("static")) {
staticCodeBlock = "static { " + System.lineSeparator() + staticCodeBlock + System.lineSeparator() + "}";
}

editor.replaceWithIndentedContent(fileName, staticBlockPosition, staticBlockPosition, staticCodeBlock,
staticBlockPosition.getCharacter());
staticBlockPosition.getCharacter());
if (importsToAdd != null) {
return Utils.addImports(importsToAdd, this, this::refreshSymbol);
}
Expand Down Expand Up @@ -169,20 +170,25 @@ public MethodCustomization getMethod(String methodNameOrSignature) {
methodName = methodNameOrSignature;
}
Optional<SymbolInformation> methodSymbol = languageClient.listDocumentSymbols(fileUri)
.stream().filter(si -> si.getKind() == SymbolKind.Method
.stream()
.filter(si -> si.getKind() == SymbolKind.Method
&& MEMBER_PARAMS.matcher(si.getName()).replaceFirst("").equals(methodName))
.filter(si -> editor.getFileLine(fileName, si.getLocation().getRange().getStart().getLine()).contains(methodNameOrSignature))
.filter(si -> editor.getFileLine(fileName, si.getLocation().getRange().getStart().getLine())
.contains(methodNameOrSignature))
.findFirst();
if (methodSymbol.isEmpty()) {
throw new IllegalArgumentException("Method " + methodNameOrSignature + " does not exist in class " + className);
throw new IllegalArgumentException(
"Method " + methodNameOrSignature + " does not exist in class " + className);
}
if (methodSignature == null) {
methodSignature = editor.getFileLine(fileName, methodSymbol.get().getLocation().getRange().getStart().getLine());
methodSignature
= editor.getFileLine(fileName, methodSymbol.get().getLocation().getRange().getStart().getLine());
methodSignature = BLOCK_OPEN.matcher(methodSignature).replaceFirst("");
methodSignature = PUBLIC_MODIFIER.matcher(methodSignature).replaceFirst("");
methodSignature = PRIVATE_MODIFIER.matcher(methodSignature).replaceFirst("");
}
return new MethodCustomization(editor, languageClient, packageName, className, methodName, methodSignature, methodSymbol.get());
return new MethodCustomization(editor, languageClient, packageName, className, methodName, methodSignature,
methodSymbol.get());
}

/**
Expand Down Expand Up @@ -214,7 +220,8 @@ public ConstructorCustomization getConstructor(String constructorNameOrSignature
}

List<SymbolInformation> constructorSymbol = languageClient.listDocumentSymbols(fileUri)
.stream().filter(si -> si.getKind() == SymbolKind.Constructor
.stream()
.filter(si -> si.getKind() == SymbolKind.Constructor
&& MEMBER_PARAMS.matcher(si.getName()).replaceFirst("").equals(constructorName))
.filter(si -> editor.getFileLine(fileName, si.getLocation().getRange().getStart().getLine())
.contains(constructorNameOrSignature))
Expand All @@ -226,12 +233,13 @@ public ConstructorCustomization getConstructor(String constructorNameOrSignature
}

if (constructorSymbol.isEmpty()) {
throw new IllegalArgumentException("Constructor " + constructorNameOrSignature + " does not exist in class "
+ className);
throw new IllegalArgumentException(
"Constructor " + constructorNameOrSignature + " does not exist in class " + className);
}

if (constructorSignature == null) {
constructorSignature = editor.getFileLine(fileName, constructorSymbol.get(0).getLocation().getRange().getStart().getLine());
constructorSignature
= editor.getFileLine(fileName, constructorSymbol.get(0).getLocation().getRange().getStart().getLine());
constructorSignature = BLOCK_OPEN.matcher(constructorSignature).replaceFirst("");
constructorSignature = PUBLIC_MODIFIER.matcher(constructorSignature).replaceFirst("");
constructorSignature = PRIVATE_MODIFIER.matcher(constructorSignature).replaceFirst("");
Expand All @@ -250,7 +258,8 @@ public ConstructorCustomization getConstructor(String constructorNameOrSignature
*/
public PropertyCustomization getProperty(String propertyName) {
Optional<SymbolInformation> propertySymbol = languageClient.listDocumentSymbols(fileUri)
.stream().filter(si -> si.getKind() == SymbolKind.Field && si.getName().equals(propertyName))
.stream()
.filter(si -> si.getKind() == SymbolKind.Field && si.getName().equals(propertyName))
.findFirst();

if (propertySymbol.isEmpty()) {
Expand All @@ -271,7 +280,8 @@ public PropertyCustomization getProperty(String propertyName) {
*/
public ConstantCustomization getConstant(String constantName) {
Optional<SymbolInformation> propertySymbol = languageClient.listDocumentSymbols(fileUri)
.stream().filter(si -> si.getKind() == SymbolKind.Constant && si.getName().equals(constantName))
.stream()
.filter(si -> si.getKind() == SymbolKind.Constant && si.getName().equals(constantName))
.findFirst();

if (propertySymbol.isEmpty()) {
Expand Down Expand Up @@ -319,7 +329,8 @@ public ConstructorCustomization addConstructor(String constructor, List<String>
}

// Find all constructor and field symbols.
List<SymbolInformation> constructorLocationFinder = languageClient.listDocumentSymbols(fileUri).stream()
List<SymbolInformation> constructorLocationFinder = languageClient.listDocumentSymbols(fileUri)
.stream()
.filter(symbol -> symbol.getKind() == SymbolKind.Field || symbol.getKind() == SymbolKind.Constructor)
.collect(Collectors.toList());

Expand Down Expand Up @@ -457,15 +468,16 @@ public ClassCustomization removeMethod(String methodNameOrSignature) {
* @return The current ClassCustomization.
*/
public ClassCustomization rename(String newName) {
WorkspaceEdit workspaceEdit = languageClient.renameSymbol(fileUri,
symbol.getLocation().getRange().getStart(), newName);
WorkspaceEdit workspaceEdit
= languageClient.renameSymbol(fileUri, symbol.getLocation().getRange().getStart(), newName);
List<FileEvent> changes = new ArrayList<>();
for (Map.Entry<String, List<TextEdit>> edit : workspaceEdit.getChanges().entrySet()) {
int i = edit.getKey().indexOf("src/main/java/");
String oldEntry = edit.getKey().substring(i);
if (editor.getContents().containsKey(oldEntry)) {
for (TextEdit textEdit : edit.getValue()) {
editor.replace(oldEntry, textEdit.getRange().getStart(), textEdit.getRange().getEnd(), textEdit.getNewText());
editor.replace(oldEntry, textEdit.getRange().getStart(), textEdit.getRange().getEnd(),
textEdit.getNewText());
}
FileEvent fileEvent = new FileEvent();
fileEvent.setUri(edit.getKey());
Expand All @@ -489,7 +501,8 @@ public ClassCustomization rename(String newName) {

String packagePath = packageName.replace(".", "/");
Optional<SymbolInformation> newClassSymbol = languageClient.findWorkspaceSymbol(newName)
.stream().filter(si -> si.getLocation().getUri().endsWith(packagePath + "/" + newName + ".java"))
.stream()
.filter(si -> si.getLocation().getUri().endsWith(packagePath + "/" + newName + ".java"))
.findFirst();
if (newClassSymbol.isEmpty()) {
throw new IllegalArgumentException("Renamed failed with new class " + newName + " not found.");
Expand All @@ -512,7 +525,8 @@ public ClassCustomization rename(String newName) {
*/
public ClassCustomization setModifier(int modifiers) {
languageClient.listDocumentSymbols(symbol.getLocation().getUri())
.stream().filter(si -> si.getKind() == SymbolKind.Class && si.getName().equals(className))
.stream()
.filter(si -> si.getKind() == SymbolKind.Class && si.getName().equals(className))
.findFirst()
.ifPresent(symbolInformation -> Utils.replaceModifier(symbolInformation, editor, languageClient,
"(?:.+ )?class " + className, "class " + className, Modifier.classModifiers(), modifiers));
Expand All @@ -532,7 +546,8 @@ public ClassCustomization addAnnotation(String annotation) {
}

Optional<SymbolInformation> symbol = languageClient.listDocumentSymbols(fileUri)
.stream().filter(si -> si.getKind() == SymbolKind.Class)
.stream()
.filter(si -> si.getKind() == SymbolKind.Class)
.findFirst();
if (symbol.isPresent()) {
if (editor.getContents().containsKey(fileName)) {
Expand All @@ -559,8 +574,11 @@ public ClassCustomization addAnnotation(String annotation) {
* @return the current class customization for chaining
*/
public ClassCustomization removeAnnotation(String annotation) {
return Utils.removeAnnotation(this, compilationUnit -> compilationUnit.getClassByName(className).get()
.getAnnotationByName(Utils.cleanAnnotationName(annotation)), this::refreshSymbol);
return Utils.removeAnnotation(this,
compilationUnit -> compilationUnit.getClassByName(className)
.get()
.getAnnotationByName(Utils.cleanAnnotationName(annotation)),
this::refreshSymbol);
}

/**
Expand Down
Loading

0 comments on commit 08384aa

Please sign in to comment.