一、前言
关于grinder源码包定义org.syntax.jedit.TextUtilities文本工具类,查找指定位置pos出现指定行的下一步的位置,具体参见源码说明部分。
二、源码说明
package org.syntax.jedit;@b@@b@import javax.swing.text.BadLocationException;@b@import javax.swing.text.Document;@b@@b@public class TextUtilities@b@{@b@ public static int findMatchingBracket(Document doc, int offset)@b@ throws BadLocationException@b@ {@b@ char cprime;@b@ boolean direction;@b@ int count;@b@ String text;@b@ int i;@b@ String text;@b@ int i;@b@ if (doc.getLength() == 0)@b@ return -1;@b@ char c = doc.getText(offset, 1).charAt(0);@b@@b@ switch (c)@b@ {@b@ case '(':@b@ cprime = ')'; direction = false; break;@b@ case ')':@b@ cprime = '('; direction = true; break;@b@ case '[':@b@ cprime = ']'; direction = false; break;@b@ case ']':@b@ cprime = '['; direction = true; break;@b@ case '{':@b@ cprime = '}'; direction = false; break;@b@ case '}':@b@ cprime = '{'; direction = true; break;@b@ default:@b@ return -1;@b@ }@b@@b@ if (direction)@b@ {@b@ count = 1;@b@@b@ text = doc.getText(0, offset);@b@@b@ for (i = offset - 1; i >= 0; --i)@b@ {@b@ char x = text.charAt(i);@b@ if (x == c) {@b@ ++count;@b@ }@b@ else if ((x == cprime) && @b@ (--count == 0)) {@b@ return i;@b@ }@b@@b@ }@b@@b@ }@b@ else@b@ {@b@ count = 1;@b@@b@ ++offset;@b@@b@ int len = doc.getLength() - offset;@b@@b@ text = doc.getText(offset, len);@b@@b@ for (i = 0; i < len; ++i)@b@ {@b@ char x = text.charAt(i);@b@@b@ if (x == c) {@b@ ++count;@b@ }@b@ else if ((x == cprime) && @b@ (--count == 0))@b@ return (i + offset);@b@@b@ }@b@@b@ }@b@@b@ return -1;@b@ }@b@@b@ public static int findWordStart(String line, int pos, String noWordSep)@b@ {@b@ char ch = line.charAt(pos - 1);@b@@b@ if (noWordSep == null)@b@ noWordSep = "";@b@ boolean selectNoLetter = (!(Character.isLetterOrDigit(ch))) && (noWordSep.indexOf(ch) == -1);@b@@b@ int wordStart = 0;@b@ for (int i = pos - 1; i >= 0; --i)@b@ {@b@ ch = line.charAt(i);@b@ if ((selectNoLetter ^ ((!(Character.isLetterOrDigit(ch))) && (noWordSep.indexOf(ch) == -1))))@b@ {@b@ wordStart = i + 1;@b@ break;@b@ }@b@ }@b@@b@ return wordStart;@b@ }@b@@b@ public static int findWordEnd(String line, int pos, String noWordSep)@b@ {@b@ char ch = line.charAt(pos);@b@@b@ if (noWordSep == null)@b@ noWordSep = "";@b@ boolean selectNoLetter = (!(Character.isLetterOrDigit(ch))) && (noWordSep.indexOf(ch) == -1);@b@@b@ int wordEnd = line.length();@b@ for (int i = pos; i < line.length(); ++i)@b@ {@b@ ch = line.charAt(i);@b@ if ((selectNoLetter ^ ((!(Character.isLetterOrDigit(ch))) && (noWordSep.indexOf(ch) == -1))))@b@ {@b@ wordEnd = i;@b@ break;@b@ }@b@ }@b@ return wordEnd;@b@ }@b@}