diff --git a/nb-configuration.xml b/nb-configuration.xml index 87a9e8f..f4848a4 100644 --- a/nb-configuration.xml +++ b/nb-configuration.xml @@ -1,28 +1,29 @@ - - - - project - 2 - 2 - 2 - true - 80 - none - 2 - 2 - 2 - true - 80 - none - WRAP_IF_LONG - true - true - true - - - false - false - false - true - - + + + + project + 2 + 2 + 2 + true + 80 + none + 2 + 2 + 2 + true + 80 + none + WRAP_IF_LONG + true + true + true + + + false + false + false + true + JDK_1.7 + + diff --git a/pom.xml b/pom.xml index f3abbaf..b767fc4 100644 --- a/pom.xml +++ b/pom.xml @@ -244,8 +244,8 @@ maven-compiler-plugin 3.1 - 1.8 - 1.8 + 1.7 + 1.7 diff --git a/src/main/java/com/welovecoding/nbeditorconfig/io/reader/FileInfoReader.java b/src/main/java/com/welovecoding/nbeditorconfig/io/reader/FileInfoReader.java index 608a8b1..b1745ec 100644 --- a/src/main/java/com/welovecoding/nbeditorconfig/io/reader/FileInfoReader.java +++ b/src/main/java/com/welovecoding/nbeditorconfig/io/reader/FileInfoReader.java @@ -7,12 +7,13 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.StringReader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.util.logging.Level; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; import java.util.logging.Logger; -import java.util.stream.Collectors; -import java.util.stream.Stream; import org.mozilla.universalchardet.UniversalDetector; import org.openide.filesystems.FileObject; import org.openide.util.Exceptions; @@ -163,9 +164,49 @@ private static String readFirstLineWithSeparator(FileObject fo, Charset charset) return firstLine; } - public static String trimTrailingWhitespace(Stream lines, String lineEnding) { - return lines.map((String content) -> { - return content.replaceAll("\\s+$", ""); - }).collect(Collectors.joining(lineEnding)); + public static String trimTrailingWhitespace(Collection lines, String lineEnding) { + StringBuilder sb = new StringBuilder(); + for (String content : lines) { + sb.append(content.replaceAll("\\s+$", "")); + sb.append(lineEnding); + } + return sb.toString().trim(); + } + + public static String trimTrailingWhitespace(String text, String lineEnding) { + List lines = readLines(text); + return trimTrailingWhitespace(lines, lineEnding); + } + + public static String replaceLineEndings(Collection lines, String lineEnding) { + StringBuilder sb = new StringBuilder(); + for (String content : lines) { + sb.append(content); + sb.append(lineEnding); + } + return sb.toString().trim(); + } + + public static String replaceLineEndings(String text, String lineEnding) { + List lines = readLines(text); + return replaceLineEndings(lines, lineEnding); + } + + public static List readLines(String text) { + List lines = new ArrayList<>(); + try (BufferedReader reader = new BufferedReader(new StringReader(text))) { + + try { + for (String line = reader.readLine(); line != null; line = reader.readLine()) { + lines.add(line); + } + reader.close(); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + return lines; } } diff --git a/src/main/java/com/welovecoding/nbeditorconfig/io/writer/FileObjectWriter.java b/src/main/java/com/welovecoding/nbeditorconfig/io/writer/FileObjectWriter.java index 3266091..8ca5f9d 100644 --- a/src/main/java/com/welovecoding/nbeditorconfig/io/writer/FileObjectWriter.java +++ b/src/main/java/com/welovecoding/nbeditorconfig/io/writer/FileObjectWriter.java @@ -25,16 +25,19 @@ public class FileObjectWriter { private static final Logger LOG = Logger.getLogger(FileObjectWriter.class.getName()); - public static synchronized void writeWithAtomicAction(DataObject dataObject, Charset cs, String content) { + public static synchronized void writeWithAtomicAction(final DataObject dataObject, final Charset cs, final String content) { try { - FileObject fo = dataObject.getPrimaryFile(); - EditorCookie cookie = dataObject.getLookup().lookup(EditorCookie.class); - NbDocument.runAtomicAsUser(cookie.openDocument(), () -> { - try (Writer out = new OutputStreamWriter(fo.getOutputStream(), cs)) { - LOG.log(Level.INFO, "\u00ac Writing file"); - out.write(content); - } catch (IOException ex) { - Exceptions.printStackTrace(ex); + final FileObject fo = dataObject.getPrimaryFile(); + final EditorCookie cookie = dataObject.getLookup().lookup(EditorCookie.class); + NbDocument.runAtomicAsUser(cookie.openDocument(), new Runnable() { + @Override + public void run() { + try (Writer out = new OutputStreamWriter(fo.getOutputStream(), cs)) { + LOG.log(Level.INFO, "\u00ac Writing file"); + out.write(content); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } } }); } catch (BadLocationException ex) { diff --git a/src/main/java/com/welovecoding/nbeditorconfig/io/writer/StyledDocumentWriter.java b/src/main/java/com/welovecoding/nbeditorconfig/io/writer/StyledDocumentWriter.java index 650b11f..e955b1d 100644 --- a/src/main/java/com/welovecoding/nbeditorconfig/io/writer/StyledDocumentWriter.java +++ b/src/main/java/com/welovecoding/nbeditorconfig/io/writer/StyledDocumentWriter.java @@ -111,12 +111,12 @@ public static void writeOnFileWithLines(File file, Charset charset, List } } - public static void writeWithEditorKit(FileInfo info) + public static void writeWithEditorKit(final FileInfo info) throws FileAccessException, IOException { - EditorCookie cookie = info.getCookie(); - StyledDocument openedDocument = cookie.openDocument(); - EditorKit kit = getEditorKit(info.getDataObject()); + final EditorCookie cookie = info.getCookie(); + final StyledDocument openedDocument = cookie.openDocument(); + final EditorKit kit = getEditorKit(info.getDataObject()); try (InputStream is = new ByteArrayInputStream(info.getContentAsBytes())) { // Backup caret position @@ -125,49 +125,55 @@ public static void writeWithEditorKit(FileInfo info) LOG.log(Level.WARNING, "Could not get Caret"); return; } - int caretPosition = info.getCurrentCaretPosition(); - Runnable runner = () -> { - NbDocument.runAtomic(openedDocument, () -> { - try { - // Wipe document - cookie.getDocument().remove(0, cookie.getDocument().getLength()); - - LOG.log(Level.INFO, "Write to \"is\": {0}", is); - LOG.log(Level.INFO, "Write to \"document\": {0}", cookie.getDocument()); - - // Read input stream into the document (which is a "write" operation) - try (Reader reader = new InputStreamReader(is, info.getCharset())) { - kit.read(reader, cookie.getDocument(), 0); - } - cookie.saveDocument(); - - info.getFileObject().setAttribute(ENCODING_SETTING, info.getCharset().name()); + final int caretPosition = info.getCurrentCaretPosition(); + Runnable runner = new Runnable() { + @Override + public void run() { + NbDocument.runAtomic(openedDocument, new Runnable() { + @Override + public void run() { + try { + // Wipe document + cookie.getDocument().remove(0, cookie.getDocument().getLength()); - // Reset caret positon -// caretPosition -= info.getCaretOffset(); - if (caretPosition < cookie.getDocument().getLength()) { - LOG.log(Level.INFO, "Moving caret position from {0} to: {1} / {2}", - new Object[]{info.getCaretOffset(), caretPosition, cookie.getDocument().getLength()}); - caret.setDot(caretPosition); - } + LOG.log(Level.INFO, "Write to \"is\": {0}", is); + LOG.log(Level.INFO, "Write to \"document\": {0}", cookie.getDocument()); - // Reformat code (to apply ident size & styles) - // TODO: Do this only if CodeStylePreferences have been changed - Reformat reformat = Reformat.get(cookie.getDocument()); - reformat.lock(); - try { - reformat.reformat(0, cookie.getDocument().getLength()); - } catch (BadLocationException ex) { - LOG.log(Level.SEVERE, "AutoFormat on document not possible: {0}", ex.getMessage()); - } finally { - reformat.unlock(); - // Save document after reformat + // Read input stream into the document (which is a "write" operation) + try (Reader reader = new InputStreamReader(is, info.getCharset())) { + kit.read(reader, cookie.getDocument(), 0); + } cookie.saveDocument(); + + info.getFileObject().setAttribute(ENCODING_SETTING, info.getCharset().name()); + + // Reset caret positon + // caretPosition -= info.getCaretOffset(); + if (caretPosition < cookie.getDocument().getLength()) { + LOG.log(Level.INFO, "Moving caret position from {0} to: {1} / {2}", + new Object[]{info.getCaretOffset(), caretPosition, cookie.getDocument().getLength()}); + caret.setDot(caretPosition); + } + + // Reformat code (to apply ident size & styles) + // TODO: Do this only if CodeStylePreferences have been changed + Reformat reformat = Reformat.get(cookie.getDocument()); + reformat.lock(); + try { + reformat.reformat(0, cookie.getDocument().getLength()); + } catch (BadLocationException ex) { + LOG.log(Level.SEVERE, "AutoFormat on document not possible: {0}", ex.getMessage()); + } finally { + reformat.unlock(); + // Save document after reformat + cookie.saveDocument(); + } + } catch (BadLocationException | IOException ex) { + LOG.log(Level.SEVERE, "Document could not be saved: {0}", ex.getMessage()); } - } catch (BadLocationException | IOException ex) { - LOG.log(Level.SEVERE, "Document could not be saved: {0}", ex.getMessage()); } }); + } }; if (SwingUtilities.isEventDispatchThread()) { diff --git a/src/main/java/com/welovecoding/nbeditorconfig/listener/Installer.java b/src/main/java/com/welovecoding/nbeditorconfig/listener/Installer.java index be094e4..f19b16b 100644 --- a/src/main/java/com/welovecoding/nbeditorconfig/listener/Installer.java +++ b/src/main/java/com/welovecoding/nbeditorconfig/listener/Installer.java @@ -16,12 +16,15 @@ public class Installer extends ModuleInstall { @Override public void restored() { if (detectOldJava()) { - String title = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-title"); - String message = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-message"); - int messageType = NotifyDescriptor.ERROR_MESSAGE; + final String title = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-title"); + final String message = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-message"); + final int messageType = NotifyDescriptor.ERROR_MESSAGE; - ActionListener actionListener = (ActionEvent e) -> { - DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, messageType)); + ActionListener actionListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, messageType)); + } }; NotificationDisplayer.getDefault().notify(title, new MetalIconFactory.FileIcon16(), message, actionListener); } @@ -51,11 +54,11 @@ public static int getMinor() { return 0; } } - + /** * Example: Patch version is "0_31" in Java 1.8.0_31 - * - * @return + * + * @return */ public static String getPatch() { try { diff --git a/src/main/java/com/welovecoding/nbeditorconfig/processor/FileInfo.java b/src/main/java/com/welovecoding/nbeditorconfig/processor/FileInfo.java index c811e27..760dbcb 100644 --- a/src/main/java/com/welovecoding/nbeditorconfig/processor/FileInfo.java +++ b/src/main/java/com/welovecoding/nbeditorconfig/processor/FileInfo.java @@ -42,15 +42,21 @@ public FileInfo(DataObject dataObject) { } public Caret getCaret() { - Runnable runner = () -> { - NbDocument.runAtomic(cookie.getDocument(), () -> { - JEditorPane pane = cookie.getOpenedPanes()[0]; - if (pane != null) { - currentCaret = pane.getCaret(); - } else { - LOG.log(Level.SEVERE, "Could not get JEditorPane for Document"); - } - }); + Runnable runner = new Runnable() { + @Override + public void run() { + NbDocument.runAtomic(cookie.getDocument(), new Runnable() { + @Override + public void run() { + JEditorPane pane = cookie.getOpenedPanes()[0]; + if (pane != null) { + currentCaret = pane.getCaret(); + } else { + LOG.log(Level.SEVERE, "Could not get JEditorPane for Document"); + } + } + }); + } }; if (SwingUtilities.isEventDispatchThread()) { runner.run(); diff --git a/src/main/java/com/welovecoding/nbeditorconfig/processor/operation/LineEndingOperation.java b/src/main/java/com/welovecoding/nbeditorconfig/processor/operation/LineEndingOperation.java index 0a0fad5..44c6a89 100644 --- a/src/main/java/com/welovecoding/nbeditorconfig/processor/operation/LineEndingOperation.java +++ b/src/main/java/com/welovecoding/nbeditorconfig/processor/operation/LineEndingOperation.java @@ -1,12 +1,10 @@ package com.welovecoding.nbeditorconfig.processor.operation; import com.welovecoding.nbeditorconfig.config.LoggerSettings; +import com.welovecoding.nbeditorconfig.io.reader.FileInfoReader; import com.welovecoding.nbeditorconfig.processor.FileInfo; -import java.io.BufferedReader; -import java.io.StringReader; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Collectors; public class LineEndingOperation { @@ -34,10 +32,8 @@ public boolean operate(FileInfo info) { } private StringBuilder replaceLineEndings(StringBuilder content, String lineEnding) { - BufferedReader reader = new BufferedReader(new StringReader(content.toString())); - // Note: As a side effect this will strip a final newline - String tempContent = reader.lines().collect(Collectors.joining(lineEnding)); + String tempContent = FileInfoReader.replaceLineEndings(content.toString(), lineEnding); // Append line ending only if that was the case in the old content if (content.toString().endsWith("\n") || content.toString().endsWith("\r")) { diff --git a/src/main/java/com/welovecoding/nbeditorconfig/processor/operation/TrimTrailingWhiteSpaceOperation.java b/src/main/java/com/welovecoding/nbeditorconfig/processor/operation/TrimTrailingWhiteSpaceOperation.java index 3ff0a02..dc1eb9e 100644 --- a/src/main/java/com/welovecoding/nbeditorconfig/processor/operation/TrimTrailingWhiteSpaceOperation.java +++ b/src/main/java/com/welovecoding/nbeditorconfig/processor/operation/TrimTrailingWhiteSpaceOperation.java @@ -1,12 +1,10 @@ package com.welovecoding.nbeditorconfig.processor.operation; import static com.welovecoding.nbeditorconfig.config.LoggerSettings.OPERATION_LOG_LEVEL; +import com.welovecoding.nbeditorconfig.io.reader.FileInfoReader; import com.welovecoding.nbeditorconfig.processor.FileInfo; -import java.io.BufferedReader; -import java.io.StringReader; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Collectors; public class TrimTrailingWhiteSpaceOperation { @@ -53,11 +51,7 @@ private void detectCaretOffset(FileInfo info) { contentUntilCaret = contentCopy.substring(0, caretPosition); } - BufferedReader reader = new BufferedReader(new StringReader(contentUntilCaret)); - - String trimmedContent = reader.lines().map((String line) -> { - return line.replaceAll("\\s+$", ""); - }).collect(Collectors.joining(info.getEndOfLine())); + String trimmedContent = FileInfoReader.trimTrailingWhitespace(contentUntilCaret, info.getEndOfLine()); // Count the characters which have been trimmed until the caret positon // (this will be our caret offset) @@ -78,12 +72,8 @@ private StringBuilder trim(FileInfo info) { String lineEnding = info.getEndOfLine(); String contentCopy = content.toString(); - BufferedReader reader = new BufferedReader(new StringReader(contentCopy)); - // Note: As a side effect this will strip a final newline! - String trimmedContent = reader.lines().map((String line) -> { - return line.replaceAll("\\s+$", ""); - }).collect(Collectors.joining(lineEnding)); + String trimmedContent = FileInfoReader.trimTrailingWhitespace(contentCopy, lineEnding); // Exchange original content with trimmed content content.delete(0, content.length()); diff --git a/src/test/java/com/welovecoding/nbeditorconfig/io/reader/FileInfoReaderTest.java b/src/test/java/com/welovecoding/nbeditorconfig/io/reader/FileInfoReaderTest.java index 4f2aced..3d36186 100644 --- a/src/test/java/com/welovecoding/nbeditorconfig/io/reader/FileInfoReaderTest.java +++ b/src/test/java/com/welovecoding/nbeditorconfig/io/reader/FileInfoReaderTest.java @@ -14,8 +14,9 @@ import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.logging.Logger; -import java.util.stream.Stream; import static junit.framework.Assert.assertEquals; import org.junit.Test; import org.openide.filesystems.FileObject; @@ -545,7 +546,7 @@ public void readInfoUTF_8_LF() throws IOException { @Test public void trimTrailingLineEndingCR() { - Stream stream = Stream.of("Hello\r", "World"); + List stream = Arrays.asList("Hello\r", "World"); String actual = FileInfoReader.trimTrailingWhitespace(stream, "\r"); assertEquals("Hello\rWorld", actual); @@ -553,7 +554,7 @@ public void trimTrailingLineEndingCR() { @Test public void trimTrailingLineEndingCRLF() { - Stream stream = Stream.of("Hello\r\n", "World"); + List stream = Arrays.asList("Hello\r\n", "World"); String actual = FileInfoReader.trimTrailingWhitespace(stream, "\r\n"); assertEquals("Hello\r\nWorld", actual); @@ -561,7 +562,7 @@ public void trimTrailingLineEndingCRLF() { @Test public void trimTrailingLineEndingLF() { - Stream stream = Stream.of("Hello\n", "World"); + List stream = Arrays.asList("Hello\n", "World"); String actual = FileInfoReader.trimTrailingWhitespace(stream, "\n"); assertEquals("Hello\nWorld", actual); @@ -569,7 +570,7 @@ public void trimTrailingLineEndingLF() { @Test public void trimTrailingTab() { - Stream stream = Stream.of("Hello\t", "World"); + List stream = Arrays.asList("Hello\t", "World"); String actual = FileInfoReader.trimTrailingWhitespace(stream, System.lineSeparator()); assertEquals("Hello" + System.lineSeparator() + "World", actual); @@ -577,7 +578,7 @@ public void trimTrailingTab() { @Test public void trimTrailingTabs() { - Stream stream = Stream.of("Hello\t\t", "World"); + List stream = Arrays.asList("Hello\t\t", "World"); String actual = FileInfoReader.trimTrailingWhitespace(stream, System.lineSeparator()); assertEquals("Hello" + System.lineSeparator() + "World", actual); @@ -585,7 +586,7 @@ public void trimTrailingTabs() { @Test public void trimTrailingWhitespace() { - Stream stream = Stream.of("Hello ", "World"); + List stream = Arrays.asList("Hello ", "World"); String actual = FileInfoReader.trimTrailingWhitespace(stream, System.lineSeparator()); assertEquals("Hello" + System.lineSeparator() + "World", actual); diff --git a/src/test/java/com/welovecoding/nbeditorconfig/processor/operation/IndentSizeOperationTest.java b/src/test/java/com/welovecoding/nbeditorconfig/processor/operation/IndentSizeOperationTest.java index 6f3cad6..9481524 100644 --- a/src/test/java/com/welovecoding/nbeditorconfig/processor/operation/IndentSizeOperationTest.java +++ b/src/test/java/com/welovecoding/nbeditorconfig/processor/operation/IndentSizeOperationTest.java @@ -92,37 +92,40 @@ public void itDetectsIfChangesAreNeeded() throws assertEquals(indentWidth, indentSizeAfter); // Save indent size - EditorCookie cookie = dataObject.getLookup().lookup(EditorCookie.class); + final EditorCookie cookie = dataObject.getLookup().lookup(EditorCookie.class); cookie.open(); - StyledDocument document = cookie.openDocument(); + final StyledDocument document = cookie.openDocument(); - NbDocument.runAtomicAsUser(document, () -> { - try { - // Save test file - cookie.saveDocument(); + NbDocument.runAtomicAsUser(document, new Runnable() { + @Override + public void run() { + try { + // Save test file + cookie.saveDocument(); - // Reformat test file - Reformat reformat = Reformat.get(document); - reformat.lock(); + // Reformat test file + Reformat reformat = Reformat.get(document); + reformat.lock(); - try { - reformat.reformat(0, document.getLength()); - } catch (BadLocationException ex) { - Exceptions.printStackTrace(ex); - } finally { - reformat.unlock(); try { - // Save formatted document - cookie.saveDocument(); - System.out.println("Content saved:"); - System.out.println(document.getText(0, document.getLength())); - } catch (IOException | BadLocationException ex) { + reformat.reformat(0, document.getLength()); + } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); + } finally { + reformat.unlock(); + try { + // Save formatted document + cookie.saveDocument(); + System.out.println("Content saved:"); + System.out.println(document.getText(0, document.getLength())); + } catch (IOException | BadLocationException ex) { + Exceptions.printStackTrace(ex); + } } + } catch (IOException ex) { + Exceptions.printStackTrace(ex); } - } catch (IOException ex) { - Exceptions.printStackTrace(ex); } });