Class: Redcar::EditViewSWT::Document

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
plugins/edit_view_swt/lib/edit_view_swt/document.rb

Defined Under Namespace

Classes: CaretListener, DocumentListener, Mark, SelectionListener

Constant Summary

Constant Summary

Constants included from Observable

Observable::ASPECTS

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Observable

#add_listener, #notify_listeners, #remove_listener

Constructor Details

- (Document) initialize(model, swt_mate_document)

A new instance of Document



8
9
10
11
12
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 8

def initialize(model, swt_mate_document)
  @model          = model
  @swt_mate_document = swt_mate_document
  @jface_document = swt_mate_document.mateText.get_document
end

Instance Attribute Details

- (Object) jface_document (readonly)

Returns the value of attribute jface_document



6
7
8
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 6

def jface_document
  @jface_document
end

Instance Method Details

- (Object) attach_modification_listeners



29
30
31
32
33
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 29

def attach_modification_listeners
  jface_document.add_document_listener(DocumentListener.new(@model))
  styledText.add_selection_listener(SelectionListener.new(@model))
  styledText.add_caret_listener(CaretListener.new(@model))
end

- (Object) block_selection_mode=(bool)



173
174
175
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 173

def block_selection_mode=(bool)
  styledText.set_block_selection(bool)
end

- (Boolean) block_selection_mode?

Returns:

  • (Boolean)


169
170
171
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 169

def block_selection_mode?
  styledText.get_block_selection
end

- (Object) create_mark(offset, gravity)



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 151

def create_mark(offset, gravity)
  line = line_at_offset(offset)
  line_offset = offset - offset_at_line(line)
  case gravity
  when :left
    category = "lefts"
  when :right
    category = "rights"
  end
  			location = @swt_mate_document.get_text_location(line, line_offset)
				@swt_mate_document.add_text_location(category, location)
				Mark.new(location, category)
end

- (Object) cursor_offset



101
102
103
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 101

def cursor_offset
  styledText.get_caret_offset
end

- (Object) cursor_offset=(offset)



110
111
112
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 110

def cursor_offset=(offset)
  styledText.set_caret_offset(offset)
end

- (Object) delete_mark(mark)



165
166
167
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 165

def delete_mark(mark)
  @swt_mate_document.remove_text_location(mark.category, mark.location)
end

- (Object) get_line(line_ix)



64
65
66
67
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 64

def get_line(line_ix)
  line_info = jface_document.get_line_information(line_ix)
  jface_document.get(line_info.offset, line_info.length)
end

- (Object) get_line_delimiter



51
52
53
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 51

def get_line_delimiter
  styledText.get_line_delimiter
end

- (Object) get_range(start, length)



69
70
71
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 69

def get_range(start, length)
  jface_document.get(start, length)
end

- (Object) length



43
44
45
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 43

def length
  jface_document.length
end

- (Object) line_at_offset(offset)



55
56
57
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 55

def line_at_offset(offset)
  jface_document.get_line_of_offset(offset)
end

- (Object) line_count



47
48
49
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 47

def line_count
  jface_document.get_number_of_lines
end

- (Object) offset_at_line(line_ix)



59
60
61
62
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 59

def offset_at_line(line_ix)
  line_ix = [line_ix, line_count - 1].min
  jface_document.get_line_offset(line_ix)
end

- (Object) replace(offset, length, text)



73
74
75
76
77
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 73

def replace(offset, length, text)
  @model.verify_text(offset, offset+length, text)
  jface_document.replace(offset, length, text)
  @model.modify_text
end

- (Object) right_click(edit_view)



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 14

def right_click(edit_view)
  menu = Menu.new
  Redcar.plugin_manager.objects_implementing(:edit_view_context_menus).each do |object|
    case object.method(:edit_view_context_menus).arity
    when 0
      menu.merge(object.edit_view_context_menus)
    when 1
      menu.merge(object.edit_view_context_menus(edit_view))
    else
      puts("Invalid edit_view_context_menus hook detected in "+object.class.name)
    end
  end
  Application::Dialog.popup_menu(menu, :pointer)
end

- (Object) scope_at(line, line_offset)



181
182
183
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 181

def scope_at(line, line_offset)
  @swt_mate_document.mateText.scope_at(line, line_offset)
end

- (Object) selection_offset



105
106
107
108
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 105

def selection_offset
  range = styledText.get_selection_range
  range.x == cursor_offset ? range.x + range.y : range.x
end

- (Object) selection_range



114
115
116
117
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 114

def selection_range
  range = styledText.get_selection_range
  range.x...(range.x + range.y)
end

- (Object) selection_ranges



119
120
121
122
123
124
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 119

def selection_ranges
  ranges = styledText.get_selection_ranges
  ranges.to_a.each_slice(2).map do |from, length|
    from...(from + length)
  end
end

- (Object) set_selection_range(cursor_offset, selection_offset)



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 126

def set_selection_range(cursor_offset, selection_offset)
  if block_selection_mode?
    start_offset, end_offset = *[cursor_offset, selection_offset].sort
    start_location = styledText.getLocationAtOffset(start_offset)
    end_location   = styledText.getLocationAtOffset(end_offset)
    styledText.set_block_selection_bounds(
      start_location.x,
      start_location.y,
      end_location.x - start_location.x,
      end_location.y - start_location.y + styledText.get_line_height
    )
  else
    styledText.set_selection_range(selection_offset, cursor_offset - selection_offset)
  end
  @model.selection_range_changed(cursor_offset, selection_offset)
end

- (Boolean) single_line?

Returns:

  • (Boolean)


35
36
37
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 35

def single_line?
  @swt_mate_document.mateText.isSingleLine
end

- (Object) styledText



177
178
179
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 177

def styledText
  @swt_mate_document.mateText.getControl
end

- (Object) text=(text)



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 79

def text=(text)
  @model.verify_text(0, length, text)
  top_pixel   = styledText.getTopPixel
  caret       = cursor_offset
  line        = line_at_offset(caret)
  caret       = caret - offset_at_line(line)

  styledText.setRedraw(false)
  styledText.setText(text)

  unless line > line_count - 1
    # The documents new text is still longer than our previous position, restore position
    line_offset = offset_at_line(line)
    styledText.setCaretOffset([line_offset + caret, offset_at_line(line + 1) - 1].min)
    styledText.setTopPixel(top_pixel)
  end

  styledText.setRedraw(true)
  @model.modify_text
  notify_listeners(:set_text)
end

- (Object) to_s



39
40
41
# File 'plugins/edit_view_swt/lib/edit_view_swt/document.rb', line 39

def to_s
  jface_document.get
end