glimmer-cs-gladiator 0.8.2 → 0.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
@@ -1,30 +1,30 @@
1
- $LOAD_PATH.unshift(File.expand_path('..', __FILE__))
2
-
3
- require 'glimmer-dsl-swt'
4
- require 'filewatcher'
5
- require 'clipboard'
6
- require 'puts_debuggerer'
7
- require 'views/glimmer/gladiator'
8
-
9
- # Custom Composite Initializer (avoid default margins)
10
- Glimmer::SWT::WidgetProxy::DEFAULT_INITIALIZERS['composite'] = lambda do |composite|
11
- if composite.get_layout.nil?
12
- layout = GridLayout.new
13
- composite.layout = layout
14
- end
15
- end
16
-
17
- # Custom LayoutProxy initialize method (avoid default margins)
18
- module Glimmer
19
- module SWT
20
- class LayoutProxy
21
- def initialize(underscored_layout_name, widget_proxy, args)
22
- @underscored_layout_name = underscored_layout_name
23
- @widget_proxy = widget_proxy
24
- args = SWTProxy.constantify_args(args)
25
- @swt_layout = self.class.swt_layout_class_for(underscored_layout_name).new(*args)
26
- @widget_proxy.swt_widget.setLayout(@swt_layout)
27
- end
28
- end
29
- end
30
- end
1
+ $LOAD_PATH.unshift(File.expand_path('..', __FILE__))
2
+
3
+ require 'glimmer-dsl-swt'
4
+ require 'filewatcher'
5
+ require 'clipboard'
6
+ require 'puts_debuggerer'
7
+ require 'views/glimmer/gladiator'
8
+
9
+ # Custom Composite Initializer (avoid default margins)
10
+ Glimmer::SWT::WidgetProxy::DEFAULT_INITIALIZERS['composite'] = lambda do |composite|
11
+ if composite.get_layout.nil?
12
+ layout = GridLayout.new
13
+ composite.layout = layout
14
+ end
15
+ end
16
+
17
+ # Custom LayoutProxy initialize method (avoid default margins)
18
+ module Glimmer
19
+ module SWT
20
+ class LayoutProxy
21
+ def initialize(underscored_layout_name, widget_proxy, args)
22
+ @underscored_layout_name = underscored_layout_name
23
+ @widget_proxy = widget_proxy
24
+ args = SWTProxy.constantify_args(args)
25
+ @swt_layout = self.class.swt_layout_class_for(underscored_layout_name).new(*args)
26
+ @widget_proxy.swt_widget.setLayout(@swt_layout)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,6 +1,6 @@
1
- require_relative '../glimmer-cs-gladiator'
2
-
3
- include Glimmer
4
-
5
- local_dir = ENV['LOCAL_DIR'] || '.'
6
- gladiator(project_dir_path: local_dir).open
1
+ require_relative '../glimmer-cs-gladiator'
2
+
3
+ include Glimmer
4
+
5
+ local_dir = ENV['LOCAL_DIR'] || '.'
6
+ gladiator(project_dir_path: local_dir).open
@@ -6,7 +6,7 @@ module Glimmer
6
6
  include Glimmer
7
7
  include Glimmer::DataBinding::ObservableModel
8
8
 
9
- IGNORE_PATHS = ['.gladiator', '.git', 'coverage', 'packages', 'node_modules', 'tmp', 'vendor', 'pkg', 'dist']
9
+ IGNORE_PATHS = ['.gladiator', '.gladiator-scratchpad', '.git', 'coverage', 'packages', 'node_modules', 'tmp', 'vendor', 'pkg', 'dist']
10
10
 
11
11
  attr_accessor :selected_child, :filter, :children, :filtered_path_options, :filtered_path, :display_path, :ignore_paths
12
12
  attr_reader :name, :parent, :path
@@ -177,7 +177,8 @@ module Glimmer
177
177
  # scratchpad scenario
178
178
  if selected_path.empty? # Scratchpad
179
179
  @selected_child&.write_dirty_content
180
- return (self.selected_child = File.new)
180
+ @scratchpad = (self.selected_child = File.new('', project_dir.path)) if @scratchpad.nil? || @scratchpad.closed?
181
+ return @scratchpad
181
182
  end
