torkify-vim 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: adeb531f05cf1bddee1aef705eab74564bf1176d
4
+ data.tar.gz: 703b246f8ae037b41c3d7973773618a4883d6cfa
5
+ SHA512:
6
+ metadata.gz: af086f8ac5f1378ff0471d980438c6f9ec3890b91067bf6f6d4793cffd9f8f7e30f347fe4b6296c3cfc7b45eff7fa242ff49e29c69798112a5641748f88f04b2
7
+ data.tar.gz: be4caa2e9178d63dcb5a0b69cae3fee9c72746978f5d8b05409646bcb4acd1c00e7e14247036c573dddb6e2e41fd4a9ebddb99ec2a96ab0ad87f410413d6fa37
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in torkify-vim.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jon Cairns
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Torkify::Vim
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'torkify-vim'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install torkify-vim
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ require "torkify/vim/version"
2
+ require "torkify/vim/observer"
@@ -0,0 +1,23 @@
1
+ require 'torkify'
2
+ require 'torkify/log/test_error'
3
+
4
+ module Torkify::Vim
5
+ class ErrorSplitter
6
+ def self.delimiter
7
+ "================"
8
+ end
9
+
10
+ def call(error)
11
+ split_text = error.text.split("\n")
12
+ split_text.map { |text|
13
+ Torkify::Log::TestError.new(error.filename,
14
+ error.lnum,
15
+ text,
16
+ error.type)
17
+ } << Torkify::Log::TestError.new(error.filename,
18
+ error.lnum,
19
+ self.class.delimiter,
20
+ error.type)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,78 @@
1
+ require_relative "quickfix"
2
+ require_relative "remote"
3
+ require_relative "error_splitter"
4
+
5
+ module Torkify::Vim
6
+ class Observer
7
+ attr_reader :quickfix, :split_errors, :split_error_threshold, :me
8
+
9
+ def initialize(options = {})
10
+ @vimserver = options[:server]
11
+ @me = "vim"
12
+
13
+ @split_errors = !!options[:split_errors]
14
+ @split_error_threshold = options.fetch(:split_error_threshold, 30).to_i
15
+
16
+ detect_server! true
17
+ end
18
+
19
+ def detect_server!(first_time = false)
20
+ @quickfix ||= begin
21
+ vim = if @vimserver
22
+ Remote.new(@vimserver)
23
+ else
24
+ Remote.from_first_server
25
+ end
26
+ Quickfix::API.new(vim)
27
+ rescue RemoteError => e
28
+ Torkify.logger.error { "[#{me}] #{e}" } if first_time
29
+ nil
30
+ end
31
+ end
32
+
33
+ def on_idle(event)
34
+ detect_server!
35
+ self
36
+ end
37
+
38
+ def on_absorb(event)
39
+ quickfix.clear if quickfix
40
+ self
41
+ end
42
+
43
+ def on_pass(event)
44
+ on_pass_or_fail(event)
45
+ self
46
+ end
47
+
48
+ def on_fail(event)
49
+ on_pass_or_fail(event)
50
+ self
51
+ end
52
+
53
+ def on_pass_or_fail(event)
54
+ return unless quickfix
55
+ populator = Quickfix::Populator.new(quickfix)
56
+ populator.exclude File.basename(event.log_file.chomp('.log'))
57
+
58
+ errors = errors_from_event(event)
59
+ Torkify.logger.debug { "[#{me}] number of errors: #{errors.length}" }
60
+
61
+ populator.populate errors
62
+ quickfix.open if populator.errors_populated > 0
63
+ self
64
+ end
65
+
66
+ def errors_from_event(event)
67
+ if split_errors && event.errors.length < split_error_threshold
68
+ Torkify.logger.debug { "[#{me}] number of errors before splitting: #{event.errors.length}" }
69
+ splitter = ErrorSplitter.new
70
+ event.errors.each_with_object([]) { |error, split|
71
+ split.concat(splitter.call(error))
72
+ }
73
+ else
74
+ event.errors
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,131 @@
1
+ require "json"
2
+
3
+ module Torkify::Vim::Quickfix
4
+ class API
5
+ def initialize(vim)
6
+ @stringifier = Stringifier.new
7
+ @vim = vim
8
+ end
9
+
10
+ def get
11
+ existing = "[" + @vim.expr('getqflist()').gsub(/(?<=})\n(?={)/, ",") + "]"
12
+ qflist = existing.gsub(/(?<=[^\\])"/, '\"')
13
+ .gsub("'", '"')
14
+ .gsub("\n", '\n')
15
+ if qflist.length > 0
16
+ JSON.parse(qflist)
17
+ else
18
+ []
19
+ end
20
+ rescue => e
21
+ Torkify.logger.fatal { "Couldn't retrieve vim quickfix list: {#{e.class.name}} #{e.message}" }
22
+ []
23
+ end
24
+
25
+ def buffer_from_file(file)
26
+ @vim.expr("bufnr(\"#{file}\")").to_i
27
+ end
28
+
29
+ def set(errors)
30
+ error_strings = errors.map { |e| @stringifier.convert e }
31
+ if error_strings.any?
32
+ error_strings.each_slice(100).each_with_index do |strings, i|
33
+ action = i == 0 ? '' : ', "a"'
34
+ @vim.expr("setqflist([#{strings.join(",")}]#{action})")
35
+ end
36
+ else
37
+ clear
38
+ end
39
+ end
40
+
41
+ def clear
42
+ @vim.expr("setqflist([])")
43
+ @vim.send ':cclose<CR>'
44
+ self
45
+ end
46
+
47
+ def open
48
+ if @vim.expr("mode()") == "n"
49
+ @vim.send ':cw<CR>'
50
+ end
51
+ end
52
+ end
53
+
54
+ class Populator
55
+ attr_reader :errors_populated
56
+
57
+ def initialize(api)
58
+ @api = api
59
+ @excluded_buffers = []
60
+ @errors_populated = 0
61
+ end
62
+
63
+ def exclude(file)
64
+ if file && file.length > 0
65
+ bufnum = @api.buffer_from_file(file)
66
+ if bufnum > 0 && !@excluded_buffers.include?(bufnum)
67
+ @excluded_buffers << bufnum
68
+ end
69
+ end
70
+ self
71
+ end
72
+
73
+ def populate(errors)
74
+ determine_excluded_buffers errors
75
+ existing = api.get
76
+
77
+ kept_errors = exclude_errors existing
78
+ all_errors = kept_errors + errors
79
+ @errors_populated = all_errors.length
80
+
81
+ if error_list_changed?(existing, all_errors)
82
+ api.set kept_errors + errors
83
+ end
84
+ self
85
+ end
86
+
87
+ protected
88
+ attr_reader :api, :excluded_buffers
89
+
90
+ def error_list_changed?(existing, new)
91
+ existing_msgs = existing.map { |e| e['text'] }.sort
92
+ new_msgs = new.map { |e| e['text'] }.sort
93
+ existing_msgs != new_msgs
94
+ end
95
+
96
+ def determine_excluded_buffers(errors)
97
+ unique_file_errors = errors.uniq { |e| e['filename'] }
98
+ unique_file_errors.each { |e| exclude e['filename'] }
99
+ end
100
+
101
+ def exclude_errors(errors)
102
+ if errors && errors.any?
103
+ errors.dup.keep_if { |e|
104
+ e['type'] == 'E' && !excluded_buffers.include?(e['bufnr'].to_i)
105
+ }
106
+ else
107
+ []
108
+ end
109
+ end
110
+ end
111
+
112
+ class Stringifier
113
+ def convert(enumerable)
114
+ pairs = []
115
+ enumerable.each_pair do |n, v|
116
+ pairs << quote_pair(n, v)
117
+ end
118
+ "{#{pairs.join(",")}}"
119
+ end
120
+
121
+ protected
122
+ def quote_pair(name, value)
123
+ "\"#{name}\":\"#{quote value}\""
124
+ end
125
+
126
+ def quote(string)
127
+ string.to_s.gsub(/['"\\\x0]/,'\\\\\0')
128
+ end
129
+ end
130
+ end
131
+
@@ -0,0 +1,58 @@
1
+ require 'shellwords'
2
+
3
+ module Torkify::Vim
4
+ class RemoteError < StandardError
5
+ end
6
+
7
+ class Remote
8
+ def self.servers
9
+ `vim --serverlist`.split("\n")
10
+ end
11
+
12
+ def self.from_first_server
13
+ servers = self.servers
14
+ if servers.any?
15
+ new(self.servers.first)
16
+ else
17
+ raise RemoteError, "No vim servers are available"
18
+ end
19
+ end
20
+
21
+ def initialize(servername)
22
+ Torkify.logger.debug { "[vim] using remote vim server #{servername}" }
23
+ @servername = servername
24
+ ping
25
+ end
26
+
27
+ def send(keys)
28
+ exec('send', keys)
29
+ end
30
+
31
+ def expr(expression)
32
+ exec('expr', expression)
33
+ end
34
+
35
+ protected
36
+ def exec(remote_command, argument)
37
+ cmd = "vim --servername #{@servername}"
38
+ cmd << " --remote-#{remote_command}"
39
+ cmd << " #{Shellwords.escape(argument)} 2>&1"
40
+ Torkify.logger.debug { "[vim] sending remote command: #{cmd}" }
41
+ parse_output(`#{cmd}`)
42
+ end
43
+
44
+ def parse_output(output)
45
+ output = output.strip
46
+ Torkify.logger.debug { "[vim] output from command: #{output}" }
47
+ if output =~ /^E\d+:/
48
+ raise RemoteError, output
49
+ else
50
+ output
51
+ end
52
+ end
53
+
54
+ def ping
55
+ expr("winnr()")
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ module Torkify
2
+ module Vim
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'torkify/vim/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "torkify-vim"
8
+ spec.version = Torkify::Vim::VERSION
9
+ spec.authors = ["Jon Cairns"]
10
+ spec.email = ["jon@ggapps.co.uk"]
11
+ spec.description = %q{Vim quickfix integration with torkify}
12
+ spec.summary = %q{Vim quickfix integration with torkify}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "json"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: torkify-vim
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jon Cairns
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Vim quickfix integration with torkify
70
+ email:
71
+ - jon@ggapps.co.uk
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/torkify/vim.rb
82
+ - lib/torkify/vim/error_splitter.rb
83
+ - lib/torkify/vim/observer.rb
84
+ - lib/torkify/vim/quickfix.rb
85
+ - lib/torkify/vim/remote.rb
86
+ - lib/torkify/vim/version.rb
87
+ - torkify-vim.gemspec
88
+ homepage: ''
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Vim quickfix integration with torkify
112
+ test_files: []