Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

#106 and #107 added HTML and CSS formatter #232

Merged
merged 3 commits into from
Oct 18, 2022
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ the same way as e.g. your co-workers do with their Eclipse IDE.
* [revelc.net XML Code Formatter](https://github.com/revelc/xml-formatter)
* [Jsoup XML Code Formatter](https://jsoup.org/)

* HTML
* [Jsoup XML Code Formatter](https://jsoup.org/)

* CSS
* [CSS Parser](https://cssparser.sourceforge.net/)

* Json
* [Jackson Json Code Formatter](https://github.com/FasterXML/jackson)

Expand Down Expand Up @@ -73,6 +79,8 @@ the same way as e.g. your co-workers do with their Eclipse IDE.
* Support for Vertical Blank SQL Formatter (since 1.15.0)
* Support for DBeaver (via Spotless) (since 1.15.0)
* Option to organize imports with Google and Palantir Formatter instead of NetBeans (since 1.15.2)
* Support for Jsoup HTML formatter (since 1.15.3)
* Support for CSS Parser (since 1.15.3)

Compatibility
-------------
Expand Down
36 changes: 31 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<groupId>de.funfried.netbeans.plugins</groupId>
<artifactId>externalcodeformatter</artifactId>
<version>1.15.2</version>
<version>1.15.3-SNAPSHOT</version>
<packaging>nbm</packaging>

<developers>
Expand Down Expand Up @@ -162,7 +162,7 @@
<dependency>
<groupId>de.funfried.libraries</groupId>
<artifactId>jsdt-core</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
Expand Down Expand Up @@ -193,7 +193,7 @@
<dependency>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-formatter</artifactId>
<version>0.0.34</version>
<version>0.0.35</version>
</dependency>

<!-- Google Formatter -->
Expand Down Expand Up @@ -268,20 +268,39 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.4</version>
<version>2.14.0-rc1</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4</version>
<version>2.14.0-rc1</version>
</dependency>

<!-- CSS Parser -->
<dependency>
<groupId>net.sourceforge.cssparser</groupId>
<artifactId>cssparser</artifactId>
<version>0.9.29</version>
</dependency>

<dependency>
<groupId>org.w3c.css</groupId>
<artifactId>sac</artifactId>
<version>1.3</version>
</dependency>

<!-- JSQLFormatter -->
<dependency>
<groupId>com.manticore-projects.jsqlformatter</groupId>
<artifactId>jsqlformatter</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Vertical Blank SQL Formatter -->
Expand Down Expand Up @@ -449,6 +468,12 @@
<version>4.4</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down Expand Up @@ -888,6 +913,7 @@
<configuration>
<failOnWarning>true</failOnWarning>
<ignoredUnusedDeclaredDependencies>
<ignoredUnusedDeclaredDependency>org.apache.commons:commons-text</ignoredUnusedDeclaredDependency>
<ignoredUnusedDeclaredDependency>org.osgi:org.osgi.service.prefs</ignoredUnusedDeclaredDependency>
<ignoredUnusedDeclaredDependency>org.functionaljava:functionaljava</ignoredUnusedDeclaredDependency>
</ignoredUnusedDeclaredDependencies>
Expand Down
9 changes: 9 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
</properties>

<body>
<release version="1.15.3-SNAPSHOT" date="N/A" description="Added HTML and CSS formatter">
<action dev="bahlef" type="add" issue="107">
Add JSoup HTML formatter
</action>
<action dev="bahlef" type="add" issue="106">
Add CSSParser CSS formatter
</action>
</release>

<release version="1.15.2" date="2022-10-02" description="Bug-fix release">
<action dev="bahlef" type="add" issue="215">
Added possibility to organize imports with Google and Palantir formatter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@
"SQL=SQL"
})
public enum MimeType {
// CSS("text/css"), HTML("application/xhtml+xml", "text/html"),
JAVA(JavaTokenId.language().mimeType()), JAVASCRIPT("text/javascript"), JSON("text/x-json", "^text/(.*)\\+x-json$"), XML("text/xml",
"^text/(.*)\\+xml$"), SQL("application/sql", "text/sql", "text/x-sql", "text/plain");
CSS("text/css"), HTML("application/xhtml+xml", "text/html", "text/xhtml"), JAVA(JavaTokenId.language().mimeType()), JAVASCRIPT("text/javascript"), JSON("text/x-json",
"^text/(.*)\\+x-json$"), XML("text/xml", "^text/(.*)\\+xml$"), SQL("application/sql", "text/sql", "text/x-sql", "text/plain");

private final String[] mimeTypes;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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 - initial API and implementation and/or initial documentation
*/
package de.funfried.netbeans.plugins.external.formatter.css.base;

import java.util.Collections;
import java.util.List;
import java.util.SortedSet;

import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

import org.apache.commons.lang3.tuple.Pair;
import org.netbeans.api.annotations.common.CheckForNull;

import de.funfried.netbeans.plugins.external.formatter.FormatJob;
import de.funfried.netbeans.plugins.external.formatter.FormatterService;
import de.funfried.netbeans.plugins.external.formatter.MimeType;
import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;

/**
* Abstract base implementation of {@link FormatterService} for CSS formatters.
*
* @author bahlef
*/
public abstract class AbstractCssFormatterService implements FormatterService {
/**
* Returns the {@link FormatJob}.
*
* @param document the {@link StyledDocument} which should be formatted
*/
protected abstract FormatJob getFormatJob(StyledDocument document);

/**
* {@inheritDoc}
*/
@Override
public boolean format(StyledDocument document, SortedSet<Pair<Integer, Integer>> changedElements) throws BadLocationException, FormattingFailedException {
if (!canHandle(document)) {
throw new FormattingFailedException("The file type '" + MimeType.getMimeTypeAsString(document) + "' is not supported");
}

getFormatJob(document).format();

return true;
}

/**
* {@inheritDoc}
*/
@Override
public List<MimeType> getSupportedMimeTypes() {
return Collections.singletonList(MimeType.CSS);
}

/**
* {@inheritDoc}
*/
@Override
@CheckForNull
public Boolean organizeImports(StyledDocument document, boolean afterFixImports) throws BadLocationException {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 - initial API and implementation and/or initial documentation
*/

/**
* Package containing classes for common CSS formatter logic.
*/
package de.funfried.netbeans.plugins.external.formatter.css.base;
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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 - initial API and implementation and/or initial documentation
*/
package de.funfried.netbeans.plugins.external.formatter.css.cssparser;

import java.util.prefs.Preferences;

import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

import org.openide.awt.NotificationDisplayer;
import org.openide.awt.StatusDisplayer;

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

/**
* Google formatter implementation of the {@link AbstractFormatJob} to
* format a given document using the {@link CssParserFormatterWrapper}.
*
* @author bahlef
*/
class CssParserFormatJob extends AbstractFormatJob {
/** * The {@link CssParserFormatterWrapper} implementation. */
private final CssParserFormatterWrapper formatter;

/**
* Package private constructor to create a new instance of {@link CssParserFormatJob}.
*
* @param document the {@link StyledDocument} which sould be formatted
* @param formatter the {@link CssParserFormatterWrapper} to use
*/
CssParserFormatJob(StyledDocument document, CssParserFormatterWrapper formatter) {
super(document, null);

this.formatter = formatter;
}

/**
* {@inheritDoc}
*/
@Override
public void format() throws BadLocationException {
Preferences pref = Settings.getActivePreferences(document);

int indentPref = pref.getInt(CssParserFormatterSettings.INDENT, CssParserFormatterSettings.INDENT_DEFAULT);
boolean rgbAsHexPref = pref.getBoolean(CssParserFormatterSettings.RGB_AS_HEX, CssParserFormatterSettings.RGB_AS_HEX_DEFAULT);
boolean useSingleQuotesPref = pref.getBoolean(CssParserFormatterSettings.USE_SINGLE_QUOTES, CssParserFormatterSettings.USE_SINGLE_QUOTES_DEFAULT);
boolean useSourceStringValuesPref = pref.getBoolean(CssParserFormatterSettings.USE_SOURCE_STRING_VALUES, CssParserFormatterSettings.USE_SOURCE_STRING_VALUES_DEFAULT);

String code = getCode();

try {
String formattedContent = formatter.format(code, indentPref, rgbAsHexPref, useSingleQuotesPref, useSourceStringValuesPref);

if (setFormattedCode(code, formattedContent)) {
SwingUtilities.invokeLater(() -> {
if (pref.getBoolean(Settings.SHOW_NOTIFICATIONS, false)) {
NotificationDisplayer.getDefault().notify("Format using CssParser", Icons.ICON_EXTERNAL, "", null);
}

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

throw ex;
}
}
}
Loading