?? textutilities.java
字號:
/* * TextUtilities.java - Various text functions * Copyright (C) 1998, 1999, 2000, 2001 Slava Pestov * :tabSize=8:indentSize=8:noTabs=false: * :folding=explicit:collapseFolds=1: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package org.gjt.sp.jedit;//{{{ Importsimport java.util.*;import javax.swing.text.Segment;import org.gjt.sp.jedit.syntax.*;//}}}/** * Contains several text manipulation methods. * * <ul> * <li>Bracket matching * <li>Word start and end offset calculation * <li>String comparison * <li>Converting tabs to spaces and vice versa * <li>Wrapping text * <li>String case conversion * </ul> * * @author Slava Pestov * @version $Id: TextUtilities.java,v 1.40 2003/01/30 02:58:40 spestov Exp $ */public class TextUtilities{ //{{{ getTokenAtOffset() method /** * Returns the token that contains the specified offset. * @param tokens The token list * @param offset The offset * @since jEdit 4.0pre3 */ public static Token getTokenAtOffset(Token tokens, int offset) { if(offset == 0 && tokens.id == Token.END) return tokens; for(;;) { if(tokens.id == Token.END) throw new ArrayIndexOutOfBoundsException("offset > line length"); if(tokens.offset + tokens.length > offset) return tokens; else tokens = tokens.next; } } //}}} //{{{ findMatchingBracket() method /** * Returns the offset of the bracket matching the one at the * specified offset of the buffer, or -1 if the bracket is * unmatched (or if the character is not a bracket). * @param buffer The buffer * @param line The line * @param offset The offset within that line * @since jEdit 2.6pre1 */ public static int findMatchingBracket(Buffer buffer, int line, int offset) { if(offset < 0 || offset >= buffer.getLineLength(line)) { throw new ArrayIndexOutOfBoundsException(offset + ":" + buffer.getLineLength(line)); } Segment lineText = new Segment(); buffer.getLineText(line,lineText); char c = lineText.array[lineText.offset + offset]; char cprime; // corresponding character boolean direction; // false - backwards, true - forwards switch(c) { case '(': cprime = ')'; direction = true; break; case ')': cprime = '('; direction = false; break; case '[': cprime = ']'; direction = true; break; case ']': cprime = '['; direction = false; break; case '{': cprime = '}'; direction = true; break; case '}': cprime = '{'; direction = false; break; default: return -1; } // 1 because we've already 'seen' the first bracket int count = 1; DefaultTokenHandler tokenHandler = new DefaultTokenHandler(); buffer.markTokens(line,tokenHandler); // Get the syntax token at 'offset' // only tokens with the same type will be checked for // the corresponding bracket byte idOfBracket = getTokenAtOffset(tokenHandler.getTokens(),offset).id; boolean haveTokens = true; //{{{ Forward search if(direction) { offset++; for(;;) { for(int i = offset; i < lineText.count; i++) { char ch = lineText.array[lineText.offset + i]; if(ch == c) { if(!haveTokens) { tokenHandler.init(); buffer.markTokens(line,tokenHandler); haveTokens = true; } if(getTokenAtOffset(tokenHandler.getTokens(),i).id == idOfBracket) count++; } else if(ch == cprime) { if(!haveTokens) { tokenHandler.init(); buffer.markTokens(line,tokenHandler); haveTokens = true; } if(getTokenAtOffset(tokenHandler.getTokens(),i).id == idOfBracket) { count--; if(count == 0) return buffer.getLineStartOffset(line) + i; } } } //{{{ Go on to next line line++; if(line >= buffer.getLineCount()) break; buffer.getLineText(line,lineText); offset = 0; haveTokens = false; //}}} } } //}}} //{{{ Backward search else { offset--; for(;;) { for(int i = offset; i >= 0; i--) { char ch = lineText.array[lineText.offset + i]; if(ch == c) { if(!haveTokens) { tokenHandler.init(); buffer.markTokens(line,tokenHandler); haveTokens = true; } if(getTokenAtOffset(tokenHandler.getTokens(),i).id == idOfBracket) count++; } else if(ch == cprime) { if(!haveTokens) { tokenHandler.init(); buffer.markTokens(line,tokenHandler); haveTokens = true; } if(getTokenAtOffset(tokenHandler.getTokens(),i).id == idOfBracket) { count--; if(count == 0) return buffer.getLineStartOffset(line) + i; } } } //{{{ Go on to next line line--; if(line < 0) break; buffer.getLineText(line,lineText); offset = lineText.count - 1; haveTokens = false; //}}} } } //}}} // Nothing found return -1; } //}}} //{{{ findMatchingBracketFuzzy() method /** * Works exactly like the findMatchingBracket(Bufferm int, int) method, * but if there is no (matching) bracket at the specified offset, it * looks at the next character too. The caller only needs to make sure * that the given offset is valid. * @param buffer The buffer * @param line The line * @param offset The offset within that line * @since 4.1pre1 */ public static int findMatchingBracketFuzzy(Buffer buffer, int line, int offset) { int result = findMatchingBracket(buffer,line,offset); if((result == -1)&&(offset + 1 < buffer.getLineLength(line))) { return findMatchingBracket(buffer,line,offset + 1); } else{ return result; } } //}}} //{{{ findWordStart() method /** * Locates the start 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 */ public static int findWordStart(String line, int pos, String noWordSep) { return findWordStart(line, pos, noWordSep, true); } //}}} //{{{ findWordStart() method /** * Locates the start 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 findWordStart(String line, int pos, String noWordSep, boolean joinNonWordChars) { 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; //}}} int whiteSpaceEnd = 0;loop: for(int i = pos; i >= 0; 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 + 1; //}}} //{{{ Word character... case WORD_CHAR: if(Character.isLetterOrDigit(ch) || noWordSep.indexOf(ch) != -1) { break; } else return i + 1; //}}} //{{{ Symbol... case SYMBOL: if(!joinNonWordChars && pos!=i) return i + 1; // if we see whitespace, set flag. if(Character.isWhitespace(ch)) { return i + 1; } else if(Character.isLetterOrDigit(ch) || noWordSep.indexOf(ch) != -1) { return i + 1; } else { break; } //}}} } } return whiteSpaceEnd; } //}}} //{{{ 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 */ public static int findWordEnd(String line, int pos, String noWordSep) { return findWordEnd(line, pos, noWordSep, true); } //}}} //{{{ findWordEnd() method /**
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -