Skip to content

Commit 81f1db5

Browse files
author
Dutchman101
committed
Fixed encoding issues, refactored line trimming behaviour
1 parent 7d91aff commit 81f1db5

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

.idea/artifacts/SpaceTrimmer_jar.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,32 @@ It's able to get rid of all trailing spaces for all code files in a directory an
66
All programming languages are supported, you can input a comma-separated list of code-file extensions into the app's GUI, so that all matching files are included in the operation.
77

88
Definition of GUI option 1 ("Remove trailing spaces/tabs"):
9-
- empty whitespaces at the end of a line consisting of characters (only the very end of it, no loss of indent)
9+
- remove whitespaces at the end of a line consisting of empty character spaces (no loss of indent or style)
1010
- actual tabs/spaces content in empty lines (it won't delete the empty lines itself, only bring it back to 0 bytes)
1111

1212
[Example] See below image for the effects of this option:
1313

14-
[img] https://i.imgur.com/i35spWx.png
14+
[img] https://i.imgur.com/7jLMMlk.png
1515

16-
Definition of GUI option 2 ("Remove empty lines"):
17-
- delete empty lines by completely removing them. This will most often be separator lines between blocks of code, added by the developer's preference over the lifespan of a project.
16+
Definition of GUI option 2 ("Remove excessive empty lines"):
17+
- Particular locations with more than 3 empty lines, will be brought back to 2 lines. Such lines are often separator spaces between code/function blocks, and grow inconsistent over the lifespan of a project.
1818

1919
[Example] See below image for the effects of this option:
2020

21-
[img] https://i.imgur.com/KGGnhz8.png
21+
[img] https://i.imgur.com/cu46f7E.png
2222

2323

2424
The stable, multi-threaded approach and progress bar, should allow you to work on an high amount of files simultaneously and large repositories.
2525

26-
This is freeware and free to use, distribute, compilate and modify on private and corporate level. No further updates will be commited by myself, although you're free to submit Pull requests.
26+
This is freeware and free to use for private and commercial purposes, change the license of, (re)distribute, compile and modify on private and corporate level. There are no restrictions.
2727

2828
GUI interface:
2929

30-
https://i.imgur.com/N5dEWo3.png
30+
https://i.imgur.com/QL6asxH.png
3131

32-
Processing the 1100 files in above example GUI image, costed just under 3 seconds.
32+
Processing the 1100 files in above example GUI image, took just under 3 seconds of execution time.
33+
34+
35+
36+
It should speak for itself, but if you have any specific demands, you can simply edit the source and recompile the app.
37+
Also feel free to submit pull requests!

src/nl/dutchman/spacetrimmer/ui/MainWindow.form

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
<constraints border-constraint="Center"/>
101101
<properties>
102102
<horizontalAlignment value="0"/>
103-
<text value="Remove empty lines"/>
103+
<text value="Remove excessive empty lines"/>
104104
</properties>
105105
</component>
106106
<grid id="d154c" binding="fileFormatContainer" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">

src/nl/dutchman/spacetrimmer/ui/WindowController.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,24 @@ private void initializeProcess(String directory, String extensionStr, boolean tr
8585
boolean noExtensions = extensionStr == null || extensionStr.trim().isEmpty();
8686
List<String> extensions;
8787
if (noExtensions)
88-
extensions = new ArrayList<String>(); // empty list, just in case, to avoid nullptr
88+
extensions = new ArrayList<>();
8989
else
90-
{
9190
extensions = Arrays.asList(extensionStr.split(","));
92-
}
9391

9492
ExecutorService processService = Executors.newSingleThreadExecutor();
9593
processService.submit(() -> {
9694
try
9795
{
9896

9997
BiFunction<String, List<String>, Boolean> endsWithAny = (dirName, exts) -> {
100-
for (String ext : exts)
98+
try
10199
{
102-
if (dirName.endsWith(ext))
103-
return true;
100+
return exts.contains(dirName.substring(dirName.lastIndexOf('.') + 1));
101+
}
102+
catch (IndexOutOfBoundsException e)
103+
{
104+
return false;
104105
}
105-
return false;
106106
};
107107
System.out.println("Step 0 --> Gathering files to modify");
108108
stepInformation[0] = new ProcessInformation();
@@ -111,7 +111,7 @@ private void initializeProcess(String directory, String extensionStr, boolean tr
111111
List<ProcessableFile> processableFiles = Files.walk(Paths.get(directory)).
112112
map(p -> new ProcessableFile(p.toFile(), p.toFile().length())).
113113
filter(p -> !p.getFile().isDirectory()).
114-
filter(noExtensions ? p -> true : p -> endsWithAny.apply(p.getFile().getAbsolutePath(), extensions)).
114+
filter(noExtensions ? p -> true : p -> endsWithAny.apply(p.getFile().getAbsolutePath(), extensions)).
115115
collect(Collectors.toList());
116116
stepInformation[0].end();
117117

@@ -136,13 +136,25 @@ private void initializeProcess(String directory, String extensionStr, boolean tr
136136
try
137137
{
138138
List<String> lineFiles = new ArrayList<>();
139-
for (String line : Files.readAllLines(file.getFile().toPath(), StandardCharsets.ISO_8859_1))
139+
for (String line : Files.readAllLines(file.getFile().toPath(), StandardCharsets.UTF_8))
140140
{
141141
if (trimSpaces)
142142
line = line.replaceAll("\\s+$", "");
143-
if (!trimLines || (trimLines && line != null && !line.trim().isEmpty()))
144-
lineFiles.add(line);
143+
144+
lineFiles.add(line);
145145
}
146+
147+
if (trimLines)
148+
{
149+
for (int i = lineFiles.size() - 3; i >= 0; i--)
150+
{
151+
if (lineFiles.get(i).trim().isEmpty() &&
152+
lineFiles.get(i+1).trim().isEmpty() &&
153+
lineFiles.get(i+2).trim().isEmpty())
154+
lineFiles.remove(i);
155+
}
156+
}
157+
146158
Files.write(file.getFile().toPath(), lineFiles);
147159
}
148160
catch (IOException e)

0 commit comments

Comments
 (0)