Changeset 113
- Timestamp:
- 07/16/08 03:30:07 (6 months ago)
- Files:
-
- axdt/trunk/org.axdt.as3/src/org/axdt/as3/imp/autoEdit/AS3AutoEditStrategy.java (modified) (4 diffs)
- axdt/trunk/org.axdt.as3/src/org/axdt/as3/preferences/AS3Preferences.java (modified) (2 diffs)
- axdt/trunk/org.axdt.common/META-INF/MANIFEST.MF (modified) (2 diffs)
- axdt/trunk/org.axdt.common/src/org/axdt/util (added)
- axdt/trunk/org.axdt.common/src/org/axdt/util/AxdtTextHelper.java (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
axdt/trunk/org.axdt.as3/src/org/axdt/as3/imp/autoEdit/AS3AutoEditStrategy.java
r105 r113 2 2 3 3 import org.axdt.as3.preferences.AS3Preferences; 4 import org.axdt.util.AxdtTextHelper; 4 5 import org.eclipse.imp.services.IAutoEditStrategy; 5 6 import org.eclipse.jface.preference.IPreferenceStore; 6 7 import org.eclipse.jface.text.BadLocationException; 7 import org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy;8 8 import org.eclipse.jface.text.DefaultLineTracker; 9 9 import org.eclipse.jface.text.DocumentCommand; … … 11 11 import org.eclipse.jface.text.ILineTracker; 12 12 import org.eclipse.jface.text.IRegion; 13 import org.eclipse.jface.text.TextUtilities; 13 14 14 /** 15 * Taken from org.eclipse.jface.text.TabsToSpacesConverter witch cannot be used 16 * directly as long as we support 3.2. Copyright resides with IBM Corporation. 17 */ 18 public class AS3AutoEditStrategy extends DefaultIndentLineAutoEditStrategy implements 19 IAutoEditStrategy { 15 public class AS3AutoEditStrategy implements IAutoEditStrategy { 20 16 21 17 private int fTabRatio; 22 18 private boolean ftab2Space; 19 private boolean fCloseString; 20 private boolean fCloseBrackets; 21 private boolean fCloseBraces; 22 private boolean fDeletePair; 23 23 private ILineTracker fLineTracker; 24 24 25 public void setNumberOfSpacesPerTab(int ratio) {25 public AS3AutoEditStrategy() { 26 26 IPreferenceStore store = AS3Preferences.getInstance().getStore(); 27 27 ftab2Space = store.getBoolean(AS3Preferences.TAB_TO_SPACE); 28 28 fTabRatio = store.getInt(AS3Preferences.TAB_WIDTH); 29 fCloseString = store.getBoolean(AS3Preferences.CLOSE_STRINGS); 30 fCloseBrackets = store.getBoolean(AS3Preferences.CLOSE_BRACKETS); 31 fCloseBraces = store.getBoolean(AS3Preferences.CLOSE_BRACES); 32 fDeletePair = store.getBoolean(AS3Preferences.DELETE_PAIR); 29 33 } 30 34 … … 42 46 } 43 47 44 public void customizeDocumentCommand(IDocument document, DocumentCommand command) { 45 super.customizeDocumentCommand(document, command); 46 if (ftab2Space && command.text != null) { 47 convertTabsToSpace(document, command); 48 public void customizeDocumentCommand(IDocument doc, DocumentCommand cmd) { 49 if (cmd.offset == -1 || doc.getLength() == 0 || cmd.text == null) return; 50 if (cmd.length == 0) { 51 int nlIndex = TextUtilities.endsWith(doc.getLegalLineDelimiters(), cmd.text); 52 if (nlIndex != -1) autoIndentNewLine(doc, cmd, nlIndex); 53 } 54 if (ftab2Space) convertTabsToSpace(doc, cmd); 55 if (cmd.text.length() == 1) closePair(doc, cmd); 56 if (fDeletePair && cmd.length == 1 && cmd.text.length() == 0) deletePair(doc, cmd); 57 } 58 59 private void deletePair(IDocument doc, DocumentCommand cmd) { 60 try { 61 char ch = doc.getChar(cmd.offset); 62 if (ch == '(' || ch == '{' || ch == '"' || ch == '\'' || ch == '[') { 63 char next = doc.getChar(cmd.offset + 1); 64 if (next == AxdtTextHelper.getMathingPair(ch)) { 65 if (AxdtTextHelper.getOpenPairCount(doc, ch) == 0) { 66 // delete next 67 cmd.doit = false; 68 cmd.addCommand(cmd.offset + 1, 1, "", null); 69 } 70 } 71 } 72 } catch (BadLocationException e) { 73 e.printStackTrace(); 48 74 } 49 75 } 50 76 51 protected void convertTabsToSpace(IDocument document, DocumentCommand command) { 52 String text = command.text; 77 private void autoIndentNewLine(IDocument doc, DocumentCommand cmd, int nlIndex) { 78 try { 79 int p = (cmd.offset == doc.getLength() ? cmd.offset - 1 : cmd.offset); 80 IRegion info = doc.getLineInformationOfOffset(p); 81 int start = info.getOffset(); 82 int end = AxdtTextHelper.findEndOfWhiteSpace(doc, start, cmd.offset); 83 StringBuffer buf = new StringBuffer(cmd.text); 84 if (end > start) buf.append(doc.get(start, end - start)); 85 int nlOffset = nlIndex + cmd.offset; 86 if (nlOffset > 1 && doc.getChar(nlOffset - 2) == '{') { 87 buf.append('\t'); 88 if (fCloseBraces && AxdtTextHelper.getOpenPairCount(doc, '{') > 0) { 89 // insert end of line bit here 90 StringBuilder close = new StringBuilder(); 91 if (end > start) close.append(doc.get(start, end - start)); 92 close.append('}'); 93 close.append(cmd.text.substring(nlIndex - 1)); 94 int lineEnd = AxdtTextHelper.getLineEnd(doc, cmd.offset); 95 cmd.addCommand(cmd.offset + lineEnd - cmd.offset, 0, close.toString(), null); 96 cmd.doit = false; 97 } 98 } 99 cmd.text = buf.toString(); 100 } catch (BadLocationException excp) { 101 // stop work 102 } 103 } 104 105 protected void closePair(IDocument doc, DocumentCommand cmd) { 106 try { 107 char c = cmd.text.charAt(0); 108 boolean closeString = fCloseString && (c == '"' || c == '\''); 109 if (closeString || fCloseBrackets && (c == '(' || c == '[')) { 110 if (AxdtTextHelper.getOpenPairCount(doc, c) == 0) { 111 if (!closeString || doc.getChar(cmd.offset) != c) { 112 cmd.shiftsCaret = false; 113 cmd.caretOffset = cmd.offset + 1; 114 cmd.text += AxdtTextHelper.getMathingPair(c); 115 } else if (closeString && doc.getChar(cmd.offset-1) == c) { 116 cmd.text = ""; 117 cmd.caretOffset = cmd.offset + 1; 118 } 119 } 120 } 121 } catch (BadLocationException e) { 122 e.printStackTrace(); 123 } 124 } 125 126 /* 127 * Taken from org.eclipse.jface.text.TabsToSpacesConverter witch cannot be 128 * used directly as long as we support 3.2. Copyright resides with IBM 129 * Corporation. 130 */ 131 protected void convertTabsToSpace(IDocument doc, DocumentCommand cmd) { 132 String text = cmd.text; 53 133 int index = text.indexOf('\t'); 54 134 if (index > -1) { 55 135 StringBuffer buffer = new StringBuffer(); 56 57 136 if (fLineTracker == null) { 58 137 fLineTracker = new DefaultLineTracker(); 59 138 } 60 fLineTracker.set(c ommand.text);139 fLineTracker.set(cmd.text); 61 140 int lines = fLineTracker.getNumberOfLines(); 62 63 141 try { 64 65 142 for (int i = 0; i < lines; i++) { 66 67 143 int offset = fLineTracker.getLineOffset(i); 68 144 int endOffset = offset + fLineTracker.getLineLength(i); 69 145 String line = text.substring(offset, endOffset); 70 71 146 int position = 0; 72 147 if (i == 0) { 73 IRegion firstLine = doc ument.getLineInformationOfOffset(command.offset);74 position = c ommand.offset - firstLine.getOffset();148 IRegion firstLine = doc.getLineInformationOfOffset(cmd.offset); 149 position = cmd.offset - firstLine.getOffset(); 75 150 } 76 77 151 int length = line.length(); 78 152 for (int j = 0; j < length; j++) { … … 85 159 } 86 160 } 87 88 161 } 89 90 command.text = buffer.toString(); 91 162 cmd.text = buffer.toString(); 92 163 } catch (BadLocationException x) { 93 164 } 94 165 } 95 166 } 96 97 167 } axdt/trunk/org.axdt.as3/src/org/axdt/as3/preferences/AS3Preferences.java
r105 r113 15 15 public final static String TAB_TO_SPACE = "TAB_TO_SPACE"; 16 16 public final static String TAB_WIDTH = "TAB_WIDTH"; 17 public static final String CLOSE_STRINGS = "CLOSE_STRINGS"; 18 public static final String CLOSE_BRACKETS = "CLOSE_BRACKETS"; 19 public static final String CLOSE_BRACES = "CLOSE_BRACES"; 20 public final static String DELETE_PAIR = "DELETE_PAIR"; 17 21 public final static String USE_FOLDING = "USE_FOLDING"; 18 22 public final static String FOLD_COMMENTS = "FOLD_COMMENTS"; … … 40 44 @Override 41 45 public void initializeFieldSpecs() { 42 add(SOURCE_PATHS, "Source paths", "src"); 43 add(DEPLOY_PATH, "Deploy path", "deploy"); 44 add(TAB_TO_SPACE, "Convert tabs", false); 45 add(TAB_WIDTH, "to number of spaces", 4); 46 add(SOURCE_PATHS, "Source &paths", "src"); 47 add(DEPLOY_PATH, "&Deploy path", "deploy"); 48 add(TAB_TO_SPACE, "Convert &tabs", false); 49 add(TAB_WIDTH, "to number of spa&ces", 4); 50 addGroup("Automatically close", ""); 51 add(CLOSE_STRINGS, "\"&Strings\"", true); 52 add(CLOSE_BRACKETS, "(Parentheses) and [&Brackets]", true); 53 add(CLOSE_BRACES, "{&Braces} on newline", true); 54 endGroup(); 55 add(DELETE_PAIR, "Delete right matching pair (if direct neighbor)", true); 46 56 addPage(PAGE_FOLDING, ""); 47 57 add(USE_FOLDING, "Enable &folding", true); axdt/trunk/org.axdt.common/META-INF/MANIFEST.MF
r85 r113 9 9 org.eclipse.core.runtime, 10 10 org.eclipse.core.resources, 11 org.eclipse.ui.editors 11 org.eclipse.ui.editors, 12 org.eclipse.text 12 13 Eclipse-LazyStart: true 13 14 Export-Package: org.axdt.actions, … … 15 16 org.axdt.preferences, 16 17 org.axdt.ui.dialog, 18 org.axdt.util, 17 19 org.axdt.wizards
