@ Override protected void insertUpdate ( DefaultDocumentEvent chng , AttributeSet attr ) { super . insertUpdate ( chng , attr ) ; // Update comment marks Element root = getDefaultRootElement ( ) ; DocumentEvent . ElementChange ec = chng . getChange ( root ) ; if ( ec != null ) { Element [ ] added = ec . getChildrenAdded ( ) ; boolean inComment = false ; for ( Element elem : added ) { int p0 = elem . getStartOffset ( ) ; int p1 = elem . getEndOffset ( ) ; String s ; try { s = getText ( p0 , p1 - p0 ) ; } catch ( BadLocationException bl ) { s = "" ; } if ( inComment ) { MutableAttributeSet a = ( MutableAttributeSet ) elem . getAttributes ( ) ; a . addAttribute ( CommentAttribute , CommentAttribute ) ; int index = s . indexOf ( "*/" ) ; if ( index >= 0 ) { // found an end of comment, turn off marks inComment = false ; } } else { // scan for multiline comment int index = s . indexOf ( "/*" ) ; if ( index >= 0 ) { // found a start of comment, see if it spans lines index = s . indexOf ( "*/" , index ) ; if ( index < 0 ) { // it spans lines inComment = true ; } } } } } }
Updates document structure as a result of text insertion . This will happen within a write lock . The superclass behavior of updating the line map is executed followed by marking any comment areas that should backtracked before scanning .