Skip to content

Commit 732ab99

Browse files
committed
Added Palantir Java formatter, DBeaver SQL formatter (via spotless) and Vertical Blank SQL formatter
1 parent 0c21c09 commit 732ab99

32 files changed

Lines changed: 2394 additions & 2 deletions

pom.xml

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<groupId>de.funfried.netbeans.plugins</groupId>
1919
<artifactId>externalcodeformatter</artifactId>
20-
<version>1.14.3</version>
20+
<version>1.15.0-SNAPSHOT</version>
2121
<packaging>nbm</packaging>
2222

2323
<developers>
@@ -122,6 +122,24 @@
122122
</properties>
123123

124124
<dependencies>
125+
<!-- <dependency>
126+
<groupId>org.apache.maven</groupId>
127+
<artifactId>maven-core</artifactId>
128+
<version>3.8.4</version>
129+
</dependency>
130+
131+
<dependency>
132+
<groupId>org.apache.maven</groupId>
133+
<artifactId>maven-model</artifactId>
134+
<version>3.8.4</version>
135+
</dependency>
136+
137+
<dependency>
138+
<groupId>com.pinterest.ktlint</groupId>
139+
<artifactId>ktlint-core</artifactId>
140+
<version>0.43.2</version>
141+
</dependency>-->
142+
125143
<!-- Eclipse Formatter -->
126144
<dependency>
127145
<groupId>org.eclipse.platform</groupId>
@@ -157,6 +175,32 @@
157175
<version>${google-java-format.version}</version>
158176
</dependency>
159177

178+
<!-- Palantir Formatter -->
179+
<dependency>
180+
<groupId>com.palantir.javaformat</groupId>
181+
<artifactId>palantir-java-format</artifactId>
182+
<version>2.16.0</version>
183+
</dependency>
184+
185+
<dependency>
186+
<groupId>com.palantir.javaformat</groupId>
187+
<artifactId>palantir-java-format-spi</artifactId>
188+
<version>2.16.0</version>
189+
<exclusions>
190+
<exclusion>
191+
<groupId>com.fasterxml.jackson.core</groupId>
192+
<artifactId>*</artifactId>
193+
</exclusion>
194+
</exclusions>
195+
</dependency>
196+
197+
<!-- Spotless Formatter -->
198+
<dependency>
199+
<groupId>com.diffplug.spotless</groupId>
200+
<artifactId>spotless-lib</artifactId>
201+
<version>2.22.0</version>
202+
</dependency>
203+
160204
<!-- XML Formatter -->
161205
<dependency>
162206
<groupId>net.revelc.code.formatter</groupId>
@@ -190,6 +234,13 @@
190234
<version>0.1.9</version>
191235
</dependency>
192236

237+
<!-- Vertical Blank SQL Formatter -->
238+
<dependency>
239+
<groupId>com.github.vertical-blank</groupId>
240+
<artifactId>sql-formatter</artifactId>
241+
<version>2.0.3</version>
242+
</dependency>
243+
193244
<!-- NetBeans -->
194245
<dependency>
195246
<groupId>org.netbeans.api</groupId>

src/changes/changes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
</properties>
99

