TBKeyPathToolbar.m 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // FLEXKeyPathToolbar.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 6/11/17.
  6. // Copyright © 2017 Tanner Bennett. All rights reserved.
  7. //
  8. #import "TBKeyPathToolbar.h"
  9. #import "TBKeyPathTokenizer.h"
  10. @interface TBKeyPathToolbar ()
  11. @property (nonatomic, copy) TBToolbarAction tapHandler;
  12. @end
  13. @implementation TBKeyPathToolbar
  14. + (instancetype)toolbarWithHandler:(TBToolbarAction)tapHandler suggestions:(NSArray<NSString *> *)suggestions {
  15. NSArray *buttons = [self
  16. buttonsForKeyPath:TBKeyPath.empty suggestions:suggestions handler:tapHandler
  17. ];
  18. TBKeyPathToolbar *me = [self toolbarWithButtons:buttons];
  19. me.tapHandler = tapHandler;
  20. return me;
  21. }
  22. + (NSArray<TBToolbarButton*> *)buttonsForKeyPath:(TBKeyPath *)keyPath
  23. suggestions:(NSArray<NSString *> *)suggestions
  24. handler:(TBToolbarAction)handler {
  25. NSMutableArray *buttons = [NSMutableArray array];
  26. TBToken *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:[TBToolbarButton buttonWithTitle:@"-" action:handler]];
  40. [buttons addObject:[TBToolbarButton buttonWithTitle:@"+" action:handler]];
  41. }
  42. [buttons addObject:[TBToolbarButton buttonWithTitle:@"*" action:handler]];
  43. } else {
  44. [buttons addObject:[TBToolbarButton buttonWithTitle:@"*" action:handler]];
  45. [buttons addObject:[TBToolbarButton buttonWithTitle:@"*." action:handler]];
  46. }
  47. break;
  48. default: {
  49. if (lastKey.options & TBWildcardOptionsPrefix) {
  50. if (lastKeyIsMethod) {
  51. if (lastKey.string.length) {
  52. [buttons addObject:[TBToolbarButton buttonWithTitle:@"*" action:handler]];
  53. }
  54. } else {
  55. if (lastKey.string.length) {
  56. [buttons addObject:[TBToolbarButton buttonWithTitle:@"*." action:handler]];
  57. }
  58. }
  59. }
  60. else if (lastKey.options & TBWildcardOptionsSuffix) {
  61. if (!lastKeyIsMethod) {
  62. [buttons addObject:[TBToolbarButton buttonWithTitle:@"*" action:handler]];
  63. [buttons addObject:[TBToolbarButton buttonWithTitle:@"*." action:handler]];
  64. }
  65. }
  66. }
  67. }
  68. for (NSString *suggestion in suggestions) {
  69. [buttons addObject:[TBToolbarSuggestedButton buttonWithTitle:suggestion action:handler]];
  70. }
  71. return buttons;
  72. }
  73. - (void)setKeyPath:(TBKeyPath *)keyPath suggestions:(NSArray<NSString *> *)suggestions {
  74. self.buttons = [self.class
  75. buttonsForKeyPath:keyPath suggestions:suggestions handler:self.tapHandler
  76. ];
  77. }
  78. @end