182
183
  full_selected_path = selected_path.include?(project_dir.path) ? selected_path : ::File.join(project_dir.path, selected_path)
183
184
  return if ::Dir.exist?(full_selected_path) ||
@@ -145,7 +145,13 @@ module Glimmer
145
145
  end
146
146
 
147
147
  def scratchpad?
148
- path.to_s.empty?
148
+ path.to_s.empty? # TODO consider updating to set the .gladiator-scratchpad path directly
149
+ end
150
+
151
+ def scratchpad_file
152
+ scratchpad_file = ::File.join(project_dir.path, '.gladiator-scratchpad')
153
+ ::File.write(scratchpad_file, content)
154
+ scratchpad_file
149
155
  end
150
156
 
151
157
  def backup_properties
@@ -221,12 +227,17 @@ module Glimmer
221
227
  end
222
228
 
223
229
  def close
230
+ @closed = true
224
231
  stop_filewatcher
225
232
  remove_all_observers
226
233
  initialize(path, project_dir)
227
234
  Command.clear(self)
228
235
  end
229
236
 
237
+ def closed?
238
+ @closed
239
+ end
240
+
230
241
  def read_dirty_content
231
242
  path.empty? ? '' : ::File.read(path)
232
243
  end
@@ -627,13 +638,13 @@ module Glimmer
627
638
 
628
639
  def run
629
640
  if scratchpad?
630
- TOPLEVEL_BINDING.receiver.send(:eval, content)
641
+ load scratchpad_file
631
642
  else
632
643
  write_dirty_content
633
644
  load path
634
645
  end
635
646
  end
636
-
647
+
637
648
  def lines
638
649
  need_padding = dirty_content.to_s.end_with?("\n")
639
650
  splittable_content = need_padding ? "#{dirty_content} " : dirty_content
@@ -120,7 +120,7 @@ module Glimmer
120
120
  }
121
121
  on_swt_keydown { |key_event|
122
122
  focused_gladiator = display.focus_control.shell&.get_data('custom_shell')
123
- focused_gladiator.handle_display_shortcut(key_event) if !focused_gladiator.nil? && key_event.widget.shell == focused_gladiator&.swt_widget
123
+ focused_gladiator.handle_display_shortcut(key_event) if !focused_gladiator.nil? && focused_gladiator.is_a?(Glimmer::Gladiator) && key_event.widget.shell == focused_gladiator&.swt_widget
124
124
  }
125
125
  }
126
126
  end
@@ -200,7 +200,9 @@ module Glimmer
200
200
  on_widget_disposed {
201
201
  project_dir.selected_child&.write_dirty_content
202
202
  tab_item_file = the_tab_item.swt_tab_item.get_data('file')
203
- tab_item_file.close unless [@tab_folder1, @tab_folder2].compact.map(&:items).flatten(1).detect {|ti| ti.get_data('file') == tab_item_file}
203
+ if (@tab_folder1 != @current_tab_folder && !@tab_folder1&.items&.detect {|ti| ti.get_data('file') == tab_item_file}) || (@tab_folder2 != @current_tab_folder && !@tab_folder2&.items&.detect {|ti| ti.get_data('file') == tab_item_file})
204
+ tab_item_file.close
205
+ end
204
206
  }
205
207
  }
206
208
  @current_tab_item.swt_tab_item.setData('file_path', selected_file.path)
@@ -690,7 +692,7 @@ module Glimmer
690
692
  Gladiator.drag = true
691
693
  tab_folder = event.widget.getControl
692
694
  tab_item = tab_folder.getItem(Point.new(event.x, event.y))
693
- event_data = tab_item.getData('file_path')
695
+ event_data = tab_item&.getData('file_path')
694
696
  }
