@@ -54,23 +54,26 @@ function defaultRenderMatch(match: MatchResult) {
5454 return decorations
5555}
5656
57+ function bracketDeco ( state : EditorState ) {
58+ let decorations : Range < Decoration > [ ] = [ ]
59+ let config = state . facet ( bracketMatchingConfig )
60+ for ( let range of state . selection . ranges ) {
61+ if ( ! range . empty ) continue
62+ let match = matchBrackets ( state , range . head , - 1 , config )
63+ || ( range . head > 0 && matchBrackets ( state , range . head - 1 , 1 , config ) )
64+ || ( config . afterCursor &&
65+ ( matchBrackets ( state , range . head , 1 , config ) ||
66+ ( range . head < state . doc . length && matchBrackets ( state , range . head + 1 , - 1 , config ) ) ) )
67+ if ( match )
68+ decorations = decorations . concat ( config . renderMatch ( match , state ) )
69+ }
70+ return Decoration . set ( decorations , true )
71+ }
72+
5773const bracketMatchingState = StateField . define < DecorationSet > ( {
58- create ( ) { return Decoration . none } ,
74+ create ( state ) { return bracketDeco ( state ) } ,
5975 update ( deco , tr ) {
60- if ( ! tr . docChanged && ! tr . selection ) return deco
61- let decorations : Range < Decoration > [ ] = [ ]
62- let config = tr . state . facet ( bracketMatchingConfig )
63- for ( let range of tr . state . selection . ranges ) {
64- if ( ! range . empty ) continue
65- let match = matchBrackets ( tr . state , range . head , - 1 , config )
66- || ( range . head > 0 && matchBrackets ( tr . state , range . head - 1 , 1 , config ) )
67- || ( config . afterCursor &&
68- ( matchBrackets ( tr . state , range . head , 1 , config ) ||
69- ( range . head < tr . state . doc . length && matchBrackets ( tr . state , range . head + 1 , - 1 , config ) ) ) )
70- if ( match )
71- decorations = decorations . concat ( config . renderMatch ( match , tr . state ) )
72- }
73- return Decoration . set ( decorations , true )
76+ return tr . docChanged || tr . selection ? bracketDeco ( tr . state ) : deco
7477 } ,
7578 provide : f => EditorView . decorations . from ( f )
7679} )
0 commit comments