-
Notifications
You must be signed in to change notification settings - Fork 54
/
DocumentLineList.java
111 lines (99 loc) · 3.18 KB
/
DocumentLineList.java
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Copyright (c) 2015-2017 Angelo ZERR.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
*/
package org.eclipse.tm4e.ui.internal.model;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.tm4e.core.model.AbstractLineList;
import org.eclipse.tm4e.ui.TMUIPlugin;
/**
* TextMate {@link AbstractLineList} implementation with Eclipse
* {@link IDocument}.
*
* Goal of this class is to synchronize Eclipse {@link DocumentEvent} with
* TextMate model lines.
*
*/
public class DocumentLineList extends AbstractLineList {
private final IDocument document;
private InternalListener listener;
public DocumentLineList(IDocument document) {
this.document = document;
this.listener = new InternalListener();
document.addDocumentListener(listener);
for (int i = 0; i < document.getNumberOfLines(); i++) {
addLine(i);
}
}
private class InternalListener implements IDocumentListener {
@Override
public void documentAboutToBeChanged(DocumentEvent event) {
try {
if (!DocumentHelper.isInsert(event)) {
// Remove or Replace (Remove + Insert)
removeLine(event);
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
private void removeLine(DocumentEvent event) throws BadLocationException {
int startLine = DocumentHelper.getStartLine(event);
int endLine = DocumentHelper.getEndLine(event, true);
for (int i = endLine; i > startLine; i--) {
DocumentLineList.this.removeLine(i);
}
}
@Override
public void documentChanged(DocumentEvent event) {
IDocument document = event.getDocument();
try {
int startLine = DocumentHelper.getStartLine(event);
if (!DocumentHelper.isRemove(event)) {
int endLine = DocumentHelper.getEndLine(event, false);
// Insert new lines
for (int i = startLine; i < endLine; i++) {
DocumentLineList.this.addLine(i + 1);
}
if (startLine == endLine) {
DocumentLineList.this.updateLine(startLine);
}
} else {
// Update line
DocumentLineList.this.updateLine(startLine);
}
invalidateLine(startLine);
} catch (BadLocationException e) {
TMUIPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TMUIPlugin.PLUGIN_ID, e.getMessage(), e));
}
}
}
@Override
public int getNumberOfLines() {
return document.getNumberOfLines();
}
@Override
public String getLineText(int line) throws Exception {
return DocumentHelper.getLineText(document, line, false);
}
@Override
public int getLineLength(int line) throws Exception {
return document.getLineLength(line);
}
@Override
public void dispose() {
document.removeDocumentListener(listener);
}
}