]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/MatchingBracketsEditStrategy.java
LabelDecorator.decorateLabel can return null
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / editor / MatchingBracketsEditStrategy.java
1 package org.simantics.scl.ui.editor;
2
3 import org.eclipse.jface.text.DocumentCommand;
4 import org.eclipse.jface.text.IAutoEditStrategy;
5 import org.eclipse.jface.text.IDocument;
6
7 public class MatchingBracketsEditStrategy implements IAutoEditStrategy {
8
9         char justAddedClosingBracket = (char)0;
10         
11         public void customizeDocumentCommand(IDocument d, DocumentCommand c) {
12                 if (c.length == 0 && c.text != null) {
13                         if(c.text.equals("(")) {
14                                 c.text = "()";
15                                 justAddedClosingBracket = ')';
16                         }
17                         else if(c.text.equals("[")) {
18                                 c.text = "[]";
19                                 justAddedClosingBracket = ']';
20                         }
21                         else {
22                                 if(c.text.length() == 1 && c.text.charAt(0) == justAddedClosingBracket) {
23                                         c.text = "";
24                                         c.shiftsCaret = false;
25                                         c.caretOffset = c.offset+1;
26                                 }
27                                 justAddedClosingBracket = (char)0;
28                                 return;
29                         }
30                         
31                         // Puts caret between brackets
32                         c.caretOffset = c.offset+1;
33                         c.shiftsCaret = false;                  
34                 }               
35                 else
36                         justAddedClosingBracket = (char)0;
37         }
38         
39 }