Module: Ruber::State::EnvironmentState

Included in:
Plugin, ProjectExtension
Defined in:
plugins/state/state.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) restore_environment(env, data)



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'plugins/state/state.rb', line 104

def restore_environment env, data
  unnamed_docs = []
  env.close_editors env.views, false
  data[:tabs].each_with_index do |t, i|
    restore_tab env, t, data[:cursor_positions][i] || [], unnamed_docs
  end
  active_editor = data[:active_view]
  if active_editor
    editor = env.tabs[active_editor[0]].to_a[active_editor[1]]
    env.activate_editor editor
    editor.set_focus if editor
  end
end

- (Pane) restore_tab(env, tab, cursor_positions, unnamed_docs)

Restores a pane

Restoring a pane means creating the editors which were contained in the pane, in the correct disposition.

The contents of the pane are described by the array data, which has the following format:

  • if it has a single element, the corresponding pane contains only a view. If the element is a string, it must represent the URL of the document the view is associated with. If it is a number, it means the view is associated with a document which hasn’t been saved
  • if it has more than one element, it means that the pane contains more than one view. The first element represents the orientation of the pane and can be either Qt::Horizontal or Qt::Vertical. The other elements can be either strings, numbers or arrays. A string or number means, as above, the URL of the document associated with the view or a document which isn’t associated with a file. An array (which should have the same format as this) means a sub pane

Parameters:

  • data (Array)

    the array containing the description of the contents of the pane

  • docs (Array<Document>)

    the document not associated with files to use for the numeric entries of data. A number n in data means to use the entry n in docs

Returns:

  • (Pane)

    the new pane



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'plugins/state/state.rb', line 144

def restore_tab env, tab, cursor_positions, unnamed_docs
  world = Ruber[:world]
  find_first_view = lambda do |array|
    if array.size == 1 then array[0]
    elsif array[1].is_a? Array then find_first_view.call array[1]
    else array[1]
    end
  end
  recreate_pane = lambda do |pn, array|
    orientation = array[0]
    last_view = pn.view
    array.each_with_index do |e, i|
      #the first element of the array is the orientation; the second is
      #the first view, which is already contained in the pane
      next if i < 2 
      view = e.is_a?(Array) ? find_first_view.call(e) : e
      if view.is_a?(String) 
        doc = world.document(KDE::Url.new(view)) || world.new_document
      else doc = unnamed_docs[view] ||= world.new_document
      end
      view = doc.create_view
      pn.split last_view, view, orientation
      last_view = view
      recreate_pane.call view.parent, e if e.is_a? Array
    end
    recreate_pane.call pn.splitter.widget(0), array[1] if array[1].is_a?(Array)
  end
  view = find_first_view.call tab
  if view.is_a?(String) 
    doc = world.document(KDE::Url.new(view))
  else doc = unnamed_docs[view] ||= Ruber[:world].new_document
  end
  view = env.editor_for! doc, :existing => :never, :new => :new_tab
  pane = view.parent
  recreate_pane.call pane, tab
  pane.views.each_with_index do |v, i|
    pos = cursor_positions[i]
    v.go_to *pos if pos
  end
  pane
end

- (Array<Array,Integer,String>) tab_to_tree(pane, docs) (private)

A representation of a pane’s configuration suitable to be written to a configuration object

Parameters:

  • pane (Pane)

    the pane to return the representation for

  • docs (Hash{Document=>Integer})

    a map between documents not associated with files and the number to represent them

Returns:



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'plugins/state/state.rb', line 197

def tab_to_tree pane, docs
  if pane.single_view?
    doc = pane.view.document
    return [doc.has_file? ? doc.url.url : docs[doc]]
  end
  panes = {}
  tab_to_tree_prc = lambda do |pn|
    if pn.single_view?
      doc = pn.view.document
      panes[pn.parent_pane] << (doc.has_file? ? doc.url.url : docs[doc])
    else 
      data = [pn.orientation]
      panes[pn] = data
      panes[pn.parent_pane] << data if pn.parent_pane
    end
  end
  tab_to_tree_prc.call pane
  pane.each_pane :recursive, &tab_to_tree_prc
  panes[pane]
end

- (Hash) tabs_state(env)

The open tabs configuration in a form suitable to be written to a configuration object

Returns:

  • (Hash)

    A hash containing the following keys:

    • :tabs: an array of arrays, with each inner array representing one tab, with the format described in #restore_pane. This value is the one to write under the state/tabs entry in a project or configuration object
    • :cursor_positions:an array of arrays. Each inner array corresponds to a tab and contains the cursor position of each view. Each cursor position is represented as an array with two elements: the first is the line, the second is the column. The order the views are is the same used by Pane#each_view. This value is the one to write under the state/cursor_positions entry in a project or configuration object
    • :active_view: the active view. It is represented by a size 2 array, with the first element being the index of the tab of the active view and the second being the index of the view in the corresponding pane (according to the order used by Pane#each_view). If there’s no active view, this entry is nil. This is the value to write under the state/active_view entry in a project or configuration object


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'plugins/state/state.rb', line 74

def tabs_state env
  res = {}
  doc_map = {}
  doc_idx = 0
  env.documents.each do |doc|
    if !doc.has_file?
      doc_map[doc] = doc_idx
      doc_idx += 1
    end
  end
  tabs = env.tabs
  tabs_tree = []
  cursor_positions = []
  tabs.each do |t|
    tabs_tree << tab_to_tree(t, doc_map)
    cursor_positions << t.map do |v|
      pos = v.cursor_position
      [pos.line, pos.column]
    end
  end
  res[:tabs] = tabs_tree
  res[:cursor_positions] = cursor_positions
  active = env.views[0]
  if active
    active_tab = env.tab(active)
    res[:active_view] = [tabs.index(active_tab), active_tab.to_a.index(active)]
  end
  res
end