@@ -17,25 +17,44 @@ class TemplateBracketTypedHandler : TypedHandlerDelegate() {
1717 val offset = editor.caretModel.offset
1818 val text = editor.document.charsSequence
1919
20- if (c == ' ' ) {
21- return handleAutoComplete(project, editor, text, offset)
20+ if (c == ' {' || c == ' !' || c == ' -' ) {
21+ val result = handleAutoComplete(project, editor, text, offset)
22+ if (result == Result .STOP ) return result
2223 }
2324
2425 if (c == ' !' || c == ' -' ) {
2526 synchronizeBrackets(project, editor, text, offset)
2627 }
2728
29+ if (c == ' ' ) {
30+ handleSpaceInBrackets(project, editor, text, offset)
31+ }
32+
2833 return Result .CONTINUE
2934 }
3035
36+ private fun handleSpaceInBrackets (project : Project , editor : Editor , text : CharSequence , offset : Int ) {
37+ val textBefore = text.subSequence(0 , offset).toString()
38+ val textAfter = text.subSequence(offset, text.length).toString()
39+
40+ for (pair in BRACKET_PAIRS ) {
41+ if (textBefore.endsWith(pair.opening + " " ) && textAfter.startsWith(pair.closing)) {
42+ WriteCommandAction .runWriteCommandAction(project) {
43+ editor.document.insertString(offset, " " )
44+ }
45+ return
46+ }
47+ }
48+ }
49+
3150 private fun handleAutoComplete (project : Project , editor : Editor , text : CharSequence , offset : Int ): Result {
3251 if (offset < 2 ) return Result .CONTINUE
3352
3453 val textBefore = text.subSequence(0 , offset).toString()
3554
3655 for (pair in AUTO_COMPLETE_PAIRS ) {
37- if (textBefore.endsWith(pair.opening + " " )) {
38- if (! hasClosingBracketAhead (text, offset, pair.closing.trim() )) {
56+ if (textBefore.endsWith(pair.opening)) {
57+ if (! hasAnyClosingBracketAhead (text, offset)) {
3958 insertClosingBracket(project, editor, offset, pair.closing)
4059 return Result .STOP
4160 }
@@ -97,16 +116,20 @@ class TemplateBracketTypedHandler : TypedHandlerDelegate() {
97116 return if (nextOpenIndex == - 1 ) closingIndex else - 1
98117 }
99118
100- private fun hasClosingBracketAhead (text : CharSequence , offset : Int , closing : String ): Boolean {
119+ private fun hasAnyClosingBracketAhead (text : CharSequence , offset : Int ): Boolean {
101120 val textAfter = text.subSequence(offset, text.length).toString()
102121
103- val nextClose = textAfter.indexOf(closing)
104- if (nextClose == - 1 ) return false
105-
106- val opening = BRACKET_PAIRS .find { it.closing == closing }?.opening ? : return false
107- val nextOpen = textAfter.indexOf(opening)
122+ for (pair in BRACKET_PAIRS ) {
123+ val nextClose = textAfter.indexOf(pair.closing)
124+ if (nextClose != - 1 ) {
125+ val nextOpen = textAfter.indexOf(pair.opening)
126+ if (nextOpen == - 1 || nextClose < nextOpen) {
127+ return true
128+ }
129+ }
130+ }
108131
109- return nextOpen == - 1 || nextClose < nextOpen
132+ return false
110133 }
111134
112135 private fun insertClosingBracket (project : Project , editor : Editor , offset : Int , closing : String ) {
@@ -129,9 +152,9 @@ val BRACKET_PAIRS = listOf(
129152)
130153
131154private val AUTO_COMPLETE_PAIRS = listOf (
132- TemplateBracketTypedHandler .BracketPair (" {{--" , " --}}" ),
133- TemplateBracketTypedHandler .BracketPair (" {{!!" , " !!}}" ),
134- TemplateBracketTypedHandler .BracketPair (" {{" , " }}" ),
155+ TemplateBracketTypedHandler .BracketPair (" {{--" , " --}}" ),
156+ TemplateBracketTypedHandler .BracketPair (" {{!!" , " !!}}" ),
157+ TemplateBracketTypedHandler .BracketPair (" {{" , " }}" ),
135158)
136159
137160val INSTANCE = TemplateBracketTypedHandler ()
0 commit comments