Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax: Use 'Character' style names and 'hi def link' commands, to fix issue #64 and issue #124 #150

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
source 'https://rubygems.org'
gem 'vimrunner', '0.3.0'
gem 'rake', '10.0.4'
gem 'rspec', '~> 2.13.0'

gem 'vimrunner', '0.3.4'
gem 'rake', '~> 11.0'
19 changes: 7 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.1.3)
rake (0.9.2.2)
rspec (2.9.0)
rspec-core (~> 2.9.0)
rspec-expectations (~> 2.9.0)
rspec-mocks (~> 2.9.0)
rspec-core (2.9.0)
rspec-expectations (2.9.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.9.0)
rake (11.3.0)
vimrunner (0.3.4)

PLATFORMS
ruby

DEPENDENCIES
rake
rspec
rake (~> 11.0)
vimrunner (= 0.3.4)

BUNDLED WITH
1.16.1
14 changes: 10 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
require 'rspec/core/rake_task'
require 'rake/testtask'

RSpec::Core::RakeTask.new

task :test => :spec
task :default => :spec
task :test => :spec

Rake::TestTask.new(:spec) do |t|
t.libs.push "lib"
t.test_files = Dir.glob('spec/*_spec.rb')
t.verbose = true
t.warning = true
end

4 changes: 4 additions & 0 deletions spec/fixtures/misc/jump_to_matching_curly.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object Main {
foo('}')
foo('{')
}
4 changes: 4 additions & 0 deletions spec/fixtures/misc/jump_to_matching_paren.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object Main {
foo(')')
foo('(')
}
File renamed without changes.
64 changes: 64 additions & 0 deletions spec/go_to_matching_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require_relative './spec_helper'

# This test simulates what happens in plugin/matchparen.vim, to ensure syntax/scala.vim
# uses style names that use the correct magic words.

describe 'default matching behavior' do
it 'jumps over parentheses in single-quotes' do
with_fixture('misc/jump_to_matching_paren.scala') do |file|
start_vim do |vim|
vim.edit file.path
vim.command 'set ft=scala'
vim.normal '2gg'
vim.normal '6|'
line_num, col_num = searchpairpos(vim, '(', '', ')', 'nW')

line_num.must_equal '2'
col_num.must_equal '10'

vim.normal '3gg'
vim.normal '10|'
line_num, col_num = searchpairpos(vim, '(', '', ')', 'bnW')

line_num.must_equal '3'
col_num.must_equal '6'
end
end
end

it 'jumps over curly braces in single-quotes' do
with_fixture('misc/jump_to_matching_curly.scala') do |file|
start_vim do |vim|
vim.edit file.path
vim.command 'set ft=scala'
vim.normal '1gg'
vim.normal '13|'
line_num, col_num = searchpairpos(vim, '{', '', '}', 'nW')

line_num.must_equal '4'
col_num.must_equal '1'

vim.normal '4gg'
vim.normal '1|'
line_num, col_num = searchpairpos(vim, '{', '', '}', 'bnW')

line_num.must_equal '1'
col_num.must_equal '13'
end
end
end
end

def read_pair(str)
str.match(/\[(\d+), (\d+)\]/)[1, 2]
end

