<commit_before>#include "Plugin.pch"

static callbackFunc asyncCallback;

const char* DialogOpenFilePanel(const char* title,
                                const char* directory,
                                const char* filters,
                                bool multiselect) {
    StandaloneFileBrowser* dialog = [[StandaloneFileBrowser alloc] init];
    NSString* paths = [dialog dialogOpenFilePanel:[NSString stringWithUTF8String:title ?: ""]
                                        directory:[NSString stringWithUTF8String:directory ?: ""]
                                          filters:[NSString stringWithUTF8String:filters ?: ""]
                                      multiselect:multiselect
                                   canChooseFiles:YES
                                 canChooseFolders:NO];
    return [paths UTF8String];
}

void DialogOpenFilePanelAsync(const char* title,
                              const char* directory,
                              const char* filters,
                              bool multiselect,
                              callbackFunc cb) {
    asyncCallback = cb;
    StandaloneFileBrowser* dialog = [[StandaloneFileBrowser alloc] init];
    [dialog dialogOpenFilePanelAsync:[NSString stringWithUTF8String:title ?: ""]
                           directory:[NSString stringWithUTF8String:directory ?: ""]
                             filters:[NSString stringWithUTF8String:filters ?: ""]
                         multiselect:multiselect
                      canChooseFiles:YES
                    canChooseFolders:NO];
}

const char* DialogOpenFolderPanel(const char* title,
                                  const char* directory,
                                  bool multiselect) {
    StandaloneFileBrowser* dialog = [[StandaloneFileBrowser alloc] init];
    NSString* paths = [dialog dialogOpenFilePanel:[NSString stringWithUTF8String:title ?: ""]
                                        directory:[NSString stringWithUTF8String:directory ?: ""]
                                          filters:[NSString stringWithUTF8String:""]
                                      multiselect:multiselect
                                   canChooseFiles:NO
                                 canChooseFolders:YES];
    return [paths UTF8String];
}

void DialogOpenFolderPanelAsync(const char* title,
                                const char* directory,
                                bool multiselect,
                                callbackFunc cb) {
    asyncCallback = cb;
    StandaloneFileBrowser* dialog = [[StandaloneFileBrowser alloc] init];
    [dialog dialogOpenFilePanelAsync:[NSString stringWithUTF8String:title ?: ""]
                           directory:[NSString stringWithUTF8String:directory ?: ""]
                             filters:[NSString stringWithUTF8String:""]
                         multiselect:multiselect
                      canChooseFiles:NO
                    canChooseFolders:YES];
}

const char* DialogSaveFilePanel(const char* title,
                                const char* directory,
                                const char* defaultName,
                                const char* filters) {
    StandaloneFileBrowser* dialog = [[StandaloneFileBrowser alloc] init];
    NSString* paths = [dialog dialogSaveFilePanel:[NSString stringWithUTF8String:title ?: ""]
                                        directory:[NSString stringWithUTF8String:directory ?: ""]
                                      defaultName:[NSString stringWithUTF8String:defaultName ?: ""]
                                          filters:[NSString stringWithUTF8String:filters ?: ""]];
    return [paths UTF8String];
}

void DialogSaveFilePanelAsync(const char* title,
                              const char* directory,
                              const char* defaultName,
                              const char* filters,
                              callbackFunc cb) {
    asyncCallback = cb;
    StandaloneFileBrowser* dialog = [[StandaloneFileBrowser alloc] init];
    [dialog dialogSaveFilePanelAsync:[NSString stringWithUTF8String:title ?: ""]
                           directory:[NSString stringWithUTF8String:directory ?: ""]
                         defaultName:[NSString stringWithUTF8String:defaultName ?: ""]
                             filters:[NSString stringWithUTF8String:filters ?: ""]];
}


@implementation StandaloneFileBrowser

- (id)init {
    if (self = [super init]) {
        NSLog(@"init");
    }
    return self;
}

- (NSString*)dialogOpenFilePanel:(NSString*)title
                       directory:(NSString*)directory
                         filters:(NSString*)filters
                     multiselect:(BOOL)multiselect
                  canChooseFiles:(BOOL)canChooseFiles
                canChooseFolders:(BOOL)canChooseFolders {

    NSOpenPanel* panel = [self createOpenPanel:title
                                     directory:directory
                                       filters:filters
                                   multiselect:multiselect
                                canChooseFiles:canChooseFiles
                              canChooseFolders:canChooseFolders];
    if (panel && [panel runModal] == NSModalResponseOK && [[panel URLs] count] > 0) {
        NSMutableArray* paths = [NSMutableArray arrayWithCapacity:[[panel URLs] count]];
        for (int i = 0; i < [[panel URLs] count]; i++) {
            NSURL* url = [[panel URLs] objectAtIndex:i];
            [paths addObject:[url path]];
        }
        NSString* seperator = [NSString stringWithFormat:@"%c", 28];
        return [paths componentsJoinedByString:seperator];
    }

    return @"";
}

