1 12 package org.eclipse.ui.texteditor; 13 14 import java.util.ResourceBundle ; 15 16 import org.eclipse.swt.custom.StyledText; 17 18 import org.eclipse.jface.text.BadLocationException; 19 import org.eclipse.jface.text.IDocument; 20 import org.eclipse.jface.text.IRegion; 21 import org.eclipse.jface.text.Position; 22 import org.eclipse.jface.text.source.ISourceViewer; 23 24 46 public class InsertLineAction extends TextEditorAction { 47 48 52 protected boolean fAbove; 53 54 61 public InsertLineAction(ResourceBundle bundle, String prefix, ITextEditor textEditor, boolean above) { 62 super(bundle, prefix, textEditor); 63 fAbove= above; 64 } 65 66 69 public void update() { 70 super.update(); 71 if (isEnabled()) 72 setEnabled(canModifyEditor()); 73 } 74 75 78 public void run() { 79 87 88 ITextEditor ed= getTextEditor(); 89 if (!(ed instanceof AbstractTextEditor)) 90 return; 91 92 if (!validateEditorInputState()) 93 return; 94 95 AbstractTextEditor editor= (AbstractTextEditor) ed; 96 ISourceViewer sv= editor.getSourceViewer(); 97 if (sv == null) 98 return; 99 100 IDocument document= sv.getDocument(); 101 if (document == null) 102 return; 103 104 StyledText st= sv.getTextWidget(); 105 if (st == null || st.isDisposed()) 106 return; 107 108 try { 109 int widgetOffset= st.getCaretOffset(); 111 int offset= AbstractTextEditor.widgetOffset2ModelOffset(sv, widgetOffset); 112 int currentLineNumber= document.getLineOfOffset(offset); 113 IRegion currentLine= document.getLineInformation(currentLineNumber); 114 115 int insertionOffset= -1; 116 if (fAbove) { 117 if (currentLineNumber != 0) { 118 IRegion previousLine= document.getLineInformation(currentLineNumber - 1); 119 insertionOffset= previousLine.getOffset() + previousLine.getLength(); 120 } 121 } else { 122 insertionOffset= currentLine.getOffset() + currentLine.getLength(); 123 } 124 125 boolean updateCaret= true; 126 int widgetInsertionOffset= AbstractTextEditor.modelOffset2WidgetOffset(sv, insertionOffset); 127 if (widgetInsertionOffset == -1 && fAbove) { 128 insertionOffset= currentLine.getOffset() + getIndentationLength(document, currentLine); 132 widgetInsertionOffset= AbstractTextEditor.modelOffset2WidgetOffset(sv, insertionOffset); 133 updateCaret= false; 134 } 135 if (widgetInsertionOffset == -1) 136 return; 137 138 Position caret= new Position(insertionOffset, 0); 140 document.addPosition(caret); 141 st.setSelectionRange(widgetInsertionOffset, 0); 142 143 st.replaceTextRange(widgetInsertionOffset, 0, st.getLineDelimiter()); 145 146 document.removePosition(caret); 149 if (updateCaret && st.getSelection().x == widgetInsertionOffset) { 150 int widgetCaret= AbstractTextEditor.modelOffset2WidgetOffset(sv, caret.getOffset()); 151 if (widgetCaret != -1) 152 st.setSelectionRange(widgetCaret, 0); 153 st.showSelection(); 154 } 155 156 } catch (BadLocationException e) { 157 } 159 } 160 161 170 private int getIndentationLength(IDocument document, IRegion line) throws BadLocationException { 171 int pos= line.getOffset(); 172 int max= pos + line.getLength(); 173 while (pos < max) { 174 if (!Character.isWhitespace(document.getChar(pos))) 175 break; 176 pos++; 177 } 178 return pos - line.getOffset(); 179 } 180 } 181 | Popular Tags |