Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1332,9 +1332,12 @@ public Void visitAnnotatedType(AnnotatedTypeTree node, Void unused) {
return null;
}

// TODO(cushon): Use Flags.COMPACT_RECORD_CONSTRUCTOR once if/when we drop support for Java 11
// TODO(cushon): Use Flags if/when we drop support for Java 11

protected static final long COMPACT_RECORD_CONSTRUCTOR = 1L << 51;

protected static final long RECORD = 1L << 61;

@Override
public Void visitMethod(MethodTree node, Void unused) {
sync(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package com.google.googlejavaformat.java.java14;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.MoreCollectors.onlyElement;
import static com.google.common.collect.MoreCollectors.toOptional;

import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
import com.google.googlejavaformat.Op;
import com.google.googlejavaformat.OpsBuilder;
import com.google.googlejavaformat.java.JavaInputAstVisitor;
Expand All @@ -26,13 +27,13 @@
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.InstanceOfTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.SwitchExpressionTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.YieldTree;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.TreeInfo;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -112,20 +113,17 @@ public void visitRecordDeclaration(ClassTree node) {
if (!node.getTypeParameters().isEmpty()) {
typeParametersRest(node.getTypeParameters(), hasSuperInterfaceTypes ? plusFour : ZERO);
}
MethodTree constructor =
node.getMembers().stream()
.filter(JCMethodDecl.class::isInstance)
.map(JCMethodDecl.class::cast)
.filter(
m -> (m.mods.flags & COMPACT_RECORD_CONSTRUCTOR) == COMPACT_RECORD_CONSTRUCTOR)
.collect(onlyElement());
ImmutableList<JCVariableDecl> parameters =
compactRecordConstructor(node)
.map(m -> ImmutableList.copyOf(m.getParameters()))
.orElseGet(() -> recordVariables(node));
token("(");
if (!constructor.getParameters().isEmpty() || constructor.getReceiverParameter() != null) {
if (!parameters.isEmpty()) {
// Break before args.
builder.breakToFill("");
}
visitFormals(
Optional.ofNullable(constructor.getReceiverParameter()), constructor.getParameters());
// record headers can't declare receiver parameters
visitFormals(/* receiver= */ Optional.empty(), parameters);
token(")");
if (hasSuperInterfaceTypes) {
builder.breakToFill(" ");
Expand Down Expand Up @@ -157,6 +155,22 @@ public void visitRecordDeclaration(ClassTree node) {
dropEmptyDeclarations();
}

private static Optional<JCMethodDecl> compactRecordConstructor(ClassTree node) {
return node.getMembers().stream()
.filter(JCMethodDecl.class::isInstance)
.map(JCMethodDecl.class::cast)
.filter(m -> (m.mods.flags & COMPACT_RECORD_CONSTRUCTOR) == COMPACT_RECORD_CONSTRUCTOR)
.collect(toOptional());
}

private static ImmutableList<JCVariableDecl> recordVariables(ClassTree node) {
return node.getMembers().stream()
.filter(JCVariableDecl.class::isInstance)
.map(JCVariableDecl.class::cast)
.filter(m -> (m.mods.flags & RECORD) == RECORD)
.collect(toImmutableList());
}

@Override
public Void visitInstanceOf(InstanceOfTree node, Void unused) {
sync(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class Java14 {
}
}

record Foo(int id) {}

record Rcv(int id) {
public Rcv(Rcv this, int id) {
this.id = id;
}
}

void g() {
var block = """
hello
Expand All @@ -35,4 +43,4 @@ class Java14 {
case WEDNESDAY -> 9;
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class Java14 {
}
}

record Foo(int id) {}

record Rcv(int id) {
public Rcv(Rcv this, int id) {
this.id = id;
}
}

void g() {
var block = """
hello
Expand Down
8 changes: 4 additions & 4 deletions idea_plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

plugins {
id "org.jetbrains.intellij" version "0.4.11"
id "org.jetbrains.intellij" version "0.4.18"
}

repositories {
Expand All @@ -31,14 +31,14 @@ apply plugin: 'java'

intellij {
pluginName = "google-java-format"
version = "193.4932.9-EAP-SNAPSHOT"
version = "2020.1"
}

patchPluginXml {
pluginDescription = "Formats source code using the google-java-format tool. This version of " +
"the plugin uses version ${googleJavaFormatVersion} of the tool."
version = "${googleJavaFormatVersion}.0.3"
sinceBuild = '173'
version = "${googleJavaFormatVersion}.0.5"
sinceBuild = '201'
untilBuild = ''
}

Expand Down
23 changes: 10 additions & 13 deletions idea_plugin/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

<change-notes><![CDATA[
<dl>
<dt>1.7.0.5</dt>
<dd>Added a version for 2020.1+ IDEs.</dd>
<dt>1.7.0.4</dt>
<dd>Marked the plugin as being incompatible with 2020.1+ IDEs.</dd>
<dt>1.7.0.3</dt>
<dd>Fixed the plugin on 2019.3 IDEs.</dd>
<dt>1.7.0.2</dt>
Expand All @@ -23,19 +27,12 @@
</dl>
]]></change-notes>

<project-components>
<component>
<implementation-class>
com.google.googlejavaformat.intellij.GoogleJavaFormatInstaller
</implementation-class>
<loadForDefaultProject/>
</component>
<component>
<implementation-class>
com.google.googlejavaformat.intellij.InitialConfigurationComponent
</implementation-class>
</component>
</project-components>
<applicationListeners>
<listener class="com.google.googlejavaformat.intellij.InitialConfigurationProjectManagerListener"
topic="com.intellij.openapi.project.ProjectManagerListener"/>
<listener class="com.google.googlejavaformat.intellij.GoogleJavaFormatInstaller"
topic="com.intellij.openapi.project.ProjectManagerListener"/>
</applicationListeners>

<extensions defaultExtensionNs="com.intellij">
<projectConfigurable instance="com.google.googlejavaformat.intellij.GoogleJavaFormatConfigurable"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,25 @@

package com.google.googlejavaformat.intellij;

import static com.google.common.base.Preconditions.checkState;

import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.application.impl.ApplicationInfoImpl;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.ide.plugins.PluginManagerCore;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.serviceContainer.PlatformComponentManagerImpl;
import org.picocontainer.MutablePicoContainer;
import com.intellij.serviceContainer.ComponentManagerImpl;
import org.jetbrains.annotations.NotNull;

/**
* A component that replaces the default IntelliJ {@link CodeStyleManager} with one that formats via
* google-java-format.
*/
final class GoogleJavaFormatInstaller implements ProjectComponent {

private static final String CODE_STYLE_MANAGER_KEY = CodeStyleManager.class.getName();

private final Project project;

private GoogleJavaFormatInstaller(Project project) {
this.project = project;
}
final class GoogleJavaFormatInstaller implements ProjectManagerListener {

@Override
public void projectOpened() {
public void projectOpened(@NotNull Project project) {
installFormatter(project);
}

Expand All @@ -57,20 +49,9 @@ private static void installFormatter(Project project) {
}

private static void setManager(Project project, CodeStyleManager newManager) {
if (useNewServicesApi()) {
PlatformComponentManagerImpl platformComponentManager =
(PlatformComponentManagerImpl) project;
IdeaPluginDescriptor plugin = PluginManager.getPlugin(PluginId.getId("google-java-format"));
platformComponentManager.registerServiceInstance(CodeStyleManager.class, newManager, plugin);
} else {
MutablePicoContainer container = (MutablePicoContainer) project.getPicoContainer();
container.unregisterComponent(CODE_STYLE_MANAGER_KEY);
container.registerComponentInstance(CODE_STYLE_MANAGER_KEY, newManager);
}
}

private static boolean useNewServicesApi() {
ApplicationInfo appInfo = ApplicationInfoImpl.getInstance();
return appInfo.getBuild().getBaselineVersion() >= 193;
ComponentManagerImpl platformComponentManager = (ComponentManagerImpl) project;
IdeaPluginDescriptor plugin = PluginManagerCore.getPlugin(PluginId.getId("google-java-format"));
checkState(plugin != null, "Couldn't locate our own PluginDescriptor.");
platformComponentManager.registerServiceInstance(CodeStyleManager.class, newManager, plugin);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,28 @@
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManagerListener;
import org.jetbrains.annotations.NotNull;

final class InitialConfigurationComponent implements ProjectComponent {
final class InitialConfigurationProjectManagerListener implements ProjectManagerListener {

private static final String NOTIFICATION_TITLE = "Enable google-java-format";
private static final NotificationGroup NOTIFICATION_GROUP =
new NotificationGroup(NOTIFICATION_TITLE, NotificationDisplayType.STICKY_BALLOON, true);

private final Project project;
private final GoogleJavaFormatSettings settings;
@Override
public void projectOpened(@NotNull Project project) {

public InitialConfigurationComponent(Project project, GoogleJavaFormatSettings settings) {
this.project = project;
this.settings = settings;
}
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);

@Override
public void projectOpened() {
if (settings.isUninitialized()) {
settings.setEnabled(false);
displayNewUserNotification();
displayNewUserNotification(project, settings);
}
}

private void displayNewUserNotification() {
private void displayNewUserNotification(Project project, GoogleJavaFormatSettings settings) {
Notification notification =
new Notification(
NOTIFICATION_GROUP.getDisplayId(),
Expand Down