1 11 package org.eclipse.jface.text; 12 13 14 23 public class TabsToSpacesConverter implements IAutoEditStrategy { 24 25 private int fTabRatio; 26 private ILineTracker fLineTracker; 27 28 29 public void setNumberOfSpacesPerTab(int ratio) { 30 fTabRatio= ratio; 31 } 32 33 public void setLineTracker(ILineTracker lineTracker) { 34 fLineTracker= lineTracker; 35 } 36 37 private int insertTabString(StringBuffer buffer, int offsetInLine) { 38 39 if (fTabRatio == 0) 40 return 0; 41 42 int remainder= offsetInLine % fTabRatio; 43 remainder= fTabRatio - remainder; 44 for (int i= 0; i < remainder; i++) 45 buffer.append(' '); 46 return remainder; 47 } 48 49 public void customizeDocumentCommand(IDocument document, DocumentCommand command) { 50 String text= command.text; 51 if (text == null) 52 return; 53 54 int index= text.indexOf('\t'); 55 if (index > -1) { 56 57 StringBuffer buffer= new StringBuffer (); 58 59 fLineTracker.set(command.text); 60 int lines= fLineTracker.getNumberOfLines(); 61 62 try { 63 64 for (int i= 0; i < lines; i++) { 65 66 int offset= fLineTracker.getLineOffset(i); 67 int endOffset= offset + fLineTracker.getLineLength(i); 68 String line= text.substring(offset, endOffset); 69 70 int position= 0; 71 if (i == 0) { 72 IRegion firstLine= document.getLineInformationOfOffset(command.offset); 73 position= command.offset - firstLine.getOffset(); 74 } 75 76 int length= line.length(); 77 for (int j= 0; j < length; j++) { 78 char c= line.charAt(j); 79 if (c == '\t') { 80 position += insertTabString(buffer, position); 81 } else { 82 buffer.append(c); 83 ++ position; 84 } 85 } 86 87 } 88 89 command.text= buffer.toString(); 90 91 } catch (BadLocationException x) { 92 } 93 } 94 } 95 } 96 | Popular Tags |