Skip to content

Commit

Permalink
renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Sep 19, 2011
1 parent b0d4bca commit 79db2d7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
44 changes: 22 additions & 22 deletions lib/ruco/editor/colors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def style_map
map = super
return map if @colors_took_too_long

# add colors to style map, disable colors if syntax-parsing takes too long
# disable colors if syntax-parsing takes too long
begin
styles = Timeout.timeout(STYLING_TIMEOUT) do
styled_lines[@window.visible_lines]
syntax = Timeout.timeout(STYLING_TIMEOUT) do
syntax_info[@window.visible_lines]
end
rescue Timeout::Error
# this takes too long, just go on without styles
Expand All @@ -24,7 +24,7 @@ def style_map
return map
end

colorize(map, styles)
add_syntax_highlighting_to_style_map(map, syntax)

if @selection
# add selection a second time so it stays on top
Expand All @@ -35,52 +35,52 @@ def style_map

private

def styled_lines
def syntax_info
# initially color everything
@@styled_lines ||= parse_lines
@@syntax_info ||= syntax_for_lines
@@last_recoloring ||= Time.now.to_f

current_time = Time.now.to_f
if @@last_recoloring + RECOLORING_TIMEOUT < current_time
# re-color everything max every 2 seconds
@@styled_lines = parse_lines
@@syntax_info = syntax_for_lines
@@last_recoloring = Time.now.to_f
else
# re-color the current + 2 surrounding lines (in case of line changes)
recolor = [line - INSTANT_RECOLORING_RANGE, 0].max..(line + INSTANT_RECOLORING_RANGE)
parsed = parse_lines(recolor)
recolor.to_a.size.times{|i| parsed[i] ||= [] } # for empty lines [] => [[],[],[]]
@@styled_lines[recolor] = parsed
lines_to_recolor = [line - INSTANT_RECOLORING_RANGE, 0].max..(line + INSTANT_RECOLORING_RANGE)
parsed = syntax_for_lines(lines_to_recolor)
lines_to_recolor.to_a.size.times{|i| parsed[i] ||= [] } # for empty lines [] => [[],[],[]]
@@syntax_info[lines_to_recolor] = parsed
end

@@styled_lines
@@syntax_info
end

def parse_lines(range=nil)
def syntax_for_lines(range=nil)
if language = @options[:language]
parsed_lines = (range ? lines[range] : lines)
SyntaxParser.parse_lines(parsed_lines, [language.name.downcase, language.lexer])
lines_to_parse = (range ? lines[range] : lines)
SyntaxParser.syntax_for_lines(lines_to_parse, [language.name.downcase, language.lexer])
else
[]
end
end

def colorize(map, styled_lines)
return unless styled_lines
def add_syntax_highlighting_to_style_map(map, syntax_info)
return unless syntax_info

styled_lines.each_with_index do |style_positions, line|
next unless style_positions
style_positions.each do |syntax_element, columns|
syntax_info.each_with_index do |syntax_positions, line|
next unless syntax_positions
syntax_positions.each do |syntax_element, columns|
columns = columns.move(-@window.left)
style = style_for_element(syntax_element)
style = style_for_syntax_element(syntax_element)
if style and columns.first >= 0
map.add(style, line, columns)
end
end
end
end

def style_for_element(syntax_element)
def style_for_syntax_element(syntax_element)
@theme ||= Ruco::TMTheme.new(theme_file)
@style_for_element ||= {}
@style_for_element[syntax_element] ||= begin
Expand Down
2 changes: 1 addition & 1 deletion lib/ruco/syntax_parser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Ruco
module SyntaxParser
def self.parse_lines(lines, languages)
def self.syntax_for_lines(lines, languages)
syntax = nil
languages.detect{|l| syntax = syntax_node(l) }

Expand Down
10 changes: 5 additions & 5 deletions playground/benchmark_syntax_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
require 'ruco/syntax_parser'
require 'ruco/array_processor'
t = Time.now.to_f
Ruco::SyntaxParser.parse_lines(text.split("\n"), [language.name.downcase, language.lexer])
Ruco::SyntaxParser.parse_lines(text.split("\n"), [language.name.downcase, language.lexer])
Ruco::SyntaxParser.parse_lines(text.split("\n"), [language.name.downcase, language.lexer])
Ruco::SyntaxParser.parse_lines(text.split("\n"), [language.name.downcase, language.lexer])
Ruco::SyntaxParser.parse_lines(text.split("\n"), [language.name.downcase, language.lexer])
Ruco::SyntaxParser.syntax_for_lines(text.split("\n"), [language.name.downcase, language.lexer])
Ruco::SyntaxParser.syntax_for_lines(text.split("\n"), [language.name.downcase, language.lexer])
Ruco::SyntaxParser.syntax_for_lines(text.split("\n"), [language.name.downcase, language.lexer])
Ruco::SyntaxParser.syntax_for_lines(text.split("\n"), [language.name.downcase, language.lexer])
Ruco::SyntaxParser.syntax_for_lines(text.split("\n"), [language.name.downcase, language.lexer])
puts (Time.now.to_f - t)
2 changes: 1 addition & 1 deletion spec/ruco/syntax_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Ruco::SyntaxParser do
def parse(text, languages=['ruby'])
Ruco::SyntaxParser.parse_lines(text, languages)
Ruco::SyntaxParser.syntax_for_lines(text, languages)
end

describe :parse_lines do
Expand Down

0 comments on commit 79db2d7

Please sign in to comment.