def searchpairpos(vim, head, middle, tail, flags)
# distilled from vim80/plugin/matchparen.vim.
expr = <<-EXPR
searchpairpos('#{head}', '#{middle}', '#{tail}', '#{flags}', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\\\|character\\\\|singlequote\\\\|escape\\\\|comment"')
EXPR

read_pair vim.echo expr
end

29 changes: 18 additions & 11 deletions spec/import_sorting_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
require "spec_helper"
require_relative './spec_helper'

describe ":SortScalaImports" do

describe "Sorting across groups" do
["vanilla", "no_newline", "no_newline_after", "no_package",
"multiple_newlines"].each do |name|
it "should sort vanilla file" do
actual = sort_fixture_across_groups name
expected = expected(name)
actual.should eq(expected)
end
describe :SortScalaImports do
each_fixture('sort_imports') do |fixture|
it %'will sort imports in file #{fixture}' do
actual = sort_fixture_across_groups(fixture)
actual.must_equal expected(fixture)
end
end
end

def sort_fixture_across_groups(fixture_name)
with_fixture(fixture_name) do |temp_file|
# A global variable changes in each session,
# so start a new vim each time.
start_vim do |vim|
vim.edit temp_file.path
vim.command 'let g:scala_sort_across_groups=1'
vim.command 'SortScalaImports'
vim.write
end
end
end
80 changes: 55 additions & 25 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,74 @@
require 'vimrunner'
require 'tempfile'
require 'minitest'
require 'minitest/spec'
require 'minitest/autorun'

PWD = File.expand_path File.dirname(__FILE__)
PWD = File.expand_path('..', __FILE__)

RSpec.configure do |config|
def start_vim_without_plugins
Vimrunner.start do |vim|
vim.command 'filetype on'
vim.command 'set verbose=2'

config.before(:suite) do
VIM = Vimrunner.start
VIM.add_plugin(File.expand_path('../..', __FILE__), 'plugin/scala.vim')
end
# Prepend our local path to ensure the built-in scala code is not used.
vim.prepend_runtimepath(File.expand_path('../..', __FILE__))
# puts 'vimrunner runtimepath:', vim.echo('&runtimepath')

plugin_loader = lambda do
vim.command 'runtime! ftdetect/scala.vim'
vim.command 'runtime! ftplugin/scala.vim'
vim.command 'runtime! ftplugin/scala/tagbar.vim'
vim.command 'runtime! indent/scala.vim'
vim.command 'runtime! plugin/scala.vim'
vim.command 'runtime! syntax/scala.vim'
vim.command 'runtime! compiler/scala.vim'
vim.command 'runtime! after/syntax/scala.vim'
end

config.after(:suite) do
VIM.kill
yield vim, plugin_loader
end
end

def sort_fixture_across_groups(name)
fixture_path = "#{PWD}/fixtures/#{name}.scala"
def start_vim
start_vim_without_plugins do |vim, plugin_loader|
plugin_loader.call
yield vim
end
end

temp_file = Tempfile.new('vim-scala-')
temp_file.write File.read(fixture_path)
temp_file.rewind
def with_temp_file(source_filename)
temp_file = Tempfile.new('vim-scala-testing-')
begin
temp_file.write File.read(source_filename)
temp_file.rewind

VIM.edit temp_file.path
yield temp_file

VIM.command "let g:scala_sort_across_groups=1"
VIM.command "SortScalaImports"
VIM.write
temp_file.rewind
temp_file.read
ensure
temp_file.close
temp_file.unlink
end
end

temp_file.rewind
output = temp_file.read
def with_fixture(fixture_name, &block)
with_temp_file fixture_path(fixture_name), &block
end

temp_file.close
temp_file.unlink
def each_fixture(dir)
Dir.each_child(File.join(PWD, 'fixtures', dir)) do |child|
next if child.end_with? '.expected.scala'
yield File.join(dir, child)
end
end

output
def fixture_path(filename)
%'#{PWD}/fixtures/#{filename}'
end

def expected(name)
path = "#{PWD}/fixtures/#{name}.expected.scala"
File.read(path)
def expected(filename)
File.read fixture_path filename.gsub(/scala$/, 'expected.scala')
end

30 changes: 30 additions & 0 deletions spec/syntax_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative './spec_helper'

describe 'syntax/scala.vim' do
it 'allows the user to override scala-specific links' do
start_vim_without_plugins do |vim, plugin_loader|
# Set our custom value, then load the plugins.
# This simulates vim loading .vimrc then later loading the filetype plugin.
vim.command 'hi! CustomUserValue ctermfg=Red guifg=Red'
vim.command 'hi! link scalaKeyword CustomUserValue'
plugin_loader.call

# 'verbose hi scalaKeyword' returns 'scalaKeyword xxx links to NNN'
# That NNN should still be our custom value.
keyword_linked_to = vim.command('verbose hi scalaKeyword').split[4]
keyword_linked_to.must_equal "CustomUserValue"
end
end

it 'allows the user to override scala-specific colors' do
start_vim_without_plugins do |vim, plugin_loader|
# Set our custom value, then load the plugins.
# This simulates vim loading .vimrc then later loading the filetype plugin.
vim.command 'hi! scalaKeyword ctermfg=123 guifg=123'
plugin_loader.call

keyword_fg_color = vim.echo('synIDattr(synIDtrans(hlID("scalaKeyword")), "fg")')
keyword_fg_color.must_equal '123'
end
end
end
Loading