- (void)dialogOpenFilePanelAsync:(NSString*)title
                       directory:(NSString*)directory
                         filters:(NSString*)filters
                     multiselect:(BOOL)multiselect
                  canChooseFiles:(BOOL)canChooseFiles
                canChooseFolders:(BOOL)canChooseFolders {

    NSOpenPanel* panel = [self createOpenPanel:title
                                     directory:directory
                                       filters:filters
                                   multiselect:multiselect
                                canChooseFiles:canChooseFiles
                              canChooseFolders:canChooseFolders];
    [self performSelectorOnMainThread:@selector(dialogOpenFilePanelAsyncSelector:) withObject:panel waitUntilDone:NO];
}

- (void)dialogOpenFilePanelAsyncSelector:(NSOpenPanel*)panel {
    NSString* pathsStr = @"";
    if (panel && [panel runModal] == NSModalResponseOK && [[panel URLs] count] > 0) {
        NSMutableArray* paths = [NSMutableArray arrayWithCapacity:[[panel URLs] count]];
        for (int i = 0; i < [[panel URLs] count]; i++) {
            NSURL* url = [[panel URLs] objectAtIndex:i];
            [paths addObject:[url path]];
        }
        NSString* seperator = [NSString stringWithFormat:@"%c", 28];
        pathsStr = [paths componentsJoinedByString:seperator];
    }
    asyncCallback([pathsStr UTF8String]);
}

- (NSOpenPanel*)createOpenPanel:(NSString*)title
                      directory:(NSString*)directory
                        filters:(NSString*)filters
                    multiselect:(BOOL)multiselect
                 canChooseFiles:(BOOL)canChooseFiles
               canChooseFolders:(BOOL)canChooseFolders {
    @try {
        NSMutableArray* filterItems = [[NSMutableArray alloc] init];
        NSMutableArray* extensions = [[NSMutableArray alloc] init];
        [self parseFilter:filters filters:filterItems extensions:extensions];

        NSOpenPanel* panel = [NSOpenPanel openPanel];

        if (filterItems.count > 0) {
            PopUpButtonHandler* popUpHandler = [[PopUpButtonHandler alloc] initWithPanel:panel];
            [popUpHandler setExtensions:extensions];

            NSView *accessoryView = [[NSView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 200, 24.0)];
            NSTextField *label = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 60, 22)];
            [label setEditable:NO];
            [label setStringValue:@"File type:"];
            [label setBordered:NO];
            [label setBezeled:NO];
            [label setDrawsBackground:NO];

            NSPopUpButton *popupButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(61.0, 2, 140, 22.0) pullsDown:NO];
            [popupButton addItemsWithTitles:filterItems];
            [popupButton setTarget:popUpHandler];
            [popupButton setAction:@selector(selectFormatOpen:)];

            [accessoryView addSubview:label];
            [accessoryView addSubview:popupButton];

            [panel setAccessoryView:accessoryView];
            if ([panel respondsToSelector:@selector(setAccessoryViewDisclosed:)]) {
                [panel setAccessoryViewDisclosed:YES];
            }
            [panel setAllowedFileTypes:(NSArray*)[extensions objectAtIndex:0]];
        }

        if ([title length] != 0) {
            [panel setMessage:title];
        }
        [panel setCanChooseFiles:canChooseFiles];
        [panel setCanChooseDirectories:canChooseFolders];
        [panel setAllowsMultipleSelection:multiselect];
        [panel setDirectoryURL:[NSURL fileURLWithPath:directory]];

        return panel;
    }
    @catch (NSException *exception) {
        NSLog(@"SFB::dialogOpenFilePanel Exception: %@", exception.reason);
        return nil;
    }
}
<commit_msg>FIX: OpenFolderPanel dialog has no option to create new directory on Mac #33<commit_after>#include "Plugin.pch"

static callbackFunc asyncCallback;

