|
| 1 | +AbstractProvider = require './AbstractProvider' |
| 2 | + |
| 3 | +SelectionView = require './GetterSetterProvider/SelectionView' |
| 4 | + |
| 5 | +module.exports = |
| 6 | + |
| 7 | +##* |
| 8 | +# Provides getter and setter (accessor and mutator) generation capabilities. |
| 9 | +## |
| 10 | +class GetterSetterProvider extends AbstractProvider |
| 11 | + ###* |
| 12 | + * The view that allows the user to select the properties to generate for. |
| 13 | + ### |
| 14 | + selectionView: null |
| 15 | + |
| 16 | + ###* |
| 17 | + * @inheritdoc |
| 18 | + ### |
| 19 | + activate: (service) -> |
| 20 | + super(service) |
| 21 | + |
| 22 | + @selectionView = new SelectionView(@onConfirm.bind(this), @onCancel.bind(this)) |
| 23 | + |
| 24 | + atom.commands.add 'atom-workspace', "php-integrator-refactoring:generate-getter": => |
| 25 | + @executeCommand(true, false) |
| 26 | + |
| 27 | + atom.commands.add 'atom-workspace', "php-integrator-refactoring:generate-setter": => |
| 28 | + @executeCommand(false, true) |
| 29 | + |
| 30 | + atom.commands.add 'atom-workspace', "php-integrator-refactoring:generate-getter-setter-pair": => |
| 31 | + @executeCommand(true, true) |
| 32 | + |
| 33 | + # TODO: The base menu should always be the same, add to base class. |
| 34 | + # TODO: The menu ordering is not ideal. |
| 35 | + # TODO: Add docblocks everywhere. |
| 36 | + |
| 37 | + atom.menu.add([ |
| 38 | + { |
| 39 | + 'label': 'Packages' |
| 40 | + 'submenu': [ |
| 41 | + { |
| 42 | + 'label': 'PHP Integrator', |
| 43 | + 'submenu': [ |
| 44 | + { |
| 45 | + 'label': 'Refactoring' |
| 46 | + 'submenu': [ |
| 47 | + {'label': 'Generate Getter(s)', 'command': 'php-integrator-refactoring:generate-getter'}, |
| 48 | + {'label': 'Generate Setter(s)', 'command': 'php-integrator-refactoring:generate-setter'}, |
| 49 | + {'label': 'Generate Getter And Setter Pair(s)', 'command': 'php-integrator-refactoring:generate-getter-setter-pair'}, |
| 50 | + ] |
| 51 | + } |
| 52 | + ] |
| 53 | + } |
| 54 | + ] |
| 55 | + } |
| 56 | + ]) |
| 57 | + |
| 58 | + executeCommand: (enableGetterGeneration, enableSetterGeneration) -> |
| 59 | + activeTextEditor = atom.workspace.getActiveTextEditor() |
| 60 | + |
| 61 | + return if not activeTextEditor |
| 62 | + |
| 63 | + @selectionView.storeFocusedElement() |
| 64 | + @selectionView.present() |
| 65 | + |
| 66 | + currentClassName = @service.determineFullClassName(activeTextEditor) |
| 67 | + |
| 68 | + @service.getClassInfo(currentClassName, true).then (classInfo) => |
| 69 | + items = [] |
| 70 | + |
| 71 | + for name, property of classInfo.properties |
| 72 | + isClassType = false |
| 73 | + enablePhp7Features = false |
| 74 | + |
| 75 | + # TODO: Fill in enableTypeHintGeneration based on if it's a class type or not. If enablePhp7Features |
| 76 | + # is true, this can always be true. |
| 77 | + |
| 78 | + items.push({ |
| 79 | + name : name |
| 80 | + type : if property.return.type then property.return.resolvedType else 'mixed' |
| 81 | + needsGetter : enableGetterGeneration |
| 82 | + needsSetter : enableSetterGeneration |
| 83 | + enablePhp7Features : enablePhp7Features |
| 84 | + enableTypeHintGeneration : enablePhp7Features or isClassType # NOTE: Not relevant for getters. |
| 85 | + editor : activeTextEditor |
| 86 | + }) |
| 87 | + |
| 88 | + @selectionView.setItems(items) |
| 89 | + |
| 90 | + # TODO: We should actually be adding an 'unresolved' type. The 'type' is already partially resolved due |
| 91 | + # to the base package's NameResolver (node visitor). |
| 92 | + # TODO: Class properties that already have a getter/setter should be crossed out (strikethrough). |
| 93 | + # TODO: Support multiple items with check marks, like git-plus' "Stage Files" view. |
| 94 | + |
| 95 | + ###* |
| 96 | + * @inheritdoc |
| 97 | + ### |
| 98 | + deactivate: () -> |
| 99 | + @selectionView.destroy() |
| 100 | + |
| 101 | + # TODO: Remove commands and menu items again? |
| 102 | + |
| 103 | + onCancel: () -> |
| 104 | + |
| 105 | + |
| 106 | + onConfirm: (item) -> |
| 107 | + getter = '' |
| 108 | + |
| 109 | + # TODO: Very silly, but Atom won't automatically maintain indentation, so we'll have to fetch the cursor's |
| 110 | + # column and insert that many spaces to every line... |
| 111 | + |
| 112 | + # TODO: Only generate type hints for class properties (optionally add a checkbox to the selection view |
| 113 | + # to specify whether the user wants type hints for basic types as well if he using PHP 7). |
| 114 | + |
| 115 | + if item.needsGetter |
| 116 | + returnTypeDeclaration = '' |
| 117 | + |
| 118 | + if item.enablePhp7Features |
| 119 | + returnTypeDeclaration = ': ' + item.type |
| 120 | + |
| 121 | + getterName = 'get' + item.name.substr(0, 1).toUpperCase() + item.name.substr(1) |
| 122 | + |
| 123 | + getter = """ |
| 124 | + /** |
| 125 | + * Retrieves the currently set #{item.name}. |
| 126 | + * |
| 127 | + * @return #{item.type} |
| 128 | + */ |
| 129 | + public function #{getterName}()#{returnTypeDeclaration} |
| 130 | + { |
| 131 | + return $this->#{item.name}; |
| 132 | + } |
| 133 | + """ |
| 134 | + |
| 135 | + setter = '' |
| 136 | + |
| 137 | + if item.needsSetter |
| 138 | + typePrefix = '' |
| 139 | + |
| 140 | + if item.enableTypeHintGeneration |
| 141 | + typePrefix = item.type + ' ' |
| 142 | + |
| 143 | + returnTypeDeclaration = '' |
| 144 | + |
| 145 | + if item.enablePhp7Features |
| 146 | + returnTypeDeclaration = ': self' |
| 147 | + |
| 148 | + setterName = 'set' + item.name.substr(0, 1).toUpperCase() + item.name.substr(1) |
| 149 | + |
| 150 | + setter = """ |
| 151 | + /** |
| 152 | + * Sets the #{item.name} to use. |
| 153 | + * |
| 154 | + * @param #{item.type} $value |
| 155 | + * |
| 156 | + * @return $this |
| 157 | + */ |
| 158 | + public function #{setterName}(#{typePrefix}$value)#{returnTypeDeclaration} |
| 159 | + { |
| 160 | + $this->#{item.name} = $value; |
| 161 | + return $this; |
| 162 | + } |
| 163 | + """ |
| 164 | + |
| 165 | + item.editor.insertText((getter + "\n\n" + setter).trim()) |
0 commit comments