
+467
Under review
Code Folding
See http://en.wikipedia.org/wiki/Code_folding
Allow the user to selectively hide and display sections of a currently-edited file.
Duplicates
5
Customer support service by UserEcho
It's a pretty basic feature for most code editors - though with that said, most editors have a terrible 'folder' in that it does not fold certain functions accurately.
I am working in Lua (Corona SDK) and it would be very beneficial to have the option to fold my functions and conditional statements.
Please update Textastic to include this feature.
Like that, may be show an option «Show folding panel» (like «Show line numbers»)
Love the app, but this is a big pain point for me. Is there any update on this request?
how’re we making out on this request? It’s been an amazing feature option for over a decade now lol
in fact… here’s most of the code needed… I’ll be making a plug-in for jailbroken ios if you guys aren’t going to implement it
Feel free to do that. There would be much more needed to implement this efficiently and for all supported languages.
Just running a regex on the whole text on potentially every keystroke would not be how I'd approach this.
Also, one of the biggest problems for me is how to do a touch-friendly UI for expanding/collapsing and have big enough touch targets on small iPhone screens while not interfering with text selection.
#import <objc/runtime.h>
#import <Foundation/Foundation.h>
%hook MyTextEditorClass
// Helper function to identify foldable sections in Objective-C++ code
- (NSArray *)findFoldableSectionsInText:(NSString *)text {
NSMutableArray *foldableSections = [NSMutableArray array];
// Regular expressions to detect Objective-C and C++ blocks
NSString *objcClassRegex = @"@interface\\s+(\\w+)\\s*\\{";
NSString *cppClassRegex = @"class\\s+(\\w+)\\s*\\{";
NSString *functionRegex = @"(\\w+)\\s+\\w+::?\\w*\\s*\\(.*\\)\\s*\\{";
NSError *error = nil;
// Detect Objective-C classes
NSRegularExpression *objcClassExpr = [NSRegularExpression regularExpressionWithPattern:objcClassRegex options:0 error:&error];
[objcClassExpr enumerateMatchesInString:text options:0 range:NSMakeRange(0, [text length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
[foldableSections addObject:@{@"type": @"objc_class", @"range": NSStringFromRange(result.range)}];
}];
// Detect C++ classes
NSRegularExpression *cppClassExpr = [NSRegularExpression regularExpressionWithPattern:cppClassRegex options:0 error:&error];
[cppClassExpr enumerateMatchesInString:text options:0 range:NSMakeRange(0, [text length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
[foldableSections addObject:@{@"type": @"cpp_class", @"range": NSStringFromRange(result.range)}];
}];
// Detect functions
NSRegularExpression *functionExpr = [NSRegularExpression regularExpressionWithPattern:functionRegex options:0 error:&error];
[functionExpr enumerateMatchesInString:text options:0 range:NSMakeRange(0, [text length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
[foldableSections addObject:@{@"type": @"function", @"range": NSStringFromRange(result.range)}];
}];
return foldableSections;
}
// Hook into the method that renders text (or any method that handles text view updates)
- (void)renderTextWithFolding {
NSString *text = [self getText]; // This method should retrieve the text content from the editor view
// Find foldable sections
NSArray *foldableSections = [self findFoldableSectionsInText:text];
// For each foldable section, we could either hide it or show a "fold" button in the UI
for (NSDictionary *section in foldableSections) {
NSString *type = section[@"type"];
NSRange range = NSRangeFromString(section[@"range"]);
// Example: For foldable class blocks, we might hide the content or add a UI fold button
if ([type isEqualToString:@"objc_class"] || [type isEqualToString:@"cpp_class"]) {
[self addFoldButtonForRange:range];
} else if ([type isEqualToString:@"function"]) {
[self addFoldButtonForRange:range];
}
}
}
// Helper function to add a fold button (UI interaction logic)
- (void)addFoldButtonForRange:(NSRange)range {
// Assuming you have access to the text view/UI, add a button or visual cue for the fold action
// This could involve adding a small "v" icon or something to represent folding
// Example: insert a UI button for collapsing/expanding at a specific position
[self insertFoldButtonAtPosition:range.location];
}
// Helper function to insert a fold button at a specific position
- (void)insertFoldButtonAtPosition:(NSUInteger)position {
// Insert a fold button in the UI at the given text position (this will depend on how your app is rendering text)
// This might involve adding a button or some UI element at the position in the text view
}
%end