Skip to content
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
1 change: 1 addition & 0 deletions src/main/java/com/sleekbyte/tailor/common/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class Messages {
public static final String FORCE_CAST = "Force casts should be avoided";
public static final String BLANK_LINE_BEFORE = "must have at least one blank line before it";
public static final String BLANK_LINE_AFTER = "must have at least one blank line after it";
public static final String LEADING_WHITESPACE = "should not start with whitespace";

// Usage messages
public static final String CMD_LINE_SYNTAX = "tailor";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void verify() throws IOException {
verifyFileLength(maxLengths.maxFileLength);
verifyLineLengths(maxLengths.maxLineLength);
verifyNewlineTerminated();
verifyNoLeadingWhitespace();
}

private void verifyFileLength(int maxLines) throws IOException {
Expand Down Expand Up @@ -71,4 +72,11 @@ private void verifyNewlineTerminated() throws IOException {
}
}

private void verifyNoLeadingWhitespace() throws IOException {
if (SourceFileUtil.hasLeadingWhitespace(this.inputFile)) {
Location location = new Location(1, 1);
this.printer.warn(Messages.FILE + Messages.LEADING_WHITESPACE, location);
}
}

}
17 changes: 17 additions & 0 deletions src/main/java/com/sleekbyte/tailor/utils/SourceFileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.antlr.v4.runtime.ParserRuleContext;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.LineNumberReader;
Expand Down Expand Up @@ -110,4 +111,20 @@ public static boolean singleNewlineTerminated(File inputFile) throws IOException
randomAccessFile.seek(inputFile.length() - 2);
return (Byte.compare(randomAccessFile.readByte(), NEWLINE_DELIMITER) != 0);
}

/**
* Checks whether a file contains any leading whitespace characters.
*
* @param inputFile the file to check for leading whitespace
* @return true if file starts with whitespace
* @throws IOException if the file cannot be read
*/
public static boolean hasLeadingWhitespace(File inputFile) throws IOException {
BufferedReader reader = Files.newBufferedReader(inputFile.toPath());
int character = reader.read();
if (character != -1 && Character.isWhitespace((char) character)) {
return true;
}
return false;
}
}
35 changes: 35 additions & 0 deletions src/test/java/com/sleekbyte/tailor/utils/SourceFileUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,41 @@ public void testNewlineTerminatedWithSomeContentAndMultipleNewlines() throws IOE
assertFalse(SourceFileUtil.singleNewlineTerminated(inputFile));
}

@Test
public void testHasLeadingWhitespaceBlankFile() throws IOException {
assertFalse(SourceFileUtil.hasLeadingWhitespace(inputFile));
}

@Test
public void testHasLeadingWhitespaceOnlyNewline() throws IOException {
writeNumOfLines(1, "");
assertTrue(SourceFileUtil.hasLeadingWhitespace(inputFile));
}

@Test
public void testHasLeadingWhitespaceWithSingleLine() throws IOException {
writeNumOfLines(1, NORMAL_LINE);
assertFalse(SourceFileUtil.hasLeadingWhitespace(inputFile));
}

@Test
public void testHasLeadingWhitespaceWithSingleLineAndPrecedingNewline() throws IOException {
writeNumOfLines(1, "\n" + NORMAL_LINE);
assertTrue(SourceFileUtil.hasLeadingWhitespace(inputFile));
}

@Test
public void testHasLeadingWhitespaceWithSingleLineAndPrecedingSpace() throws IOException {
writeNumOfLines(1, " " + NORMAL_LINE);
assertTrue(SourceFileUtil.hasLeadingWhitespace(inputFile));
}

@Test
public void testHasLeadingWhitespaceWithSingleLineAndPrecedingTab() throws IOException {
writeNumOfLines(1, "\t" + NORMAL_LINE);
assertTrue(SourceFileUtil.hasLeadingWhitespace(inputFile));
}

private void writeNumOfLines(int numOfLines, String data) {
for (int i = 1; i <= numOfLines; i++) {
writer.println(data);
Expand Down