]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/MatchingBracketsEditStrategy.java
Fixed multiple issues causing dangling references to discarded queries
[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("(")) { //$NON-NLS-1$
14                                 c.text = "()"; //$NON-NLS-1$
15                                 justAddedClosingBracket = ')';
16                         }
17                         else if(c.text.equals("[")) { //$NON-NLS-1$
18                                 c.text = "[]"; //$NON-NLS-1$
19                                 justAddedClosingBracket = ']';
20                         }
21                         else {
22                                 if(c.text.length() == 1 && c.text.charAt(0) == justAddedClosingBracket) {
23                                         c.text = ""; //$NON-NLS-1$
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 }