Class: Ruber::SyntaxChecker::SyntaxCheckerExtension

Inherits:
Qt::Object
  • Object
show all
Includes:
Extension
Defined in:
plugins/syntax_checker/syntax_checker.rb

Overview

Document extension which checks the syntax for a document. The syntax check happens:

  • when the document becomes active
  • when the document is saved
  • one second after the last modification (if the user has enabled automatic checks)

The check can only be done if a syntax checker for the document’s mimetype or file extension exists. New syntax checkers must be added to the syntax checker plugin using the Ruber::SyntaxChecker::SyntaxCheckerPlugin#register_syntax_checker method, and can be removed using Ruber::SyntaxChecker::SyntaxCheckerPlugin#remove_syntax_checker.

The appropriate syntax checker for the document is chosen when the extension is added to the document, and is changed (if needed) whenever the document_name of the document changes.

Note: in the documentation of this class, the term document will refer to the Document passed as argument to the constructor.

Instance Attribute Summary (collapse)

Attributes included from Extension

plugin

Instance Method Summary (collapse)

Methods included from Extension

#query_close, #remove_from_project, #save_settings, #shutdown

Signal Summary

Constructor Details

- (SyntaxCheckerExtension) initialize(prj)

Creates a new instance

Parameters:



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'plugins/syntax_checker/syntax_checker.rb', line 341

def initialize prj
  super
  @plugin = Ruber[:syntax_checker]
  @doc = prj.document
  @errors = []
  @checker = nil
  connect @doc, SIGNAL(:activated), self, SLOT(:document_activated)
  connect @doc, SIGNAL(:deactivated), self, SLOT(:document_deactivated)
  @doc.connect(SIGNAL('modified_changed(bool, QObject*)')) do |mod, _|
    check if !mod
  end
  connect @doc, SIGNAL('document_name_changed(QString, QObject*)'), self, SLOT(:create_syntax_checker)
  @doc.connect SIGNAL('text_changed(QObject*)') do
    if @doc.active?
      @plugin.stop_timer
      @plugin.start_timer
    end
  end
  @thread = nil
  create_syntax_checker
end

Instance Attribute Details

- (<SyntaxCheckerPlugin::ErrorDescription>) errors (readonly)

A list of the syntax errors found in the document

The list is empty if the document doesn’t contain syntax errors or if it hasn’t been checked yet

document

Returns:



334
335
336
# File 'plugins/syntax_checker/syntax_checker.rb', line 334

def errors
  @errors
end

Instance Method Details

- (nil) check(async = false)

TODO:

the decision on whether the check should be synchronous or asynchronous

Starts a syntax check for the document

The syntax check can be synchronous or asynchronous, according to the value of the argument. In the first case, this method won’t return until the syntax check has finished and the UI has been updated. In the second case, the method will start the syntax check in a new thread and return immediately. The #check_done signal will be emitted when the syntax check has been finished.

Nothing will be done if no checker exists for the document.

Note: while an asynchronous syntax check avoids freezing the UI if it takes a long time, it seems that it takes much longer than a synchronous check.

should be delegated to the checker

Parameters:

  • async (Boolean) (defaults to: false)

    whether the syntax check should or not be asynchronous

Returns:

  • (nil)


401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'plugins/syntax_checker/syntax_checker.rb', line 401

def check async = false
  return unless @checker
  @plugin.stop_timer
  if async
    @threak.kill if @thread
    @thread = Thread.new(@doc.text) do |str|
      res = @checker.check @doc.text
      emit check_done res
    end
  else
    res = @checker.check @doc.text
    update_ui res
  end
  nil
end

Slot Signature:

check()

- (Object) check_syntax(async = false)



382
383
384
# File 'plugins/syntax_checker/syntax_checker.rb', line 382

def check_syntax async = false
  
end

- (nil) create_syntax_checker (private)

Creates a syntax checker for the document

If needed, it also removes the old one and immediately performs a syntax check

Returns:

  • (nil)


426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'plugins/syntax_checker/syntax_checker.rb', line 426

def create_syntax_checker
  new_checker = Ruber[:syntax_checker].syntax_checker_for @doc
  if @checker.class != new_checker.class
    @checker.disconnect if @checker
    @checker = nil
    if new_checker
      @checker = new_checker
      connect self, SIGNAL('check_done(QString)'), self, SLOT('update_ui(QString)')
    end
    check if @doc.active?
  end
  nil
end

Slot Signature:

create_syntax_checker()

- (nil) document_activated (private)

Informs the extension that the document became active

Returns:

  • (nil)


458
459
460
461
462
# File 'plugins/syntax_checker/syntax_checker.rb', line 458

def document_activated
  connect @plugin, SIGNAL(:timeout), self, SLOT(:check)
  check
  nil
end

Slot Signature:

document_activated()

- (nil) document_deactivated (private)

Informs the extension that the document is not active anymore

Returns:

  • (nil)


445
446
447
448
449
450
451
# File 'plugins/syntax_checker/syntax_checker.rb', line 445

def document_deactivated
  @thread.kill if @thread
  @plugin.stop_timer
  @plugin.disconnect SIGNAL(:timeout), self, SLOT(:check)
  Ruber[:syntax_checker].mark_document_as :unknown
  nil
end

Slot Signature:

document_deactivated()

- (nil) update_ui(str)

Sets the syntax status according to the results of the last syntax check

It marks the current document as having correct or incorrect syntax depending on the contents of str.

the one that a syntax checker’s check method). It is passed to the syntax checker’s convert_check_result method

Parameters:

  • str (String)

    the string containing the results of the syntax check (such as

Returns:

  • (nil)


374
375
376
377
378
379
380
# File 'plugins/syntax_checker/syntax_checker.rb', line 374

def update_ui str
  @errors = @checker.convert_check_result str
  if @errors.empty? then @plugin.mark_document_as :correct
  else @plugin.mark_document_as :error, @errors
  end
  nil
end

Slot Signature:

update_ui(QString)

Signal Details

- check_done(QString result)

Signal emitted after a syntax check has been completed

Parameters:

  • result (String)

    a string containing the results of the syntax check