Skip to content

Commit

Permalink
SynchronizableDocument: fix (BadPositionCategoryException)
Browse files Browse the repository at this point in the history
Adds missing synchronized overrides of underlying AbstractDocument

To synchronize all accesses to AbstractDocument.fEndPositions especially
by addPositionCategory()

eclipse-jdt/eclipse.jdt.ui#1067
  • Loading branch information
jukzi committed Jan 8, 2025
1 parent 690fac0 commit 802c967
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
*******************************************************************************/
package org.eclipse.core.internal.filebuffers;

import java.util.List;
import java.util.Map;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.BadPartitioningException;
import org.eclipse.jface.text.BadPositionCategoryException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.DocumentRewriteSession;
import org.eclipse.jface.text.DocumentRewriteSessionType;
import org.eclipse.jface.text.IDocumentExtension4;
Expand All @@ -38,6 +42,97 @@ public class SynchronizableDocument extends Document implements ISynchronizable

private Object fLockObject;

@Override
protected void updateDocumentStructures(DocumentEvent event) {
Object lockObject= getLockObject();
if (lockObject == null) {
super.updateDocumentStructures(event);
return;
}
synchronized (lockObject) {
super.updateDocumentStructures(event);
}
}

@Override
public void removePositionCategory(String category) throws BadPositionCategoryException {
Object lockObject= getLockObject();
if (lockObject == null) {
super.removePositionCategory(category);
return;
}
synchronized (lockObject) {
super.removePositionCategory(category);
}
}

@Override
public String[] getPositionCategories() {
Object lockObject= getLockObject();
if (lockObject == null) {
return super.getPositionCategories();
}
synchronized (lockObject) {
return super.getPositionCategories();
}
}

@Override
protected Map<String, List<Position>> getDocumentManagedPositions() {
Object lockObject= getLockObject();
if (lockObject == null) {
return super.getDocumentManagedPositions();
}
synchronized (lockObject) {
return super.getDocumentManagedPositions();
}
}

@Override
public boolean containsPositionCategory(String category) {
Object lockObject= getLockObject();
if (lockObject == null) {
return super.containsPositionCategory(category);
}
synchronized (lockObject) {
return super.containsPositionCategory(category);
}
}

@Override
public boolean containsPosition(String category, int offset, int length) {
Object lockObject= getLockObject();
if (lockObject == null) {
return super.containsPosition(category, offset, length);
}
synchronized (lockObject) {
return super.containsPosition(category, offset, length);
}
}

@Override
public int computeIndexInCategory(String category, int offset) throws BadLocationException, BadPositionCategoryException {
Object lockObject= getLockObject();
if (lockObject == null) {
return super.computeIndexInCategory(category, offset);
}
synchronized (lockObject) {
return super.computeIndexInCategory(category, offset);
}
}

@Override
public void addPositionCategory(String category) {
Object lockObject= getLockObject();
if (lockObject == null) {
super.addPositionCategory(category);
return;
}
synchronized (lockObject) {
super.addPositionCategory(category);
}
}

@Override
public synchronized void setLockObject(Object lockObject) {
fLockObject= lockObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,12 @@ public void addPosition(String category, Position position) throws BadLocationEx

List<Position> list= fPositions.get(category);
if (list == null)
throw new BadPositionCategoryException();
throw new BadPositionCategoryException(category);
list.add(computeIndexInPositionList(list, position.offset), position);

List<Position> endPositions= fEndPositions.get(category);
if (endPositions == null)
throw new BadPositionCategoryException();
throw new BadPositionCategoryException(category);
endPositions.add(computeIndexInPositionList(endPositions, position.offset + position.length - 1, false), position);
}

Expand Down Expand Up @@ -514,7 +514,7 @@ public int computeIndexInCategory(String category, int offset) throws BadLocatio

List<Position> c= fPositions.get(category);
if (c == null)
throw new BadPositionCategoryException();
throw new BadPositionCategoryException(category);

return computeIndexInPositionList(c, offset);
}
Expand Down Expand Up @@ -925,7 +925,7 @@ public Position[] getPositions(String category) throws BadPositionCategoryExcept

List<Position> c= fPositions.get(category);
if (c == null)
throw new BadPositionCategoryException();
throw new BadPositionCategoryException(category);

Position[] positions= new Position[c.size()];
c.toArray(positions);
Expand Down Expand Up @@ -980,12 +980,12 @@ public void removePosition(String category, Position position) throws BadPositio

List<Position> c= fPositions.get(category);
if (c == null)
throw new BadPositionCategoryException();
throw new BadPositionCategoryException(category);
removeFromPositionsList(c, position, true);

List<Position> endPositions= fEndPositions.get(category);
if (endPositions == null)
throw new BadPositionCategoryException();
throw new BadPositionCategoryException(category);
removeFromPositionsList(endPositions, position, false);
}

Expand Down Expand Up @@ -1043,7 +1043,7 @@ public void removePositionCategory(String category) throws BadPositionCategoryEx
return;

if ( !containsPositionCategory(category))
throw new BadPositionCategoryException();
throw new BadPositionCategoryException(category);

fPositions.remove(category);
fEndPositions.remove(category);
Expand Down Expand Up @@ -1674,7 +1674,7 @@ private List<Position> getStartingPositions(String category, int offset, int len
private List<Position> getEndingPositions(String category, int offset, int length) throws BadPositionCategoryException {
List<Position> positions= fEndPositions.get(category);
if (positions == null)
throw new BadPositionCategoryException();
throw new BadPositionCategoryException(category);

int indexStart= computeIndexInPositionList(positions, offset, false);
int indexEnd= computeIndexInPositionList(positions, offset + length, false);
Expand Down

0 comments on commit 802c967

Please sign in to comment.