⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textutilities.java

📁 开源的java 编辑器源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		return findWordEnd(line, pos, noWordSep, true);	} //}}}	//{{{ findWordEnd() method	/**	 * Locates the end of the word at the specified position.	 * @param line The text	 * @param pos The position	 * @param noWordSep Characters that are non-alphanumeric, but	 * should be treated as word characters anyway	 * @param joinNonWordChars Treat consecutive non-alphanumeric	 * characters as one word	 * @since jEdit 4.1pre2	 */	public static int findWordEnd(String line, int pos, String noWordSep,		boolean joinNonWordChars)	{		return findWordEnd(line,pos,noWordSep,joinNonWordChars,false);	} //}}}	//{{{ findWordEnd() method	/**	 * Locates the end of the word at the specified position.	 * @param line The text	 * @param pos The position	 * @param noWordSep Characters that are non-alphanumeric, but	 * should be treated as word characters anyway	 * @param joinNonWordChars Treat consecutive non-alphanumeric	 * characters as one word	 * @param eatWhitespace Include whitespace at end of word	 * @since jEdit 4.2pre5	 */	public static int findWordEnd(String line, int pos, String noWordSep,		boolean joinNonWordChars, boolean eatWhitespace)	{		if(pos != 0)			pos--;		char ch = line.charAt(pos);		if(noWordSep == null)			noWordSep = "";		//{{{ the character under the cursor changes how we behave.		int type;		if(Character.isWhitespace(ch))			type = WHITESPACE;		else if(Character.isLetterOrDigit(ch)			|| noWordSep.indexOf(ch) != -1)			type = WORD_CHAR;		else			type = SYMBOL;		//}}}loop:		for(int i = pos; i < line.length(); i++)		{			ch = line.charAt(i);			switch(type)			{			//{{{ Whitespace...			case WHITESPACE:				// only select other whitespace in this case				if(Character.isWhitespace(ch))					break;				else					return i; //}}}			//{{{ Word character...			case WORD_CHAR:				if(Character.isLetterOrDigit(ch) ||					noWordSep.indexOf(ch) != -1)				{					break;				}				// whitespace; include in word if eating				else if(Character.isWhitespace(ch)					&& eatWhitespace)				{					type = WHITESPACE;					break;				}				else					return i; //}}}			//{{{ Symbol...			case SYMBOL:				if(!joinNonWordChars && i != pos)					return i;				// if we see whitespace, set flag.				if(Character.isWhitespace(ch))				{					if(eatWhitespace)					{						type = WHITESPACE;						break;					}					else						return i;				}				else if(Character.isLetterOrDigit(ch) ||					noWordSep.indexOf(ch) != -1)				{					return i;				}				else				{					break;				} //}}}			}		}		return line.length();	} //}}}	//{{{ spacesToTabs() method	/**	 * Converts consecutive spaces to tabs in the specified string.	 * @param in The string	 * @param tabSize The tab size	 */	public static String spacesToTabs(String in, int tabSize)	{		StringBuffer buf = new StringBuffer();		int width = 0;		int whitespace = 0;		for(int i = 0; i < in.length(); i++)		{			switch(in.charAt(i))			{			case ' ':				whitespace++;				width++;				break;			case '\t':				int tab = tabSize - (width % tabSize);				width += tab;				whitespace += tab;				break;			case '\n':				if(whitespace != 0)				{					buf.append(MiscUtilities						.createWhiteSpace(whitespace,tabSize,						width - whitespace));				}				whitespace = 0;				width = 0;				buf.append('\n');				break;			default:				if(whitespace != 0)				{					buf.append(MiscUtilities						.createWhiteSpace(whitespace,tabSize,						width - whitespace));					whitespace = 0;				}				buf.append(in.charAt(i));				width++;				break;			}		}		if(whitespace != 0)		{			buf.append(MiscUtilities.createWhiteSpace(whitespace,tabSize,				width - whitespace));		}                return buf.toString();	} //}}}	//{{{ tabsToSpaces() method	/**	 * Converts tabs to consecutive spaces in the specified string.	 * @param in The string	 * @param tabSize The tab size	 */	public static String tabsToSpaces(String in, int tabSize)	{		StringBuffer buf = new StringBuffer();		int width = 0;		for(int i = 0; i < in.length(); i++)		{			switch(in.charAt(i))			{			case '\t':				int count = tabSize - (width % tabSize);				width += count;				while(--count >= 0)					buf.append(' ');				break;			case '\n':				width = 0;				buf.append(in.charAt(i));				break;			default:				width++;				buf.append(in.charAt(i));				break;                        }                }                return buf.toString();	} //}}}	//{{{ format() method	/**	 * Formats the specified text by merging and breaking lines to the	 * specified width.	 * @param text The text	 * @param maxLineLen The maximum line length	 */	public static String format(String text, int maxLineLength, int tabSize)	{		StringBuffer buf = new StringBuffer();		int index = 0;		for(;;)		{			int newIndex = text.indexOf("\n\n",index);			if(newIndex == -1)				break;			formatParagraph(text.substring(index,newIndex),				maxLineLength,tabSize,buf);			buf.append("\n\n");			index = newIndex + 2;		}		if(index != text.length())		{			formatParagraph(text.substring(index),				maxLineLength,tabSize,buf);		}		return buf.toString();	} //}}}	//{{{ getStringCase() method	public static final int MIXED = 0;	public static final int LOWER_CASE = 1;	public static final int UPPER_CASE = 2;	public static final int TITLE_CASE = 3;	/**	 * Returns if the specified string is all upper case, all lower case,	 * or title case (first letter upper case, rest lower case).	 * @param str The string	 * @since jEdit 4.0pre1	 */	public static int getStringCase(String str)	{		if(str.length() == 0)			return MIXED;		int state = -1;		char ch = str.charAt(0);		if(Character.isLetter(ch))		{			if(Character.isUpperCase(ch))				state = UPPER_CASE;			else				state = LOWER_CASE;		}		for(int i = 1; i < str.length(); i++)		{			ch = str.charAt(i);			if(!Character.isLetter(ch))				continue;			switch(state)			{			case UPPER_CASE:				if(Character.isLowerCase(ch))				{					if(i == 1)						state = TITLE_CASE;					else						return MIXED;				}				break;			case LOWER_CASE:			case TITLE_CASE:				if(Character.isUpperCase(ch))					return MIXED;				break;			}		}		return state;	} //}}}	//{{{ toTitleCase() method	/**	 * Converts the specified string to title case, by capitalizing the	 * first letter.	 * @param str The string	 * @since jEdit 4.0pre1	 */	public static String toTitleCase(String str)	{		if(str.length() == 0)			return str;		else		{			return Character.toUpperCase(str.charAt(0))				+ str.substring(1).toLowerCase();		}	} //}}}	//{{{ Private members	private static final int WHITESPACE = 0;	private static final int WORD_CHAR = 1;	private static final int SYMBOL = 2;	//{{{ formatParagraph() method	private static void formatParagraph(String text, int maxLineLength,		int tabSize, StringBuffer buf)	{		// align everything to paragraph's leading indent		int leadingWhitespaceCount = MiscUtilities.getLeadingWhiteSpace(text);		String leadingWhitespace = text.substring(0,leadingWhitespaceCount);		int leadingWhitespaceWidth = MiscUtilities.getLeadingWhiteSpaceWidth(text,tabSize);		buf.append(leadingWhitespace);		int lineLength = leadingWhitespaceWidth;		StringTokenizer st = new StringTokenizer(text);		while(st.hasMoreTokens())		{			String word = st.nextToken();			if(lineLength == leadingWhitespaceWidth)			{				// do nothing			}			else if(lineLength + word.length() + 1 > maxLineLength)			{				buf.append('\n');				buf.append(leadingWhitespace);				lineLength = leadingWhitespaceWidth;			}			else			{				buf.append(' ');				lineLength++;			}			buf.append(word);			lineLength += word.length();		}	} //}}}	//}}}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -