TextRange (TextRange range) { int startOffset = range.getStartOffset(); FoldRegion region = editor.getFoldingModel().getCollapsedRegionAtOffset(startOffset); if (region != null) startOffset = region.getStartOffset(); int endOffset = range.getEndOffset(); region = editor.getFoldingModel().getCollapsedRegionAtOffset(endOffset); if (region != null && endOffset > region.getStartOffset()) endOffset = region.getEndOffset(); return new TextRange(startOffset, endOffset); }