starscope 1.0.4 → 1.1.0

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: 9f58bc7549fa2f4d8d7da61d2771a1e6f75939e2
4
+ data.tar.gz: 1452b9af5221dc6bc2147d0149870e564e2344fc
5
+ SHA512:
6
+ metadata.gz: d9c15bef6cef5e856d023944439efac3a0caa9c2b55f1b018e50d8f7c4fa6f2ced00b8bb595639958547426db52ae23abb9487611ff883638950dbac9876da48
7
+ data.tar.gz: 821ec54d44933500bffc87b871410fbd08cdbc3b9c2e9c0f28a9c4dadb289d07fd12215993763e0982b26013d82e1fd589a1cb3eba876bd852e958c495c17df5
data/.gitignore CHANGED
@@ -16,4 +16,3 @@ tmp
16
16
  # YARD artifacts
17
17
  .yardoc
18
18
  _yardoc
19
- doc/
data/CHANGELOG.md CHANGED
@@ -1,7 +1,30 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
- v1.0.4 (trunk)
4
+ v1.1.0 (trunk)
5
+ --------------------
6
+
7
+ Bug Fixes:
8
+ * Fixed cscope export when the string for a token appeared multiple times on
9
+ the same line, for example the function call `go("go")`.
10
+ * Fixed cscope export of function calls made from the global scope.
11
+ * Fixed cscope export of functions that end in punctuation (e.g. `include?`)
12
+ * Fixed golang parsing of global function calls.
13
+ * Fixed crash in line-mode when pressing `<return>` with no input.
14
+
15
+ Improvements:
16
+ * Optimized and refactored database update path. Now much simpler and about 8%
17
+ faster.
18
+ * Documented the database format and language API. Added a user guide which is
19
+ much more complete than the simpler output of the `--help` flag.
20
+
21
+ Misc:
22
+ * Rename --no-progress to --quiet, and make sure all operations provide some
23
+ indication of success/failure except when quieted.
24
+ * Dynamically load language extractors, so new ones can be dropped in with no
25
+ other code changes.
26
+
27
+ v1.0.4 (2014-06-10)
5
28
  --------------------
6
29
 
7
30
  Improvements:
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- starscope (1.0.4)
4
+ starscope (1.1.0)
5
5
  oj (~> 2.9)
6
6
  parser (~> 2.1)
7
7
  ruby-progressbar (~> 1.5)
@@ -12,14 +12,14 @@ GEM
12
12
  ast (2.0.0)
13
13
  coderay (1.1.0)
14
14
  method_source (0.8.2)
15
- minitest (5.3.4)
16
- oj (2.9.4)
15
+ minitest (5.4.0)
16
+ oj (2.9.9)
17
17
  parser (2.1.9)
18
18
  ast (>= 1.1, < 3.0)
19
19
  slop (~> 3.4, >= 3.4.5)
20
- pry (0.9.12.6)
21
- coderay (~> 1.0)
22
- method_source (~> 0.8)
20
+ pry (0.10.0)
21
+ coderay (~> 1.1.0)
22
+ method_source (~> 0.8.1)
23
23
  slop (~> 3.4)
24
24
  rake (10.3.2)
25
25
  ruby-progressbar (1.5.1)
data/README.md CHANGED
@@ -37,6 +37,14 @@ $ starscope -e cscope
37
37
 
38
38
  More flags and options are available, run `starscope --help` for the complete list.
39
39
 
40
+ More Documentation
41
+ ------------------
42
+
43
+ * [User Guide](doc/USER_GUIDE.md)
44
+ * [Database Format](doc/DB_FORMAT.md)
45
+ * [Language Support](doc/LANGUAGE_SUPPORT.md)
46
+ * [Version History](CHANGELOG.md)
47
+
40
48
  Other Uses
41
49
  ----------
42
50
 
data/Rakefile CHANGED
@@ -2,8 +2,7 @@ require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new do |t|
5
- t.libs << 'lib/starscope'
6
- t.test_files = FileList['test/lib/test_*.rb']
5
+ t.test_files = FileList['test/unit/test_*.rb', 'test/functional/test_*.rb']
7
6
  end
