From 354519f879216778f65734bf2b7dafaab157fcb4 Mon Sep 17 00:00:00 2001 From: Ryan Gustafson Date: Fri, 10 Oct 2008 02:14:14 +0000 Subject: [PATCH] Fix to support EOLs different from the current System's. git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@6586 51baf565-9d33-0410-a72c-fc3788e3496d --- .../pmd/util/designer/CodeEditorTextPane.java | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/pmd/src/net/sourceforge/pmd/util/designer/CodeEditorTextPane.java b/pmd/src/net/sourceforge/pmd/util/designer/CodeEditorTextPane.java index aa39438506..b6170fd4a4 100644 --- a/pmd/src/net/sourceforge/pmd/util/designer/CodeEditorTextPane.java +++ b/pmd/src/net/sourceforge/pmd/util/designer/CodeEditorTextPane.java @@ -1,25 +1,22 @@ package net.sourceforge.pmd.util.designer; -import java.util.StringTokenizer; - import javax.swing.JTextPane; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.util.LineGetter; public class CodeEditorTextPane extends JTextPane implements LineGetter { - - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + + private String[] getLines() { + // Support files with line separators from various platforms + return getText().split("\r\n|\r|\n"); + } public String getLine(int number) { - int count = 1; - for (StringTokenizer st = new StringTokenizer(getText(), "\n"); st.hasMoreTokens();) { - String tok = st.nextToken(); - if (count == number) { - return tok; - } - count++; - } + String[] lines= getLines(); + if (number < lines.length) { + return lines[number]; + } throw new RuntimeException("Line number " + number + " not found"); } @@ -46,7 +43,7 @@ public class CodeEditorTextPane extends JTextPane implements LineGetter { } public void select(Node node) { - String[] lines = getText().split(LINE_SEPARATOR); + String[] lines = getLines(); if (node.getBeginLine() >= 0) { setSelectionStart(getPosition(lines, node.getBeginLine(), node.getBeginColumn())); setSelectionEnd(getPosition(lines, node.getEndLine(), node.getEndColumn()) + 1);