command-t-standalone 0.0.2

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.
@@ -0,0 +1,164 @@
1
+ // Copyright 2010 Wincent Colaiuta. All rights reserved.
2
+ //
3
+ // Redistribution and use in source and binary forms, with or without
4
+ // modification, are permitted provided that the following conditions are met:
5
+ //
6
+ // 1. Redistributions of source code must retain the above copyright notice,
7
+ // this list of conditions and the following disclaimer.
8
+ // 2. Redistributions in binary form must reproduce the above copyright notice,
9
+ // this list of conditions and the following disclaimer in the documentation
10
+ // and/or other materials provided with the distribution.
11
+ //
12
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
13
+ // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14
+ // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15
+ // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
16
+ // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17
+ // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18
+ // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19
+ // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20
+ // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21
+ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
+ // POSSIBILITY OF SUCH DAMAGE.
23
+
24
+ #include <stdlib.h> /* for qsort() */
25
+ #include <string.h> /* for strcmp() */
26
+ #include "matcher.h"
27
+ #include "ext.h"
28
+ #include "ruby_compat.h"
29
+
30
+ // comparison function for use with qsort
31
+ int comp_alpha(const void *a, const void *b)
32
+ {
33
+ VALUE a_val = *(VALUE *)a;
34
+ VALUE b_val = *(VALUE *)b;
35
+ ID to_s = rb_intern("to_s");
36
+
37
+ VALUE a_str = rb_funcall(a_val, to_s, 0);
38
+ VALUE b_str = rb_funcall(b_val, to_s, 0);
39
+ char *a_p = RSTRING_PTR(a_str);
40
+ long a_len = RSTRING_LEN(a_str);
41
+ char *b_p = RSTRING_PTR(b_str);
42
+ long b_len = RSTRING_LEN(b_str);
43
+ int order = 0;
44
+ if (a_len > b_len)
45
+ {
46
+ order = strncmp(a_p, b_p, b_len);
47
+ if (order == 0)
48
+ order = 1; // shorter string (b) wins
49
+ }
50
+ else if (a_len < b_len)
51
+ {
52
+ order = strncmp(a_p, b_p, a_len);
53
+ if (order == 0)
54
+ order = -1; // shorter string (a) wins
55
+ }
56
+ else
57
+ order = strncmp(a_p, b_p, a_len);
58
+ return order;
59
+ }
60
+
61
+ // comparison function for use with qsort
62
+ int comp_score(const void *a, const void *b)
63
+ {
64
+ VALUE a_val = *(VALUE *)a;
65
+ VALUE b_val = *(VALUE *)b;
66
+ ID score = rb_intern("score");
67
+ double a_score = RFLOAT_VALUE(rb_funcall(a_val, score, 0));
68
+ double b_score = RFLOAT_VALUE(rb_funcall(b_val, score, 0));
69
+ if (a_score > b_score)
70
+ return -1; // a scores higher, a should appear sooner
71
+ else if (a_score < b_score)
72
+ return 1; // b scores higher, a should appear later
73
+ else
74
+ return comp_alpha(a, b);
75
+ }
76
+
77
+ VALUE CommandTMatcher_initialize(int argc, VALUE *argv, VALUE self)
78
+ {
79
+ // process arguments: 1 mandatory, 1 optional
80
+ VALUE scanner, options;
81
+ if (rb_scan_args(argc, argv, "11", &scanner, &options) == 1)
82
+ options = Qnil;
83
+ if (NIL_P(scanner))
84
+ rb_raise(rb_eArgError, "nil scanner");
85
+ rb_iv_set(self, "@scanner", scanner);
86
+
87
+ // check optional options hash for overrides
88
+ VALUE always_show_dot_files = CommandT_option_from_hash("always_show_dot_files", options);
89
+ if (always_show_dot_files != Qtrue)
90
+ always_show_dot_files = Qfalse;
91
+ VALUE never_show_dot_files = CommandT_option_from_hash("never_show_dot_files", options);
92
+ if (never_show_dot_files != Qtrue)
93
+ never_show_dot_files = Qfalse;
94
+ rb_iv_set(self, "@always_show_dot_files", always_show_dot_files);
95
+ rb_iv_set(self, "@never_show_dot_files", never_show_dot_files);
96
+ return Qnil;
97
+ }
98
+
99
+ VALUE CommandTMatcher_sorted_matches_for(VALUE self, VALUE abbrev, VALUE options)
100
+ {
101
+ // process optional options hash
102
+ VALUE limit_option = CommandT_option_from_hash("limit", options);
103
+
104
+ // get unsorted matches
105
+ VALUE matches = CommandTMatcher_matches_for(self, abbrev);
106
+
107
+ abbrev = StringValue(abbrev);
108
+ if (RSTRING_LEN(abbrev) == 0 ||
109
+ (RSTRING_LEN(abbrev) == 1 && RSTRING_PTR(abbrev)[0] == '.'))
110
+ // alphabetic order if search string is only "" or "."
111
+ qsort(RARRAY_PTR(matches), RARRAY_LEN(matches), sizeof(VALUE), comp_alpha);
112
+ else
113
+ // for all other non-empty search strings, sort by score
114
+ qsort(RARRAY_PTR(matches), RARRAY_LEN(matches), sizeof(VALUE), comp_score);
115
+
116
+ // apply optional limit option
117
+ long limit = NIL_P(limit_option) ? 0 : NUM2LONG(limit_option);
118
+ if (limit == 0 || RARRAY_LEN(matches) < limit)
119
+ limit = RARRAY_LEN(matches);
120
+
121
+ // will return an array of strings, not an array of Match objects
122
+ for (long i = 0; i < limit; i++)
123
+ {
124
+ VALUE str = rb_funcall(RARRAY_PTR(matches)[i], rb_intern("to_s"), 0);
125
+ RARRAY_PTR(matches)[i] = str;
126
+ }
127
+
128
+ // trim off any items beyond the limit
129
+ if (limit < RARRAY_LEN(matches))
130
+ (void)rb_funcall(matches, rb_intern("slice!"), 2, LONG2NUM(limit),
131
+ LONG2NUM(RARRAY_LEN(matches) - limit));
132
+ return matches;
133
+ }
134
+
135
+ VALUE CommandTMatcher_matches_for(VALUE self, VALUE abbrev)
136
+ {
137
+ if (NIL_P(abbrev))
138
+ rb_raise(rb_eArgError, "nil abbrev");
139
+ VALUE matches = rb_ary_new();
140
+ VALUE scanner = rb_iv_get(self, "@scanner");
141
+ VALUE always_show_dot_files = rb_iv_get(self, "@always_show_dot_files");
142
+ VALUE never_show_dot_files = rb_iv_get(self, "@never_show_dot_files");
143
+ VALUE options = Qnil;
144
+ if (always_show_dot_files == Qtrue)
145
+ {
146
+ options = rb_hash_new();
147
+ rb_hash_aset(options, ID2SYM(rb_intern("always_show_dot_files")), always_show_dot_files);
148
+ }
149
+ else if (never_show_dot_files == Qtrue)
150
+ {
151
+ options = rb_hash_new();
152
+ rb_hash_aset(options, ID2SYM(rb_intern("never_show_dot_files")), never_show_dot_files);
153
+ }
154
+ abbrev = rb_funcall(abbrev, rb_intern("downcase"), 0);
155
+ VALUE paths = rb_funcall(scanner, rb_intern("paths"), 0);
156
+ for (long i = 0, max = RARRAY_LEN(paths); i < max; i++)
157
+ {
158
+ VALUE path = RARRAY_PTR(paths)[i];
159
+ VALUE match = rb_funcall(cCommandTMatch, rb_intern("new"), 3, path, abbrev, options);
160
+ if (rb_funcall(match, rb_intern("matches?"), 0) == Qtrue)
161
+ rb_funcall(matches, rb_intern("push"), 1, match);
162
+ }
163
+ return matches;
164
+ }
@@ -0,0 +1,30 @@
1
+ // Copyright 2010 Wincent Colaiuta. All rights reserved.
2
+ //
3
+ // Redistribution and use in source and binary forms, with or without
4
+ // modification, are permitted provided that the following conditions are met:
5
+ //
6
+ // 1. Redistributions of source code must retain the above copyright notice,
7
+ // this list of conditions and the following disclaimer.
8
+ // 2. Redistributions in binary form must reproduce the above copyright notice,
9
+ // this list of conditions and the following disclaimer in the documentation
10
+ // and/or other materials provided with the distribution.
11
+ //
12
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
13
+ // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14
+ // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15
+ // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
16
+ // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17
+ // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18
+ // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19
+ // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20
+ // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21
+ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
+ // POSSIBILITY OF SUCH DAMAGE.
23
+
24
+ #include <ruby.h>
25
+
26
+ extern VALUE CommandTMatcher_initialize(int argc, VALUE *argv, VALUE self);
27
+ extern VALUE CommandTMatcher_sorted_matches_for(VALUE self, VALUE abbrev, VALUE options);
28
+
29
+ // most likely the function will be subsumed by the sorted_matcher_for function
30
+ extern VALUE CommandTMatcher_matches_for(VALUE self, VALUE abbrev);
@@ -0,0 +1,49 @@
1
+ // Copyright 2010 Wincent Colaiuta. All rights reserved.
2
+ //
3
+ // Redistribution and use in source and binary forms, with or without
4
+ // modification, are permitted provided that the following conditions are met:
5
+ //
6
+ // 1. Redistributions of source code must retain the above copyright notice,
7
+ // this list of conditions and the following disclaimer.
8
+ // 2. Redistributions in binary form must reproduce the above copyright notice,
9
+ // this list of conditions and the following disclaimer in the documentation
10
+ // and/or other materials provided with the distribution.
11
+ //
12
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
13
+ // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14
+ // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15
+ // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
16
+ // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17
+ // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18
+ // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19
+ // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20
+ // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21
+ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
+ // POSSIBILITY OF SUCH DAMAGE.
23
+
24
+ #include <ruby.h>
25
+
26
+ // for compatibility with older versions of Ruby which don't declare RSTRING_PTR
27
+ #ifndef RSTRING_PTR
28
+ #define RSTRING_PTR(s) (RSTRING(s)->ptr)
29
+ #endif
30
+
31
+ // for compatibility with older versions of Ruby which don't declare RSTRING_LEN
32
+ #ifndef RSTRING_LEN
33
+ #define RSTRING_LEN(s) (RSTRING(s)->len)
34
+ #endif
35
+
36
+ // for compatibility with older versions of Ruby which don't declare RARRAY_PTR
37
+ #ifndef RARRAY_PTR
38
+ #define RARRAY_PTR(a) (RARRAY(a)->ptr)
39
+ #endif
40
+
41
+ // for compatibility with older versions of Ruby which don't declare RARRAY_LEN
42
+ #ifndef RARRAY_LEN
43
+ #define RARRAY_LEN(a) (RARRAY(a)->len)
44
+ #endif
45
+
46
+ // for compatibility with older versions of Ruby which don't declare RFLOAT_VALUE
47
+ #ifndef RFLOAT_VALUE
48
+ #define RFLOAT_VALUE(f) (RFLOAT(f)->value)
49
+ #endif
@@ -0,0 +1,28 @@
1
+ # Copyright 2010-2011 Wincent Colaiuta. All rights reserved.
2
+ #
3
+ # Redistribution and use in source and binary forms, with or without
4
+ # modification, are permitted provided that the following conditions are met:
5
+ #
6
+ # 1. Redistributions of source code must retain the above copyright notice,
7
+ # this list of conditions and the following disclaimer.
8
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
9
+ # this list of conditions and the following disclaimer in the documentation
10
+ # and/or other materials provided with the distribution.
11
+ #
12
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
13
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
16
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
+ # POSSIBILITY OF SUCH DAMAGE.
23
+
24
+ # require 'command-t/vim'
25
+
26
+ module CommandT
27
+ class Scanner; end
28
+ end # module CommandT
@@ -0,0 +1,110 @@
1
+ # Copyright 2010-2011 Wincent Colaiuta. All rights reserved.
2
+ #
3
+ # Redistribution and use in source and binary forms, with or without
4
+ # modification, are permitted provided that the following conditions are met:
5
+ #
6
+ # 1. Redistributions of source code must retain the above copyright notice,
7
+ # this list of conditions and the following disclaimer.
8
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
9
+ # this list of conditions and the following disclaimer in the documentation
10
+ # and/or other materials provided with the distribution.
11
+ #
12
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
13
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
16
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
+ # POSSIBILITY OF SUCH DAMAGE.
23
+
24
+ # require 'command-t/vim'
25
+ require 'command-t-standalone/scanner'
26
+
27
+ module CommandT
28
+ # Reads the current directory recursively for the paths to all regular files.
29
+ class FileScanner < Scanner
30
+ class FileLimitExceeded < ::RuntimeError; end
31
+ attr_accessor :path
32
+
33
+ def initialize path = Dir.pwd, options = {}
34
+ @paths = {}
35
+ @paths_keys = []
36
+ @path = path
37
+ @max_depth = options[:max_depth] || 15
38
+ @max_files = options[:max_files] || 10_000
39
+ @max_caches = options[:max_caches] || 1
40
+ @scan_dot_directories = options[:scan_dot_directories] || false
41
+ end
42
+
43
+ def paths
44
+ return @paths[@path] if @paths.has_key?(@path)
45
+ begin
46
+ ensure_cache_under_limit
47
+ @paths[@path] = []
48
+ @depth = 0
49
+ @files = 0
50
+ @prefix_len = @path.chomp('/').length
51
+ add_paths_for_directory @path, @paths[@path]
52
+ rescue FileLimitExceeded
53
+ end
54
+ @paths[@path]
55
+ end
56
+
57
+ def flush
58
+ @paths = {}
59
+ end
60
+
61
+ private
62
+
63
+ def ensure_cache_under_limit
64
+ # Ruby 1.8 doesn't have an ordered hash, so use a separate stack to
65
+ # track and expire the oldest entry in the cache
66
+ if @max_caches > 0 && @paths_keys.length >= @max_caches
67
+ @paths.delete @paths_keys.shift
68
+ end
69
+ @paths_keys << @path
70
+ end
71
+
72
+ def path_excluded? path
73
+ return false
74
+ # first strip common prefix (@path) from path to match VIM's behavior
75
+ path = path[(@prefix_len + 1)..-1]
76
+ path = VIM::escape_for_single_quotes path
77
+ ::VIM::evaluate("empty(expand(fnameescape('#{path}')))").to_i == 1
78
+ end
79
+
80
+ def looped_symlink? path
81
+ if File.symlink?(path)
82
+ target = File.expand_path(File.readlink(path), File.dirname(path))
83
+ target.include?(@path) || @path.include?(target)
84
+ end
85
+ end
86
+
87
+ def add_paths_for_directory dir, accumulator
88
+ Dir.foreach(dir) do |entry|
89
+ next if ['.', '..'].include?(entry)
90
+ path = File.join(dir, entry)
91
+ unless path_excluded?(path)
92
+ if File.file?(path)
93
+ @files += 1
94
+ raise FileLimitExceeded if @files > @max_files
95
+ accumulator << path[@prefix_len + 1..-1]
96
+ elsif File.directory?(path)
97
+ next if @depth >= @max_depth
98
+ next if (entry.match(/\A\./) && !@scan_dot_directories)
99
+ next if looped_symlink?(path)
100
+ @depth += 1
101
+ add_paths_for_directory path, accumulator
102
+ @depth -= 1
103
+ end
104
+ end
105
+ end
106
+ rescue Errno::EACCES
107
+ # skip over directories for which we don't have access
108
+ end
109
+ end # class FileScanner
110
+ end # module CommandT
@@ -0,0 +1,3 @@
1
+ module CommandT
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: command-t-standalone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Howard Yeh
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-22 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: CommandT finder as standalone gem
15
+ email:
16
+ - howard@metacircus.com
17
+ executables: []
18
+ extensions:
19
+ - lib/command-t-standalone/extconf.rb
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - command-t-standalone.gemspec
28
+ - lib/command-t-standalone.rb
29
+ - lib/command-t-standalone/depend
30
+ - lib/command-t-standalone/ext.c
31
+ - lib/command-t-standalone/ext.h
32
+ - lib/command-t-standalone/extconf.rb
33
+ - lib/command-t-standalone/finder.rb
34
+ - lib/command-t-standalone/finder/file_finder.rb
35
+ - lib/command-t-standalone/match.c
36
+ - lib/command-t-standalone/match.h
37
+ - lib/command-t-standalone/match_window.rb
38
+ - lib/command-t-standalone/matcher.c
39
+ - lib/command-t-standalone/matcher.h
40
+ - lib/command-t-standalone/ruby_compat.h
41
+ - lib/command-t-standalone/scanner.rb
42
+ - lib/command-t-standalone/scanner/file_scanner.rb
43
+ - lib/command-t-standalone/version.rb
44
+ homepage: http://github.com/hayeah
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.11
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: ditto
68
+ test_files: []