X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.ui%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fui%2Feditor%2FMatchingBracketsEditStrategy.java;fp=bundles%2Forg.simantics.scl.ui%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fui%2Feditor%2FMatchingBracketsEditStrategy.java;h=5a9b12e3979b469fa3749db6292dcf32d6eacd69;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/MatchingBracketsEditStrategy.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/MatchingBracketsEditStrategy.java new file mode 100755 index 000000000..5a9b12e39 --- /dev/null +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/MatchingBracketsEditStrategy.java @@ -0,0 +1,39 @@ +package org.simantics.scl.ui.editor; + +import org.eclipse.jface.text.DocumentCommand; +import org.eclipse.jface.text.IAutoEditStrategy; +import org.eclipse.jface.text.IDocument; + +public class MatchingBracketsEditStrategy implements IAutoEditStrategy { + + char justAddedClosingBracket = (char)0; + + public void customizeDocumentCommand(IDocument d, DocumentCommand c) { + if (c.length == 0 && c.text != null) { + if(c.text.equals("(")) { + c.text = "()"; + justAddedClosingBracket = ')'; + } + else if(c.text.equals("[")) { + c.text = "[]"; + justAddedClosingBracket = ']'; + } + else { + if(c.text.length() == 1 && c.text.charAt(0) == justAddedClosingBracket) { + c.text = ""; + c.shiftsCaret = false; + c.caretOffset = c.offset+1; + } + justAddedClosingBracket = (char)0; + return; + } + + // Puts caret between brackets + c.caretOffset = c.offset+1; + c.shiftsCaret = false; + } + else + justAddedClosingBracket = (char)0; + } + +}