/** Reformat a block of code. * @param doc document to work with * @param startOffset offset at which the formatting starts * @param endOffset offset at which the formatting ends * @return length of the reformatted code */ public static int reformat(BaseDocument doc, int startOffset, int endOffset) throws BadLocationException { return doc.getFormatter().reformat(doc, startOffset, endOffset); }
/** Get the width of one indentation level. * The algorithm first checks whether there's a value for the INDENT_SHIFT_WIDTH * setting. If so it uses it, otherwise it uses <code>formatter.getSpacesPerTab()</code>. * * @see getTabSize() * @see Formatter.getSpacesPerTab() */ public int getShiftWidth() { if (shiftWidth != null) { return shiftWidth.intValue(); } else { return getFormatter().getSpacesPerTab(); } }
/** * Reformat a block of code. * * @param doc * document to work with * @param startOffset * offset at which the formatting starts * @param endOffset * offset at which the formatting ends * @return length of the reformatted code */ public static int reformat(BaseDocument doc, int startOffset, int endOffset) throws BadLocationException { return doc.getFormatter().reformat(doc, startOffset, endOffset); }
/** * Get the width of one indentation level. The algorithm first checks * whether there's a value for the INDENT_SHIFT_WIDTH setting. If so it uses * it, otherwise it uses <code>formatter.getSpacesPerTab()</code>. * * @see getTabSize() * @see Formatter.getSpacesPerTab() */ public int getShiftWidth() { if (shiftWidth != null) { return shiftWidth.intValue(); } else { return getFormatter().getSpacesPerTab(); } }
private static String getIndentString(BaseDocument doc, int startOffset, String str) { try { Formatter f = doc.getFormatter(); CharArrayWriter cw = new CharArrayWriter(); Writer w = f.createWriter(doc, startOffset, cw); w.write(str, 0, str.length()); w.close(); String out = new String(cw.toCharArray()); int i = 0; for (; i < out.length(); i++) { if (out.charAt(i) != ' ' && out.charAt(i) != '\t') break; } // System.out.println(out+"|"); // NOI18N // System.out.println(out.substring(0,i)+"^"); // NOI18N return out.substring(0, i); } catch (java.io.IOException e) { return ""; } } }
/** Get the visual column corresponding to the position after pressing * the TAB key. * @param doc document to work with * @param offset position at which the TAB was pressed */ public static int getNextTabColumn(BaseDocument doc, int offset) throws BadLocationException { int col = getVisualColumn(doc, offset); int tabSize = doc.getFormatter().getSpacesPerTab(); return (col + tabSize) / tabSize * tabSize; }
/** * Get the visual column corresponding to the position after pressing the * TAB key. * * @param doc * document to work with * @param offset * position at which the TAB was pressed */ public static int getNextTabColumn(BaseDocument doc, int offset) throws BadLocationException { int col = getVisualColumn(doc, offset); int tabSize = doc.getFormatter().getSpacesPerTab(); return (col + tabSize) / tabSize * tabSize; }
/** * @deprecated * @see Formatter.insertTabString() */ public static String getTabInsertString(BaseDocument doc, int offset) throws BadLocationException { int col = getVisualColumn(doc, offset); Formatter f = doc.getFormatter(); boolean expandTabs = f.expandTabs(); if (expandTabs) { int spacesPerTab = f.getSpacesPerTab(); int len = (col + spacesPerTab) / spacesPerTab * spacesPerTab - col; return new String(Analyzer.getSpacesBuffer(len), 0, len); } else { // insert pure tab return "\t"; // NOI18N } }
/** @deprecated * @see Formatter.insertTabString() */ public static String getTabInsertString(BaseDocument doc, int offset) throws BadLocationException { int col = getVisualColumn(doc, offset); Formatter f = doc.getFormatter(); boolean expandTabs = f.expandTabs(); if (expandTabs) { int spacesPerTab = f.getSpacesPerTab(); int len = (col + spacesPerTab) / spacesPerTab * spacesPerTab - col; return new String(Analyzer.getSpacesBuffer(len), 0, len); } else { // insert pure tab return "\t"; // NOI18N } }
protected boolean doExpansion(int dotPos, String expandStr, ActionEvent evt) throws BadLocationException { Object expansion = translateAbbrev(expandStr); boolean expanded = false; if (expansion instanceof String) { // expand to string BaseDocument doc = editorUI.getDocument(); String ins = (String)expansion; int offset = ins.indexOf('|'); if (offset >= 0) { if (offset > 0) doc.insertString(dotPos, ins.substring(0, offset), null); if (offset+1 < ins.length()) doc.insertString(dotPos + offset, ins.substring(offset + 1), null); Caret caret = editorUI.getComponent().getCaret(); caret.setDot(dotPos + offset); } else { doc.insertString(dotPos, ins, null); } if(ins.indexOf("\n") != -1) { // NOI18N Formatter formatter = doc.getFormatter(); formatter.reformat(doc, dotPos, dotPos + ins.length()); } expanded = true; } else if (expansion instanceof Action) { ((Action)expansion).actionPerformed(evt); expanded = true; } return expanded; }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; } target.replaceSelection(""); Caret caret = target.getCaret(); int dotPos = caret.getDot(); BaseDocument doc = (BaseDocument) target.getDocument(); dotPos = doc.getFormatter().indentNewLine(doc, dotPos); caret.setDot(dotPos); } }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; } try { Caret caret = target.getCaret(); BaseDocument doc = Utilities.getDocument(target); if (caret.isSelectionVisible()) { doc.getFormatter().changeBlockIndent(doc, target.getSelectionStart(), target.getSelectionEnd(), right ? +1 : -1); } else { doc.getFormatter().shiftLine(doc, caret.getDot(), right); } } catch (GuardedException e) { target.getToolkit().beep(); } catch (BadLocationException e) { e.printStackTrace(); } } } }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; } try { Caret caret = target.getCaret(); BaseDocument doc = Utilities.getDocument(target); if (caret.isSelectionVisible()) { doc.getFormatter().changeBlockIndent(doc, target.getSelectionStart(), target.getSelectionEnd(), right ? +1 : -1); } else { doc.getFormatter().shiftLine(doc, caret.getDot(), right); } } catch (GuardedException e) { target.getToolkit().beep(); } catch (BadLocationException e) { e.printStackTrace(); } } } }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; } BaseDocument doc = (BaseDocument)target.getDocument(); Caret caret = target.getCaret(); int dotPos = caret.getDot(); doc.atomicLock(); try{ target.replaceSelection(""); int newDotPos = dotPos; // dot stays where it was doc.getFormatter().indentNewLine(doc, dotPos); // newline caret.setDot(newDotPos); } finally { doc.atomicUnlock(); } } }
/** Check the characters that should cause reindenting the line. */ protected void checkIndentHotChars(JTextComponent target, String typedText) { BaseDocument doc = Utilities.getDocument(target); if (doc != null) { Caret caret = target.getCaret(); Formatter f = doc.getFormatter(); if (f instanceof ExtFormatter) { ExtFormatter ef = (ExtFormatter)f; int[] fmtBlk = ef.getReformatBlock(target, typedText); if (fmtBlk != null) { try { fmtBlk[0] = Utilities.getRowStart(doc, fmtBlk[0]); fmtBlk[1] = Utilities.getRowEnd(doc, fmtBlk[1]); //this was the of #18922, that causes the bug #20198 //ef.reformat(doc, fmtBlk[0], fmtBlk[1]); //bugfix of the bug #20198. Bug #18922 is fixed too as well as #6968 ef.reformat(doc, fmtBlk[0], fmtBlk[1], true); } catch (BadLocationException e) { } catch (IOException e) { } } } } }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; } target.replaceSelection(""); Caret caret = target.getCaret(); BaseDocument doc = (BaseDocument)target.getDocument(); doc.atomicLock(); try{ Object cookie = beforeBreak(target, doc, caret); int dotPos = caret.getDot(); int newDotPos = doc.getFormatter().indentNewLine(doc, dotPos); caret.setDot(newDotPos); afterBreak(target, doc, caret, cookie); } finally { doc.atomicUnlock(); } } }
if (caret.isSelectionVisible()) { // block selected try { doc.getFormatter().changeBlockIndent(doc, target.getSelectionStart(), target.getSelectionEnd(), -1); } catch (GuardedException e) { int endOffset = Utilities.getRowEnd(doc, caret.getDot()); if (firstNW == -1 || (firstNW >= caret.getDot())) doc.getFormatter().changeBlockIndent(doc, startOffset, endOffset, -1); else {
/** Check the characters that should cause reindenting the line. */ protected void checkIndentHotChars(JTextComponent target, String typedText) { BaseDocument doc = Utilities.getDocument(target); if (doc != null) { Caret caret = target.getCaret(); Formatter f = doc.getFormatter(); if (f instanceof ExtFormatter) { ExtFormatter ef = (ExtFormatter) f; int[] fmtBlk = ef.getReformatBlock(target, typedText); if (fmtBlk != null) { try { fmtBlk[0] = Utilities.getRowStart(doc, fmtBlk[0]); fmtBlk[1] = Utilities.getRowEnd(doc, fmtBlk[1]); // this was the of #18922, that causes the bug // #20198 // ef.reformat(doc, fmtBlk[0], fmtBlk[1]); // bugfix of the bug #20198. Bug #18922 is fixed too // as well as #6968 ef.reformat(doc, fmtBlk[0], fmtBlk[1], true); } catch (BadLocationException e) { } catch (IOException e) { } } } } }
if (caret.isSelectionVisible()) { // block selected try { doc.getFormatter().changeBlockIndent(doc, target.getSelectionStart(), target.getSelectionEnd(), -1); } catch (GuardedException e) { target.getToolkit().beep(); int endOffset = Utilities.getRowEnd(doc, caret.getDot()); if (firstNW == -1 || (firstNW >= caret.getDot())) doc.getFormatter().changeBlockIndent(doc, startOffset, endOffset, -1); else {
public void actionPerformed(ActionEvent evt, JTextComponent target) { // shift-enter while editing aka startNewLineAction if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; } BaseDocument doc = (BaseDocument)target.getDocument(); doc.atomicLock(); try { target.replaceSelection(""); //NOI18N Caret caret = target.getCaret(); // insert and remove '-' to remember caret // position int dotpos = caret.getDot(); doc.insertString(dotpos,"-",null); //NOI18N doc.remove(dotpos,1); int eolDot = Utilities.getRowEnd(target, caret.getDot()); int newDotPos = doc.getFormatter().indentNewLine(doc,eolDot); caret.setDot(newDotPos); } catch (BadLocationException ex) { ex.printStackTrace(); } finally{ doc.atomicUnlock(); } } }