Skip to content

Commit

Permalink
Merge pull request apache#7903 from sdedic/lsp/document-filter
Browse files Browse the repository at this point in the history
 Do not break additional DocumentFilters
  • Loading branch information
sdedic authored Dec 21, 2024
2 parents 9d5bc1e + 9b75397 commit 9ae7041
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public abstract class CloneableEditorSupport extends CloneableOpenSupport {
private boolean annotationsLoaded;

private DocFilter docFilter;
private DocumentOpenClose.DocumentRef filteredDocRef;

private final Object checkModificationLock = new Object();

Expand Down Expand Up @@ -493,16 +494,20 @@ final void addDocListener(Document d) {
if (Boolean.TRUE.equals(d.getProperty("supportsModificationListener"))) { // NOI18N
d.putProperty("modificationListener", getListener()); // NOI18N
}

if (d instanceof AbstractDocument) {
AbstractDocument aDoc = (AbstractDocument) d;
DocumentFilter origFilter = aDoc.getDocumentFilter();
docFilter = new DocFilter(origFilter);
aDoc.setDocumentFilter(docFilter);
} else { // Put property for non-AD
DocumentFilter origFilter = (DocumentFilter) d.getProperty(DocumentFilter.class);
docFilter = new DocFilter(origFilter);
d.putProperty(DocumentFilter.class, docFilter);
DocFilter df = docFilter;
if (df == null || df.origDocument != openClose.docRef) {
if (d instanceof AbstractDocument) {
AbstractDocument aDoc = (AbstractDocument) d;
DocumentFilter origFilter = aDoc.getDocumentFilter();
docFilter = new DocFilter(origFilter, openClose.docRef);
aDoc.setDocumentFilter(docFilter);
} else { // Put property for non-AD
DocumentFilter origFilter = (DocumentFilter) d.getProperty(DocumentFilter.class);
docFilter = new DocFilter(origFilter, openClose.docRef);
d.putProperty(DocumentFilter.class, docFilter);
}
} else {
df.docFilterDisabled = false;
}
d.addDocumentListener(getListener());
}
Expand All @@ -511,15 +516,9 @@ final void removeDocListener(Document d) {
if (Boolean.TRUE.equals(d.getProperty("supportsModificationListener"))) { // NOI18N
d.putProperty("modificationListener", null); // NOI18N
}

if (docFilter != null) {
if (d instanceof AbstractDocument) {
AbstractDocument aDoc = (AbstractDocument) d;
aDoc.setDocumentFilter(docFilter.origFilter);
} else { // Put property for non-AD
d.putProperty(DocumentFilter.class, docFilter.origFilter);
}
docFilter = null;
DocFilter df = docFilter;
if (df != null) {
df.docFilterDisabled = true;
}
d.removeDocumentListener(getListener());
}
Expand Down Expand Up @@ -2325,14 +2324,17 @@ public void run() {
private final class DocFilter extends DocumentFilter {

final DocumentFilter origFilter;
final Reference origDocument;
boolean docFilterDisabled;

DocFilter(DocumentFilter origFilter) {
DocFilter(DocumentFilter origFilter, Reference origDocument) {
this.origFilter = origFilter;
this.origDocument = origDocument;
}

@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
boolean origModified = checkModificationAllowed(offset);
boolean origModified = docFilterDisabled || checkModificationAllowed(offset);
boolean success = false;
try {
if (origFilter != null) {
Expand All @@ -2352,7 +2354,7 @@ public void insertString(FilterBypass fb, int offset, String string, AttributeSe

@Override
public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
boolean origModified = checkModificationAllowed(offset);
boolean origModified = docFilterDisabled || checkModificationAllowed(offset);
boolean success = false;
try {
if (origFilter != null) {
Expand All @@ -2372,7 +2374,7 @@ public void remove(FilterBypass fb, int offset, int length) throws BadLocationEx

@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
boolean origModified = checkModificationAllowed(offset);
boolean origModified = docFilterDisabled || checkModificationAllowed(offset);
boolean success = false;
try {
if (origFilter != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ public String toString() {

}

private final class DocumentRef extends WeakReference<StyledDocument> implements Runnable {
final class DocumentRef extends WeakReference<StyledDocument> implements Runnable {

public DocumentRef(StyledDocument doc) {
super(doc, org.openide.util.Utilities.activeReferenceQueue());
Expand Down

0 comments on commit 9ae7041

Please sign in to comment.