1010
<body>
11+
<release version="1.15.0-SNAPSHOT" date="N/A" description="Additional formatters">
12+
<action dev="bahlef" type="add">
13+
Additional formatters
14+
</action>
15+
</release>
16+
1117
<release version="1.14.3" date="2022-01-22" description="Bug fix release">
1218
<action dev="bahlef" type="fix" issue="152">
1319
Erroneous formatting with Eclipse
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2020 bahlef.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v2.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v20.html
7+
* Contributors:
8+
* bahlef - initial API and implementation and/or initial documentation
9+
*/
10+
package de.funfried.netbeans.plugins.external.formatter.java.palantir;
11+
12+
import java.util.SortedSet;
13+
import java.util.prefs.Preferences;
14+
15+
import javax.swing.SwingUtilities;
16+
import javax.swing.text.BadLocationException;
17+
import javax.swing.text.StyledDocument;
18+
19+
import org.apache.commons.lang3.tuple.Pair;
20+
import org.openide.awt.NotificationDisplayer;
21+
import org.openide.awt.StatusDisplayer;
22+
23+
import de.funfried.netbeans.plugins.external.formatter.AbstractFormatJob;
24+
import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
25+
import de.funfried.netbeans.plugins.external.formatter.ui.Icons;
26+
import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
27+
28+
/**
29+
* Palantir formatter implementation of the {@link AbstractFormatJob} to
30+
* format a given document using the {@link PalantirJavaFormatterWrapper}.
31+
*
32+
* @author bahlef
33+
*/
34+
class PalantirFormatJob extends AbstractFormatJob {
35+
/** The {@link PalantirJavaFormatterWrapper} implementation. */
36+
private final PalantirJavaFormatterWrapper formatter;
37+
38+
/**
39+
* Package private constructor to create a new instance of {@link PalantirFormatJob}.
40+
*
41+
* @param document the {@link StyledDocument} which sould be formatted
42+
* @param formatter the {@link PalantirJavaFormatterWrapper} to use
43+
* @param changedElements the ranges which should be formatted
44+
*/
45+
PalantirFormatJob(StyledDocument document, PalantirJavaFormatterWrapper formatter, SortedSet<Pair<Integer, Integer>> changedElements) {
46+
super(document, changedElements);
47+
48+
this.formatter = formatter;
49+
}
50+
51+
/**
52+
* {@inheritDoc}
53+
*/
54+
@Override
55+
public void format() throws BadLocationException {
56+
Preferences pref = Settings.getActivePreferences(document);
57+
58+
String code = getCode();
59+
60+
SortedSet<Pair<Integer, Integer>> regions = getFormatableSections(code);
61+
62+
try {
63+
String formattedContent = formatter.format(code, regions);
64+
if (setFormattedCode(code, formattedContent)) {
65+
SwingUtilities.invokeLater(() -> {
66+
if (pref.getBoolean(Settings.SHOW_NOTIFICATIONS, false)) {
67+
NotificationDisplayer.getDefault().notify("Format using Palantir formatter", Icons.ICON_EXTERNAL, "", null);
68+
}
69+
70+
StatusDisplayer.getDefault().setStatusText("Format using Palantir formatter");
71+
});
72+
}
73+
} catch (FormattingFailedException ex) {
74+
SwingUtilities.invokeLater(() -> {
75+
StatusDisplayer.getDefault().setStatusText("Failed to format using Palantir formatter: " + ex.getMessage());
76+
});
77+
78+
throw ex;
79+
}
80+
}
81+
}
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
/*
2+
* Copyright (c) 2020 bahlef.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v2.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v20.html
7+
* Contributors:
8+
* bahlef - initial API and implementation and/or initial documentation
9+
*/
10+
package de.funfried.netbeans.plugins.external.formatter.java.palantir;
11+
12+
import java.util.SortedSet;
13+
import java.util.prefs.Preferences;
14+
15+
import javax.swing.text.Document;
16+
import javax.swing.text.StyledDocument;
17+
18+
import org.apache.commons.lang3.tuple.Pair;
19+
import org.netbeans.api.annotations.common.CheckForNull;
20+
import org.netbeans.api.annotations.common.NonNull;
21+
import org.netbeans.api.editor.guards.GuardedSectionManager;
22+
import org.netbeans.api.project.Project;
23+
import org.openide.util.NbBundle;
24+
import org.openide.util.lookup.ServiceProvider;
25+
26+
import com.palantir.javaformat.java.JavaFormatterOptions;
27+
28+
import de.funfried.netbeans.plugins.external.formatter.FormatJob;
29+
import de.funfried.netbeans.plugins.external.formatter.FormatterService;
30+
import de.funfried.netbeans.plugins.external.formatter.java.base.AbstractJavaFormatterService;
31+
import de.funfried.netbeans.plugins.external.formatter.java.palantir.ui.PalantirJavaFormatterOptionsPanel;
32+
import de.funfried.netbeans.plugins.external.formatter.ui.options.FormatterOptionsPanel;
33+
import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
34+
35+
/**
36+
* Palantir implementation of the {@link AbstractJavaFormatterService}.
37+
*
38+
* @author bahlef
39+
*/
40+
@NbBundle.Messages({
41+
"FormatterName=Palantir Java Code Formatter"
42+
})
43+
@ServiceProvider(service = FormatterService.class, position = 2000)
44+
public class PalantirJavaFormatterService extends AbstractJavaFormatterService {
45+
/** The ID of this formatter service. */
46+
public static final String ID = "palantir-java-formatter";
47+
48+
/** * The {@link PalantirJavaFormatterWrapper} implementation. */
49+
private final PalantirJavaFormatterWrapper formatter = new PalantirJavaFormatterWrapper();
50+
51+
/**
52+
* {@inheritDoc}
53+
*/
54+
@Override
55+
public boolean canHandle(Document document) {
56+
// Cannot handle guarded blocks properly due to a bug in the Google Java Code Formatter:
57+
// https://github.com/google/google-java-format/issues/433
58+
if (document instanceof StyledDocument) {
59+
StyledDocument styledDoc = (StyledDocument) document;
60+
61+
if (GuardedSectionManager.getInstance(styledDoc) != null) {
62+
return false;
63+
}
64+
}
65+
66+
return super.canHandle(document);
67+
}
68+
69+
/**
70+
* {@inheritDoc}
71+
*/
72+
@NonNull
73+
@Override
74+
public String getDisplayName() {
75+
return NbBundle.getMessage(PalantirJavaFormatterService.class, "FormatterName");
76+
}
77+
78+
/**
79+
* {@inheritDoc}
80+
*/
81+
@NonNull
82+
@Override
83+
public String getId() {
84+
return ID;
85+
}
86+
87+
/**
88+
* {@inheritDoc}
89+
*/
90+
@Override
91+
public FormatterOptionsPanel createOptionsPanel(Project project) {
92+
return new PalantirJavaFormatterOptionsPanel(project);
93+
}
94+
95+
/**
96+
* {@inheritDoc}
97+
*/
98+
@CheckForNull
99+
@Override
100+
public Integer getContinuationIndentSize(Document document) {
101+
if (document == null) {
102+
return null;
103+
}
104+
105+
Integer ret = null;
106+
107+
Preferences preferences = Settings.getActivePreferences(document);
108+
if (isUseFormatterIndentationSettings(preferences)) {
109+
ret = 8;
110+
}
111+
112+
return ret;
113+
}
114+
115+
/**
116+
* {@inheritDoc}
117+
*/
118+
@CheckForNull
119+
@Override
120+
public Integer getIndentSize(Document document) {
121+
if (document == null) {
122+
return null;
123+
}
124+
125+
Integer ret = null;
126+
127+
Preferences preferences = Settings.getActivePreferences(document);
128+
if (isUseFormatterIndentationSettings(preferences)) {
129+
ret = 4;
130+
}
131+
132+
return ret;
133+
}
134+
135+
/**
136+
* {@inheritDoc}
137+
*/
138+
@CheckForNull
139+
@Override
140+
public Integer getRightMargin(Document document) {
141+
if (document == null) {
142+
return null;
143+
}
144+
145+
return JavaFormatterOptions.Style.PALANTIR.maxLineLength();
146+
}
147+
148+
/**
149+
* {@inheritDoc}
150+
*/
151+
@Override
152+
protected FormatJob getFormatJob(StyledDocument document, SortedSet<Pair<Integer, Integer>> changedElements) {
153+
return new PalantirFormatJob(document, formatter, changedElements);
154+
}
155+
156+
/**
157+
* {@inheritDoc}
158+
*/
159+
@CheckForNull
160+
@Override
161+
public Integer getSpacesPerTab(Document document) {
162+
if (document == null) {
163+
return null;
164+
}
165+
166+
Integer ret = null;
167+
168+
Preferences preferences = Settings.getActivePreferences(document);
169+
if (isUseFormatterIndentationSettings(preferences)) {
170+
if (preferences.getBoolean(Settings.OVERRIDE_TAB_SIZE, true)) {
171+
ret = preferences.getInt(Settings.OVERRIDE_TAB_SIZE_VALUE, 4);
172+
} else {
173+
ret = 4;
174+
}
175+
}
176+
177+
return ret;
178+
}
179+
180+
/**
181+
* {@inheritDoc}
182+
*/
183+
@CheckForNull
184+
@Override
185+
public Boolean isExpandTabToSpaces(Document document) {
186+
if (document == null) {
187+
return null;
188+
}
189+
190+
Boolean ret = null;
191+
192+
Preferences preferences = Settings.getActivePreferences(document);
193+
if (isUseFormatterIndentationSettings(preferences)) {
194+
ret = true;
195+
}
196+
197+
return ret;
198+
}
199+
200+
/**
201+
* Returns {@code true} if using the formatter indentation settings from the external
202+
* formatter is activated, otherwise {@code false}.
203+
*
204+
* @param prefs the {@link Preferences} where to check
205+
*
206+
* @return {@code true} if using the formatter indentation settings from the external
207+
* formatter is activated, otherwise {@code false}
208+
*/
209+
private boolean isUseFormatterIndentationSettings(Preferences prefs) {
210+
return prefs.getBoolean(Settings.ENABLE_USE_OF_INDENTATION_SETTINGS, true);
211+
}
212+
}

0 commit comments

Comments
 (0)