📄 jedittextarea.java
字号:
font = defaultFont; else font = styles[id].getFont(); int len = tokens.length; if(offset < len) { return (int)(x + renderer.charsWidth( text,off,offset,font,x,painter)); } else { x += renderer.charsWidth( text,off,len,font,x,painter); off += len; offset -= len; } tokens = tokens.next; } } /** * 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) { return xToOffset(line,x,true); } /** * Converts an x co-ordinate to an offset within a line. * @param line The line * @param x The x co-ordinate * @param round Round up to next letter if past the middle of a letter? * @since jEdit 3.2pre6 */ public int xToOffset(int line, int x, boolean round) { Token tokens = buffer.markTokens(line).getFirstToken(); getLineText(line,lineSegment); char[] text = lineSegment.array; int off = lineSegment.offset; Toolkit toolkit = painter.getToolkit(); Font defaultFont = painter.getFont(); SyntaxStyle[] styles = painter.getStyles(); float[] widthArray = new float[] { horizontalOffset }; for(;;) { byte id = tokens.id; if(id == Token.END) return lineSegment.count; Font font; if(id == Token.NULL) font = defaultFont; else font = styles[id].getFont(); int len = tokens.length; int offset = renderer.xToOffset(text,off,len,font,x, painter,round,widthArray); if(offset != -1) return offset - lineSegment.offset; off += len; 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) { return xyToOffset(x,y,true); } /** * 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 * @param round Round up to next letter if past the middle of a letter? * @since jEdit 3.2pre6 */ public int xyToOffset(int x, int y, boolean round) { FontMetrics fm = painter.getFontMetrics(); int height = fm.getHeight(); int line = y / height + firstLine; if(line < 0) return 0; else if(line >= getVirtualLineCount()) { // WRONG!!! // return getBufferLength(); return getLineEndOffset(buffer.virtualToPhysical( buffer.getVirtualLineCount() - 1)) - 1; } else { line = buffer.virtualToPhysical(line); return getLineStartOffset(line) + xToOffset(line,x); } } /** * Marks a line as needing a repaint. * @param line The line to invalidate */ public final void invalidateLine(int line) { line = buffer.physicalToVirtual(line); FontMetrics fm = painter.getFontMetrics(); int y = lineToY(line) + fm.getDescent() + fm.getLeading(); painter.repaint(0,y,painter.getWidth(),fm.getHeight()); gutter.repaint(0,y,gutter.getWidth(),fm.getHeight()); } /** * Marks a range of lines as needing a repaint. * @param firstLine The first line to invalidate * @param lastLine The last line to invalidate */ public final void invalidateLineRange(int firstLine, int lastLine) { firstLine = buffer.physicalToVirtual(firstLine); // all your bugs are belong to us if(lastLine > buffer.virtualToPhysical( buffer.getVirtualLineCount() - 1)) { lastLine = (lastLine - buffer.getLineCount()) + buffer.getVirtualLineCount(); } else lastLine = buffer.physicalToVirtual(lastLine); FontMetrics fm = painter.getFontMetrics(); int y = lineToY(firstLine) + fm.getDescent() + fm.getLeading(); int height = (lastLine - firstLine + 1) * fm.getHeight(); painter.repaint(0,y,painter.getWidth(),height); gutter.repaint(0,y,gutter.getWidth(),height); } /** * Repaints the lines containing the selection. */ public final void invalidateSelectedLines() { for(int i = 0; i < selection.size(); i++) { Selection s = (Selection)selection.elementAt(i); invalidateLineRange(s.startLine,s.endLine); } } /** * Returns the buffer this text area is editing. */ public final Buffer getBuffer() { return buffer; } /** * Sets the buffer this text area is editing. * @param buffer The buffer */ public void setBuffer(Buffer buffer) { if(this.buffer == buffer) return; if(this.buffer != null) { this.buffer.removeDocumentListener(documentHandler); this.buffer.removeFoldListener(foldHandler); } this.buffer = buffer; buffer.addDocumentListener(documentHandler); buffer.addFoldListener(foldHandler); documentHandlerInstalled = true; maxHorizontalScrollWidth = 0; painter.updateTabSize(); setCaretPosition(0); updateScrollBars(); painter.repaint(); gutter.repaint(); } /** * Returns the length of the buffer. Equivalent to calling * <code>getBuffer().getLength()</code>. */ public final int getBufferLength() { return buffer.getLength(); } /** * Returns the number of lines in the document. */ public final int getLineCount() { return buffer.getLineCount(); } /** * Returns the number of visible lines in the document (which may * be less than the total due to folding). * @since jEdit 3.1pre1 */ public final int getVirtualLineCount() { return buffer.getVirtualLineCount(); } /** * Returns the line containing the specified offset. * @param offset The offset */ public final int getLineOfOffset(int offset) { return buffer.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 = buffer.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 = buffer.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 = buffer.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 buffer.getText(0,buffer.getLength()); } catch(BadLocationException bl) { bl.printStackTrace(); return null; } } /** * Sets the entire text of this text area. */ public void setText(String text) { try { buffer.beginCompoundEdit(); buffer.remove(0,buffer.getLength()); buffer.insertString(0,text,null); } catch(BadLocationException bl) { bl.printStackTrace(); } finally { buffer.endCompoundEdit(); } } /** * Returns the specified substring of the buffer. * @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 buffer.getText(start,len); } catch(BadLocationException bl) { bl.printStackTrace(); return null; } } /** * Copies the specified substring of the buffer 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 { buffer.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) { Element lineElement = buffer.getDefaultRootElement() .getElement(lineIndex); int start = lineElement.getStartOffset(); getText(start,lineElement.getEndOffset() - start - 1,segment); } /** * Selects all text in the buffer. */ public final void selectAll() { setSelection(new Selection.Range(0,buffer.getLength())); moveCaretPosition(buffer.getLength(),true); } /** * Selects the current line. * @since jEdit 2.7pre2 */ public void selectLine() { int caretLine = getCaretLine(); int start = getLineStartOffset(caretLine); int end = getLineEndOffset(caretLine) - 1; setSelection(new Selection.Range(start,end)); moveCaretPosition(end); } /** * Selects the paragraph at the caret position. * @since jEdit 2.7pre2 */ public void selectParagraph() { int caretLine = getCaretLine(); if(getLineLength(caretLine) == 0) { view.getToolkit().beep(); return; } int start = caretLine; int end = caretLine; while(start >= 0) { if(getLineLength(start) == 0) break; else start--; } while(end < getLineCount()) { if(getLineLength(end) == 0) break; else end++; } int selectionStart = getLineStartOffset(start + 1); int selectionEnd = getLineEndOffset(end - 1) - 1; setSelection(new Selection.Range(selectionStart, selectionEnd)); moveCaretPosition(selectionEnd); } /** * Selects the word at the caret position. * @since jEdit 2.7pre2 */ public void selectWord() { int line = getCaretLine(); int lineStart = getLineStartOffset(line); int offset = getCaretPosition() - lineStart; if(getLineLength(line) == 0) return; String lineText = getLineText(line); String noWordSep = (String)buffer.getProperty("noWordSep"); if(offset == getLineLength(line)) offset--; int wordStart = TextUtilities.findWordStart(lineText,offset,noWordSep); int wordEnd = TextUtilities.findWordEnd(lineText,offset+1,noWordSep); setSelection(new Selection.Range(lineStart + wordStart, lineStart + wordEnd)); moveCaretPosition(lineStart + wordEnd); } // OLD (NON-MULTI AWARE) SELECTION API /** * @deprecated Instead, obtain a Selection instance using * any means, and call its <code>getStart()</code> method */ public final int getSelectionStart() { if(selection.size() != 1) return caret; return ((Selection)selection.elementAt(0)).getStart(); } /** * @deprecated Instead, obtain a Selection instance using * any means, and call its <code>getStart(int)</code> method */ public int getSelectionStart(int line) { if(selection.size() != 1) return caret; return ((Selection)selection.elementAt(0)).getStart( buffer,line); } /** * @deprecated Instead, obtain a Selection instance using * any means, and call its <code>getStartLine()</code> method */ public final int getSelectionStartLine() { if(selection.size() != 1) return caret; return ((Selection)selection.elementAt(0)).getStartLine(); } /** * @deprecated Do not use. */ public final void setSelectionStart(int selectionStart) { select(selectionStart,getSelectionEnd(),true); } /** * @deprecated Instead, obtain a Selection instance using * any means, and call its <code>getEnd()</code> method */ public final int getSelectionEnd() { if(selection.size() != 1) return caret; return ((Selection)selection.elementAt(0)).getEnd(); } /** * @deprecated Instead, obtain a Selection instance using * any means, and call its <code>getEnd(int)</code> method */ public int getSelectionEnd(int line) { if(selection.size() != 1) return caret; return ((Selection)selection.elementAt(0)).getEnd( buffer,line); } /** * @deprecated Instead, obtain a Selection instance using * any means, and call its <code>getEndLine()</code> method */ public final int getSelectionEndLine() { if(selection.size() != 1) return caret; return ((Selection)selection.elementAt(0)).getEndLine(); } /** * @deprecated Do not use. */ public final void setSelectionEnd(int selectionEnd) { select(getSelectionStart(),selectionEnd,true); } /** * @deprecated Do not use. */ public final int getMarkPosition() { Selection s = getSelectionAtOffset(caret); if(s == null) return caret; if(s.start == caret) return s.end; else if(s.end == caret) return s.start; else return caret; } /** * @deprecated Do not use. */ public final int getMarkLine() { if(selection.size() != 1) return caretLine; Selection s = (Selection)selection.elementAt(0); if(s.start == caret) return s.endLine; else if(s.end == caret) return s.startLine; else return caretLine; } /** * @deprecated Instead, call either <code>addToSelection()</code>, * or <code>setSelection()</code> with a new Selection instance. */ public void select(int start, int end)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -