Skip to content
This repository has been archived by the owner on Oct 1, 2021. It is now read-only.

Commit

Permalink
Make the apidock search more faster.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmexe committed Mar 2, 2009
1 parent 4d42540 commit bcb38bb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
41 changes: 32 additions & 9 deletions bundles/rails-apidock-bundle.el
@@ -1,20 +1,43 @@
(defconst rails/apidock-bundle/buffer-name "*rails-apidock-bundle*")
(defconst rails/apidock-bundle/script-file-name "bundles/rails-apidock-bundle/search.rb")

(defun rails/apidock-bundle/get-proc ()
(let ((proc (get-buffer-process rails/apidock-bundle/buffer-name)))
(unless proc
(setq
proc
(start-process rails/apidock-bundle/buffer-name
rails/apidock-bundle/buffer-name
rails/ruby/command
(rails/apidock-bundle/locate-script)))
(set-process-query-on-exit-flag proc nil))
proc))

(defun rails/apidock-bundle/get-result (mod query)
(let ((proc (rails/apidock-bundle/get-proc)))
(with-current-buffer rails/apidock-bundle/buffer-name
(kill-region (point-min) (point-max))
(process-send-string proc (format "%s %s\n" mod query))
(while
(progn
(sleep-for 0 100)
(goto-char (point-max))
(goto-char (point-at-bol 0))
(not (string= "APIDOCK_EOF"
(buffer-substring (line-beginning-position)
(line-end-position))))))
(read (buffer-substring-no-properties (point-min)
(point))))))

(defun rails/apidock-bundle/locate-script ()
(let* ((path (file-name-directory (locate-library "rails-reloaded")))
(script (concat path rails/apidock-bundle/script-file-name)))
(when (file-exists-p script)
script)))

(defun rails/apidock-bundle/search (q &optional mod)
(let* ((mod (or mod "rails"))
(result (shell-command-to-string
(format "ruby %s %s %s"
(rails/apidock-bundle/locate-script)
mod
q))))
(unless (string-ext/empty-p result)
(read result))))
(defun rails/apidock-bundle/search (query &optional mod)
(let ((mod (or mod "rails")))
(rails/apidock-bundle/get-result mod query)))

(rails/defbundle "Apidock"
(:triggers
Expand Down
20 changes: 14 additions & 6 deletions bundles/rails-apidock-bundle/search.rb
Expand Up @@ -3,9 +3,11 @@
require 'rubygems'
require 'activesupport'

LIMIT = 12

def create_data(data_file, file)
result = []
File.open(file, "rb") do |f|
File.open(file, "r") do |f|
str = f.read
keywords,data = str.split(";")
data.gsub!(/^[^=]+= /, "")
Expand All @@ -21,7 +23,7 @@ def create_data(data_file, file)
result << i
end
end
File.open(data_file, "wb") do |f|
File.open(data_file, "w") do |f|
f.write Marshal.dump(result)
end
end
Expand All @@ -47,7 +49,7 @@ def search(mod, str)
return if result.empty?

result = result.sort_by{ |a| a[:score] }.reverse
to_lisp(mod, result)
to_lisp(mod, result[0..LIMIT])
end

def to_lisp(mod, data)
Expand Down Expand Up @@ -79,6 +81,12 @@ def to_lisp(mod, data)
"(" + result.join("\n") + ")"
end

mod = ARGV.size > 1 ? ARGV.first : "rails"
q = ARGV.last
puts search(mod, q) if q
STDOUT.flush
loop do
line = STDIN.gets
mod, q = /(\w+)(.*)$/.match(line)[1..2]
q.strip!
puts search(mod, q) unless q.blank?
puts "APIDOCK_EOF"
STDOUT.flush
end
2 changes: 1 addition & 1 deletion rails-resources.el
Expand Up @@ -310,7 +310,7 @@

;;; ---------------------------------------------------------
;;; - Menu functions
;;;
;;; TODO: remove bellow after the toggle refactoring.

(defun rails/resources/items-to-menu (menu items &optional name-func)
(let ((menu menu))
Expand Down

0 comments on commit bcb38bb

Please sign in to comment.