Class: PWKeep::Editor

Inherits:
Object
  • Object
show all
Defined in:
lib/pwkeep/editor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options) ⇒ Editor

Returns a new instance of Editor.



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/pwkeep/editor.rb', line 357

def initialize(data, options)
  @options = options

  content = data
  @options[:language] ||= LanguageSniffer.detect(@file, :content => content).language
  content.tabs_to_spaces! if @options[:convert_tabs]

  # cleanup newline formats
  @newline = content.match("\r\n|\r|\n")
  @newline = (@newline ? @newline[0] : "\n")
  content.gsub!(/\r\n?/,"\n")

  @saved_content = content
  @text_area = Ruco::EditorArea.new(content, @options)
  @history = @text_area.history
  restore_session
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



344
345
346
# File 'lib/pwkeep/editor.rb', line 344

def file
  @file
end

#historyObject (readonly)

Returns the value of attribute history.



346
347
348
# File 'lib/pwkeep/editor.rb', line 346

def history
  @history
end

#saved_contentObject (readonly)

Returns the value of attribute saved_content.



347
348
349
# File 'lib/pwkeep/editor.rb', line 347

def saved_content
  @saved_content
end

Instance Method Details

#contentObject



408
409
410
# File 'lib/pwkeep/editor.rb', line 408

def content
  text_area.content.freeze # no modifications allowed
end

#find(text) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/pwkeep/editor.rb', line 375

def find(text)
  move(:relative, 0,0) # reset selection
  start = text_area.content.index(text, text_area.index_for_position+1)
  return unless start

  # select the found word
  finish = start + text.size
  move(:to_index, finish)
  selecting{ move(:to_index, start) }

  true
end

#modified?Boolean

Returns:

  • (Boolean)


388
389
390
# File 'lib/pwkeep/editor.rb', line 388

def modified?
  @saved_content != text_area.content
end

#saveObject



392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/pwkeep/editor.rb', line 392

def save
  lines = text_area.send(:lines)
  lines.each(&:rstrip!) if @options[:remove_trailing_whitespace_on_save]
  lines << '' if @options[:blank_line_before_eof_on_save] and lines.last.to_s !~ /^\s*$/
  content = lines * @newline

  @saved_content = content.gsub(/\r?\n/, "\n")

  true
rescue Object => e
  e.message
end

#store_sessionObject



405
406
# File 'lib/pwkeep/editor.rb', line 405

def store_session
end