1 11 package org.eclipse.ui.texteditor; 12 13 import java.util.ResourceBundle ; 14 15 import org.eclipse.core.runtime.Assert; 16 17 import org.eclipse.jface.viewers.ISelection; 18 import org.eclipse.jface.viewers.ISelectionProvider; 19 20 import org.eclipse.jface.text.BadLocationException; 21 import org.eclipse.jface.text.IDocument; 22 import org.eclipse.jface.text.ITextSelection; 23 24 25 31 public class JoinLinesAction extends TextEditorAction { 32 33 private String fJoint= null; 34 35 36 44 public JoinLinesAction(ResourceBundle bundle, String prefix, ITextEditor editor, String joint) { 45 super(bundle, prefix, editor); 46 Assert.isLegal(joint != null); 47 fJoint= joint; 48 update(); 49 } 50 51 54 public void run() { 55 56 ITextEditor editor= getTextEditor(); 57 if (editor == null) 58 return; 59 60 if (!validateEditorInputState()) 61 return; 62 63 IDocument document= getDocument(editor); 64 if (document == null) 65 return; 66 67 ITextSelection selection= getSelection(editor); 68 if (selection == null) 69 return; 70 71 int startLine= selection.getStartLine(); 72 int endLine= selection.getEndLine(); 73 try { 74 int caretOffset= joinLines(document, startLine, endLine); 75 if (caretOffset > -1) 76 editor.selectAndReveal(caretOffset, 0); 77 } catch (BadLocationException e) { 78 } 80 81 } 82 83 89 private static IDocument getDocument(ITextEditor editor) { 90 91 IDocumentProvider documentProvider= editor.getDocumentProvider(); 92 if (documentProvider == null) 93 return null; 94 95 IDocument document= documentProvider.getDocument(editor.getEditorInput()); 96 if (document == null) 97 return null; 98 99 return document; 100 } 101 102 108 private static ITextSelection getSelection(ITextEditor editor) { 109 110 ISelectionProvider selectionProvider= editor.getSelectionProvider(); 111 if (selectionProvider == null) 112 return null; 113 114 ISelection selection= selectionProvider.getSelection(); 115 if (!(selection instanceof ITextSelection)) 116 return null; 117 118 return (ITextSelection) selection; 119 } 120 121 124 public void update() { 125 super.update(); 126 if (!isEnabled()) 127 return; 128 129 if (!canModifyEditor()) { 130 setEnabled(false); 131 return; 132 } 133 134 ITextEditor editor= getTextEditor(); 135 setEnabled(editor.isEditable()); 136 } 137 138 147 private int joinLines(IDocument document, int startLine, int endLine) throws BadLocationException { 148 if (startLine == document.getNumberOfLines() - 1) { 149 return -1; 151 } 152 153 if (startLine == endLine) 154 endLine++; 156 StringBuffer buffer= new StringBuffer (); 157 for (int line= startLine; line <= endLine; line++) { 158 buffer.append(trim(document, line, line == startLine)); 159 if (line != endLine) 160 buffer.append(fJoint); 161 } 162 163 int startLineOffset= document.getLineOffset(startLine); 164 int endLineOffset= document.getLineOffset(endLine) + document.getLineLength(endLine) - getLineDelimiterLength(document, endLine); 165 String replaceString= buffer.toString(); 166 document.replace(startLineOffset, endLineOffset - startLineOffset, replaceString); 167 return startLineOffset + replaceString.length(); 168 } 169 170 private String trim(IDocument document, int line, boolean ignoreLeadingWhitespace) throws BadLocationException { 171 int lineOffset= document.getLineOffset(line); 172 int lineLength= document.getLineLength(line); 173 lineLength= lineLength - getLineDelimiterLength(document, line); 174 if (!ignoreLeadingWhitespace) 175 return document.get(lineOffset, lineLength).trim(); 176 177 while (lineLength > 0 && Character.isWhitespace(document.getChar(lineOffset + lineLength - 1))) 178 lineLength--; 179 180 return document.get(lineOffset, lineLength); 181 } 182 183 private int getLineDelimiterLength(IDocument document, int line) throws BadLocationException { 184 String lineDelimiter= document.getLineDelimiter(line); 185 return lineDelimiter != null ? lineDelimiter.length() : 0; 186 187 } 188 189 } 190 | Popular Tags |