blob: d803e0e8fd97fc278f78418cca65301b83e339d3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
package org.jetbrains.plugins.coursecreator;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.event.DocumentAdapter;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.impl.event.DocumentEventImpl;
import org.jetbrains.plugins.coursecreator.format.TaskFile;
import org.jetbrains.plugins.coursecreator.format.TaskWindow;
/**
* author: liana
* data: 7/16/14.
* Listens changes in study files and updates
* coordinates of all the windows in current task file
*/
public abstract class StudyDocumentListener extends DocumentAdapter {
private final TaskFile myTaskFile;
private int oldLine;
private int oldLineStartOffset;
private TaskWindow myTaskWindow;
public StudyDocumentListener(TaskFile taskFile) {
myTaskFile = taskFile;
}
//remembering old end before document change because of problems
// with fragments containing "\n"
@Override
public void beforeDocumentChange(DocumentEvent e) {
int offset = e.getOffset();
int oldEnd = offset + e.getOldLength();
Document document = e.getDocument();
oldLine = document.getLineNumber(oldEnd);
oldLineStartOffset = document.getLineStartOffset(oldLine);
int line = document.getLineNumber(offset);
int offsetInLine = offset - document.getLineStartOffset(line);
LogicalPosition pos = new LogicalPosition(line, offsetInLine);
myTaskWindow = myTaskFile.getTaskWindow(document, pos);
}
@Override
public void documentChanged(DocumentEvent e) {
if (e instanceof DocumentEventImpl) {
if (!needModify()) {
return;
}
DocumentEventImpl event = (DocumentEventImpl)e;
Document document = e.getDocument();
int offset = e.getOffset();
int change = event.getNewLength() - event.getOldLength();
if (myTaskWindow != null) {
updateTaskWindowLength(e.getNewFragment(), myTaskWindow, change);
}
int newEnd = offset + event.getNewLength();
int newLine = document.getLineNumber(newEnd);
int lineChange = newLine - oldLine;
myTaskFile.incrementLines(oldLine + 1, lineChange);
int newEndOffsetInLine = offset + e.getNewLength() - document.getLineStartOffset(newLine);
int oldEndOffsetInLine = offset + e.getOldLength() - oldLineStartOffset;
myTaskFile.updateLine(lineChange, oldLine, newEndOffsetInLine, oldEndOffsetInLine);
}
}
protected abstract void updateTaskWindowLength(CharSequence fragment, TaskWindow taskWindow, int change);
protected abstract boolean needModify();
}
|