FLEXRuntimeBrowserToolbar.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // FLEXRuntimeBrowserToolbar.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 6/11/17.
  6. // Copyright © 2017 Tanner Bennett. All rights reserved.
  7. //
  8. #import "FLEXRuntimeBrowserToolbar.h"
  9. #import "FLEXRuntimeKeyPathTokenizer.h"
  10. @interface FLEXRuntimeBrowserToolbar ()
  11. @property (nonatomic, copy) FLEXKBToolbarAction tapHandler;
  12. @end
  13. @implementation FLEXRuntimeBrowserToolbar
  14. + (instancetype)toolbarWithHandler:(FLEXKBToolbarAction)tapHandler suggestions:(NSArray<NSString *> *)suggestions {
  15. NSArray *buttons = [self
  16. buttonsForKeyPath:FLEXRuntimeKeyPath.empty suggestions:suggestions handler:tapHandler
  17. ];
  18. FLEXRuntimeBrowserToolbar *me = [self toolbarWithButtons:buttons];
  19. me.tapHandler = tapHandler;
  20. return me;
  21. }
  22. + (NSArray<FLEXKBToolbarButton*> *)buttonsForKeyPath:(FLEXRuntimeKeyPath *)keyPath
  23. suggestions:(NSArray<NSString *> *)suggestions
  24. handler:(FLEXKBToolbarAction)handler {
  25. NSMutableArray *buttons = [NSMutableArray new];
  26. FLEXSearchToken *lastKey = nil;
  27. BOOL lastKeyIsMethod = NO;
  28. if (keyPath.methodKey) {
  29. lastKey = keyPath.methodKey;
  30. lastKeyIsMethod = YES;
  31. } else {
  32. lastKey = keyPath.classKey ?: keyPath.bundleKey;
  33. }
  34. switch (lastKey.options) {
  35. case TBWildcardOptionsNone:
  36. case TBWildcardOptionsAny:
  37. if (lastKeyIsMethod) {
  38. if (!keyPath.instanceMethods) {
  39. [buttons addObject:[FLEXKBToolbarButton buttonWithTitle:@"-" action:handler]];
  40. [buttons addObject:[FLEXKBToolbarButton buttonWithTitle:@"+" action:handler]];
  41. }
  42. [buttons addObject:[FLEXKBToolbarButton buttonWithTitle:@"*" action:handler]];
  43. } else {
  44. [buttons addObject:[FLEXKBToolbarButton buttonWithTitle:@"*" action:handler]];
  45. [buttons addObject:[FLEXKBToolbarButton buttonWithTitle:@"*." action:handler]];
  46. }
  47. break;
  48. default: {
  49. if (lastKey.options & TBWildcardOptionsPrefix) {
  50. if (lastKeyIsMethod) {
  51. if (lastKey.string.length) {
  52. [buttons addObject:[FLEXKBToolbarButton buttonWithTitle:@"*" action:handler]];
  53. }
  54. } else {
  55. if (lastKey.string.length) {
  56. [buttons addObject:[FLEXKBToolbarButton buttonWithTitle:@"*." action:handler]];
  57. }
  58. }
  59. }
  60. else if (lastKey.options & TBWildcardOptionsSuffix) {
  61. if (!lastKeyIsMethod) {
  62. [buttons addObject:[FLEXKBToolbarButton buttonWithTitle:@"*" action:handler]];
  63. [buttons addObject:[FLEXKBToolbarButton buttonWithTitle:@"*." action:handler]];
  64. }
  65. }
  66. }
  67. }
  68. for (NSString *suggestion in suggestions) {
  69. [buttons addObject:[FLEXKBToolbarSuggestedButton buttonWithTitle:suggestion action:handler]];
  70. }
  71. return buttons;
  72. }
  73. - (void)setKeyPath:(FLEXRuntimeKeyPath *)keyPath suggestions:(NSArray<NSString *> *)suggestions {
  74. self.buttons = [self.class
  75. buttonsForKeyPath:keyPath suggestions:suggestions handler:self.tapHandler
  76. ];
  77. }
  78. @end