Skip to content

Commit

Permalink
Fixed encoding issues, refactored line trimming behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Dutchman101 committed Sep 24, 2018
1 parent 7d91aff commit 81f1db5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
8 changes: 8 additions & 0 deletions .idea/artifacts/SpaceTrimmer_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@ It's able to get rid of all trailing spaces for all code files in a directory an
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.

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

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

[img] https://i.imgur.com/i35spWx.png
[img] https://i.imgur.com/7jLMMlk.png

Definition of GUI option 2 ("Remove empty lines"):
- 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.
Definition of GUI option 2 ("Remove excessive empty lines"):
- 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.

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

[img] https://i.imgur.com/KGGnhz8.png
[img] https://i.imgur.com/cu46f7E.png


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

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.
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.

GUI interface:

https://i.imgur.com/N5dEWo3.png
https://i.imgur.com/QL6asxH.png

Processing the 1100 files in above example GUI image, costed just under 3 seconds.
Processing the 1100 files in above example GUI image, took just under 3 seconds of execution time.



It should speak for itself, but if you have any specific demands, you can simply edit the source and recompile the app.
Also feel free to submit pull requests!
2 changes: 1 addition & 1 deletion src/nl/dutchman/spacetrimmer/ui/MainWindow.form
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<constraints border-constraint="Center"/>
<properties>
<horizontalAlignment value="0"/>
<text value="Remove empty lines"/>
<text value="Remove excessive empty lines"/>
</properties>
</component>
<grid id="d154c" binding="fileFormatContainer" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
Expand Down
34 changes: 23 additions & 11 deletions src/nl/dutchman/spacetrimmer/ui/WindowController.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,24 @@ private void initializeProcess(String directory, String extensionStr, boolean tr
boolean noExtensions = extensionStr == null || extensionStr.trim().isEmpty();
List<String> extensions;
if (noExtensions)
extensions = new ArrayList<String>(); // empty list, just in case, to avoid nullptr
extensions = new ArrayList<>();
else
{
extensions = Arrays.asList(extensionStr.split(","));
}

ExecutorService processService = Executors.newSingleThreadExecutor();
processService.submit(() -> {
try
{

BiFunction<String, List<String>, Boolean> endsWithAny = (dirName, exts) -> {
for (String ext : exts)
try
{
if (dirName.endsWith(ext))
return true;
return exts.contains(dirName.substring(dirName.lastIndexOf('.') + 1));
}
catch (IndexOutOfBoundsException e)
{
return false;
}
return false;
};
System.out.println("Step 0 --> Gathering files to modify");
stepInformation[0] = new ProcessInformation();
Expand All @@ -111,7 +111,7 @@ private void initializeProcess(String directory, String extensionStr, boolean tr
List<ProcessableFile> processableFiles = Files.walk(Paths.get(directory)).
map(p -> new ProcessableFile(p.toFile(), p.toFile().length())).
filter(p -> !p.getFile().isDirectory()).
filter(noExtensions ? p -> true : p -> endsWithAny.apply(p.getFile().getAbsolutePath(), extensions)).
filter(noExtensions ? p -> true : p -> endsWithAny.apply(p.getFile().getAbsolutePath(), extensions)).
collect(Collectors.toList());
stepInformation[0].end();

Expand All @@ -136,13 +136,25 @@ private void initializeProcess(String directory, String extensionStr, boolean tr
try
{
List<String> lineFiles = new ArrayList<>();
for (String line : Files.readAllLines(file.getFile().toPath(), StandardCharsets.ISO_8859_1))
for (String line : Files.readAllLines(file.getFile().toPath(), StandardCharsets.UTF_8))
{
if (trimSpaces)
line = line.replaceAll("\\s+$", "");
if (!trimLines || (trimLines && line != null && !line.trim().isEmpty()))
lineFiles.add(line);

lineFiles.add(line);
}

if (trimLines)
{
for (int i = lineFiles.size() - 3; i >= 0; i--)
{
if (lineFiles.get(i).trim().isEmpty() &&
lineFiles.get(i+1).trim().isEmpty() &&
lineFiles.get(i+2).trim().isEmpty())
lineFiles.remove(i);
}
}

Files.write(file.getFile().toPath(), lineFiles);
}
catch (IOException e)
Expand Down

0 comments on commit 81f1db5

Please sign in to comment.