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

📄 jedittextarea.java

📁 jedit中独立出来的语法高亮组件
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	/**	 * Converts an x co-ordinate to an offset within a line.	 * @param line The line	 * @param x The x co-ordinate	 */	public int xToOffset(int line, int x)	{		TokenMarker tokenMarker = getTokenMarker();		/* Use painter's cached info for speed */		FontMetrics fm = painter.getFontMetrics();		getLineText(line,lineSegment);		char[] segmentArray = lineSegment.array;		int segmentOffset = lineSegment.offset;		int segmentCount = lineSegment.count;		int width = horizontalOffset;		if(tokenMarker == null)		{			for(int i = 0; i < segmentCount; i++)			{				char c = segmentArray[i + segmentOffset];				int charWidth;				if(c == '\t')					charWidth = (int)painter.nextTabStop(width,i)						- width;				else					charWidth = fm.charWidth(c);				if(painter.isBlockCaretEnabled())				{					if(x - charWidth <= width)						return i;				}				else				{					if(x - charWidth / 2 <= width)						return i;				}				width += charWidth;			}			return segmentCount;		}		else		{			Token tokens;			if(painter.currentLineIndex == line && painter				.currentLineTokens != null)				tokens = painter.currentLineTokens;			else			{				painter.currentLineIndex = line;				tokens = painter.currentLineTokens					= tokenMarker.markTokens(lineSegment,line);			}			int offset = 0;			Toolkit toolkit = painter.getToolkit();			Font defaultFont = painter.getFont();			SyntaxStyle[] styles = painter.getStyles();			for(;;)			{				byte id = tokens.id;				if(id == Token.END)					return offset;				if(id == Token.NULL)					fm = painter.getFontMetrics();				else					fm = styles[id].getFontMetrics(defaultFont);				int length = tokens.length;				for(int i = 0; i < length; i++)				{					char c = segmentArray[segmentOffset + offset + i];					int charWidth;					if(c == '\t')						charWidth = (int)painter.nextTabStop(width,offset + i)							- width;					else						charWidth = fm.charWidth(c);					if(painter.isBlockCaretEnabled())					{						if(x - charWidth <= width)							return offset + i;					}					else					{						if(x - charWidth / 2 <= width)							return offset + i;					}					width += charWidth;				}				offset += length;				tokens = tokens.next;			}		}	}	/**	 * Converts a point to an offset, from the start of the text.	 * @param x The x co-ordinate of the point	 * @param y The y co-ordinate of the point	 */	public int xyToOffset(int x, int y)	{		int line = yToLine(y);		int start = getLineStartOffset(line);		return start + xToOffset(line,x);	}	/**	 * Returns the document this text area is editing.	 */	public final SyntaxDocument getDocument()	{		return document;	}	/**	 * Sets the document this text area is editing.	 * @param document The document	 */	public void setDocument(SyntaxDocument document)	{		if(this.document == document)			return;		if(this.document != null)			this.document.removeDocumentListener(documentHandler);		this.document = document;		document.addDocumentListener(documentHandler);		select(0,0);		updateScrollBars();		painter.repaint();	}	/**	 * Returns the document's token marker. Equivalent to calling	 * <code>getDocument().getTokenMarker()</code>.	 */	public final TokenMarker getTokenMarker()	{		return document.getTokenMarker();	}	/**	 * Sets the document's token marker. Equivalent to caling	 * <code>getDocument().setTokenMarker()</code>.	 * @param tokenMarker The token marker	 */	public final void setTokenMarker(TokenMarker tokenMarker)	{		document.setTokenMarker(tokenMarker);	}	/**	 * Returns the length of the document. Equivalent to calling	 * <code>getDocument().getLength()</code>.	 */	public final int getDocumentLength()	{		return document.getLength();	}	/**	 * Returns the number of lines in the document.	 */	public final int getLineCount()	{		return document.getDefaultRootElement().getElementCount();	}	/**	 * Returns the line containing the specified offset.	 * @param offset The offset	 */	public final int getLineOfOffset(int offset)	{		return document.getDefaultRootElement().getElementIndex(offset);	}	/**	 * Returns the start offset of the specified line.	 * @param line The line	 * @return The start offset of the specified line, or -1 if the line is	 * invalid	 */	public int getLineStartOffset(int line)	{		Element lineElement = document.getDefaultRootElement()			.getElement(line);		if(lineElement == null)			return -1;		else			return lineElement.getStartOffset();	}	/**	 * Returns the end offset of the specified line.	 * @param line The line	 * @return The end offset of the specified line, or -1 if the line is	 * invalid.	 */	public int getLineEndOffset(int line)	{		Element lineElement = document.getDefaultRootElement()			.getElement(line);		if(lineElement == null)			return -1;		else			return lineElement.getEndOffset();	}	/**	 * Returns the length of the specified line.	 * @param line The line	 */	public int getLineLength(int line)	{		Element lineElement = document.getDefaultRootElement()			.getElement(line);		if(lineElement == null)			return -1;		else			return lineElement.getEndOffset()				- lineElement.getStartOffset() - 1;	}	/**	 * Returns the entire text of this text area.	 */	public String getText()	{		try		{			return document.getText(0,document.getLength());		}		catch(BadLocationException bl)		{			bl.printStackTrace();			return null;		}	}	/**	 * Sets the entire text of this text area.	 */	public void setText(String text)	{		try		{			document.beginCompoundEdit();			document.remove(0,document.getLength());			document.insertString(0,text,null);		}		catch(BadLocationException bl)		{			bl.printStackTrace();		}		finally		{			document.endCompoundEdit();		}	}	/**	 * Returns the specified substring of the document.	 * @param start The start offset	 * @param len The length of the substring	 * @return The substring, or null if the offsets are invalid	 */	public final String getText(int start, int len)	{		try		{			return document.getText(start,len);		}		catch(BadLocationException bl)		{			bl.printStackTrace();			return null;		}	}	/**	 * Copies the specified substring of the document into a segment.	 * If the offsets are invalid, the segment will contain a null string.	 * @param start The start offset	 * @param len The length of the substring	 * @param segment The segment	 */	public final void getText(int start, int len, Segment segment)	{		try		{			document.getText(start,len,segment);		}		catch(BadLocationException bl)		{			bl.printStackTrace();			segment.offset = segment.count = 0;		}	}	/**	 * Returns the text on the specified line.	 * @param lineIndex The line	 * @return The text, or null if the line is invalid	 */	public final String getLineText(int lineIndex)	{		int start = getLineStartOffset(lineIndex);		return getText(start,getLineEndOffset(lineIndex) - start - 1);	}	/**	 * Copies the text on the specified line into a segment. If the line	 * is invalid, the segment will contain a null string.	 * @param lineIndex The line	 */	public final void getLineText(int lineIndex, Segment segment)	{		int start = getLineStartOffset(lineIndex);		getText(start,getLineEndOffset(lineIndex) - start - 1,segment);	}	/**	 * Returns the selection start offset.	 */	public final int getSelectionStart()	{		return selectionStart;	}	/**	 * Returns the offset where the selection starts on the specified	 * line.	 */	public int getSelectionStart(int line)	{		if(line == selectionStartLine)			return selectionStart;		else if(rectSelect)		{			Element map = document.getDefaultRootElement();			int start = selectionStart - map.getElement(selectionStartLine)				.getStartOffset();			Element lineElement = map.getElement(line);			int lineStart = lineElement.getStartOffset();			int lineEnd = lineElement.getEndOffset() - 1;			return Math.min(lineEnd,lineStart + start);		}		else			return getLineStartOffset(line);	}	/**	 * Returns the selection start line.	 */	public final int getSelectionStartLine()	{		return selectionStartLine;	}	/**	 * Sets the selection start. The new selection will be the new	 * selection start and the old selection end.	 * @param selectionStart The selection start	 * @see #select(int,int)	 */	public final void setSelectionStart(int selectionStart)	{		select(selectionStart,selectionEnd);	}	/**	 * Returns the selection end offset.	 */	public final int getSelectionEnd()	{		return selectionEnd;	}	/**	 * Returns the offset where the selection ends on the specified	 * line.	 */	public int getSelectionEnd(int line)	{		if(line == selectionEndLine)			return selectionEnd;		else if(rectSelect)		{			Element map = document.getDefaultRootElement();			int end = selectionEnd - map.getElement(selectionEndLine)				.getStartOffset();			Element lineElement = map.getElement(line);			int lineStart = lineElement.getStartOffset();			int lineEnd = lineElement.getEndOffset() - 1;			return Math.min(lineEnd,lineStart + end);		}		else			return getLineEndOffset(line) - 1;	}	/**	 * Returns the selection end line.	 */	public final int getSelectionEndLine()	{		return selectionEndLine;	}	/**	 * Sets the selection end. The new selection will be the old	 * selection start and the bew selection end.	 * @param selectionEnd The selection end	 * @see #select(int,int)	 */	public final void setSelectionEnd(int selectionEnd)	{		select(selectionStart,selectionEnd);	}	/**	 * Returns the caret position. This will either be the selection	 * start or the selection end, depending on which direction the	 * selection was made in.	 */	public final int getCaretPosition()	{		return (biasLeft ? selectionStart : selectionEnd);	}	/**	 * Returns the caret line.	 */	public final int getCaretLine()	{		return (biasLeft ? selectionStartLine : selectionEndLine);	}	/**	 * Returns the mark position. This will be the opposite selection	 * bound to the caret position.	 * @see #getCaretPosition()	 */	public final int getMarkPosition()	{		return (biasLeft ? selectionEnd : selectionStart);	}	/**	 * Returns the mark line.	 */	public final int getMarkLine()	{		return (biasLeft ? selectionEndLine : selectionStartLine);	}	/**	 * Sets the caret position. The new selection will consist of the	 * caret position only (hence no text will be selected)	 * @param caret The caret position	 * @see #select(int,int)	 */	public final void setCaretPosition(int caret)	{		select(caret,caret);	}	/**	 * Selects all text in the document.	 */	public final void selectAll()	{		select(0,getDocumentLength());	}	/**	 * Moves the mark to the caret position.	 */	public final void selectNone()	{		select(getCaretPosition(),getCaretPosition());	}	/**	 * Selects from the start offset to the end offset. This is the	 * general selection method used by all other selecting methods.	 * The caret position will be start if start &lt; end, and end	 * if end &gt; start.	 * @param start The start offset	 * @param end The end offset	 */	public void select(int start, int end)	{		int newStart, newEnd;		boolean newBias;		if(start <= end)		{			newStart = start;			newEnd = end;			newBias = false;		}		else		{			newStart = end;			newEnd = start;			newBias = true;		}		if(newStart < 0 || newEnd > getDocumentLength())		{			throw new IllegalArgumentException("Bounds out of"				+ " range: " + newStart + "," +				newEnd);		}

⌨️ 快捷键说明

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