lsif_parser 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: 4f71116bf688f9097e2f5558c7450adfc6d504f3e599936cb8cdbf8ddd7a9f69
4
+ data.tar.gz: 75786976bacdebb1edcdf212769e86658c1b78792ebe19f9b376f03b234f15b5
5
+ SHA512:
6
+ metadata.gz: 40e600e6e4eced8240842366c9249843d83ddb397aca4a8d3d4257fdee47da06641edbf56e38068690db85bfa3a8274a2474c0a84727c5c38a03cf871f665cc7
7
+ data.tar.gz: 03ee795e0476f0292eb500742abf333652fff876f96c8dff2b7a859f006ece09b982b81be454758fcbe2a7ba87532f8ad44611b66989e39b16a22f746be6efd0
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.5
7
+ before_install: gem install bundler -v 1.17.3
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in lsif_parser.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lsif_parser (0.1.0)
5
+ rouge (~> 3.17)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ rake (10.5.0)
12
+ rouge (3.17.0)
13
+ rspec (3.9.0)
14
+ rspec-core (~> 3.9.0)
15
+ rspec-expectations (~> 3.9.0)
16
+ rspec-mocks (~> 3.9.0)
17
+ rspec-core (3.9.1)
18
+ rspec-support (~> 3.9.1)
19
+ rspec-expectations (3.9.1)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.9.0)
22
+ rspec-mocks (3.9.1)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.9.0)
25
+ rspec-support (3.9.2)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.17)
32
+ lsif_parser!
33
+ rake (~> 10.0)
34
+ rspec (~> 3.0)
35
+
36
+ BUNDLED WITH
37
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Igor Drozdov
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,68 @@
1
+ # LsifParser
2
+
3
+ A library for parsing LSIF files using Ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'lsif_parser'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install lsif_parser
20
+
21
+ ## Usage
22
+
23
+ Examples of using a particular class of the library can be found in the tests
24
+ (`spec` folder)
25
+
26
+ The basic usage with default strategy (and the one exposed by the binary)
27
+ processes an LSIF file and creates a folder with JSON files which imitate the
28
+ structure of the original project:
29
+
30
+ Project structure:
31
+
32
+ ```
33
+ app
34
+ controllers
35
+ application_controller.rb
36
+ models
37
+ application.rb
38
+ root.rb
39
+ ```
40
+
41
+ Code navigation data structure:
42
+
43
+ ```
44
+ app
45
+ controllers
46
+ application_controller.rb.json
47
+ models
48
+ application.rb.json
49
+ root.rb.json
50
+ ```
51
+
52
+ ```ruby
53
+ LsifParser::IO.process('dump.lsif', doc_prefix: '/Users/igordrozdov/Documents/Projects/project')
54
+ ```
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and merge requests are welcome on GitLab at https://gitlab.com/igor-drozdov/lsif_parser.
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'lsif_parser'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/lsif_parser ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH << File.join(__dir__, '..', 'lib')
5
+
6
+ require 'lsif_parser'
7
+
8
+ raise 'Please specify file name as the first argument' unless ARGV[0]
9
+ raise 'Please specify doc prefix as the second argument' unless ARGV[1]
10
+
11
+ LsifParser::IO.process(ARGV[0], doc_prefix: ARGV[1])
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lsif_parser/version'
4
+ require 'lsif_parser/io'
5
+ require 'lsif_parser/docs'
6
+ require 'lsif_parser/ranges'
7
+ require 'lsif_parser/hovers'
8
+ require 'lsif_parser/highlight'
9
+ require 'lsif_parser/docs_strategy'
10
+ require 'lsif_parser/ranges_strategy'
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LsifParser
4
+ # Processes and stores documents data and links to their ranges
5
+ class Docs
6
+ PREFIX = 'file://'
7
+
8
+ attr_reader :docs, :doc_ranges, :ranges
9
+
10
+ def initialize(prefix)
11
+ @prefix = prefix.delete_prefix('/')
12
+ @docs = {}
13
+ @doc_ranges = {}
14
+ @ranges = Ranges.new
15
+ end
16
+
17
+ def read(line)
18
+ case line['label']
19
+ when 'document'
20
+ add(line)
21
+ when 'contains'
22
+ add_ranges(line)
23
+ else
24
+ @ranges.read(line)
25
+ end
26
+ end
27
+
28
+ def each
29
+ docs.each do |id, path|
30
+ next if path.start_with?(PREFIX)
31
+
32
+ yield id, path
33
+ end
34
+ end
35
+
36
+ def find(id)
37
+ @docs[id]
38
+ end
39
+
40
+ private
41
+
42
+ def add(line)
43
+ id = line['id']
44
+ url = line['uri']
45
+
46
+ docs[id] =
47
+ url
48
+ .delete_prefix("#{PREFIX}/#{prefix}")
49
+ .delete_prefix('/')
50
+ end
51
+
52
+ def add_ranges(line)
53
+ @doc_ranges[line['outV']] = line['inVs']
54
+ end
55
+
56
+ attr_reader :prefix
57
+ end
58
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'fileutils'
5
+ require 'lsif_parser/ranges_strategy'
6
+
7
+ module LsifParser
8
+ # Creates a folder json files with code navigation data
9
+ # The folder imitates the structure of the original folder
10
+ class DocsStrategy
11
+ def initialize(ranges_strategy: RangesStrategy.new)
12
+ @ranges_strategy = ranges_strategy
13
+ end
14
+
15
+ def process(file_path, docs)
16
+ docs.each do |id, path|
17
+ ranges_for_file = @ranges_strategy.process(docs.doc_ranges[id], docs)
18
+
19
+ _, source_file_name = File.split(file_path)
20
+ file_dir, file_name = File.split(path)
21
+ absolute_dir, = FileUtils.mkdir_p("#{source_file_name}.tmp/#{file_dir}")
22
+
23
+ File.open([absolute_dir, "#{file_name}.json"].join('/'), 'w') do |f|
24
+ f.write(JSON.dump(ranges_for_file))
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rouge'
4
+
5
+ module LsifParser
6
+ # Highlight hovers
7
+ class Highlight
8
+ # HTML formatter for a hover
9
+ class HTMLGitlab < Rouge::Formatters::HTML
10
+ tag 'html_gitlab'
11
+
12
+ def initialize(tag: nil)
13
+ @line_number = 1
14
+ @tag = tag
15
+ end
16
+
17
+ def stream(tokens)
18
+ is_first = true
19
+ token_lines(tokens) do |line|
20
+ yield "\n" unless is_first
21
+ is_first = false
22
+
23
+ yield %(<span id="LC#{@line_number}" class="line" lang="#{@tag}">)
24
+ line.each { |token, value| yield span(token, value.chomp) }
25
+ yield %(</span>)
26
+
27
+ @line_number += 1
28
+ end
29
+ end
30
+ end
31
+
32
+ def self.run(hover)
33
+ lexer = Rouge::Lexer.find_fancy(hover['language'])
34
+ tag = lexer.tag
35
+ tokens = lexer.lex(hover['value'], continue: false)
36
+ HTMLGitlab.format(tokens, tag: tag)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LsifParser
4
+ # Processes and stores hovers data for ranges
5
+ class Hovers
6
+ def initialize
7
+ @data = {}
8
+ @refs = {}
9
+ @hovers = {}
10
+ end
11
+
12
+ def read(line)
13
+ case line['label']
14
+ when 'hoverResult'
15
+ add_data(line)
16
+ when 'textDocument/hover'
17
+ add(line)
18
+ when 'textDocument/references'
19
+ add_refs(line)
20
+ end
21
+ end
22
+
23
+ def for(id)
24
+ ref_id = @refs[id]
25
+ hover_id = @hovers[ref_id]
26
+ @data[hover_id]
27
+ end
28
+
29
+ private
30
+
31
+ def add(line)
32
+ @hovers[line['outV']] = line['inV']
33
+ end
34
+
35
+ def add_data(line)
36
+ @data[line['id']] = line['result']['contents']
37
+ end
38
+
39
+ def add_refs(line)
40
+ @refs[line['inV']] = line['outV']
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'lsif_parser/docs_strategy'
5
+
6
+ module LsifParser
7
+ # Processes an LSIF file
8
+ class IO
9
+ def self.process(path, doc_prefix: '', docs_strategy: DocsStrategy.new)
10
+ docs = Docs.new(doc_prefix)
11
+
12
+ File.open(path, 'r').each_line do |raw_line|
13
+ line = JSON.parse(raw_line)
14
+
15
+ docs.read(line)
16
+ end
17
+
18
+ docs_strategy.process(path, docs)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LsifParser
4
+ # Processes and stores ranges data and links to their hovers and definitions
5
+ class Ranges
6
+ def initialize
7
+ @ranges = {}
8
+ @def_refs = {}
9
+ @hovers = Hovers.new
10
+ end
11
+
12
+ def read(line)
13
+ case line['label']
14
+ when 'range'
15
+ add(line)
16
+ when 'item'
17
+ add_item(line)
18
+ else
19
+ @hovers.read(line)
20
+ end
21
+ end
22
+
23
+ def find(id)
24
+ @ranges[id]
25
+ end
26
+
27
+ def hover_for(id)
28
+ @hovers.for(id)
29
+ end
30
+
31
+ def def_ref_for(id)
32
+ @def_refs[id]
33
+ end
34
+
35
+ private
36
+
37
+ def add(line)
38
+ id = line['id']
39
+ start_data = line['start']
40
+
41
+ @ranges[id] = {
42
+ start_line: start_data['line'],
43
+ start_char: start_data['character']
44
+ }
45
+ end
46
+
47
+ def add_item(line)
48
+ type = line['property']
49
+
50
+ return unless %w[definitions references].include?(type)
51
+
52
+ line['inVs'].each do |range_id|
53
+ @ranges[range_id]&.merge!(ref_id: line['outV'])
54
+ end
55
+
56
+ return unless type == 'definitions'
57
+
58
+ @def_refs[line['outV']] = {
59
+ id: line['inVs'].first,
60
+ doc_id: line['document']
61
+ }
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lsif_parser/highlight'
4
+
5
+ module LsifParser
6
+ # Serializes ranges data with hovers and definition paths
7
+ class RangesStrategy
8
+ def process(ranges, docs)
9
+ ranges&.map do |range_id|
10
+ range = docs.ranges.find(range_id)
11
+ ref_id = range[:ref_id]
12
+
13
+ {
14
+ start_line: range[:start_line],
15
+ start_char: range[:start_char],
16
+ definition_path: def_path_for(docs, ref_id),
17
+ hover: hover_for(docs, ref_id)
18
+ }
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def hover_for(docs, ref_id)
25
+ docs.ranges.hover_for(ref_id)&.map do |hover|
26
+ # Documentation for a method which is added as comments
27
+ # is stored as a raw string value in LSIF file
28
+ next { value: hover } unless hover.is_a?(Hash)
29
+
30
+ { language: hover['language'], value: Highlight.run(hover) }
31
+ end
32
+ end
33
+
34
+ def def_path_for(docs, ref_id)
35
+ def_ref = docs.ranges.def_ref_for(ref_id)
36
+ range = def_ref && docs.ranges.find(def_ref[:id])
37
+
38
+ return unless range
39
+
40
+ "#{docs.find(def_ref[:doc_id])}#L#{range[:start_line] + 1}"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LsifParser
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'lsif_parser/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'lsif_parser'
9
+ spec.version = LsifParser::VERSION
10
+ spec.authors = ['Igor Drozdov']
11
+ spec.email = ['idrozdov@gitlab.com']
12
+
13
+ spec.summary = 'Parse LSIF files using Ruby'
14
+ spec.description = 'Parse LSIF files using Ruby'
15
+ spec.homepage = 'https://gitlab.com/igor.drozdov/lsif_parser'
16
+ spec.license = 'MIT'
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem
20
+ # that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_dependency 'rouge', '~> 3.17'
29
+
30
+ spec.add_development_dependency 'bundler', '~> 1.17'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'rspec', '~> 3.0'
33
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lsif_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Drozdov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rouge
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.17'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.17'
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.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
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
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Parse LSIF files using Ruby
70
+ email:
71
+ - idrozdov@gitlab.com
72
+ executables:
73
+ - lsif_parser
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - exe/lsif_parser
88
+ - lib/lsif_parser.rb
89
+ - lib/lsif_parser/docs.rb
90
+ - lib/lsif_parser/docs_strategy.rb
91
+ - lib/lsif_parser/highlight.rb
92
+ - lib/lsif_parser/hovers.rb
93
+ - lib/lsif_parser/io.rb
94
+ - lib/lsif_parser/ranges.rb
95
+ - lib/lsif_parser/ranges_strategy.rb
96
+ - lib/lsif_parser/version.rb
97
+ - lsif_parser.gemspec
98
+ homepage: https://gitlab.com/igor.drozdov/lsif_parser
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubygems_version: 3.0.3
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Parse LSIF files using Ruby
121
+ test_files: []