Class: VER::Syntax

Inherits:
Object
  • Object
show all
Defined in:
lib/ver/syntax.rb,
lib/ver/syntax/detector.rb,
lib/ver/syntax/processor.rb

Defined Under Namespace

Modules: Detector Classes: Processor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, theme = nil) ⇒ Syntax

Returns a new instance of Syntax.



34
35
36
37
38
39
40
41
# File 'lib/ver/syntax.rb', line 34

def initialize(name, theme = nil)
  @name = name
  @first_highlight = true
  @old_theme = nil

  @parser = self.class.find_and_load(name)
  @theme  = theme || Theme.find_and_load(VER.options[:theme])
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



31
32
33
# File 'lib/ver/syntax.rb', line 31

def name
  @name
end

#parserObject

Returns the value of attribute parser.



31
32
33
# File 'lib/ver/syntax.rb', line 31

def parser
  @parser
end

#themeObject

Returns the value of attribute theme.



32
33
34
# File 'lib/ver/syntax.rb', line 32

def theme
  @theme
end

Class Method Details

.find(syntax_name) ⇒ Object



18
19
20
# File 'lib/ver/syntax.rb', line 18

def self.find(syntax_name)
  VER.find_in_loadpath("syntax/#{syntax_name}.rb")
end

.find_and_load(syntax_name) ⇒ Object



27
28
29
# File 'lib/ver/syntax.rb', line 27

def self.find_and_load(syntax_name)
  load(find(syntax_name))
end

.from_filename(filename) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/ver/syntax.rb', line 10

def self.from_filename(filename)
  if syntax_name = Detector.detect(filename)
    return new(syntax_name)
  else
    new(VER.options.filetype)
  end
end

.listObject



6
7
8
# File 'lib/ver/syntax.rb', line 6

def self.list
  VER.loadpath.map{|path| Dir[(path/'syntax/*.rb').to_s] }.flatten
end

.load(file) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
# File 'lib/ver/syntax.rb', line 22

def self.load(file)
  raise(ArgumentError, "No path to syntax file given") unless file
  Textpow::SyntaxNode.load(file)
end

Instance Method Details

#check_for_theme_change(widget) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ver/syntax.rb', line 52

def check_for_theme_change(widget)
  if @old_theme
    @old_theme.delete_tags_on(widget)
    @old_theme = nil
  end

  if @first_highlight
    @theme.create_tags_on(widget)
    @theme.apply_default_on(widget)
    @first_highlight = false
  end
end

#highlight(widget, lineno, from, to) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/ver/syntax.rb', line 43

def highlight(widget, lineno, from, to)
  check_for_theme_change(widget)

  processor = Processor.new(widget, @theme, lineno)
  @theme.remove_tags_on(widget, from, to)
  code = widget.get(from, to)
  parser.parse(code, processor)
end