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}.json")
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/*.json').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

#highlight(textarea, code, lineno = nil, from = '1.0', to = 'end') ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ver/syntax.rb', line 43

def highlight(textarea, code, lineno = nil, from = '1.0', to = 'end')
  if @old_theme
    @old_theme.delete_tags_on(textarea)
    @old_theme = nil
  end

  if @first_highlight
    @theme.create_tags_on(textarea)
    @theme.apply_default_on(textarea)
    @first_highlight = false
  else
    @theme.remove_tags_on(textarea, from, to)
  end

  pr = Processor.new(textarea, @theme, lineno || 0)

  if lineno
    pr.start_parsing(parser.scopeName)
    stack = [[parser, nil]]
    parser.parse_line(stack, code, pr)
    pr.end_parsing(parser.scopeName)
  else
    @theme.remove_tags_on(textarea, from, to)
    parser.parse(code, pr)
  end
end