1
1
{
2
- adviseBefore, contains, findLastIndex ,
3
- first, has, invert, keys ,
2
+ adviseBefore, first, invert ,
3
+ compose, last, filter ,
4
4
} = require ' underscore-plus'
5
5
6
6
pairsToPad =
7
7
' (' : ' )'
8
8
' [' : ' ]'
9
9
' {' : ' }'
10
10
11
- invertedPairsToPad = invert pairsToPad
11
+ invertedPairsToPad =
12
+ invert pairsToPad
12
13
13
14
pairsToUnpad =
14
15
' ( ' : ' )'
@@ -22,6 +23,9 @@ defaultPairs = [
22
23
' "' , " '" , ' `' ,
23
24
]
24
25
26
+ removeEscapedQuotes =
27
+ (str ) -> str .replace (/ (\\ "| \\ ')/ g , ' ' )
28
+
25
29
module .exports =
26
30
class BracketPadder
27
31
constructor : (@editor ) ->
@@ -32,15 +36,17 @@ class BracketPadder
32
36
return true unless text
33
37
return true if options ? .select or options ? .undo is ' skip'
34
38
35
- closingBracket = invertedPairsToPad[text]
36
- return true unless text is ' ' or closingBracket
39
+ openBracket = invertedPairsToPad[text]
40
+ isClosingBracket = !! openBracket
41
+
42
+ return true unless text is ' ' or isClosingBracket
37
43
38
44
if @ shouldPad (text)
39
45
@editor .insertText (' ' )
40
46
@editor .moveLeft ()
41
47
return false
42
48
43
- if @ shouldClosePair (closingBracket )
49
+ if @ shouldClosePair (text, openBracket )
44
50
@editor .moveRight (2 )
45
51
return false
46
52
@@ -51,9 +57,7 @@ class BracketPadder
51
57
previousCharacters = @ getPreviousCharacters (2 , cursor)
52
58
nextCharacters = @ getNextCharacters (2 , cursor)
53
59
54
- match = pairsToUnpad[previousCharacters]
55
-
56
- return true unless match and nextCharacters is match
60
+ return true unless pairsToUnpad[previousCharacters] is nextCharacters
57
61
58
62
@editor .moveRight ()
59
63
@editor .backspace ()
@@ -67,25 +71,24 @@ class BracketPadder
67
71
previousCharacter = @ getPreviousCharacters 1 , cursor
68
72
nextCharacter = @ getNextCharacters 1 , cursor
69
73
70
- match = pairsToPad[previousCharacter]
71
- return match and nextCharacter is match
74
+ return true if pairsToPad[previousCharacter] is nextCharacter
72
75
73
- shouldClosePair : (character ) =>
76
+ shouldClosePair : (closeBracket , openBracket ) =>
74
77
cursor = @editor .getCursorBufferPosition ()
75
- previousCharacters = @ getPreviousCharacters (cursor .column , cursor)
78
+ previousCharacters =
79
+ removeEscapedQuotes @ getPreviousCharacters (cursor .column , cursor)
80
+ nextCharacters = @ getNextCharacters (2 , cursor)
76
81
77
- match = findLastOccurringCharacter defaultPairs, previousCharacters
78
- return false unless match and match is character
82
+ return false unless previousCharacters .includes (openBracket)
79
83
80
- nextCharacters = @ getNextCharacters (2 , cursor)
84
+ unclosed = getUnclosedPairs (previousCharacters)
85
+ return false unless last (unclosed) is openBracket
81
86
82
87
return false unless first (nextCharacters) is ' '
83
-
84
- match = invert (pairsToPad)[nextCharacters .trim ()]
85
- return match and match is character
88
+ return true if nextCharacters .trim () is closeBracket
86
89
87
90
getPreviousCharacters : (count , cursor ) =>
88
- return - 1 unless cursor .column
91
+ return ' ' unless cursor .column
89
92
90
93
return @editor .getTextInBufferRange ([
91
94
cursor .traverse ([0 , - count]),
@@ -99,16 +102,35 @@ class BracketPadder
99
102
])
100
103
101
104
###
102
- * @param {Array<String>} characters
103
- * @param {String} string
105
+ * Filters out characters between `opening` and `closing` characters.
106
+ * @param {String} opening
107
+ * @param {String} closing
108
+ * @return {Function}
104
109
###
105
- findLastOccurringCharacter = (characters , string ) ->
106
- index = string .length - 1
110
+ removePairs = (opening , closing ) -> (str ) ->
111
+ if not closing
112
+ closing = opening
107
113
108
- while index --
109
- char = string[index]
114
+ regex = new RegExp ( " #{ opening } ([^ #{ opening } ]+(?= #{ closing } )) #{ closing } " , ' g ' )
115
+ str . replace (regex, ' ' )
110
116
111
- if contains (characters, char)
112
- return char
117
+ ###
118
+ * :: String -> String
119
+ ###
120
+ removeClosedPairs = compose (
121
+ removePairs (" '" ),
122
+ removePairs (' "' ),
123
+ removePairs (' `' ),
124
+ removePairs (' \\ {' , ' \\ }' ),
125
+ removePairs (' \\ [' , ' \\ ]' ),
126
+ removePairs (' \\ (' , ' \\ )' )
127
+ )
113
128
114
- return undefined
129
+ ###
130
+ * Returns any unclosed "bracket pair characters" found within `str`.
131
+ * @param {String} str
132
+ * @return {Array<String>}
133
+ ###
134
+ getUnclosedPairs = (str ) ->
135
+ trimmed = removeClosedPairs (str)
136
+ return filter (trimmed, (char ) -> defaultPairs .includes (char))
0 commit comments