const char* DialogOpenFilePanel(const char* title,
                                const char* directory,
                                const char* filters,
                                bool multiselect) {
    StandaloneFileBrowser* dialog = [[StandaloneFileBrowser alloc] init];
    NSString* paths = [dialog dialogOpenFilePanel:[NSString stringWithUTF8String:title ?: ""]
                                        directory:[NSString stringWithUTF8String:directory ?: ""]
                                          filters:[NSString stringWithUTF8String:filters ?: ""]
                                      multiselect:multiselect
                                   canChooseFiles:YES
                                 canChooseFolders:NO];
    return [paths UTF8String];
}

void DialogOpenFilePanelAsync(const char* title,
                              const char* directory,
                              const char* filters,
                              bool multiselect,
                              callbackFunc cb) {
    asyncCallback = cb;
    StandaloneFileBrowser* dialog = [[StandaloneFileBrowser alloc] init];
    [dialog dialogOpenFilePanelAsync:[NSString stringWithUTF8String:title ?: ""]
                           directory:[NSString stringWithUTF8String:directory ?: ""]
                             filters:[NSString stringWithUTF8String:filters ?: ""]
                         multiselect:multiselect
                      canChooseFiles:YES
                    canChooseFolders:NO];
}

const char* DialogOpenFolderPanel(const char* title,
                                  const char* directory,
                                  bool multiselect) {
    StandaloneFileBrowser* dialog = [[StandaloneFileBrowser alloc] init];
    NSString* paths = [dialog dialogOpenFilePanel:[NSString stringWithUTF8String:title ?: ""]
                                        directory:[NSString stringWithUTF8String:directory ?: ""]
                                          filters:[NSString stringWithUTF8String:""]
                                      multiselect:multiselect
                                   canChooseFiles:NO
                                 canChooseFolders:YES];
    return [paths UTF8String];
}

void DialogOpenFolderPanelAsync(const char* title,
                                const char* directory,
                                bool multiselect,
                                callbackFunc cb) {
    asyncCallback = cb;
    StandaloneFileBrowser* dialog = [[StandaloneFileBrowser alloc] init];
    [dialog dialogOpenFilePanelAsync:[NSString stringWithUTF8String:title ?: ""]
                           directory:[NSString stringWithUTF8String:directory ?: ""]
                             filters:[NSString stringWithUTF8String:""]
                         multiselect:multiselect
                      canChooseFiles:NO
                    canChooseFolders:YES];
}

const char* DialogSaveFilePanel(const char* title,
                                const char* directory,
                                const char* defaultName,
                                const char* filters) {
    StandaloneFileBrowser* dialog = [[StandaloneFileBrowser alloc] init];
    NSString* paths = [dialog dialogSaveFilePanel:[NSString stringWithUTF8String:title ?: ""]
                                        directory:[NSString stringWithUTF8String:directory ?: ""]
                                      defaultName:[NSString stringWithUTF8String:defaultName ?: ""]
                                          filters:[NSString stringWithUTF8String:filters ?: ""]];
    return [paths UTF8String];
}

void DialogSaveFilePanelAsync(const char* title,
                              const char* directory,
                              const char* defaultName,
                              const char* filters,
                              callbackFunc cb) {
    asyncCallback = cb;
    StandaloneFileBrowser* dialog = [[StandaloneFileBrowser alloc] init];
    [dialog dialogSaveFilePanelAsync:[NSString stringWithUTF8String:title ?: ""]
                           directory:[NSString stringWithUTF8String:directory ?: ""]
                         defaultName:[NSString stringWithUTF8String:defaultName ?: ""]
                             filters:[NSString stringWithUTF8String:filters ?: ""]];
}


@implementation StandaloneFileBrowser

- (id)init {
    if (self = [super init]) {
        NSLog(@"init");
    }
    return self;
}

- (NSString*)dialogOpenFilePanel:(NSString*)title
                       directory:(NSString*)directory
                         filters:(NSString*)filters
                     multiselect:(BOOL)multiselect
                  canChooseFiles:(BOOL)canChooseFiles
                canChooseFolders:(BOOL)canChooseFolders {

    NSOpenPanel* panel = [self createOpenPanel:title
                                     directory:directory
                                       filters:filters
                                   multiselect:multiselect
                                canChooseFiles:canChooseFiles
                              canChooseFolders:canChooseFolders];
    if (panel && [panel runModal] == NSModalResponseOK && [[panel URLs] count] > 0) {
        NSMutableArray* paths = [NSMutableArray arrayWithCapacity:[[panel URLs] count]];
        for (int i = 0; i < [[panel URLs] count]; i++) {
            NSURL* url = [[panel URLs] objectAtIndex:i];
            [paths addObject:[url path]];
        }
        NSString* seperator = [NSString stringWithFormat:@"%c", 28];
        return [paths componentsJoinedByString:seperator];
    }

    return @"";
}