695
697
  on_drag_set_data { |event|
696
698
  event.data = event_data
@@ -1,244 +1,244 @@
1
- # Copyright (c) 2020-2021 Andy Maleh
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining
4
- # a copy of this software and associated documentation files (the
5
- # "Software"), to deal in the Software without restriction, including
6
- # without limitation the rights to use, copy, modify, merge, publish,
7
- # distribute, sublicense, and/or sell copies of the Software, and to
8
- # permit persons to whom the Software is furnished to do so, subject to
9
- # the following conditions:
10
- #
11
- # The above copyright notice and this permission notice shall be
12
- # included in all copies or substantial portions of the Software.
13
- #
14
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
22
- module Glimmer
23
- class Gladiator
24
- class FileExplorerTree
25
- include Glimmer::UI::CustomWidget
26
-
27
- options :gladiator, :foreground_color
28
-
29
- body {
30
- tree {
31
- #visible bind(self, 'project_dir.filter') {|f| !f}
32
- items bind(self, :project_dir), tree_properties(children: :children, text: :name)
33
- foreground foreground_color
34
- drag_source(:drop_copy) {
35
- transfer :text
36
- on_drag_set_data { |event|
37
- Gladiator.drag = true
38
- tree = event.widget.getControl
39
- tree_item = tree.getSelection.first
40
- event.data = tree_item.getData.path
41
- }
42
- }
43
-
44
- menu {
45
- @open_menu_item = menu_item {
46
- text 'Open'
47
- on_widget_selected {
48
- project_dir.selected_child_path = extract_tree_item_path(swt_widget.getSelection.first)
49
- }
50
- }
51
- menu_item(:separator)
52
- menu_item {
53
- text 'Delete'
54
- on_widget_selected {
55
- tree_item = swt_widget.getSelection.first
56
- delete_tree_item(tree_item)
57
- }
58
- }
59
- menu_item {
60
- text 'Refresh'
61
- on_widget_selected {
62
- project_dir.refresh
63
- }
64
- }
65
- menu_item {
66
- text 'Rename'
67
- on_widget_selected {
68
- rename_selected_tree_item
69
- }
70
- }
71
- menu_item {
72
- text 'New Directory'
73
- on_widget_selected {
74
- add_new_directory_to_selected_tree_item
75
- }
76
- }
77
- menu_item {
78
- text 'New File'
79
- on_widget_selected {
80
- add_new_file_to_selected_tree_item
81
- }
82
- }
83
- }
84
-
85
- on_swt_menudetect { |event|
86
- path = extract_tree_item_path(swt_widget.getSelection.first)
87
- @open_menu_item.swt_widget.setEnabled(!::Dir.exist?(path)) if path
88
- }
89
- on_mouse_up {
90
- if Gladiator.drag_and_drop
91
- Gladiator.drag_and_drop = false
92
- else
93
- project_dir.selected_child_path = extract_tree_item_path(swt_widget.getSelection&.first)
94
- gladiator.current_text_editor&.text_widget&.setFocus
95
- end
96
- }
97
- on_key_pressed { |key_event|
98
- if Glimmer::SWT::SWTProxy.include?(key_event.keyCode, :cr)
99
- project_dir.selected_child_path = extract_tree_item_path(swt_widget.getSelection&.first)
100
- gladiator.current_text_editor&.text_widget&.setFocus
101
- end
102
- }
103
- on_paint_control {
104
- root_item = swt_widget.getItems.first
105
- if root_item && !root_item.getExpanded
106
- root_item.setExpanded(true)
107
- end
108
- }
109
-
110
- }
111
- }
112
-
113
- def project_dir
114
- gladiator.project_dir
115
- end
116
-
117
- def current_text_editor
118
- gladiator.current_text_editor
119
- end
120
-
121
- def extract_tree_item_path(tree_item)
122
- return if tree_item.nil?
123
- if tree_item.getParentItem
124
- ::File.join(extract_tree_item_path(tree_item.getParentItem), tree_item.getText)
125
- else
126
- project_dir.path
127
- end
128
- end
129
-
130
- def select_tree_item
131
- return unless project_dir.selected_child&.name
132
- tree_items_to_select = body_root.depth_first_search { |ti| ti.getData.path == project_dir.selected_child.path }
133
- swt_widget.setSelection(tree_items_to_select)
134
- end
135
-
136
- def delete_tree_item(tree_item)
137
- return if tree_item.nil?
138
- file = tree_item.getData
139
- parent_path = ::File.dirname(file.path)
140
- if file.is_a?(Gladiator::Dir)
141
- file_paths = file.all_children.select {|f| f.is_a?(Gladiator::File)}.map(&:path)
142
- file.remove_all_observers
143
- else
144
- file_paths = [file.path]
145
- end
146
- file_paths.each do |file_path|
147
- found_tab_item = gladiator.find_tab_item(file_path)
148
- if found_tab_item
149
- project_dir.selected_child_path_history.delete(found_tab_item.getData('file_path'))
150
- found_tab_item.getData('proxy')&.dispose
151
- end
152
- end
153
- file.delete! # TODO consider supporting command undo/redo
154
- project_dir.refresh(async: false)
155
- parent_tree_item = body_root.depth_first_search {|ti| ti.getData.path == parent_path}.first
156
- swt_widget.showItem(parent_tree_item)
157
- parent_tree_item.setExpanded(true)
158
- rescue => e
159
- puts e.full_message
160
- end
161
-
162
- def add_new_directory_to_selected_tree_item
163
- project_dir.pause_refresh
164
- tree_item = swt_widget.getSelection.first
165
- directory_path = extract_tree_item_path(tree_item)
166
- return if directory_path.nil?
167
- if !::Dir.exist?(directory_path)
168
- tree_item = tree_item.getParentItem
169
- directory_path = ::File.dirname(directory_path)
170
- end
171
- new_directory_path = ::File.expand_path(::File.join(directory_path, 'new_directory'))
172
- FileUtils.mkdir_p(new_directory_path)
173
- project_dir.refresh(async: false, force: true)
174
- new_tree_item = body_root.depth_first_search {|ti| ti.getData.path == new_directory_path}.first
175
- swt_widget.showItem(new_tree_item)
176
- rename_tree_item(new_tree_item)
177
- end
178
-
179
- def add_new_file_to_selected_tree_item
180
- project_dir.pause_refresh
181
- tree_item = swt_widget.getSelection.first
182
- directory_path = extract_tree_item_path(tree_item)
183
- if !::Dir.exist?(directory_path)
184
- tree_item = tree_item.getParentItem
185
- directory_path = ::File.dirname(directory_path)
186
- end
187
- new_file_path = ::File.expand_path(::File.join(directory_path, 'new_file'))
188
- FileUtils.touch(new_file_path)
189
- # TODO look into refreshing only the parent directory to avoid slowdown
190
- project_dir.refresh(async: false, force: true)
191
- new_tree_item = body_root.depth_first_search {|ti| ti.getData.path == new_file_path}.first
192
- swt_widget.showItem(new_tree_item)
193
- rename_tree_item(new_tree_item, true)
194
- end
195
-
196
- def rename_selected_tree_item
197
- project_dir.pause_refresh
198
- tree_item = swt_widget.getSelection.first
199
- rename_tree_item(tree_item)
200
- end
201
-
202
- def rename_tree_item(tree_item, new_file = false)
203
- original_file = tree_item.getData
204
- current_file = project_dir.selected_child_path == original_file.path
205
- found_tab_item = gladiator.find_tab_item(original_file.path)
206
- found_text_editor = found_tab_item&.getData('text_editor')
207
- body_root.edit_tree_item(
208
- tree_item,
209
- after_write: -> (edited_tree_item) {
210
- file = edited_tree_item.getData
211
- file_path = file.path
212
- file.name
213
- if ::File.file?(file_path)
214
- if new_file
215
- project_dir.selected_child_path = file_path
216
- else
217
- found_text_editor&.file = file
218
- found_tab_item&.setData('file', file)
219
- found_tab_item&.setData('file_path', file.path)
220
- found_tab_item&.setText(file.name)
221
- body_root.pack_same_size
222
- if current_file
223
- project_dir.selected_child_path = file_path
224
- else
225
- selected_tab_item&.getData('text_editor')&.text_widget&.setFocus
226
- end
227
- async_exec {
228
- swt_widget.showItem(edited_tree_item)
229
- }
230
- end
231
- end
232
- project_dir.resume_refresh
233
- },
234
- after_cancel: -> {
235
- project_dir.resume_refresh
236
- }
237
- )
238
- end
239
-
240
- end
241
-
242
- end
243
-
244
- end
1
+ # Copyright (c) 2020-2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ module Glimmer
23
+ class Gladiator
24
+ class FileExplorerTree
25
+ include Glimmer::UI::CustomWidget
26
+
27
+ options :gladiator, :foreground_color
28
+
29
+ body {
30
+ tree {
31
+ #visible bind(self, 'project_dir.filter') {|f| !f}
32
+ items bind(self, :project_dir), tree_properties(children: :children, text: :name)
33
+ foreground foreground_color
34
+ drag_source(:drop_copy) {
35
+ transfer :text
36
+ on_drag_set_data { |event|
37
+ Gladiator.drag = true
38
+ tree = event.widget.getControl
39
+ tree_item = tree.getSelection.first
40
+ event.data = tree_item.getData.path
41
+ }
42
+ }
43
+
44
+ menu {
45
+ @open_menu_item = menu_item {
46
+ text 'Open'
47
+ on_widget_selected {
48
+ project_dir.selected_child_path = extract_tree_item_path(swt_widget.getSelection.first)
49
+ }
50
+ }
51
+ menu_item(:separator)
52
+ menu_item {
53
+ text 'Delete'
54
+ on_widget_selected {
55
+ tree_item = swt_widget.getSelection.first
56
+ delete_tree_item(tree_item)
57
+ }
58
+ }
59
+ menu_item {
60
+ text 'Refresh'
61
+ on_widget_selected {
62
+ project_dir.refresh
63
+ }
64
+ }
65
+ menu_item {
66
+ text 'Rename'
67
+ on_widget_selected {
68
+ rename_selected_tree_item
69
+ }
70
+ }
71
+ menu_item {
72
+ text 'New Directory'
73
+ on_widget_selected {
74
+ add_new_directory_to_selected_tree_item
75
+ }
76
+ }
77
+ menu_item {
78
+ text 'New File'
79
+ on_widget_selected {
80
+ add_new_file_to_selected_tree_item
81
+ }
82
+ }
83
+ }
84
+
85
+ on_swt_menudetect { |event|
86
+ path = extract_tree_item_path(swt_widget.getSelection.first)
87
+ @open_menu_item.swt_widget.setEnabled(!::Dir.exist?(path)) if path
88
+ }
89
+ on_mouse_up {
90
+ if Gladiator.drag_and_drop
91
+ Gladiator.drag_and_drop = false
92
+ else
93
+ project_dir.selected_child_path = extract_tree_item_path(swt_widget.getSelection&.first)
94
+ gladiator.current_text_editor&.text_widget&.setFocus
95
+ end
96
+ }
97
+ on_key_pressed { |key_event|
98
+ if Glimmer::SWT::SWTProxy.include?(key_event.keyCode, :cr)
99
+ project_dir.selected_child_path = extract_tree_item_path(swt_widget.getSelection&.first)
100
+ gladiator.current_text_editor&.text_widget&.setFocus
101
+ end
102
+ }
103
+ on_paint_control {
104
+ root_item = swt_widget.getItems.first
105
+ if root_item && !root_item.getExpanded
106
+ root_item.setExpanded(true)
107
+ end
108
+ }
109
+
110
+ }
111
+ }
112
+
113
+ def project_dir
114
+ gladiator.project_dir
115
+ end
116
+
117
+ def current_text_editor
118
+ gladiator.current_text_editor
119
+ end
120
+
121
+ def extract_tree_item_path(tree_item)
122
+ return if tree_item.nil?
123
+ if tree_item.getParentItem
124
+ ::File.join(extract_tree_item_path(tree_item.getParentItem), tree_item.getText)
125
+ else
126
+ project_dir.path
127
+ end
128
+ end
129
+
130
+ def select_tree_item
131
+ return unless project_dir.selected_child&.name
132
+ tree_items_to_select = body_root.depth_first_search { |ti| ti.getData.path == project_dir.selected_child.path }
133
+ swt_widget.setSelection(tree_items_to_select)
134
+ end
135
+
136
+ def delete_tree_item(tree_item)
137
+ return if tree_item.nil?
138
+ file = tree_item.getData
139
+ parent_path = ::File.dirname(file.path)
140
+ if file.is_a?(Gladiator::Dir)
141
+ file_paths = file.all_children.select {|f| f.is_a?(Gladiator::File)}.map(&:path)
142
+ file.remove_all_observers
143
+ else
144
+ file_paths = [file.path]
145
+ end
146
+ file_paths.each do |file_path|
147
+ found_tab_item = gladiator.find_tab_item(file_path)
148
+ if found_tab_item
149
+ project_dir.selected_child_path_history.delete(found_tab_item.getData('file_path'))
150
+ found_tab_item.getData('proxy')&.dispose
151
+ end
152
+ end
153
+ file.delete! # TODO consider supporting command undo/redo
154
+ project_dir.refresh(async: false)
155
+ parent_tree_item = body_root.depth_first_search {|ti| ti.getData.path == parent_path}.first
156
+ swt_widget.showItem(parent_tree_item)
157
+ parent_tree_item.setExpanded(true)
158
+ rescue => e
159
+ puts e.full_message
160
+ end
161
+
162
+ def add_new_directory_to_selected_tree_item
163
+ project_dir.pause_refresh
164
+ tree_item = swt_widget.getSelection.first
165
+ directory_path = extract_tree_item_path(tree_item)
166
+ return if directory_path.nil?
167
+ if !::Dir.exist?(directory_path)
168
+ tree_item = tree_item.getParentItem
169
+ directory_path = ::File.dirname(directory_path)
170
+ end
171
+ new_directory_path = ::File.expand_path(::File.join(directory_path, 'new_directory'))
172
+ FileUtils.mkdir_p(new_directory_path)
173
+ project_dir.refresh(async: false, force: true)
174
+ new_tree_item = body_root.depth_first_search {|ti| ti.getData.path == new_directory_path}.first
175
+ swt_widget.showItem(new_tree_item)
176
+ rename_tree_item(new_tree_item)
177
+ end
178
+
179
+ def add_new_file_to_selected_tree_item
180
+ project_dir.pause_refresh
181
+ tree_item = swt_widget.getSelection.first
182
+ directory_path = extract_tree_item_path(tree_item)
183
+ if !::Dir.exist?(directory_path)
184
+ tree_item = tree_item.getParentItem
185
+ directory_path = ::File.dirname(directory_path)
186
+ end
187
+ new_file_path = ::File.expand_path(::File.join(directory_path, 'new_file'))
188
+ FileUtils.touch(new_file_path)
189
+ # TODO look into refreshing only the parent directory to avoid slowdown
190
+ project_dir.refresh(async: false, force: true)
191
+ new_tree_item = body_root.depth_first_search {|ti| ti.getData.path == new_file_path}.first
192
+ swt_widget.showItem(new_tree_item)
193
+ rename_tree_item(new_tree_item, true)
194
+ end
195
+
196
+ def rename_selected_tree_item
197
+ project_dir.pause_refresh
198
+ tree_item = swt_widget.getSelection.first
199
+ rename_tree_item(tree_item)
200
+ end
201
+
202
+ def rename_tree_item(tree_item, new_file = false)
203
+ original_file = tree_item.getData
204
+ current_file = project_dir.selected_child_path == original_file.path
205
+ found_tab_item = gladiator.find_tab_item(original_file.path)
206
+ found_text_editor = found_tab_item&.getData('text_editor')
207
+ body_root.edit_tree_item(
208
+ tree_item,
209
+ after_write: -> (edited_tree_item) {
210
+ file = edited_tree_item.getData
211
+ file_path = file.path
212
+ file.name
213
+ if ::File.file?(file_path)
214
+ if new_file
215
+ project_dir.selected_child_path = file_path
216
+ else
217
+ found_text_editor&.file = file
218
+ found_tab_item&.setData('file', file)
219
+ found_tab_item&.setData('file_path', file.path)
220
+ found_tab_item&.setText(file.name)
221
+ body_root.pack_same_size
222
+ if current_file
223
+ project_dir.selected_child_path = file_path
224
+ else
225
+ selected_tab_item&.getData('text_editor')&.text_widget&.setFocus
226
+ end
227
+ async_exec {
228
+ swt_widget.showItem(edited_tree_item)
229
+ }
230
+ end
231
+ end
232
+ project_dir.resume_refresh
233
+ },
234
+ after_cancel: -> {
235
+ project_dir.resume_refresh
236
+ }
237
+ )
238
+ end
239
+
240
+ end
241
+
242
+ end
243
+
244
+ end