Skip to content

Commit

Permalink
Use rewritten VimL Rake task
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrmont committed Apr 23, 2020
1 parent 02b24b0 commit 0219b49
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 135 deletions.
42 changes: 5 additions & 37 deletions lib/rouge/lexers/viml.rb
Expand Up @@ -49,11 +49,13 @@ def self.keywords
name = m[0]
keywords = self.class.keywords

if mapping_contains?(keywords[:command], name)
if keywords[:command].include? name
token Keyword
elsif mapping_contains?(keywords[:option], name)
elsif keywords[:function].include? name
token Name::Builtin
elsif mapping_contains?(keywords[:auto], name)
elsif keywords[:option].include? name
token Name::Builtin
elsif keywords[:auto].include? name
token Name::Builtin
else
token Text
Expand All @@ -63,40 +65,6 @@ def self.keywords
# no errors in VimL!
rule %r/./m, Text
end

def mapping_contains?(mapping, word)
shortest, longest = find_likely_mapping(mapping, word)

shortest and word.start_with?(shortest) and
longest and longest.start_with?(word)
end

# binary search through the mappings to find the one that's likely
# to actually work.
def find_likely_mapping(mapping, word)
min = 0
max = mapping.size

until max == min
mid = (max + min) / 2

cmp, _ = mapping[mid]

case word <=> cmp
when 1
# too low
min = mid + 1
when -1
# too high
max = mid
when 0
# just right, abort!
return mapping[mid]
end
end

mapping[max - 1]
end
end
end
end
15 changes: 11 additions & 4 deletions lib/rouge/lexers/viml/keywords.rb

Large diffs are not rendered by default.

22 changes: 0 additions & 22 deletions spec/lexers/viml_spec.rb
Expand Up @@ -4,28 +4,6 @@
describe Rouge::Lexers::VimL do
let(:subject) { Rouge::Lexers::VimL.new }

describe 'mapping binary search' do
let(:mapping) {
[['aa', 'aaa'],
['bb', 'bb'],
['cc', 'ccc']]
}

it 'finds a likely mapping' do
likely = subject.find_likely_mapping(mapping, 'aa')
assert { likely == ['aa', 'aaa'] }

likely = subject.find_likely_mapping(mapping, 'abc')
assert { likely == ['aa', 'aaa'] }

likely = subject.find_likely_mapping(mapping, 'bbbbb')
assert { likely == ['bb', 'bb'] }

likely = subject.find_likely_mapping(mapping, 'z')
assert { likely == ['cc', 'ccc'] }
end
end

describe 'guessing' do
include Support::Guessing

Expand Down
7 changes: 7 additions & 0 deletions spec/visual/samples/viml
Expand Up @@ -565,3 +565,10 @@ function! phpcomplete#CompletePHP(findstart, base)

endfunction
" vim:set foldmethod=marker:

if &term =~ '256color'
" Disable Background Color Erase (BCE) so that color schemes
" work properly when Vim is used inside tmux and GNU screen.
" See also http://snk.tuxfamily.org/log/vim-256color-bce.html
set t_ut=
endif
72 changes: 0 additions & 72 deletions tasks/builtins/vim.rake

This file was deleted.

86 changes: 86 additions & 0 deletions tasks/builtins/viml.rake
@@ -0,0 +1,86 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

require 'open-uri'

VIML_SYNTAX_URI = "https://raw.githubusercontent.com/vim/vim/master/runtime/syntax/vim.vim"
VIML_KEYWORDS_FILE = "./lib/rouge/lexers/viml/keywords.rb"

namespace :builtins do
task :viml do
generator = Rouge::Tasks::Builtins::VimL.new

input = URI.open(VIML_SYNTAX_URI) { |f| f.read }
keywords = generator.extract_keywords(input)

output = generator.render_output(keywords)

File.write(VIML_KEYWORDS_FILE, output)
end
end

module Rouge
module Tasks
module Builtins
class VimL
def extract_keywords(input)
keywords = Hash.new { |h,k| h[k] = Set.new }

input.each_line do |line|
line =~ %r(^
(?: syn[ ]keyword[ ](vim(?:AutoEvent|Command|FuncName|Option))[ ]contained )
(.*)
$)x

next unless $1

name = $1
functions = $2

key = case name
when "vimAutoEvent"
:auto
when "vimCommand"
:command
when "vimFuncName"
:function
when "vimOption"
:option
end

functions.scan(/([\w:]+)(?:\[(\w+)\])?/) do
keywords[key].merge [$1, "#{$1}#{$2}"]
end
end

keywords
end

def render_output(keywords, &b)
return enum_for(:render_output, keywords).to_a.join("\n") unless b

yield "# -*- coding: utf-8 -*- #"
yield "# frozen_string_literal: true"
yield ""
yield "# DO NOT EDIT"
yield "# This file is automatically generated by `rake builtins:viml`."
yield "# See tasks/builtins/viml.rake for more info."
yield ""
yield "module Rouge"
yield " module Lexers"
yield " class VimL"
yield " def self.keywords"
yield " @keywords ||= {}.tap do |kw|"
keywords.each do |k,v|
yield " kw[#{k.inspect}] = Set.new #{v.to_a.inspect}"
end
yield " end"
yield " end"
yield " end"
yield " end"
yield "end"
end
end
end
end
end

0 comments on commit 0219b49

Please sign in to comment.