CheckNoBreak extends Rule { @Override public List<Advice> evaluate(AnnotatedString s, AnnotatedString original) { List<Advice> out_list = new ArrayList<Advice>(); List<String> lines = s.getLines(); int env_level = 0; for (int line_cnt = 0; line_cnt < lines.size(); line_cnt++) { String line = lines.get(line_cnt); if (line.matches(".*\\\\begin\\s*\\{\\s*(equation|equation\\*|align|align\\*|table|tabular|verbatim|lstlisting|IEEEkeywords|figure|matrix|bmatrix|Bmatrix|pmatrix|vmatrix|Vmatrix|smallmatrix).*") || line.matches(".*\\\\\\[.*")) { env_level++; } if (env_level == 0) { Matcher mat = m_breakPattern.matcher(line); if (mat.find()) { Position start_pos = s.getSourcePosition(new Position(line_cnt, mat.start())); Position end_pos = s.getSourcePosition(new Position(line_cnt, mat.start() + mat.group(0).length())); Range r = new Range(start_pos, end_pos); out_list.add(new Advice(this, r, "You should not break lines manually in a paragraph. Either start a new paragraph or stay in the current one.", original.getResourceName(), original.getLine(start_pos.getLine()), original.getOffset(start_pos))); } } if (line.matches(".*\\\\end\\s*\\{\\s*(equation|equation\\*|align|align\\*|table|tabular|verbatim|lstlisting|IEEEkeywords|figure|matrix|bmatrix|Bmatrix|pmatrix|vmatrix|Vmatrix|smallmatrix).*") || line.matches(".*\\\\\\].*")) { env_level--; } } return out_list; } CheckNoBreak(); @Override List<Advice> evaluate(AnnotatedString s, AnnotatedString original); @Override String getDescription();  }
@Test public void test1() { AnnotatedString in_string = AnnotatedString.read(new Scanner("Lorem ipsum dolor sit amet.\n\n" + "Lorem ipsum dolor sit amet.")); Rule r = new CheckNoBreak(); List<Advice> ad_list = r.evaluate(in_string, in_string); assertTrue(ad_list.isEmpty()); }
@Test public void test2() { AnnotatedString in_string = AnnotatedString.read(new Scanner("Lorem ipsum dolor sit amet.\\\\\n" + "Lorem ipsum dolor sit amet.")); Rule r = new CheckNoBreak(); List<Advice> ad_list = r.evaluate(in_string, in_string); assertEquals(1, ad_list.size()); }
@Test public void test3() { AnnotatedString in_string = AnnotatedString.read(new Scanner("\\begin{align*}\n" + "Lorem ipsum dolor sit amet.\\\\\n" + "Lorem ipsum dolor sit amet.\n"+ "\\end{align*}")); Rule r = new CheckNoBreak(); List<Advice> ad_list = r.evaluate(in_string, in_string); assertTrue(ad_list.isEmpty()); }