8
7
 
9
8
  desc 'Run tests'
data/bin/starscope CHANGED
@@ -11,11 +11,10 @@ DEFAULT_DB='.starscope.db'
11
11
  DEFAULT_CTAGS='tags'
12
12
  DEFAULT_CSCOPE='cscope.out'
13
13
 
14
- options = {:show_progress => true,
15
- :read => true,
14
+ options = {:read => true,
16
15
  :write => true,
17
16
  :update => true,
18
- :verbose => false,
17
+ :output => :normal,
19
18
  :db => DEFAULT_DB
20
19
  }
21
20
 
@@ -31,6 +30,9 @@ database by recursing in the current directory.
31
30
 
32
31
  Scoped queries must use `::` as the scope separator, even for languages which
33
32
  have their own scope syntax.
33
+
34
+ Website: https://github.com/eapache/starscope
35
+ User Manual: https://github.com/eapache/starscope/blob/master/doc/USER_GUIDE.md
34
36
  END
35
37
 
36
38
  opts.separator "\nQueries"
@@ -73,11 +75,19 @@ END
73
75
  puts StarScope::VERSION
74
76
  exit
75
77
  end
76
- opts.on("--verbose", "Print extra details") do
77
- options[:verbose] = true
78
+ opts.on("--verbose", "Print extra status messages") do
79
+ if options[:output] == :quiet
80
+ $stderr.puts "Can't be both verbose and quiet"
81
+ exit(1)
82
+ end
83
+ options[:output] = :verbose
78
84
  end
79
- opts.on("--no-progress", "Don't show progress-bar when updating DB") do
80
- options[:show_progress] = false
85
+ opts.on("--quiet", "Print fewer messages") do
86
+ if options[:output] == :verbose
87
+ $stderr.puts "Can't be both verbose and quiet"
88
+ exit(1)
89
+ end
90
+ options[:output] = :quiet
81
91
  end
82
92
 
83
93
  opts.separator <<END
@@ -90,7 +100,9 @@ END
90
100
  end.parse!
91
101
 
92
102
  def print_summary(db)
93
- db.summary.each do |name, count|
103
+ summary = db.summary
104
+ puts "No tables" if summary.empty?
105
+ summary.each do |name, count|
94
106
  printf("%-8s %5d records\n", name, count)
95
107
  end
96
108
  end
@@ -101,12 +113,12 @@ def run_query(db, table, value)
101
113
  return false
102
114
  end
103
115
  results = db.query(table.to_sym, value)
104
- if results
116
+ if !results || results.empty?
117
+ puts "No results found."
118
+ else
105
119
  results.sort_by {|x| x[:name].join(' ')}.each do |rec|
106
120
  puts StarScope::Record.format(rec)
107
121
  end
108
- else
109
- puts "No results found."
110
122
  end
111
123
  return true
112
124
  rescue StarScope::DB::NoTableError
@@ -128,23 +140,28 @@ rescue StarScope::DB::NoTableError
128
140
  return false
129
141
  end
130
142
 
131
- def export(db, param)
143
+ def export(db, param, output)
132
144
  format, path = param.split(',', 2)
145
+
133
146
  case format
134
- when 'ctags'
135
- File.open(path || DEFAULT_CTAGS, 'w') do |file|
136
- db.export_ctags(file)
137
- end
138
- when 'cscope'
139
- File.open(path || DEFAULT_CSCOPE, 'w') do |file|
140
- db.export_cscope(file)
141
- end
147
+ when 'ctags'; path ||= DEFAULT_CTAGS
148
+ when 'cscope'; path ||= DEFAULT_CSCOPE
142
149
  else
143
- puts "Unrecognized export format"
150
+ $stderr.puts "Unrecognized export format \"#{format}\""
151
+ return
152
+ end
153
+
154
+ print "Exporting to '#{path}' in format '#{format}'... " unless output == :quiet
155
+ File.open(path, 'w') do |file|
156
+ case format
157
+ when 'ctags'; db.export_ctags(file)
158
+ when 'cscope'; db.export_cscope(file)
159
+ end
144
160
  end
161
+ puts "done" unless output == :quiet
145
162
  end
146
163
 
147
- db = StarScope::DB.new(options[:show_progress], options[:verbose])
164
+ db = StarScope::DB.new(options[:output])
148
165
 
149
166
  db_exists = File.exists?(options[:db])
150
167
 
@@ -176,7 +193,7 @@ new_data ||= updated
176
193
 
177
194
  db.save(options[:db]) if options[:write] and (new_data or not db_exists)
178
195
 
179
- export(db, options[:export]) if options[:export]
196
+ export(db, options[:export], options[:output]) if options[:export]
180
197
 
181
198
  if options[:query]
182
199
  table, query = options[:query].split(',', 2)
@@ -212,6 +229,7 @@ if options[:linemode]
212
229
  puts "Run your query as 'TABLE QUERY' or run '!help' for more information."
213
230
  begin
214
231
  while input = Readline.readline("> ", true)
232
+ next if input.empty?
215
233
  cmd, param = input.split(' ', 2)
216
234
  if cmd[0] == '!'
217
235
  case cmd[1..-1]
@@ -219,7 +237,7 @@ if options[:linemode]
219
237
  dump(db, param)
220
238
  when "export"
221
239
  if param
222
- export(db, param)
240
+ export(db, param, :normal)
223
241
  else
224
242
  puts "!export requires an argument"
225
243
  end
data/doc/DB_FORMAT.md ADDED
@@ -0,0 +1,42 @@
1
+ Database Format
2
+ ===============
3
+
4
+ The StarScope database format has gone through a couple of iterations over the
5
+ last year, but the current one is pretty nice and I don't see it changing again
6
+ in the near future. We magically read old formats and convert them, so we only
7
+ document the current version here.
8
+
9
+ The basic format of the database is [gzipped](https://en.wikipedia.org/wiki/Gzip)
10
+ text. Gzip is a common compression algorithm; most languages include support for
11
+ it by default, and it is readable on effectively every platform. Underneath the
12
+ compression, the file has three lines:
13
+ * version number
14
+ * metadata
15
+ * databases
16
+
17
+ Version Number
18
+ --------------
19
+
20
+ This is just the ascii character '5'. If the format changes signficantly, it
21
+ will be increased, but that is unlikely.
22
+
23
+ Metadata
24
+ --------
25
+
26
+ This is a [JSON](https://en.wikipedia.org/wiki/Json) object. Keys include:
27
+ * `:paths` - the paths containing the files to scan
28
+ * `:excludes` - the paths and patterns to exclude from scanning
29
+ * `:files` - the files previously scanned (including things like last-modified
30
+ time)
31
+ * `:version` - The StarScope version which wrote the database (not the same as
32
+ the database format version).
33
+
34
+ Databases
35
+ ---------
36
+
37
+ This is also a [JSON](https://en.wikipedia.org/wiki/Json) object. The keys are
38
+ table names, and the values are the tables themselves. Each table is an array of
39
+ records, and each record is itself a JSON object. The only keys that are
40
+ guaranteed to exist in every record are `:file` (the name of the file the record
41
+ is from) and `:name` (an array containing the individual components of the
42
+ fully-scoped name).
@@ -0,0 +1,80 @@
1
+ Language Support
2
+ ================
3
+
4
+ Already Supported
5
+ -----------------
6
+
7
+ * [Ruby](https://www.ruby-lang.org/)
8
+ * [Golang](http://golang.org/)
9
+
10
+ How to Add Another Language
11
+ ---------------------------
12
+
13
+ Adding support for a new language is really easy, and pretty generic - all of
14
+ the fancy features like cscope export should "just work" since they don't depend
15
+ on anything language-specific, they just read the internal database record
16
+ format.
17
+
18
+ For this doc, we're going to pretend to add support for a language called
19
+ "MyLanguage". Create a file called `mylanguage.rb` in `lib/starscope/langs/` and
20
+ drop the following template in:
21
+
22
+ ```ruby
23
+ module StarScope::Lang
24
+ module Mylanguage
25
+
26
+ def self.match_file(name)
27
+ name.end_with?(".mylang")
28
+ end
29
+
30
+ def self.extract(file)
31
+ # TODO
32
+ end
33
+
34
+ end
35
+ end
36
+ ```
37
+
38
+ This code is pretty simple: we define a module called
39
+ `StarScope::Lang::Mylanguage` and give it two public module methods:
40
+ * `match_file` takes the name of the file and returns a boolean if that file is
41
+ written in MyLanguage or not. This can be as simple as checking the file
42
+ extension (which the sample code does) or looking for a shell #! line, or
43
+ anything you want.
44
+ * `extract` takes a readable file handle pointing to the file, and must parse
45
+ the file, `yield`ing records as it finds function definitions and the like.
46
+
47
+ The record requirements are pretty straight-forward:
48
+ ```ruby
49
+ yield table, name, extras={}
50
+ ```
51
+ The first yielded argument must be the symbol of the table in which the record
52
+ belongs (basically its type). Current tables already include:
53
+ * `calls` for function calls
54
+ * `defs` for definitions of classes, functions, modules, etc.
55
+ * `end` for the matching *end* of each definition
56
+ * `assigns` for variable assignment
57
+ * `requires` for required files in Ruby
58
+ * `imports` for imported packages in Golang
59
+
60
+ Try to use pre-existing tables where possible, but feel free to add more if the
61
+ language has some weird feature that doesn't map to any of the above. You don't
62
+ have to do anything special to create a new table, just yield the appropriate
63
+ symbol.
64
+
65
+ The second yielded argument is the name (string or symbol) of the token that
66
+ you want to add: the name of the function being called, or the name of the class
67
+ being defined, etc. If the name is scoped (such as "def MyModule::MyClass") pass
68
+ an array `["MyModule", "MyClass"]`.
69
+
70
+ Finally, the entirely-optional `extras` is a hash of whatever other extra values
71
+ you want. Some existing ones that you may want to use include:
72
+ * `line_no` for the line (starting at 1) where the record occurs - this is
73
+ necessary for cscope and ctags export
74
+ * `col` for the column in the line where the name occurs
75
+ * `type` for the type of definition (`:func`, `:class`, etc)
76
+
77
+ And that's it! Parse your files, yield your records, and the StarScope engine
78
+ takes care of everything else for you. If you've added support for a language
79
+ that you think others might find useful, please contribute it (with tests!) via
80
+ pull request.
data/doc/USER_GUIDE.md ADDED
@@ -0,0 +1,135 @@
1
+ StarScope User Guide
2
+ ====================
3
+
4
+
5
+ Installation
6
+ ------------
7
+
8
+ StarScope is a ruby gem available at https://rubygems.org/gems/starscope.
9
+ Install it with:
10
+ ```
11
+ $ gem install starscope
12
+ ```
13
+
14
+ This should place a program called `starscope` in your path.
15
+
16
+ Quick Start
17
+ -----------
18
+
19
+ Build your database by just running it in the project directory:
20
+ ```
21
+ $ cd ~/my-project
22
+ $ starscope
23
+ ```
24
+
25
+ Ask it things directly:
26
+ ```
27
+ $ starscope -q calls,new # Lists all callers of new
28
+ ```
29
+
30
+ Export it to various formats for use with your editor:
31
+ ```
32
+ $ starscope -e ctags
33
+ $ starscope -e cscope
34
+ ```
35
+
36
+ Database Options
37
+ ----------------
38
+
39
+ The default database is `.starscope.db` in the current directory. If you want
40
+ to use another file, specify one with `-f` or `--file`.
41
+
42
+ The default behaviour is always to read the database (if it exists), update it,
43
+ and write out the updated version. You can control this behaviour by passing any
44
+ of the `--no-read`, `--no-write` and `--no-update` flags.
45
+
46
+ To get a summary of the current database contents, pass the `-s` or `--summary`
47
+ flag.
48
+
49
+ Paths
50
+ -----
51
+
52
+ StarScope has powerful options with sane defaults for managing which paths get
53
+ scanned for files and which do not. By default when creating a new database,
54
+ StarScope will scan all files recursively in the current directory. To scan
55
+ specific paths or files instead, pass them as arguments (so `starscope myfolder`
56
+ would only scan files in `myfolder/`).
57
+
58
+ Paths are saved in the database metadata - once you have created a database with
59
+ custom paths, all subsequent operations will remember and use those paths even
60
+ when they are not explicitly specified. At any time, specifying *more* paths
61
+ will add them (and the files they contain) to the database without removing any
62
+ existing paths.
63
+
64
+ You can also exclude certain paths from processing by passing the `-x` or
65
+ `--exclude` flag with the desired pattern. In a new project, `starscope
66
+ --exclude test/` will scan all files *except* the ones in the `test/` directory.
67
+ Excluded patterns are also remembered, and can be added at any time. If an
68
+ existing file in the database matches a newly added exclusion rule, it will be
69
+ removed.
70
+
71
+ Queries
72
+ -------
73
+
74
+ To query the starscope database, pass the `-q` or `--query` flag with an
75
+ argument in the following format: `TABLE,QUERY`. For example, `-q calls,new`
76
+ would list all callers of `new` and `-q defs,bar` would list places that define
77
+ a method or class named `bar`. See the [language support
78
+ documentation](LANGUAGE_SUPPORT.md) for a list of the most common tables, or use
79
+ the `--summary` flag to list all the tables in the current database.
80
+
81
+ You can also search for scoped names such as `MyClass.new`. To do this, you must
82
+ specify the scope with `::`, even if the language or instance you are searching
83
+ for uses another character like a dot. So, for example, `-q calls,MyClass::new`.
84
+
85
+ Queries using regular expressions are generally supported, and will be tried
86
+ against both the base name and the fully-qualified name (again using `::` as the
87
+ scope separator).
88
+
89
+ Exporting
90
+ ---------
91
+
92
+ StarScope can export its database into two other formats for use with
93
+ third-party tools:
94
+ * cscope format (default path: `cscope.out`)
95
+ * ctags format (default path: `tags`)
96
+
97
+ To export, pass the `-e` or `--export` flag with one of the above formats. Each
98
+ format has its own default path which is where the exported file will go. If you
99
+ want to specify a custom location, append it after the format with a comma (so
100
+ `--export cscope,myfile` would export to `myfile` in the cscope format).
101
+
102
+ You can also dump entire tables (or the entire database) to raw text output for
103
+ manual inspection. This is mostly useful for debugging, but means you can pipe
104
+ it to sed (for example) if you wanted to do something fancy. This can be done
105
+ with the `-d` or `--dump` flag, which takes an optional argument of which table
106
+ to dump (if no table is specified, it dumps all tables).
107
+
108
+ Line-Mode
109
+ ---------
110
+
111
+ Specifying `-l` or `--line-mode` places you into line-oriented mode, letting you
112
+ run multiple queries without reloading the database each time. In line mode,
113
+ input is normally a query of the form `TABLE QUERY`, or a special command
114
+ starting with a `!`. Recognized special commands generally map to non-line-mode
115
+ options:
116
+ * `!dump [TABLE]` - same as the `--dump` flag
117
+ * `!export FORMAT[,PATH]` - same as the `--export` flag
118
+ * `!summary` - same as the `--summary` flag
119
+ * `!update` - updates the database without exiting line-mode
120
+ * `!help` - prints basic line-mode help
121
+ * `!version` - same as the `--version` flag
122
+ * `!quit` - exit line-mode
123
+
124
+
125
+ Miscellaneous
126
+ -------------
127
+
128
+ Pass `-h` or `--help` to get a brief summary of the availble options.
129
+
130
+ Pass `-v` or `--version` to get the current version number.
131
+
132
+ Pass `--verbose` to get a great deal more output about what it is doing.
133
+
134
+ Pass `--quiet` to remove all non-critical output (you will still get error
135
+ messages, query results, etc).