Changeset 113

Show
Ignore:
Timestamp:
07/16/08 03:30:07 (6 months ago)
Author:
mb0
Message:

ASSIGNED - # 68: Smart AutoEditStrategies?
http://axdt.org/ticket/68

lots of cool stuff... see ticket

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • axdt/trunk/org.axdt.as3/src/org/axdt/as3/imp/autoEdit/AS3AutoEditStrategy.java

    r105 r113  
    22 
    33import org.axdt.as3.preferences.AS3Preferences; 
     4import org.axdt.util.AxdtTextHelper; 
    45import org.eclipse.imp.services.IAutoEditStrategy; 
    56import org.eclipse.jface.preference.IPreferenceStore; 
    67import org.eclipse.jface.text.BadLocationException; 
    7 import org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy; 
    88import org.eclipse.jface.text.DefaultLineTracker; 
    99import org.eclipse.jface.text.DocumentCommand; 
     
    1111import org.eclipse.jface.text.ILineTracker; 
    1212import org.eclipse.jface.text.IRegion; 
     13import org.eclipse.jface.text.TextUtilities; 
    1314 
    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 { 
     15public class AS3AutoEditStrategy implements IAutoEditStrategy { 
    2016 
    2117        private int fTabRatio; 
    2218        private boolean ftab2Space; 
     19        private boolean fCloseString; 
     20        private boolean fCloseBrackets; 
     21        private boolean fCloseBraces; 
     22        private boolean fDeletePair; 
    2323        private ILineTracker fLineTracker; 
    2424 
    25         public void setNumberOfSpacesPerTab(int ratio) { 
     25        public AS3AutoEditStrategy() { 
    2626                IPreferenceStore store = AS3Preferences.getInstance().getStore(); 
    2727                ftab2Space = store.getBoolean(AS3Preferences.TAB_TO_SPACE); 
    2828                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); 
    2933        } 
    3034 
     
    4246        } 
    4347 
    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(); 
    4874                } 
    4975        } 
    5076 
    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; 
    53133                int index = text.indexOf('\t'); 
    54134                if (index > -1) { 
    55135                        StringBuffer buffer = new StringBuffer(); 
    56  
    57136                        if (fLineTracker == null) { 
    58137                                fLineTracker = new DefaultLineTracker(); 
    59138                        } 
    60                         fLineTracker.set(command.text); 
     139                        fLineTracker.set(cmd.text); 
    61140                        int lines = fLineTracker.getNumberOfLines(); 
    62  
    63141                        try { 
    64  
    65142                                for (int i = 0; i < lines; i++) { 
    66  
    67143                                        int offset = fLineTracker.getLineOffset(i); 
    68144                                        int endOffset = offset + fLineTracker.getLineLength(i); 
    69145                                        String line = text.substring(offset, endOffset); 
    70  
    71146                                        int position = 0; 
    72147                                        if (i == 0) { 
    73                                                 IRegion firstLine = document.getLineInformationOfOffset(command.offset); 
    74                                                 position = command.offset - firstLine.getOffset(); 
     148                                                IRegion firstLine = doc.getLineInformationOfOffset(cmd.offset); 
     149                                                position = cmd.offset - firstLine.getOffset(); 
    75150                                        } 
    76  
    77151                                        int length = line.length(); 
    78152                                        for (int j = 0; j < length; j++) { 
     
    85159                                                } 
    86160                                        } 
    87  
    88161                                } 
    89  
    90                                 command.text = buffer.toString(); 
    91  
     162                                cmd.text = buffer.toString(); 
    92163                        } catch (BadLocationException x) { 
    93164                        } 
    94165                } 
    95166        } 
    96  
    97167} 
  • axdt/trunk/org.axdt.as3/src/org/axdt/as3/preferences/AS3Preferences.java

    r105 r113  
    1515        public final static String TAB_TO_SPACE = "TAB_TO_SPACE"; 
    1616        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"; 
    1721        public final static String USE_FOLDING = "USE_FOLDING"; 
    1822        public final static String FOLD_COMMENTS = "FOLD_COMMENTS"; 
     
    4044        @Override 
    4145        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); 
    4656                addPage(PAGE_FOLDING, ""); 
    4757                add(USE_FOLDING, "Enable &folding", true); 
  • axdt/trunk/org.axdt.common/META-INF/MANIFEST.MF

    r85 r113  
    99 org.eclipse.core.runtime, 
    1010 org.eclipse.core.resources, 
    11  org.eclipse.ui.editors 
     11 org.eclipse.ui.editors, 
     12 org.eclipse.text 
    1213Eclipse-LazyStart: true 
    1314Export-Package: org.axdt.actions, 
     
    1516 org.axdt.preferences, 
    1617 org.axdt.ui.dialog, 
     18 org.axdt.util, 
    1719 org.axdt.wizards