📄 chunkcache.java
字号:
/* * ChunkCache.java - Intermediate layer between token lists from a TokenMarker * and what you see on screen * :tabSize=8:indentSize=8:noTabs=false: * :folding=explicit:collapseFolds=1: * * Copyright (C) 2001, 2004 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.util.*;import org.gjt.sp.jedit.Buffer;import org.gjt.sp.jedit.Debug;import org.gjt.sp.jedit.syntax.*;import org.gjt.sp.util.Log;//}}}/** * Manages low-level text display tasks. * * @author Slava Pestov * @version $Id: ChunkCache.java,v 1.94 2004/01/29 02:36:58 spestov Exp $ */class ChunkCache{ //{{{ ChunkCache constructor ChunkCache(JEditTextArea textArea) { this.textArea = textArea; out = new ArrayList(); tokenHandler = new DisplayTokenHandler(); } //}}} //{{{ getMaxHorizontalScrollWidth() method int getMaxHorizontalScrollWidth() { int max = 0; for(int i = 0; i < firstInvalidLine; i++) { LineInfo info = lineInfo[i]; if(info.width > max) max = info.width; } return max; } //}}} //{{{ getScreenLineOfOffset() method int getScreenLineOfOffset(int line, int offset) { if(line < textArea.getFirstPhysicalLine()) { return -1; } else if(line == textArea.getFirstPhysicalLine() && offset < getLineInfo(0).offset) { return -1; } else if(line > textArea.getLastPhysicalLine()) { return -1; } else { int screenLine; if(line == lastScreenLineP) { LineInfo last = getLineInfo(lastScreenLine); if(offset >= last.offset && offset < last.offset + last.length) { return lastScreenLine; } } screenLine = -1; // Find the screen line containing this offset for(int i = 0; i < textArea.getVisibleLines(); i++) { LineInfo info = getLineInfo(i); if(info.physicalLine > line) { // line is invisible? return i - 1; //return -1; } else if(info.physicalLine == line) { if(offset >= info.offset && offset < info.offset + info.length) { screenLine = i; break; } } } if(screenLine == -1) return -1; else { lastScreenLineP = line; lastScreenLine = screenLine; return screenLine; } } } //}}} //{{{ recalculateVisibleLines() method void recalculateVisibleLines() { LineInfo[] newLineInfo = new LineInfo[textArea.getVisibleLines()]; int start; if(lineInfo == null) start = 0; else { start = Math.min(lineInfo.length,newLineInfo.length); System.arraycopy(lineInfo,0,newLineInfo,0,start); } for(int i = start; i < newLineInfo.length; i++) newLineInfo[i] = new LineInfo(); lineInfo = newLineInfo; lastScreenLine = lastScreenLineP = -1; } //}}} //{{{ setBuffer() method void setBuffer(Buffer buffer) { this.buffer = buffer; lastScreenLine = lastScreenLineP = -1; } //}}} //{{{ scrollDown() method void scrollDown(int amount) { int visibleLines = textArea.getVisibleLines(); System.arraycopy(lineInfo,amount,lineInfo,0,visibleLines - amount); for(int i = visibleLines - amount; i < visibleLines; i++) { lineInfo[i] = new LineInfo(); } firstInvalidLine -= amount; if(firstInvalidLine < 0) firstInvalidLine = 0; if(Debug.CHUNK_CACHE_DEBUG) { System.err.println("f > t.f: only " + amount + " need updates"); } lastScreenLine = lastScreenLineP = -1; } //}}} //{{{ scrollUp() method void scrollUp(int amount) { System.arraycopy(lineInfo,0,lineInfo,amount, textArea.getVisibleLines() - amount); for(int i = 0; i < amount; i++) { lineInfo[i] = new LineInfo(); } // don't try this at home int oldFirstInvalidLine = firstInvalidLine; firstInvalidLine = 0; updateChunksUpTo(amount); firstInvalidLine = oldFirstInvalidLine + amount; if(firstInvalidLine > textArea.getVisibleLines()) firstInvalidLine = textArea.getVisibleLines(); if(Debug.CHUNK_CACHE_DEBUG) { Log.log(Log.DEBUG,this,"f > t.f: only " + amount + " need updates"); } lastScreenLine = lastScreenLineP = -1; } //}}} //{{{ invalidateAll() method void invalidateAll() { firstInvalidLine = 0; lastScreenLine = lastScreenLineP = -1; } //}}} //{{{ invalidateChunksFrom() method void invalidateChunksFrom(int screenLine) { if(Debug.CHUNK_CACHE_DEBUG) Log.log(Log.DEBUG,this,"Invalidate from " + screenLine); firstInvalidLine = Math.min(screenLine,firstInvalidLine); if(screenLine <= lastScreenLine) lastScreenLine = lastScreenLineP = -1; } //}}} //{{{ invalidateChunksFromPhys() method void invalidateChunksFromPhys(int physicalLine) { for(int i = 0; i < firstInvalidLine; i++) { LineInfo info = lineInfo[i]; if(info.physicalLine == -1 || info.physicalLine >= physicalLine) { firstInvalidLine = i; if(i <= lastScreenLine) lastScreenLine = lastScreenLineP = -1; break; } } } //}}} //{{{ getLineInfo() method LineInfo getLineInfo(int screenLine) { updateChunksUpTo(screenLine); return lineInfo[screenLine]; } //}}} //{{{ getLineSubregionCount() method int getLineSubregionCount(int physicalLine) { if(!textArea.displayManager.softWrap) return 1; out.clear(); lineToChunkList(physicalLine,out); int size = out.size(); if(size == 0) return 1; else return size; } //}}} //{{{ getSubregionOfOffset() method /** * Returns the subregion containing the specified offset. A subregion * is a subset of a physical line. Each screen line corresponds to one * subregion. Unlike the {@link #getScreenLineOfOffset()} method, * this method works with non-visible lines too. */ int getSubregionOfOffset(int offset, LineInfo[] lineInfos) { for(int i = 0; i < lineInfos.length; i++) { LineInfo info = lineInfos[i]; if(offset >= info.offset && offset < info.offset + info.length) return i; } return -1; } //}}} //{{{ xToSubregionOffset() method /** * Converts an x co-ordinate within a subregion into an offset from the * start of that subregion. * @param physicalLine The physical line number * @param subregion The subregion; if -1, then this is the last * subregion. * @param x The x co-ordinate * @param round Round up to next character if x is past the middle of a * character? */ int xToSubregionOffset(int physicalLine, int subregion, int x, boolean round) { LineInfo[] infos = getLineInfosForPhysicalLine(physicalLine); if(subregion == -1) subregion += infos.length; return xToSubregionOffset(infos[subregion],x,round); } //}}} //{{{ xToSubregionOffset() method /** * Converts an x co-ordinate within a subregion into an offset from the * start of that subregion. * @param info The line info object * @param x The x co-ordinate * @param round Round up to next character if x is past the middle of a * character? */ int xToSubregionOffset(LineInfo info, int x, boolean round) { int offset = Chunk.xToOffset(info.chunks,x,round); if(offset == -1 || offset == info.offset + info.length) offset = info.offset + info.length - 1; return offset; } //}}} //{{{ subregionOffsetToX() method /** * Converts an offset within a subregion into an x co-ordinate. * @param physicalLine The physical line * @param offset The offset */ int subregionOffsetToX(int physicalLine, int offset) { LineInfo[] infos = getLineInfosForPhysicalLine(physicalLine); LineInfo info = infos[getSubregionOfOffset(offset,infos)]; return subregionOffsetToX(info,offset); } //}}} //{{{ subregionOffsetToX() method /** * Converts an offset within a subregion into an x co-ordinate. * @param info The line info object * @param offset The offset */ int subregionOffsetToX(LineInfo info, int offset) { return (int)Chunk.offsetToX(info.chunks,offset); } //}}} //{{{ getSubregionStartOffset() method /** * Returns the start offset of the specified subregion of the specified * physical line. * @param line The physical line number * @param offset An offset */ int getSubregionStartOffset(int line, int offset) { LineInfo[] lineInfos = getLineInfosForPhysicalLine(line); LineInfo info = lineInfos[getSubregionOfOffset(offset,lineInfos)]; return textArea.getLineStartOffset(info.physicalLine) + info.offset; } //}}} //{{{ getSubregionEndOffset() method /** * Returns the end offset of the specified subregion of the specified * physical line. * @param line The physical line number * @param offset An offset */ int getSubregionEndOffset(int line, int offset) { LineInfo[] lineInfos = getLineInfosForPhysicalLine(line); LineInfo info = lineInfos[getSubregionOfOffset(offset,lineInfos)]; return textArea.getLineStartOffset(info.physicalLine) + info.offset + info.length; } //}}} //{{{ getBelowPosition() method /** * @param physicalLine The physical line number * @param offset The offset * @param x The location * @param ignoreWrap If true, behave as if soft wrap is off even if it
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -