OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012, the Dart project authors. | 2 * Copyright (c) 2012, the Dart project authors. |
3 * | 3 * |
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except | 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except |
5 * in compliance with the License. You may obtain a copy of the License at | 5 * in compliance with the License. You may obtain a copy of the License at |
6 * | 6 * |
7 * http://www.eclipse.org/legal/epl-v10.html | 7 * http://www.eclipse.org/legal/epl-v10.html |
8 * | 8 * |
9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License | 9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License |
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express | 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express |
11 * or implied. See the License for the specific language governing permissions a
nd limitations under | 11 * or implied. See the License for the specific language governing permissions a
nd limitations under |
12 * the License. | 12 * the License. |
13 */ | 13 */ |
14 | 14 |
15 package com.google.dart.tools.ui.web.yaml; | 15 package com.google.dart.tools.ui.web.yaml; |
16 | 16 |
17 import com.google.dart.tools.ui.web.utils.WebEditorAutoIndentStrategy; | |
18 | |
19 import org.eclipse.jface.text.BadLocationException; | 17 import org.eclipse.jface.text.BadLocationException; |
| 18 import org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy; |
20 import org.eclipse.jface.text.DocumentCommand; | 19 import org.eclipse.jface.text.DocumentCommand; |
21 import org.eclipse.jface.text.IDocument; | 20 import org.eclipse.jface.text.IDocument; |
22 import org.eclipse.jface.text.IRegion; | 21 import org.eclipse.jface.text.IRegion; |
| 22 import org.eclipse.jface.text.TextUtilities; |
23 | 23 |
24 /** | 24 /** |
25 * An indent strategy for yaml. | 25 * An indent strategy for yaml. |
26 */ | 26 */ |
27 class YamlAutoIndentStrategy extends WebEditorAutoIndentStrategy { | 27 public class YamlAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy { |
28 | 28 |
29 public YamlAutoIndentStrategy() { | 29 public YamlAutoIndentStrategy() { |
30 | 30 |
31 } | 31 } |
32 | 32 |
33 @Override | 33 @Override |
34 protected void doAutoIndentAfterNewLine(IDocument document, DocumentCommand co
mmand) { | 34 public void customizeDocumentCommand(IDocument d, DocumentCommand c) { |
35 if (command.offset == -1 || document.getLength() == 0) { | 35 if (c.length == 0 && c.text != null |
| 36 && TextUtilities.endsWith(d.getLegalLineDelimiters(), c.text) != -1) { |
| 37 autoIndentAfterNewLine(d, c); |
| 38 } |
| 39 } |
| 40 |
| 41 /** |
| 42 * Copies the indentation of the previous line. |
| 43 * |
| 44 * @param d the document to work on |
| 45 * @param c the command to deal with |
| 46 */ |
| 47 private void autoIndentAfterNewLine(IDocument d, DocumentCommand c) { |
| 48 if (c.offset == -1 || d.getLength() == 0) { |
36 return; | 49 return; |
37 } | 50 } |
38 | 51 |
39 try { | 52 try { |
40 // find start of line | 53 // find start of line |
41 int location = (command.offset == document.getLength() ? command.offset -
1 : command.offset); | 54 int p = (c.offset == d.getLength() ? c.offset - 1 : c.offset); |
42 IRegion lineInfo = document.getLineInformationOfOffset(location); | 55 IRegion info = d.getLineInformationOfOffset(p); |
43 int start = lineInfo.getOffset(); | 56 int start = info.getOffset(); |
44 | 57 |
45 boolean endsInColon = (document.getChar(command.offset - 1) == ':'); | 58 boolean endsInColon = (d.getChar(c.offset - 1) == ':'); |
46 | 59 |
47 // find white spaces | 60 // find white spaces |
48 int end = findEndOfWhiteSpace(document, start, command.offset); | 61 int end = findEndOfWhiteSpace(d, start, c.offset); |
49 | 62 |
50 StringBuffer buf = new StringBuffer(command.text); | 63 StringBuffer buf = new StringBuffer(c.text); |
51 | 64 |
52 if (end > start) { | 65 if (end > start) { |
53 // append to input | 66 // append to input |
54 buf.append(document.get(start, end - start)); | 67 buf.append(d.get(start, end - start)); |
55 } | 68 } |
56 | 69 |
57 if (endsInColon) { | 70 if (endsInColon) { |
58 buf.append(" "); | 71 buf.append(" "); |
59 } | 72 } |
60 | 73 |
61 command.text = buf.toString(); | 74 c.text = buf.toString(); |
62 } catch (BadLocationException excp) { | 75 } catch (BadLocationException excp) { |
63 // stop work | 76 // stop work |
64 } | 77 } |
65 } | 78 } |
66 | 79 |
67 } | 80 } |
OLD | NEW |