disposing range markers

This commit is contained in:
Dmitry Avdeev
2014-10-22 11:10:47 +04:00
parent bf8c5b4625
commit f3f51ea7f0
2 changed files with 60 additions and 23 deletions
@@ -527,7 +527,13 @@ public class CommentByBlockCommentHandler extends MultiCaretCodeInsightActionHan
shift += commentPrefix.length();
}
return processDocument(myDocument, myDocument.createRangeMarker(startOffset, endOffset + shift), commenter, true);
RangeMarker marker = myDocument.createRangeMarker(startOffset, endOffset + shift);
try {
return processDocument(myDocument, marker, commenter, true);
}
finally {
marker.dispose();
}
}
static TextRange processDocument(Document document, RangeMarker marker, Commenter commenter, boolean escape) {
@@ -657,7 +663,6 @@ public class CommentByBlockCommentHandler extends MultiCaretCodeInsightActionHan
return;
}
RangeMarker marker = myDocument.createRangeMarker(range);
String text = myDocument.getCharsSequence().subSequence(range.getStartOffset(), range.getEndOffset()).toString();
int startOffset = range.getStartOffset();
//boolean endsProperly = CharArrayUtil.regionMatches(chars, range.getEndOffset() - commentSuffix.length(), commentSuffix);
@@ -693,18 +698,23 @@ public class CommentByBlockCommentHandler extends MultiCaretCodeInsightActionHan
}
}
for (int i = ranges.size() - 1; i >= 0; i--) {
Couple<TextRange> toDelete = ranges.get(i);
myDocument.deleteString(toDelete.first.getStartOffset(), toDelete.first.getEndOffset());
int shift = toDelete.first.getEndOffset() - toDelete.first.getStartOffset();
myDocument.deleteString(toDelete.second.getStartOffset() - shift, toDelete.second.getEndOffset() - shift);
if (commenter.getCommentedBlockCommentPrefix() != null) {
commentNestedComments(myDocument, new TextRange(toDelete.first.getEndOffset() - shift, toDelete.second.getStartOffset() - shift),
commenter);
RangeMarker marker = myDocument.createRangeMarker(range);
try {
for (int i = ranges.size() - 1; i >= 0; i--) {
Couple<TextRange> toDelete = ranges.get(i);
myDocument.deleteString(toDelete.first.getStartOffset(), toDelete.first.getEndOffset());
int shift = toDelete.first.getEndOffset() - toDelete.first.getStartOffset();
myDocument.deleteString(toDelete.second.getStartOffset() - shift, toDelete.second.getEndOffset() - shift);
if (commenter.getCommentedBlockCommentPrefix() != null) {
commentNestedComments(myDocument, new TextRange(toDelete.first.getEndOffset() - shift, toDelete.second.getStartOffset() - shift),
commenter);
}
}
}
processDocument(myDocument, marker, commenter, false);
processDocument(myDocument, marker, commenter, false);
}
finally {
marker.dispose();
}
}
}
@@ -513,6 +513,20 @@ public class CommentByLineCommentHandler extends MultiCaretCodeInsightActionHand
return;
}
RangeMarker marker = endOffset > startOffset ? block.editor.getDocument().createRangeMarker(startOffset, endOffset) : null;
try {
if (doUncommentLine(line, document, commenter, startOffset, endOffset)) return;
if (marker != null) {
CommentByBlockCommentHandler.processDocument(document, marker, commenter, false);
}
}
finally {
if (marker != null) {
marker.dispose();
}
}
}
private static boolean doUncommentLine(int line, Document document, Commenter commenter, int startOffset, int endOffset) {
String prefix = commenter.getLineCommentPrefix();
if (prefix != null) {
CharSequence chars = document.getCharsSequence();
@@ -549,14 +563,14 @@ public class CommentByLineCommentHandler extends MultiCaretCodeInsightActionHand
}
}
document.deleteString(startOffset, startOffset + charsToDelete);
return;
return true;
}
String text = document.getCharsSequence().subSequence(startOffset, endOffset).toString();
prefix = commenter.getBlockCommentPrefix();
final String suffix = commenter.getBlockCommentSuffix();
if (prefix == null || suffix == null) {
return;
return true;
}
IntArrayList prefixes = new IntArrayList();
@@ -581,9 +595,7 @@ public class CommentByLineCommentHandler extends MultiCaretCodeInsightActionHand
for (int i = prefixes.size() - 1; i >= 0; i--) {
uncommentRange(document, startOffset + prefixes.get(i), Math.min(startOffset + suffixes.get(i) + suffix.length(), endOffset), commenter);
}
if (marker != null) {
CommentByBlockCommentHandler.processDocument(document, marker, commenter, false);
}
return false;
}
private static void commentLine(Block block, int line, int offset) {
@@ -597,11 +609,26 @@ public class CommentByLineCommentHandler extends MultiCaretCodeInsightActionHand
return;
}
String prefix = commenter.getLineCommentPrefix();
int endOffset = document.getLineEndOffset(line);
RangeMarker marker = document.createRangeMarker(offset, endOffset);
marker.setGreedyToLeft(true);
marker.setGreedyToRight(true);
try {
if (doCommentLine(block, line, offset, endOffset, commenter, document)) return;
CommentByBlockCommentHandler.processDocument(document, marker, commenter, true);
}
finally {
marker.dispose();
}
}
private static boolean doCommentLine(Block block,
int line,
int offset,
int endOffset,
Commenter commenter,
Document document) {
String prefix = commenter.getLineCommentPrefix();
if (prefix != null) {
if (commenter instanceof CommenterWithLineSuffix) {
endOffset = CharArrayUtil.shiftBackward(document.getCharsSequence(), endOffset, " \t");
@@ -621,8 +648,8 @@ public class CommentByLineCommentHandler extends MultiCaretCodeInsightActionHand
else {
prefix = commenter.getBlockCommentPrefix();
String suffix = commenter.getBlockCommentSuffix();
if (prefix == null || suffix == null) return;
if (endOffset == offset && block.startLine != block.endLine) return;
if (prefix == null || suffix == null) return true;
if (endOffset == offset && block.startLine != block.endLine) return true;
final int textLength = document.getTextLength();
final CharSequence chars = document.getCharsSequence();
offset = CharArrayUtil.shiftForward(chars, offset, " \t");
@@ -635,7 +662,7 @@ public class CommentByLineCommentHandler extends MultiCaretCodeInsightActionHand
}
if (endOffset < offset ||
offset == textLength - 1 && line != document.getLineCount() - 1) {
return;
return true;
}
final String text = chars.subSequence(offset, endOffset).toString();
final IntArrayList prefixes = new IntArrayList();
@@ -694,7 +721,7 @@ public class CommentByLineCommentHandler extends MultiCaretCodeInsightActionHand
document.insertString(offset, prefix);
}
}
CommentByBlockCommentHandler.processDocument(document, marker, commenter, true);
return false;
}
private static class Block {