📄 jedittextarea.java
字号:
for(int i = 0; i < selection.size(); i++) { Selection s = (Selection)selection.elementAt(i); if(offset >= s.start && offset <= s.end) return s; } } return null; } //}}} //{{{ removeFromSelection() method /** * Deactivates the specified selection. * @param s The selection * @since jEdit 3.2pre1 */ public void removeFromSelection(Selection sel) { selection.removeElement(sel); invalidateLineRange(sel.startLine,sel.endLine); // to hide current line highlight invalidateLine(caretLine); fireCaretEvent(); } //}}} /* //{{{ removeFromSelection() method /** * Deactivates the selection at the specified offset. If there is * no selection at that offset, does nothing. * @param offset The offset * @since jEdit 3.2pre1 */ public void removeFromSelection(int offset) { Selection sel = getSelectionAtOffset(offset); if(sel == null) return; selection.removeElement(sel); invalidateLineRange(sel.startLine,sel.endLine); // to hide current line highlight invalidateLine(caretLine); fireCaretEvent(); } //}}} */ //{{{ resizeSelection() method /** * Resizes the selection at the specified offset, or creates a new * one if there is no selection at the specified offset. This is a * utility method that is mainly useful in the mouse event handler * because it handles the case of end being before offset gracefully * (unlike the rest of the selection API). * @param offset The offset * @param end The new selection end * @param extraEndVirt Only for rectangular selections - specifies how * far it extends into virtual space. * @param rect Make the selection rectangular? * @since jEdit 3.2pre1 */ public void resizeSelection(int offset, int end, int extraEndVirt, boolean rect) { Selection s = getSelectionAtOffset(offset); if(s != null) { invalidateLineRange(s.startLine,s.endLine); selection.removeElement(s); } boolean reversed = false; if(end < offset) { int tmp = offset; offset = end; end = tmp; reversed = true; } Selection newSel; if(rect) { Selection.Rect rectSel = new Selection.Rect(offset,end); if(reversed) rectSel.extraStartVirt = extraEndVirt; else rectSel.extraEndVirt = extraEndVirt; newSel = rectSel; } else newSel = new Selection.Range(offset,end); _addToSelection(newSel); fireCaretEvent(); } //}}} //{{{ extendSelection() method /** * Extends the selection at the specified offset, or creates a new * one if there is no selection at the specified offset. This is * different from resizing in that the new chunk is added to the * selection in question, instead of replacing it. * @param offset The offset * @param end The new selection end * @param rect Make the selection rectangular? * @since jEdit 3.2pre1 */ public void extendSelection(int offset, int end) { Selection s = getSelectionAtOffset(offset); if(s != null) { invalidateLineRange(s.startLine,s.endLine); selection.removeElement(s); if(offset == s.start) { offset = end; end = s.end; } else if(offset == s.end) { offset = s.start; } } if(end < offset) { int tmp = end; end = offset; offset = tmp; } _addToSelection(new Selection.Range(offset,end)); fireCaretEvent(); } //}}} //{{{ getSelectedText() method /** * Returns the text in the specified selection. * @param s The selection * @since jEdit 3.2pre1 */ public String getSelectedText(Selection s) { StringBuffer buf = new StringBuffer(); s.getText(buffer,buf); return buf.toString(); } //}}} //{{{ getSelectedText() method /** * Returns the text in all active selections. * @param separator The string to insert between each text chunk * (for example, a newline) * @since jEdit 3.2pre1 */ public String getSelectedText(String separator) { if(selection.size() == 0) return null; StringBuffer buf = new StringBuffer(); for(int i = 0; i < selection.size(); i++) { if(i != 0) buf.append(separator); ((Selection)selection.elementAt(i)).getText(buffer,buf); } return buf.toString(); } //}}} //{{{ getSelectedText() method /** * Returns the text in all active selections, with a newline * between each text chunk. */ public String getSelectedText() { return getSelectedText("\n"); } //}}} //{{{ setSelectedText() method /** * Replaces the selection with the specified text. * @param s The selection * @param selectedText The new text * @since jEdit 3.2pre1 */ public void setSelectedText(Selection s, String selectedText) { if(!isEditable()) { throw new InternalError("Text component" + " read only"); } try { buffer.beginCompoundEdit(); moveCaretPosition(s.setText(buffer,selectedText)); } // No matter what happends... stops us from leaving buffer // in a bad state finally { buffer.endCompoundEdit(); } // no no no!!!! //selectNone(); } //}}} //{{{ setSelectedText() method /** * Replaces the selection at the caret with the specified text. * If there is no selection at the caret, the text is inserted at * the caret position. */ public void setSelectedText(String selectedText) { if(!isEditable()) { throw new InternalError("Text component" + " read only"); } Selection[] selection = getSelection(); if(selection.length == 0) { // for compatibility with older jEdit versions buffer.insert(caret,selectedText); } else { try { int newCaret = -1; buffer.beginCompoundEdit(); for(int i = 0; i < selection.length; i++) { newCaret = selection[i].setText(buffer,selectedText); } moveCaretPosition(newCaret); } finally { buffer.endCompoundEdit(); } } selectNone(); } //}}} //{{{ getSelectedLines() method /** * Returns a sorted array of line numbers on which a selection or * selections are present.<p> * * This method is the most convenient way to iterate through selected * lines in a buffer. The line numbers in the array returned by this * method can be passed as a parameter to such methods as * {@link org.gjt.sp.jedit.Buffer#getLineText(int)}. * * @since jEdit 3.2pre1 */ public int[] getSelectedLines() { if(selection.size() == 0) return new int[] { caretLine }; Integer line; Hashtable hash = new Hashtable(); for(int i = 0; i < selection.size(); i++) { Selection s = (Selection)selection.elementAt(i); int endLine = (s.end == getLineStartOffset(s.endLine) ? s.endLine - 1 : s.endLine); for(int j = s.startLine; j <= endLine; j++) { line = new Integer(j); hash.put(line,line); } } int[] returnValue = new int[hash.size()]; int i = 0; Enumeration keys = hash.keys(); while(keys.hasMoreElements()) { line = (Integer)keys.nextElement(); returnValue[i++] = line.intValue(); } Arrays.sort(returnValue); return returnValue; } //}}} //{{{ showSelectLineRangeDialog() method /** * Displays the 'select line range' dialog box, and selects the * specified range of lines. * @since jEdit 2.7pre2 */ public void showSelectLineRangeDialog() { new SelectLineRange(view); } //}}} //}}} //{{{ Caret //{{{ blinkCaret() method /** * Blinks the caret. */ public final void blinkCaret() { if(caretBlinks) { blink = !blink; invalidateLine(caretLine); } else blink = true; } //}}} //{{{ centerCaret() method /** * Centers the caret on the screen. * @since jEdit 2.7pre2 */ public void centerCaret() { int offset = getScreenLineStartOffset(visibleLines / 2); if(offset == -1) getToolkit().beep(); else setCaretPosition(offset); } //}}} //{{{ setCaretPosition() method /** * Sets the caret position and deactivates the selection. * @param caret The caret position */ public void setCaretPosition(int newCaret) { invalidateSelectedLines(); selection.removeAllElements(); moveCaretPosition(newCaret,true); } //}}} //{{{ setCaretPosition() method /** * Sets the caret position and deactivates the selection. * @param caret The caret position * @param doElectricScroll Do electric scrolling? */ public void setCaretPosition(int newCaret, boolean doElectricScroll) { invalidateSelectedLines(); selection.removeAllElements(); moveCaretPosition(newCaret,doElectricScroll); } //}}} //{{{ moveCaretPosition() method /** * Sets the caret position without deactivating the selection. * @param caret The caret position */ public void moveCaretPosition(int newCaret) { moveCaretPosition(newCaret,true); } //}}} //{{{ moveCaretPosition() method /** * Sets the caret position without deactivating the selection. * @param caret The caret position * @param doElectricScroll Do electric scrolling? */ public void moveCaretPosition(int newCaret, boolean doElectricScroll) { if(newCaret < 0 || newCaret > buffer.getLength()) { throw new IllegalArgumentException("caret out of bounds: " + newCaret); } if(caret == newCaret) { if(view.getTextArea() == this) finishCaretUpdate(doElectricScroll,false); } else { int newCaretLine = getLineOfOffset(newCaret); magicCaret = -1; if(!foldVisibilityManager.isLineVisible(newCaretLine)) { if(foldVisibilityManager.isNarrowed()) { int collapseFolds = buffer.getIntegerProperty( "collapseFolds",0); if(collapseFolds != 0) { foldVisibilityManager.expandFolds(collapseFolds); foldVisibilityManager.expandFold(newCaretLine,false); } else foldVisibilityManager.expandAllFolds(); } else foldVisibilityManager.expandFold(newCaretLine,false); } if(caretLine == newCaretLine) { if(caretScreenLine != -1) invalidateScreenLineRange(caretScreenLine,caretScreenLine); } else { int newCaretScreenLine = chunkCache.getScreenLineOfOffset(newCaretLine, newCaret - buffer.getLineStartOffset(newCaretLine)); if(caretScreenLine == -1) invalidateScreenLineRange(newCaretScreenLine,newCaretScreenLine); else invalidateScreenLineRange(caretScreenLine,newCaretScreenLine); caretScreenLine = newCaretScreenLine; } caret = newCaret; caretLine = newCaretLine; if(view.getTextArea() == this) finishCaretUpdate(doElectricScroll,true); } } //}}} //{{{ getCaretPosition() method /** * Returns a zero-based index of the caret position. */ public int getCaretPosition() { return caret; } //}}} //{{{ getCaretLine() method /** * Returns the line number containing the caret. */ public int getCaretLine() { return caretLine; } //}}} //{{{ getMagicCaretPosition() method /** * @deprecated Do not call this method. */ public final int getMagicCaretPosition() { if(magicCaret == -1) { magicCaret = offsetToX(caretLine,caret - getLineStartOffset(caretLine)); } return magicCaret; } //}}} //{{{ setMagicCaretPosition() method /** * Sets the `magic' caret position. This can be used to preserve * the column position when moving up and down lines. * @param magicCaret The magic caret position */ public final void setMagicCaretPosition(int magicCaret) { this.magicCaret = magicCaret; } //}}} //{{{ addCaretListener() method /** * Adds a caret change listener to this text area. * @param listener The listener */ public final void addCaretListener(CaretListener listener) { listenerList.add(CaretListener.class,listener); } //}}} //{{{ removeCaretListener() method /** * Removes a caret change listener from this text area. * @param listener The listener */ public final void removeCaretListener(CaretListener listener) { listenerList.remove(CaretListener.class,listener); } //}}} //{{{ getBracketPosition() method /** * Returns the position of the highlighted bracket (the bracket * matching the one before the caret) */ public final int getBracketPosition() { return bracketPosition; } //}}} //{{{ getBracketLine() method /** * Returns the line of the highlighted bracket (the bracket * matching the one before the caret) */ public final int getBracketLine() { return bracketLine; } //}}} //{{{ goToNextBracket() method /** * Moves the caret to the next closing bracket. * @since jEdit 2.7pre2. */ public void goToNextBracket(boolean select) { String text = getText(caret,buffer.getLength() - caret - 1); int newCaret = -1;loop: for(int i = 0; i < text.length(); i++) { switch(text.charAt(i)) { case ')': case ']': case '}': newCaret = caret + i + 1; break loop; } } if(newCaret == -1) getToolkit().beep(); else { if(select) extendSelection(caret,newCaret); else if(!multi) selectNone(); moveCaretPosition(newCaret); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -