Skip to content

Commit

Permalink
#36 Added Eclipse Javascript formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
funfried committed Mar 3, 2020
1 parent a9d089a commit 00439c7
Show file tree
Hide file tree
Showing 53 changed files with 2,483 additions and 488 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ External Java Code Formatters for NetBeans
(formerly known as Eclipse Java Code Formatter for NetBeans)

## What is External Java Code Formatters Plugin for NetBeans?
This plugin helps you to apply to a common code style in a team of Eclipse JDT, IntelliJ
and NetBeans IDE users. You can either use the [Google Code Formatter](https://github.com/google/google-java-format),
the [Eclipse Java Code Formatter](https://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Fguide%2Fjdt_api_codeformatter.htm),
or the [Spring Java Formatter](https://github.com/spring-io/spring-javaformat) inside the
NetBeans IDE, depending on whatever your co-workers use. The original formatting engine
of Eclipse, the Google Code Formatter and the Spring Java formatter are embedded and
allow you to format the source code the same way as e.g. your Eclipse co-workers do.
This plugin helps you to apply to a common code style in a team of Eclipse,
IntelliJ and NetBeans IDE users. You can use several external code formatters
inside the NetBeans IDE (see below), depending on whatever your co-workers use.
The original formatting engine of Eclipse, the Google Code Formatter, the Spring
Java Formatter or any other are embedded and allow you to format the source code
the same way as e.g. your co-workers do with their Eclipse IDE.

![Global settings](/src/site/resources/imgs/global.png)
![Project settings](/src/site/resources/imgs/project.png)

### Supported formatters
* Java
** [Google Code Formatter](https://github.com/google/google-java-format)
** [Eclipse Java Code Formatter](https://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Fguide%2Fjdt_api_codeformatter.htm)
** [Spring Java Formatter](https://github.com/spring-io/spring-javaformat)
* Javascript
** [Eclipse Javascript Code Formatter](https://help.eclipse.org/2019-12/index.jsp?topic=%2Forg.eclipse.wst.jsdt.doc%2Fstarted%2Foverview.html&cp%3D52_0)

### Features
* Global configuration and project specific configuration
* On save action (Disabled by default)
Expand All @@ -42,6 +49,7 @@ allow you to format the source code the same way as e.g. your Eclipse co-workers
* Support for guarded documents (documents that are created by the NetBeans GUI builder, guarded blocks are skipped of course, but everything in between can be formatted), only for Eclipse Java Code Formatter (since 1.13)
* Support for Google code formatter (since 1.13)
* Support for Spring Java formatter (since 1.14)
* Support for Eclipse Javascript formatter (since 1.14)

## Compatibility
Compatible with NetBeans 8.2+ and JDK8+.
Expand Down
622 changes: 311 additions & 311 deletions eclipse_formatter_template.xml

Large diffs are not rendered by default.

24 changes: 15 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@
</dependency>

<!-- Eclipse JavaScript Formatter -->
<!-- <dependency>
<dependency>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>jsdt-core</artifactId>
<version>2.6.0</version>
</dependency>-->
<version>2.7.0</version>
</dependency>

<!-- Spring Formatter -->
<dependency>
Expand All @@ -236,12 +236,6 @@
</exclusions>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.2-jre</version>
</dependency>

<!--
Only needed by NetBeans 8.2, if NetBeans 8.2 is not
supported anymore, this dependency can be removed
Expand Down Expand Up @@ -280,6 +274,12 @@
<version>${netbeans.version}</version>
</dependency>

<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor-util</artifactId>
<version>${netbeans.version}</version>
</dependency>

<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor</artifactId>
Expand Down Expand Up @@ -340,6 +340,12 @@
<version>${netbeans.version}</version>
</dependency>

<!-- <dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-csl-api</artifactId>
<version>${netbeans.version}</version>
</dependency>-->

<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-openide-awt</artifactId>
Expand Down
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<action dev="bahlef" type="add" issue="101">
Added Spring Java Formatter
</action>
<action dev="bahlef" type="add" issue="36">
Added Eclipse Javascript Formatter
</action>
</release>

<release version="1.13.0" date="2020-01-09" description="First release from funfried aka bahlef aka fbahle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Contributors:
* bahlef - initial API and implementation and/or initial documentation
*/
package de.funfried.netbeans.plugins.external.formatter.base;
package de.funfried.netbeans.plugins.external.formatter;

import java.io.IOException;
import java.io.StringReader;
Expand All @@ -20,14 +20,15 @@
import javax.swing.text.StyledDocument;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.tuple.Pair;
import org.netbeans.api.annotations.common.NonNull;
import org.netbeans.api.diff.Difference;
import org.netbeans.api.editor.guards.GuardedSection;
import org.netbeans.api.editor.guards.GuardedSectionManager;
import org.netbeans.lib.editor.util.swing.DocumentUtilities;
import org.openide.text.NbDocument;
import org.openide.util.Exceptions;

import de.funfried.netbeans.plugins.external.formatter.ui.editor.diff.Diff;

Expand Down Expand Up @@ -150,13 +151,7 @@ protected boolean setFormattedCode(String code, String formattedContent) throws
* @return The content of the {@code document}
*/
protected String getCode() {
try {
return document.getText(0, document.getLength());
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);

throw new UnsupportedOperationException(ex);
}
return StringUtils.trim(DocumentUtilities.getText(document).toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* bahlef - initial API and implementation and/or initial documentation
*/

package de.funfried.netbeans.plugins.external.formatter.base;
package de.funfried.netbeans.plugins.external.formatter;

import javax.swing.text.BadLocationException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Contributors:
* bahlef - initial API and implementation and/or initial documentation
*/
package de.funfried.netbeans.plugins.external.formatter.base;
package de.funfried.netbeans.plugins.external.formatter;

import java.util.Objects;
import java.util.SortedSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Saad Mufti <saad.mufti@teamaol.com>
* bahlef
*/
package de.funfried.netbeans.plugins.external.formatter.base;
package de.funfried.netbeans.plugins.external.formatter;

import java.util.Collection;
import java.util.Objects;
Expand All @@ -26,9 +26,11 @@
import org.apache.commons.lang3.tuple.Pair;
import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.api.annotations.common.NonNull;
import org.netbeans.modules.editor.NbEditorUtilities;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;

import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;

/**
Expand Down Expand Up @@ -93,6 +95,8 @@ public boolean format(StyledDocument document, SortedSet<Pair<Integer, Integer>>
formatterService.format(document, changedElements);
} catch (BadLocationException ex) {
log.log(Level.SEVERE, formatterService.getDisplayName() + " failed to format the code", ex);
} catch (FormattingFailedException ex) {
log.log(Level.INFO, formatterService.getDisplayName() + " failed to format the code", ex);
}

return true;
Expand Down Expand Up @@ -219,11 +223,13 @@ public Integer getSpacesPerTab(Document document) {
* for the given {@code document}. If the internal NetBeans formatter is configured
* or the implementation of the configured {@link FormatterService} could
* not be found {@code null} will be returned
*
* @throws Exception if an error occurs
*/
@CheckForNull
private FormatterService getActiveFormatterService(Document document) {
private FormatterService getActiveFormatterService(Document document) throws Exception {
Preferences prefs = Settings.getActivePreferences(document);
String activeFormatterId = prefs.get(Settings.ENABLED_FORMATTER, Settings.DEFAULT_FORMATTER);
String activeFormatterId = prefs.get(Settings.ENABLED_FORMATTER_PREFIX + NbEditorUtilities.getMimeType(document), Settings.DEFAULT_FORMATTER);

Collection<? extends FormatterService> formatterServices = Lookup.getDefault().lookupAll(FormatterService.class);
for (FormatterService formatterService : formatterServices) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2020 bahlef.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
* Contributors:
* bahlef - implementation and/or initial documentation
*/
package de.funfried.netbeans.plugins.external.formatter.exceptions;

/**
* {@link RuntimeException} which is thrown when an external formatter
* failed to format a given code.
*
* @author bahlef
*/
public class FormattingFailedException extends RuntimeException {
private static final long serialVersionUID = 1L;

/**
* Creates a new instance of {@link FormattingFailedException}.
*
* @param message the detail message
*/
public FormattingFailedException(String message) {
super(message);
}

/**
* Creates a new instance of {@link FormattingFailedException}.
*
* @param cause the original cause of the exception
*/
public FormattingFailedException(Throwable cause) {
super(cause);
}

/**
* Creates a new instance of {@link FormattingFailedException}.
*
* @param message the detail message
* @param cause the original cause of the exception
*/
public FormattingFailedException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Contributors:
* bahlef - initial API and implementation and/or initial documentation
*/
package de.funfried.netbeans.plugins.external.formatter.base.java;
package de.funfried.netbeans.plugins.external.formatter.java.base;

import java.util.SortedSet;

Expand All @@ -17,8 +17,8 @@
import org.apache.commons.lang3.tuple.Pair;
import org.netbeans.api.java.lexer.JavaTokenId;

import de.funfried.netbeans.plugins.external.formatter.base.FormatJob;
import de.funfried.netbeans.plugins.external.formatter.base.FormatterService;
import de.funfried.netbeans.plugins.external.formatter.FormatJob;
import de.funfried.netbeans.plugins.external.formatter.FormatterService;

/**
* Abstract base implementation of {@link FormatterService} for Java formatters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
/**
* Package containing classes for common Java formatter logic.
*/
package de.funfried.netbeans.plugins.external.formatter.base.java;
package de.funfried.netbeans.plugins.external.formatter.java.base;
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
import org.openide.awt.NotificationDisplayer;
import org.openide.awt.StatusDisplayer;

import de.funfried.netbeans.plugins.external.formatter.base.AbstractFormatJob;
import de.funfried.netbeans.plugins.external.formatter.AbstractFormatJob;
import de.funfried.netbeans.plugins.external.formatter.exceptions.CannotLoadConfigurationException;
import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
import de.funfried.netbeans.plugins.external.formatter.exceptions.ProfileNotFoundException;
import de.funfried.netbeans.plugins.external.formatter.ui.Icons;
import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
Expand Down Expand Up @@ -97,6 +98,8 @@ public void format() throws BadLocationException {
formatterFile),
NotifyDescriptor.ERROR_MESSAGE);
DialogDisplayer.getDefault().notify(notify);

StatusDisplayer.getDefault().setStatusText(String.format("Profile '%s' not found in %s", formatterProfile, formatterFile));
});

throw ex;
Expand All @@ -105,6 +108,14 @@ public void format() throws BadLocationException {
NotifyDescriptor notify = new NotifyDescriptor.Message(String.format("<html>Could not find configuration file %s.<br>Make sure the file exists and it can be read.", formatterFile),
NotifyDescriptor.ERROR_MESSAGE);
DialogDisplayer.getDefault().notify(notify);

StatusDisplayer.getDefault().setStatusText(String.format("Could not find configuration file %s. Make sure the file exists and it can be read.", formatterFile));
});

throw ex;
} catch (FormattingFailedException ex) {
SwingUtilities.invokeLater(() -> {
StatusDisplayer.getDefault().setStatusText("Failed to format using Eclipse formatter: " + ex.getMessage());
});

throw ex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;

import de.funfried.netbeans.plugins.external.formatter.base.FormatJob;
import de.funfried.netbeans.plugins.external.formatter.base.FormatterService;
import de.funfried.netbeans.plugins.external.formatter.base.java.AbstractJavaFormatterService;
import de.funfried.netbeans.plugins.external.formatter.FormatJob;
import de.funfried.netbeans.plugins.external.formatter.FormatterService;
import de.funfried.netbeans.plugins.external.formatter.java.base.AbstractJavaFormatterService;
import de.funfried.netbeans.plugins.external.formatter.ui.options.FormatterOptionsPanel;
import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;

Expand Down

0 comments on commit 00439c7

Please sign in to comment.