Attach a Document to enable line number tracking when editing. The position to track is before the first non-whitespace character on the line. Edits happening before that position will cause the line number to update accordingly. Multiple #startTracking calls will replace the tracked
(Document doc)
| 131 | * @param doc the {@link Document} to use for line number tracking |
| 132 | */ |
| 133 | public synchronized void startTracking(Document doc) { |
| 134 | //System.out.println("tracking: " + this); |
| 135 | if (doc == null) { |
| 136 | return; // null arg |
| 137 | } |
| 138 | if (doc == this.doc) { |
| 139 | return; // already tracking that doc |
| 140 | } |
| 141 | try { |
| 142 | Element line = doc.getDefaultRootElement().getElement(lineIdx); |
| 143 | if (line == null) { |
| 144 | return; // line doesn't exist |
| 145 | } |
| 146 | String lineText = doc.getText(line.getStartOffset(), line.getEndOffset() - line.getStartOffset()); |
| 147 | // set tracking position at (=before) first non-white space character on line, |
| 148 | // or, if the line consists of entirely white spaces, just before the newline |
| 149 | // character |
| 150 | pos = doc.createPosition(line.getStartOffset() + nonWhiteSpaceOffset(lineText)); |
| 151 | this.doc = doc; |
| 152 | doc.addDocumentListener(this); |
| 153 | } catch (BadLocationException ex) { |
| 154 | Messages.loge(null, ex); |
| 155 | pos = null; |
| 156 | this.doc = null; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | |
| 161 | /** |
no test coverage detected