pronto-clang_format 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: da6b480040bfb881723b002a3c288b16bbff3a65ed6553f24cc7774722fa0f77
4
+ data.tar.gz: 780c8d7b5754c41502acbcd2eda9ec7adabc85fc1fb8958c97934225487d7fd8
5
+ SHA512:
6
+ metadata.gz: 50ac0304347960daf2d12cf5860ea23ff428f592d63f60edd796b12db076c76758d34a0bd50a03210c029337f6acef21943e83b1900d8c32b9c60c085bdc2ab9
7
+ data.tar.gz: 14ba65662a824f97ee0e3150524803d04b5261ad1848dec97b33796d8ff442e1dcecce1858277dd49a0726174c822a67750fcd5f417ad967ce97e98b4a76e5d6
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Michael Jabbour
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ WIP
2
+
3
+ A pronto runner for clang-format.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+ task default: :spec
@@ -0,0 +1,5 @@
1
+ module Pronto
2
+ module ClangFormat
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,63 @@
1
+ require 'open3'
2
+ require 'rexml/document'
3
+ require 'shellwords'
4
+
5
+ # TODO: make sure clang-format returns its output sorted by offset. If this is always true, switch to using .each_with_index.to_a.reverse.bsearch { |n, i| n < clang_offset }
6
+ module Pronto
7
+ module ClangFormat
8
+ class Wrapper
9
+ def initialize
10
+ @clang_format_path = ENV['PRONTO_CLANG_FORMAT_PATH'] || 'clang-format'
11
+ end
12
+
13
+ def run(filepath)
14
+ # TODO: add environment variable for style file
15
+ stdout, stderr, = Open3.capture3("#{@clang_format_path} "\
16
+ "-output-replacements-xml #{filepath}")
17
+ if stderr && !stderr.empty?
18
+ puts "WARN: pronto-clang_format: #{filepath}: #{stderr}"
19
+ end
20
+ return [] if stdout.nil? || stdout == 0
21
+ parse_output(filepath, stdout)
22
+ end
23
+
24
+ def parse_output(filepath, output)
25
+ doc = REXML::Document.new output
26
+ doc.root.elements.map do |element|
27
+ offset = element.attributes['offset'].to_i
28
+ length = element.attributes['length'].to_i
29
+ line_no, column = ln_col_for(offset, filepath)
30
+ {
31
+ offset: offset,
32
+ line_no: line_no, column: column, length: length,
33
+ replacement: element.get_text.value
34
+ }
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ # a function that returns line no and column given byte offset in a file
41
+ def ln_col_for(offset, filepath)
42
+ preceding_newlines = newline_offsets_in(filepath)
43
+ .select { |newline| newline < offset }
44
+ ln = preceding_newlines.length + 1
45
+ line_start_offset = preceding_newlines.last || 0
46
+ col = offset - line_start_offset
47
+ [ln, col]
48
+ end
49
+
50
+ # a memoized function that returns offsets of newline characters
51
+ # in a file. used in ln_col_for
52
+ def newline_offsets_in(filepath)
53
+ @newline_offsets_in ||= Hash.new do |h, key|
54
+ # reading as binary to support CRLF and LF
55
+ text = File.read(key, mode: 'rb')
56
+ h[key] = (0..(text.length - 1))
57
+ .select { |i| text[i] == "\n" }
58
+ end
59
+ @newline_offsets_in[filepath]
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'clang_format/version'
2
+ require_relative 'clang_format_runner'
@@ -0,0 +1,48 @@
1
+ require 'pronto'
2
+ require_relative 'clang_format/wrapper'
3
+
4
+ module Pronto
5
+ class ClangFormatRunner < Runner
6
+ def initialize(_, __ = nil)
7
+ super
8
+ @inspector = ::Pronto::ClangFormat::Wrapper.new
9
+ end
10
+
11
+ def run
12
+ return [] if !@patches || @patches.count.zero?
13
+ @patches
14
+ .select { |p| valid_patch(p) }
15
+ .map { |p| inspect(p) }
16
+ .flatten.compact
17
+ end
18
+
19
+ def valid_patch(patch)
20
+ #TODO: file should be a cpp or c or a header file (make sure we aren't missing anything)
21
+ #TODO: should we consider patches with added lines only?
22
+ true
23
+ end
24
+
25
+ def inspect(patch)
26
+ puts 'inspecting patch'
27
+ filename = patch.new_file_full_path
28
+ offences = @inspector.run(filename)
29
+ offences.map do |offence|
30
+ patch.added_lines
31
+ .select { |line| line.new_lineno == offence[:line_no] }
32
+ .map { |line| new_message(offence, line) }
33
+ end
34
+ end
35
+
36
+ def new_message(offence, line)
37
+ path = line.patch.delta.new_file[:path]
38
+ Message.new(path, line, :warning, msg_for_offence(offence), nil, self.class)
39
+ end
40
+
41
+ #TODO: correct line numbers for indentation errors (clang outputs the replacement to start from the preceding line)
42
+ #TODO: better messages, maybe have categories? (e.g. indentation, missing space, superfluous space, missine newline, superfluous newline, ...)
43
+
44
+ def msg_for_offence(offence)
45
+ "col: #{offence[:column]}, len: #{offence[:length]}, offset: #{offence[:offset]}, replace with: #{offence[:replacement].dump}"
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pronto/clang_format/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'pronto-clang_format'
8
+ spec.version = Pronto::ClangFormat::VERSION
9
+ spec.authors = ['Michael Jabbour']
10
+ spec.email = ['micjabbour@gmail.com']
11
+
12
+ spec.summary = 'Pronto runner for clang-format, a tool to reformat C++'\
13
+ ' sources files according to configurable style guides.'
14
+ spec.homepage = 'https://github.com/micjabbour/pronto-clang_format'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.extra_rdoc_files = ['LICENSE.txt', 'README.md']
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'pronto', '~> 0.9.0'
26
+ spec.add_development_dependency 'bundler', '~> 1.16'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pronto-clang_format
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Jabbour
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pronto
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.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.9.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.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - micjabbour@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files:
61
+ - LICENSE.txt
62
+ - README.md
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/pronto/clang_format.rb
70
+ - lib/pronto/clang_format/version.rb
71
+ - lib/pronto/clang_format/wrapper.rb
72
+ - lib/pronto/clang_format_runner.rb
73
+ - pronto-clang_format.gemspec
74
+ homepage: https://github.com/micjabbour/pronto-clang_format
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.7.4
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Pronto runner for clang-format, a tool to reformat C++ sources files according
98
+ to configurable style guides.
99
+ test_files: []