@Test public void testGetLinesBetween() throws Exception { JTextPane pane = new JTextPane(); pane.setText("12\n\n34\n5\n6"); ArrayList<Integer> linesBetween = JTextPaneToolbox.getLinesBetween(pane, 0, 5); int lines[] = {0,1,2}; assertEquals(lines.length, linesBetween.size()); for (int i = 0; i < lines.length; i++) { assertEquals(lines[i], (int) linesBetween.get(i)); } }
public static ArrayList<Integer> getLinesBetween(JTextPane pane, int start, int end) throws BadLocationException { int startingline = transformToLineNumber(pane, start); ArrayList<Integer> lines = new ArrayList<>(); lines.add(startingline); String code = getText(pane); for(; start < end; ++start) { if(code.charAt(start) == '\n') { startingline++; lines.add(startingline); } } return lines; }
JTextPaneToolbox { public static ArrayList<Integer> getLinesBetween(JTextPane pane, int start, int end) throws BadLocationException { int startingline = transformToLineNumber(pane, start); ArrayList<Integer> lines = new ArrayList<>(); lines.add(startingline); String code = getText(pane); for(; start < end; ++start) { if(code.charAt(start) == '\n') { startingline++; lines.add(startingline); } } return lines; } }
JTextPaneToolbox { public static ArrayList<Integer> getLinesBetween(JTextPane pane, int start, int end) throws BadLocationException { int startingline = transformToLineNumber(pane, start); ArrayList<Integer> lines = new ArrayList<>(); lines.add(startingline); String code = getText(pane); for(; start < end; ++start) { if(code.charAt(start) == '\n') { startingline++; lines.add(startingline); } } return lines; }  }
JTextPaneToolbox { public static ArrayList<Integer> getLinesBetween(JTextPane pane, int start, int end) throws BadLocationException { int startingline = transformToLineNumber(pane, start); ArrayList<Integer> lines = new ArrayList<>(); lines.add(startingline); String code = getText(pane); for(; start < end; ++start) { if(code.charAt(start) == '\n') { startingline++; lines.add(startingline); } } return lines; }  static String getText(JTextPane pane); static int getDistanceToClosestLineBeginning(JTextPane pane, int pos); static int transformToLineNumber(JTextPane pane, int absPos); static int transformToLineNumber(String code, int absPos); static int getLineBeginning(JTextPane pane, int line); static int getClosestLineBeginningAfter(JTextPane pane, int absPos); static ArrayList<Integer> getLinesBetween(JTextPane pane, int start, int end); static String getCharToTheLeftOfCaret(JTextPane pane); static String getCharToTheRightOfCaret(JTextPane pane); static int getFirstCharPosInLine(JTextPane pane, int line); static int getWordBeginningAtCursor(JTextPane pane); }
JTextPaneToolbox { public static ArrayList<Integer> getLinesBetween(JTextPane pane, int start, int end) throws BadLocationException { int startingline = transformToLineNumber(pane, start); ArrayList<Integer> lines = new ArrayList<>(); lines.add(startingline); String code = getText(pane); for(; start < end; ++start) { if(code.charAt(start) == '\n') { startingline++; lines.add(startingline); } } return lines; }  static String getText(JTextPane pane); static int getDistanceToClosestLineBeginning(JTextPane pane, int pos); static int transformToLineNumber(JTextPane pane, int absPos); static int transformToLineNumber(String code, int absPos); static int getLineBeginning(JTextPane pane, int line); static int getClosestLineBeginningAfter(JTextPane pane, int absPos); static ArrayList<Integer> getLinesBetween(JTextPane pane, int start, int end); static String getCharToTheLeftOfCaret(JTextPane pane); static String getCharToTheRightOfCaret(JTextPane pane); static int getFirstCharPosInLine(JTextPane pane, int line); static int getWordBeginningAtCursor(JTextPane pane);  }