| 1 | /** |
|---|
| 2 | * |
|---|
| 3 | */ |
|---|
| 4 | package org.axdt.as3.templates; |
|---|
| 5 | |
|---|
| 6 | import java.util.StringTokenizer; |
|---|
| 7 | |
|---|
| 8 | import org.axdt.as3.preferences.AS3EditorPreferences; |
|---|
| 9 | import org.eclipse.jface.preference.IPreferenceStore; |
|---|
| 10 | import org.eclipse.jface.text.BadLocationException; |
|---|
| 11 | import org.eclipse.jface.text.IDocument; |
|---|
| 12 | import org.eclipse.jface.text.IRegion; |
|---|
| 13 | import org.eclipse.jface.text.templates.DocumentTemplateContext; |
|---|
| 14 | import org.eclipse.jface.text.templates.Template; |
|---|
| 15 | import org.eclipse.jface.text.templates.TemplateBuffer; |
|---|
| 16 | import org.eclipse.jface.text.templates.TemplateContextType; |
|---|
| 17 | import org.eclipse.jface.text.templates.TemplateException; |
|---|
| 18 | |
|---|
| 19 | public 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 | } |
|---|