📄 foldvisibilitymanager.java
字号:
/* * FoldVisibilityManager.java - Controls fold visiblity * :tabSize=8:indentSize=8:noTabs=false: * :folding=explicit:collapseFolds=1: * * Copyright (C) 2001, 2002 Slava Pestov * * 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.textarea;//{{{ Importsimport java.awt.Toolkit;import org.gjt.sp.jedit.buffer.OffsetManager;import org.gjt.sp.jedit.*;//}}}/** * Manages fold visibility.<p> * * This class contains methods for translating between physical and virtual * line numbers, for determining which lines are visible and which aren't, * and for expanding and collapsing folds.<p> * * When jEdit's folding or narrowing features are used to hide * portions of a buffer, the "virtual" line count visible * in the text area is generally not equal to the "physical" * line count of the buffer represented by the gutter's display.<p> * * You can use the {@link #physicalToVirtual(int)} and * {@link #virtualToPhysical(int)} methods to convert one type of line * number to another. * * @author Slava Pestov * @author John Gellene (API documentation) * @version $Id: FoldVisibilityManager.java,v 1.30 2003/02/21 20:12:19 spestov Exp $ * @since jEdit 4.0pre1 */public class FoldVisibilityManager{ //{{{ FoldVisibilityManager constructor public FoldVisibilityManager(Buffer buffer, OffsetManager offsetMgr, JEditTextArea textArea) { this.buffer = buffer; this.offsetMgr = offsetMgr; this.textArea = textArea; } //}}} //{{{ isNarrowed() method /** * Returns if the buffer has been narrowed. * @since jEdit 4.0pre2 */ public boolean isNarrowed() { return narrowed; } //}}} //{{{ getVirtualLineCount() method /** * Returns the number of virtual lines in the buffer. * @since jEdit 4.0pre1 */ public int getVirtualLineCount() { return offsetMgr.getVirtualLineCount(index); } //}}} //{{{ isLineVisible() method /** * Returns if the specified line is visible. * @param line A physical line index * @since jEdit 4.0pre1 */ public final boolean isLineVisible(int line) { if(line < 0 || line >= offsetMgr.getLineCount()) throw new ArrayIndexOutOfBoundsException(line); try { buffer.readLock(); return offsetMgr.isLineVisible(line,index); } finally { buffer.readUnlock(); } } //}}} //{{{ getFirstVisibleLine() method /** * Returns the physical line number of the first visible line. * @since jEdit 4.0pre3 */ public int getFirstVisibleLine() { try { buffer.readLock(); for(int i = 0; i < buffer.getLineCount(); i++) { if(offsetMgr.isLineVisible(i,index)) return i; } } finally { buffer.readUnlock(); } // can't happen? return -1; } //}}} //{{{ getLastVisibleLine() method /** * Returns the physical line number of the last visible line. * @since jEdit 4.0pre3 */ public int getLastVisibleLine() { try { buffer.readLock(); for(int i = buffer.getLineCount() - 1; i >= 0; i--) { if(offsetMgr.isLineVisible(i,index)) return i; } } finally { buffer.readUnlock(); } // can't happen? return -1; } //}}} //{{{ getNextVisibleLine() method /** * Returns the next visible line after the specified line index. * @param line A physical line index * @since jEdit 4.0pre1 */ public int getNextVisibleLine(int line) { if(line < 0 || line >= offsetMgr.getLineCount()) throw new ArrayIndexOutOfBoundsException(line); try { buffer.readLock(); if(line == buffer.getLineCount() - 1) return -1; for(int i = line + 1; i < buffer.getLineCount(); i++) { if(offsetMgr.isLineVisible(i,index)) return i; } return -1; } finally { buffer.readUnlock(); } } //}}} //{{{ getPrevVisibleLine() method /** * Returns the previous visible line before the specified line index. * @param line A physical line index * @since jEdit 4.0pre1 */ public int getPrevVisibleLine(int line) { if(line < 0 || line >= offsetMgr.getLineCount()) throw new ArrayIndexOutOfBoundsException(line); try { buffer.readLock(); if(line == 0) return -1; for(int i = line - 1; i >= 0; i--) { if(offsetMgr.isLineVisible(i,index)) return i; } return -1; } finally { buffer.readUnlock(); } } //}}} //{{{ physicalToVirtual() method /** * Converts a physical line number to a virtual line number. * @param line A physical line index * @since jEdit 4.0pre1 */ public int physicalToVirtual(int line) { try { buffer.readLock(); if(line < 0) throw new ArrayIndexOutOfBoundsException(line + " < 0"); else if(line >= offsetMgr.getLineCount()) { throw new ArrayIndexOutOfBoundsException(line + " > " + buffer.getLineCount()); } // optimization if(getVirtualLineCount() == buffer.getLineCount()) return line; while(!offsetMgr.isLineVisible(line,index) && line > 0) line--; if(line == 0 && !offsetMgr.isLineVisible(line,index)) { // inside the top narrow. return 0; } if(lastPhysical == line) { if(lastVirtual < 0 || lastVirtual >= offsetMgr.getVirtualLineCount(index)) { throw new ArrayIndexOutOfBoundsException( "cached: " + lastVirtual); } } else if(line > lastPhysical && lastPhysical != -1) { for(;;) { if(lastPhysical == line) break; if(offsetMgr.isLineVisible(lastPhysical,index)) lastVirtual++; if(lastPhysical == buffer.getLineCount() - 1) break; else lastPhysical++; } if(lastVirtual < 0 || lastVirtual >= offsetMgr.getVirtualLineCount(index)) { throw new ArrayIndexOutOfBoundsException( "fwd scan: " + lastVirtual); } } else if(line < lastPhysical && lastPhysical - line > line) { for(;;) { if(lastPhysical == line) break; if(offsetMgr.isLineVisible(lastPhysical,index)) lastVirtual--; if(lastPhysical == 0) break; else lastPhysical--; } if(lastVirtual < 0 || lastVirtual >= offsetMgr.getVirtualLineCount(index)) { throw new ArrayIndexOutOfBoundsException( "back scan: " + lastVirtual); } } else { lastPhysical = 0; // find first visible line while(!offsetMgr.isLineVisible(lastPhysical,index)) lastPhysical++; lastVirtual = 0; for(;;) { if(lastPhysical == line) break; if(offsetMgr.isLineVisible(lastPhysical,index)) lastVirtual++; if(lastPhysical == buffer.getLineCount() - 1) break; else lastPhysical++; } if(lastVirtual < 0 || lastVirtual >= offsetMgr.getVirtualLineCount(index)) { throw new ArrayIndexOutOfBoundsException( "zero scan: " + lastVirtual); } } return lastVirtual; } finally { buffer.readUnlock(); } } //}}} //{{{ virtualToPhysical() method /** * Converts a virtual line number to a physical line number. * @param line A virtual line index * @since jEdit 4.0pre1 */ public int virtualToPhysical(int line) { try { buffer.readLock(); if(line < 0) throw new ArrayIndexOutOfBoundsException(line + " < 0"); else if(line >= offsetMgr.getVirtualLineCount(index)) { throw new ArrayIndexOutOfBoundsException(line + " > " + offsetMgr.getVirtualLineCount(index)); } // optimization if(getVirtualLineCount() == buffer.getLineCount()) return line; if(lastVirtual == line) { if(lastPhysical < 0 || lastPhysical >= buffer.getLineCount()) { throw new ArrayIndexOutOfBoundsException( "cached: " + lastPhysical); } } else if(line > lastVirtual && lastVirtual != -1) { for(;;) { if(offsetMgr.isLineVisible(lastPhysical,index)) { if(lastVirtual == line) break; else lastVirtual++; } if(lastPhysical == buffer.getLineCount() - 1) break; else lastPhysical++; } if(lastPhysical < 0 || lastPhysical >= buffer.getLineCount()) { throw new ArrayIndexOutOfBoundsException( "fwd scan: " + lastPhysical); } } else if(line < lastVirtual && lastVirtual - line > line) { for(;;) { if(offsetMgr.isLineVisible(lastPhysical,index)) { if(lastVirtual == line) break; else lastVirtual--; } if(lastPhysical == 0) break; else lastPhysical--; } if(lastPhysical < 0 || lastPhysical >= buffer.getLineCount()) { throw new ArrayIndexOutOfBoundsException( "back scan: " + lastPhysical); } } else { lastPhysical = 0; // find first visible line while(!offsetMgr.isLineVisible(lastPhysical,index)) lastPhysical++; lastVirtual = 0; for(;;) { if(offsetMgr.isLineVisible(lastPhysical,index)) { if(lastVirtual == line) break; else lastVirtual++; } if(lastPhysical == buffer.getLineCount() - 1) break; else lastPhysical++; } if(lastPhysical < 0 || lastPhysical >= buffer.getLineCount()) { throw new ArrayIndexOutOfBoundsException( "zero scan: " + lastPhysical); } } return lastPhysical; } finally { buffer.readUnlock(); } } //}}} //{{{ collapseFold() method /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -