root/org.axdt.as3/src/org/axdt/as3/templates/AS3TemplateContext.java @ 3d0622cd77233477dd222f1c00c4f35afeb67815

Revision 3d0622cd77233477dd222f1c00c4f35afeb67815, 3.1 KB (checked in by mb0 <mb0@…>, 15 months ago)

moved editor related preferences from as3 preference to as3 editor preferences
editor preferences can now be used to override imp editor settings.
tab width can now be set for as3 editor.

new basic editor service that observes the life cycle of an as3 editor.
this is used to suppress the builder to build files that are currently edited.

  • Property mode set to 100644
Line 
1/**
2 *
3 */
4package org.axdt.as3.templates;
5
6import java.util.StringTokenizer;
7
8import org.axdt.as3.preferences.AS3EditorPreferences;
9import org.eclipse.jface.preference.IPreferenceStore;
10import org.eclipse.jface.text.BadLocationException;
11import org.eclipse.jface.text.IDocument;
12import org.eclipse.jface.text.IRegion;
13import org.eclipse.jface.text.templates.DocumentTemplateContext;
14import org.eclipse.jface.text.templates.Template;
15import org.eclipse.jface.text.templates.TemplateBuffer;
16import org.eclipse.jface.text.templates.TemplateContextType;
17import org.eclipse.jface.text.templates.TemplateException;
18
19public class AS3TemplateContext extends DocumentTemplateContext {
20        public AS3TemplateContext(TemplateContextType type, IDocument document, int offset, int length) {
21                super(type, document, offset, length);
22        }
23
24        @Override
25        public TemplateBuffer evaluate(Template template) throws BadLocationException,
26                        TemplateException {
27                return super.evaluate(correctIndention(template, getDocument(), getStart()));
28        }
29
30        public static Template correctIndention(Template template, IDocument d, int offset) {
31                if (offset == -1 || d.getLength() == 0) return template;
32                String pattern = template.getPattern();
33                String delim = d.getLegalLineDelimiters()[0];
34                IPreferenceStore store = AS3EditorPreferences.getInstance().getStore();
35                boolean tab2Space = store.getBoolean(AS3EditorPreferences.TAB_TO_SPACE);
36                int tabWidth = store.getInt(AS3EditorPreferences.TAB_WIDTH);
37                StringBuffer buf = new StringBuffer();
38                for (int i = 0; i < tabWidth; i++)
39                        buf.append(' ');
40                String tabSpaces = buf.toString();
41                for (String nl:d.getLegalLineDelimiters())
42                        if (nl.equals("\n")) delim = nl;
43                if (pattern.indexOf('\n') < 0 && pattern.indexOf('\r') < 0) return template;
44                try {
45                        int p = (offset == d.getLength() ? offset - 1 : offset);
46                        IRegion info = d.getLineInformationOfOffset(p);
47                        int start = info.getOffset();
48                        // find white spaces
49                        int end = findEndOfWhiteSpace(d, start, offset);
50                        buf = new StringBuffer(delim);
51                        if (end > start) {
52                                // append to input
53                                buf.append(d.get(start, end - start));
54                        }
55                        String indent = buf.toString();
56                        buf = new StringBuffer();
57                        StringTokenizer tokenizer = new StringTokenizer(pattern, "\r\n\t", true);
58                        char lastnl = 0;
59                        while (tokenizer.hasMoreTokens()) {
60                                String token = tokenizer.nextToken();
61                                char c = token.charAt(0);
62                                if (c == '\n' || c == '\r') {
63                                        if (lastnl == 0 || lastnl == c) buf.append(indent);
64                                        lastnl = c;
65                                        continue;
66                                }
67                                lastnl = 0;
68                                // TODO: tab2space preference
69                                if (tab2Space && token.charAt(0) == '\t') {
70                                        buf.append(tabSpaces);
71                                } else buf.append(token);
72                        }
73                        return new Template(template.getName(), template.getDescription(), template
74                                        .getContextTypeId(), buf.toString(), template.isAutoInsertable());
75                } catch (BadLocationException e) {
76                }
77                return template;
78        }
79
80        private static int findEndOfWhiteSpace(IDocument document, int offset, int end)
81                        throws BadLocationException {
82                while (offset < end) {
83                        char c = document.getChar(offset);
84                        if (c != ' ' && c != '\t') {
85                                return offset;
86                        }
87                        offset++;
88                }
89                return end;
90        }
91}
Note: See TracBrowser for help on using the browser.