- (void)dialogOpenFilePanelAsync:(NSString*)title
                       directory:(NSString*)directory
                         filters:(NSString*)filters
                     multiselect:(BOOL)multiselect
                  canChooseFiles:(BOOL)canChooseFiles
                canChooseFolders:(BOOL)canChooseFolders {

    NSOpenPanel* panel = [self createOpenPanel:title
                                     directory:directory
                                       filters:filters
                                   multiselect:multiselect
                                canChooseFiles:canChooseFiles
                              canChooseFolders:canChooseFolders];
    [self performSelectorOnMainThread:@selector(dialogOpenFilePanelAsyncSelector:) withObject:panel waitUntilDone:NO];
}

- (void)dialogOpenFilePanelAsyncSelector:(NSOpenPanel*)panel {
    NSString* pathsStr = @"";
    if (panel && [panel runModal] == NSModalResponseOK && [[panel URLs] count] > 0) {
        NSMutableArray* paths = [NSMutableArray arrayWithCapacity:[[panel URLs] count]];
        for (int i = 0; i < [[panel URLs] count]; i++) {
            NSURL* url = [[panel URLs] objectAtIndex:i];
            [paths addObject:[url path]];
        }
        NSString* seperator = [NSString stringWithFormat:@"%c", 28];
        pathsStr = [paths componentsJoinedByString:seperator];
    }
    asyncCallback([pathsStr UTF8String]);
}

- (NSOpenPanel*)createOpenPanel:(NSString*)title
                      directory:(NSString*)directory
                        filters:(NSString*)filters
                    multiselect:(BOOL)multiselect
                 canChooseFiles:(BOOL)canChooseFiles
               canChooseFolders:(BOOL)canChooseFolders {
    @try {
        NSMutableArray* filterItems = [[NSMutableArray alloc] init];
        NSMutableArray* extensions = [[NSMutableArray alloc] init];
        [self parseFilter:filters filters:filterItems extensions:extensions];

        NSOpenPanel* panel = [NSOpenPanel openPanel];

        if (filterItems.count > 0) {
            PopUpButtonHandler* popUpHandler = [[PopUpButtonHandler alloc] initWithPanel:panel];
            [popUpHandler setExtensions:extensions];

            NSView *accessoryView = [[NSView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 200, 24.0)];
            NSTextField *label = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 60, 22)];
            [label setEditable:NO];
            [label setStringValue:@"File type:"];
            [label setBordered:NO];
            [label setBezeled:NO];
            [label setDrawsBackground:NO];

            NSPopUpButton *popupButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(61.0, 2, 140, 22.0) pullsDown:NO];
            [popupButton addItemsWithTitles:filterItems];
            [popupButton setTarget:popUpHandler];
            [popupButton setAction:@selector(selectFormatOpen:)];

            [accessoryView addSubview:label];
            [accessoryView addSubview:popupButton];

            [panel setAccessoryView:accessoryView];
            if ([panel respondsToSelector:@selector(setAccessoryViewDisclosed:)]) {
                [panel setAccessoryViewDisclosed:YES];
            }
            [panel setAllowedFileTypes:(NSArray*)[extensions objectAtIndex:0]];
        }

        if ([title length] != 0) {
            [panel setMessage:title];
        }
        [panel setCanChooseFiles:canChooseFiles];
        [panel setCanChooseDirectories:canChooseFolders];
        [panel setAllowsMultipleSelection:multiselect];
        [panel setDirectoryURL:[NSURL fileURLWithPath:directory]];
        if ([panel respondsToSelector:@selector(setCanCreateDirectories:)]) {
            [panel setCanCreateDirectories:YES];
        }

        return panel;
    }
    @catch (NSException *exception) {
        NSLog(@"SFB::dialogOpenFilePanel Exception: %@", exception.reason);
        return nil;
    }
}

{'fraction_non_alphanumeric': 0.08839807852965748, 'fraction_numerical': 0.006474519632414369, 'mean_word_length': 1.8586567164179104, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '665349', 'n_tokens_mistral': 4947, 'n_tokens_neox': 4437, 'n_words': 891}