starscope 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,8 +1,19 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
- v0.0.4 (trunk)
5
- --------------
4
+ v0.0.5 (trunk)
5
+ -------------------
6
+
7
+ New Features:
8
+ * Export to ctags files
9
+
10
+ Improvements:
11
+ * GNU Readline behaviour in line-mode
12
+ * Additional commands available in line-mode: !dump, !help, !version
13
+ * Prints the relevant line in query mode
14
+
15
+ v0.0.4 (2013-06-08)
16
+ -------------------
6
17
 
7
18
  New Features:
8
19
  * Line-mode (use the `-l` flag)
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- starscope (0.0.4)
4
+ starscope (0.0.5)
5
5
  parser (~> 1.4)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -25,4 +25,9 @@ Ask it things with `-q`
25
25
  $ starscope.rb -q calls,new # Lists all callers of new
26
26
  ```
27
27
 
28
+ Export it for use with your editor
29
+ ```
30
+ $ starscope.rb -e ctags
31
+ ```
32
+
28
33
  [1] http://cscope.sourceforge.net/
data/bin/starscope.rb CHANGED
@@ -4,7 +4,8 @@ lib = File.expand_path('../../lib', __FILE__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  require 'optparse'
7
- require "starscope"
7
+ require 'readline'
8
+ require 'starscope'
8
9
 
9
10
  options = {auto: true}
10
11
  DEFAULT_DB=".starscope.db"
@@ -35,6 +36,9 @@ END
35
36
  end
36
37
 
37
38
  opts.separator "\nDatabase Management"
39
+ opts.on("-e", "--export FORMAT[,PATH]", "Export in FORMAT to PATH, see EXPORTING below") do |export|
40
+ options[:export] = export
41
+ end
38
42
  opts.on("-n", "--no-auto", "Don't automatically update/create the database") do
39
43
  options[:auto] = false
40
44
  end
@@ -51,6 +55,12 @@ END
51
55
  exit
52
56
  end
53
57
 
58
+ opts.separator <<END
59
+ \nEXPORTING
60
+ At the moment only one export format is supported: 'ctags'. If you don't
61
+ specify a path, the file is written to 'tags' in the current directory.
62
+ END
63
+
54
64
  end.parse!
55
65
 
56
66
  def print_summary(db)
@@ -59,15 +69,28 @@ def print_summary(db)
59
69
  end
60
70
  end
61
71
 
62
- def run_query(db, input, separator)
63
- table, value = input.split(separator, 2)
72
+ def run_query(db, table, value)
64
73
  if not value
65
- $stderr.puts "Invalid query - did you separate your table and query with '#{separator}'?"
74
+ $stderr.puts "Invalid input - no query found."
66
75
  return
67
76
  end
68
77
  puts db.query(table.to_sym, value)
78
+ return true
69
79
  rescue StarScope::DB::NoTableError
70
80
  $stderr.puts "Table '#{table}' doesn't exist."
81
+ return false
82
+ end
83
+
84
+ def dump(db, table)
85
+ if table
86
+ db.dump_table(table.to_sym)
87
+ else
88
+ db.dump_all
89
+ end
90
+ return true
91
+ rescue StarScope::DB::NoTableError
92
+ $stderr.puts "Table '#{table}' doesn't exist."
93
+ return false
71
94
  end
72
95
 
73
96
  if options[:auto] and not options[:write]
@@ -93,42 +116,70 @@ db.update if options[:read] and options[:auto]
93
116
 
94
117
  db.save(options[:write]) if options[:write]
95
118
 
96
- run_query(db, options[:query], ',') if options[:query]
119
+ if options[:export]
120
+ format, path = options[:export].split(',', 2)
121
+ case format
122
+ when 'ctags'
123
+ db.export_ctags(path || 'tags')
124
+ else
125
+ puts "Unrecognized export format"
126
+ end
127
+ end
128
+
129
+ if options[:query]
130
+ table, query = options[:query].split(',', 2)
131
+ run_query(db, table, query)
132
+ end
97
133
 
98
134
  print_summary(db) if options[:summary]
99
135
 
100
136
  if options[:dump]
101
137
  if options[:dump].is_a? String
102
- db.dump_table(options[:dump].to_sym)
138
+ dump(db, options[:dump])
103
139
  else
104
- db.dump_all
140
+ dump(db, nil)
105
141
  end
106
142
  end
107
143
 
108
- if options[:linemode]
109
- puts <<END
110
- Normal input is of the form
111
- table query
112
- and returns the result of that query. The following special commands
113
- are also recognized:
144
+ def linemode_help
145
+ <<END
146
+ Input can be a query of the form 'TABLE QUERY' or a special command starting
147
+ with a '!'. Recognized special commands are:
148
+ !dump [TABLE]
114
149
  !summary
115
150
  !update
116
- !quit
117
151
 
152
+ !help
153
+ !version
154
+ !quit
118
155
  END
119
- while true
120
- print "> "
121
- input = gets.chomp
122
- case input
123
- when "!summary"
124
- print_summary(db)
125
- when "!update"
126
- db.update
127
- db.save(options[:write]) if options[:write]
128
- when "!quit"
129
- exit
156
+ end
157
+
158
+ if options[:linemode]
159
+ puts "Run your query as 'TABLE QUERY' or run '!help' for more information."
160
+ while input = Readline.readline("> ", true)
161
+ cmd, param = input.split(' ', 2)
162
+ if cmd[0] == '!'
163
+ case cmd[1..-1]
164
+ when "dump"
165
+ dump(db, param)
166
+ when "summary"
167
+ print_summary(db)
168
+ when "update"
169
+ db.update
170
+ db.save(options[:write]) if options[:write]
171
+ when "help"
172
+ puts linemode_help
173
+ when "version"
174
+ puts StarScope::VERSION
175
+ when "quit"
176
+ exit
177
+ else
178
+ puts "Unknown command: '#{input}', try '!help'."
179
+ end
130
180
  else
131
- run_query(db, input, ' ')
181
+ success = run_query(db, cmd, param)
182
+ puts "Try '!help'." unless success
132
183
  end
133
184
  end
134
185
  end
@@ -1,11 +1,12 @@
1
1
  class StarScope::Datum
2
- attr_reader :key, :scope, :file, :line
2
+ attr_reader :key, :file
3
3
 
4
- def initialize(fqn, file, line)
4
+ def initialize(fqn, file, line_no)
5
5
  @key = fqn[-1].to_sym
6
6
  @scope = fqn[0...-1].map {|x| x.to_sym}
7
7
  @file = file
8
- @line = line
8
+ @line_no = line_no
9
+ @line = File.readlines(file)[line_no-1].chomp
9
10
  end
10
11
 
11
12
  def score_match(fqn)
@@ -25,14 +26,17 @@ class StarScope::Datum
25
26
  end
26
27
 
27
28
  def location
28
- "#{file}:#{line}"
29
+ "#{@file}:#{@line_no}"
29
30
  end
30
31
 
31
32
  def to_s
32
- if @scope.empty?
33
- "#{key} -- #{location}"
34
- else
35
- "#{@scope.join " "} #{@key} -- #{location}"
36
- end
33
+ str = ""
34
+ str << "#{@scope.join " "} " unless @scope.empty?
35
+ str << "#{@key} -- #{location}"
36
+ str << " (#{@line.strip})"
37
+ end
38
+
39
+ def ctag_line
40
+ "#{@key}\t#{@file}\t/^#{@line}$/;"
37
41
  end
38
42
  end
data/lib/starscope/db.rb CHANGED
@@ -6,9 +6,10 @@ LANGS = [StarScope::Lang::Ruby]
6
6
 
7
7
  class StarScope::DB
8
8
 
9
- DB_FORMAT = 1
9
+ DB_FORMAT = 2
10
10
 
11
11
  class NoTableError < StandardError; end
12
+ class UnknownDBFormatError < StandardError; end
12
13
 
13
14
  def initialize
14
15
  @dirs = []
@@ -19,10 +20,17 @@ class StarScope::DB
19
20
  def load(file)
20
21
  File.open(file, 'r') do |file|
21
22
  Zlib::GzipReader.wrap(file) do |file|
22
- raise "Database format not recognized" if DB_FORMAT != file.gets.to_i
23
- @dirs = load_part(file)
24
- @files = load_part(file)
25
- @tables = load_part(file)
23
+ format = file.gets.to_i
24
+ if format == DB_FORMAT
25
+ @dirs = load_part(file)
26
+ @files = load_part(file)
27
+ @tables = load_part(file)
28
+ elsif format < DB_FORMAT
29
+ # Old format, so read the directories segment then rebuild
30
+ add_dirs(load_part(file))
31
+ elsif format > DB_FORMAT
32
+ raise UnknownDBFormatError
33
+ end
26
34
  end
27
35
  end
28
36
  end
@@ -89,6 +97,25 @@ class StarScope::DB
89
97
  results.select {|result| best_score - result.score_match(fqn) < 4}
90
98
  end
91
99
 
100
+ def export_ctags(filename)
101
+ File.open(filename, 'w') do |file|
102
+ file.puts <<END
103
+ !_TAG_FILE_FORMAT 2 //
104
+ !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
105
+ !_TAG_PROGRAM_AUTHOR Evan Huus //
106
+ !_TAG_PROGRAM_NAME Starscope //
107
+ !_TAG_PROGRAM_URL https://github.com/eapache/starscope //
108
+ !_TAG_PROGRAM_VERSION #{StarScope::VERSION} //
109
+ END
110
+ defs = (@tables[:def] || []).sort {|a,b| a <=> b}
111
+ defs.each do |key, val|
112
+ val.each do |entry|
113
+ file.puts entry.ctag_line
114
+ end
115
+ end
116
+ end
117
+ end
118
+
92
119
  private
93
120
 
94
121
  def load_part(file)
@@ -1,3 +1,3 @@
1
1
  module StarScope
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/starscope.gemspec CHANGED
@@ -1,24 +1,20 @@
1
- lib = File.expand_path('../lib', __FILE__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
1
+ require File.expand_path('../lib/starscope/version.rb', __FILE__)
3
2
 
4
- require 'starscope/version.rb'
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'starscope'
5
+ gem.version = StarScope::VERSION
6
+ gem.summary = "A code indexer and analyzer"
7
+ gem.description = "A tool like the venerable cscope, but for ruby and other languages"
8
+ gem.authors = ["Evan Huus"]
9
+ gem.homepage = 'https://github.com/eapache/starscope'
10
+ gem.email = 'eapache@gmail.com'
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
13
+ gem.require_paths = ["lib"]
5
14
 
6
- Gem::Specification.new do |s|
7
- s.name = 'starscope'
8
- s.version = StarScope::VERSION
9
- s.date = '2013-06-07'
10
- s.summary = "A code indexer and analyzer"
11
- s.description = "A tool like the venerable cscope, but for ruby and other languages"
12
- s.authors = ["Evan Huus"]
13
- s.homepage = 'https://github.com/eapache/starscope'
14
- s.email = 'evan.huus@jadedpixel.com'
15
- s.files = `git ls-files`.split("\n")
16
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
- s.require_paths = ["lib"]
18
-
19
- s.add_dependency 'parser', '~> 1.4'
20
- s.add_development_dependency 'bundler', '~> 1.3'
21
- s.add_development_dependency 'rake'
22
- s.add_development_dependency 'pry'
23
- s.add_development_dependency 'pry-debugger'
15
+ gem.add_dependency 'parser', '~> 1.4'
16
+ gem.add_development_dependency 'bundler', '~> 1.3'
17
+ gem.add_development_dependency 'rake'
18
+ gem.add_development_dependency 'pry'
19
+ gem.add_development_dependency 'pry-debugger'
24
20
  end
metadata CHANGED
@@ -1,98 +1,98 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: starscope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
5
4
  prerelease:
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Evan Huus
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-07 00:00:00.000000000 Z
12
+ date: 2013-06-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
+ prerelease: false
15
16
  name: parser
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
17
+ type: :runtime
18
+ version_requirements: !ruby/object:Gem::Requirement
18
19
  requirements:
19
20
  - - ~>
20
21
  - !ruby/object:Gem::Version
21
22
  version: '1.4'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
23
  none: false
24
+ requirement: !ruby/object:Gem::Requirement
26
25
  requirements:
27
26
  - - ~>
28
27
  - !ruby/object:Gem::Version
29
28
  version: '1.4'
29
+ none: false
30
30
  - !ruby/object:Gem::Dependency
31
+ prerelease: false
31
32
  name: bundler
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
33
+ type: :development
34
+ version_requirements: !ruby/object:Gem::Requirement
34
35
  requirements:
35
36
  - - ~>
36
37
  - !ruby/object:Gem::Version
37
38
  version: '1.3'
38
- type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
39
  none: false
40
+ requirement: !ruby/object:Gem::Requirement
42
41
  requirements:
43
42
  - - ~>
44
43
  - !ruby/object:Gem::Version
45
44
  version: '1.3'
45
+ none: false
46
46
  - !ruby/object:Gem::Dependency
47
+ prerelease: false
47
48
  name: rake
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
49
+ type: :development
50
+ version_requirements: !ruby/object:Gem::Requirement
50
51
  requirements:
51
52
  - - ! '>='
52
53
  - !ruby/object:Gem::Version
53
54
  version: '0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
55
  none: false
56
+ requirement: !ruby/object:Gem::Requirement
58
57
  requirements:
59
58
  - - ! '>='
60
59
  - !ruby/object:Gem::Version
61
60
  version: '0'
61
+ none: false
62
62
  - !ruby/object:Gem::Dependency
63
+ prerelease: false
63
64
  name: pry
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
65
+ type: :development
66
+ version_requirements: !ruby/object:Gem::Requirement
66
67
  requirements:
67
68
  - - ! '>='
68
69
  - !ruby/object:Gem::Version
69
70
  version: '0'
70
- type: :development
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
71
  none: false
72
+ requirement: !ruby/object:Gem::Requirement
74
73
  requirements:
75
74
  - - ! '>='
76
75
  - !ruby/object:Gem::Version
77
76
  version: '0'
77
+ none: false
78
78
  - !ruby/object:Gem::Dependency
79
+ prerelease: false
79
80
  name: pry-debugger
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
81
+ type: :development
82
+ version_requirements: !ruby/object:Gem::Requirement
82
83
  requirements:
83
84
  - - ! '>='
84
85
  - !ruby/object:Gem::Version
85
86
  version: '0'
86
- type: :development
87
- prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
87
  none: false
88
+ requirement: !ruby/object:Gem::Requirement
90
89
  requirements:
91
90
  - - ! '>='
92
91
  - !ruby/object:Gem::Version
93
92
  version: '0'
93
+ none: false
94
94
  description: A tool like the venerable cscope, but for ruby and other languages
95
- email: evan.huus@jadedpixel.com
95
+ email: eapache@gmail.com
96
96
  executables:
97
97
  - starscope.rb
98
98
  extensions: []
@@ -120,17 +120,23 @@ rdoc_options: []
120
120
  require_paths:
121
121
  - lib
122
122
  required_ruby_version: !ruby/object:Gem::Requirement
123
- none: false
124
123
  requirements:
125
124
  - - ! '>='
126
125
  - !ruby/object:Gem::Version
126
+ segments:
127
+ - 0
128
+ hash: -3682950058124738529
127
129
  version: '0'
128
- required_rubygems_version: !ruby/object:Gem::Requirement
129
130
  none: false
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
132
  requirements:
131
133
  - - ! '>='
132
134
  - !ruby/object:Gem::Version
135
+ segments:
136
+ - 0
137
+ hash: -3682950058124738529
133
138
  version: '0'
139
+ none: false
134
140
  requirements: []
135
141
  rubyforge_project:
136
142
  rubygems_version: 1.8.23