Skip to content

Commit

Permalink
update for haxe --wait server
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudshift committed Dec 13, 2011
1 parent 8257e0e commit dcfb187
Showing 1 changed file with 94 additions and 33 deletions.
127 changes: 94 additions & 33 deletions hxc-complete.el
Original file line number Diff line number Diff line change
@@ -1,47 +1,98 @@

; Author: Ritchie Turner (blackdog@cloudshift.cl)
; Notes:
; You need to fire up your haxe server with haxe --wait 6000 (change hxc-port accordingly)
; Still need to match help.
; Parse type on ( seems to be redundant as its provided in method sig anyway.

(defvar hxc-hxml nil)
(defvar hxc-char ".")
(defvar hxc-raw nil)
(defvar hxc-eol "\n")
(defvar hxc-got-resp nil)
(defvar hxc-port 6000)

(defun hxc-init (p)
(setq hxc-hxml nil)
(if (hxc-read-hxml)
(hxc-msg (format "Welcome, new project is \"%s\"\nClasspath: %s" (car p) hxc-hxml))))

(defun hxc-complete (char)
(setq hxc-char char)
(insert char)
(backward-char)
(hxc-get-info char))
(save-buffer)
(hxc-connect))

(defun hxc-get-info (char)
(save-buffer)
(let ((raw (hxc-get-raw)))
(when raw
(if (string= char ".")
(hxc-complete-dot raw)
(hxc-complete-paren raw)))))
(defun hxc-process-filter (proc raw)
(setq hxc-raw (concat raw hxc-raw))
(when (string-match "<\/list>" hxc-raw)
(setq hxc-got-resp t)))

(defun hxc-get-raw ()
(defun hxc-connect ()
(let* ((fn (hxc-file-name))
(pkg (hxc-package))
(cmd (hxc-build-compile-string pkg (buffer-name))))
(hxc-msg cmd)
(shell-command-to-string cmd)))

(setq hxc-raw "")
(setq hxc-got-resp nil)

(let ((proc (open-network-stream "hxccomp" nil "127.0.0.1" hxc-port)))
(set-process-filter proc 'hxc-process-filter)
(process-send-string proc cmd)
(process-send-string proc "\000")
(process-send-eof proc)
(catch 'loop
(dotimes (i 3)
(accept-process-output proc 0.3)
(when hxc-got-resp
(delete-process proc)
(hxc-process-raw)
(throw 'loop i)
)))
(unless hxc-got-resp
(delete-process proc))
)))

(defun hxc-process-raw ()
(when hxc-raw
(if (string= hxc-char ".")
(hxc-complete-dot hxc-raw)
(hxc-complete-paren hxc-raw))))

(defun hxc-complete-dot (raw)
(let ((mylist (hxc-parse-methods raw)))
(when (length mylist)
(progn
(let ((selection (ido-completing-read "-> "
(mapcar (lambda (el) (car el)) mylist))))
(forward-char)
(when selection
(insert selection)))))))
(let ((methodlist (hxc-parse-methods raw)))
(when (length methodlist)
(let ((selection (ido-completing-read "-> "
(mapcar (lambda (el) (car el)) methodlist))))
(forward-char)
(when selection
(let ((sig (hxc-lookup-signature selection methodlist))
(help (hxc-lookup-help selection methodlist)))
(message sig)
(hxc-msg (concat sig "\n\n" help ))
(insert (hxc-modify-by-sig sig selection))))))))

(defun hxc-lookup-signature (selection methodlist)
(replace-regexp-in-string "&lt;" "<"
(replace-regexp-in-string "&gt;" ">"
(cadr (assoc selection methodlist)))))

(defun hxc-lookup-help (selection methodlist)
(cadr (cdr (assoc selection methodlist))))

(defun hxc-modify-by-sig (sig selection)
(let ((ss (split-string sig "->" t)))
(cond
((string= (hxc-chomp (car ss)) "Void") (concat selection "()"))
(t selection))))

(defun hxc-complete-paren (raw)
(forward-char)
(let* ((contents (hxc-parse-type raw))
(byline (replace-regexp-in-string "\\([a-zA-Z0-9]+\\s-:\\)" "\n\\1"
(replace-in-string
(replace-in-string contents "&gt;" ">") "&lt;" "<"))))
(replace-regexp-in-string "&lt;" "<"
(replace-regexp-in-string "&gt;" ">" contents)))))
(hxc-msg
(mapconcat (lambda (el)
(replace-regexp-in-string "->\s*$" "" el))
Expand All @@ -52,27 +103,30 @@
(erase-buffer)
(insert text)))

(defun hxc-build-cwd ()
(concat "--cwd "
(expand-file-name (substring (hxc-prj-dir) 0 -1))
hxc-eol))

(defun hxc-build-compile-string (pkg tmpFile)
(concat
(format "cd %s && " (hxc-prj-dir))
"haxe "
" --cache prj.cache "
(hxc-build-cwd)
(hxc-conditional-comps)
(hxc-klass pkg)
(hxc-read-hxml)
" --display " tmpFile "@" (number-to-string (point))))
(concat "-main " (hxc-klass pkg) hxc-eol)
(concat "--display " tmpFile "@" (number-to-string (point)) hxc-eol)))

(defun hxc-conditional-comps ()
(let ((bs (buffer-string)))
(when (string-match "hxc:\\s-\\(.*\\)" bs)
(concat (match-string 1 bs) " "))))
(concat (match-string 1 bs) hxc-eol))))

(defun hxc-parse-methods (s)
(delq nil
(mapcar
(lambda (el)
(when (string-match "n=\"\\(.*\\)\"><t>\\(.*\\)<\/t>" el)
(list (match-string 1 el) (match-string 2 el))))
(list (match-string 1 el) (match-string 2 el) (match-string 3 el) )))
(split-string s "\n"))))

(defun hxc-parse-type (s)
Expand All @@ -97,20 +151,27 @@

(defun hxc-read-hxml ()
(unless hxc-hxml
(let ((buildFile (concat (hxc-prj-dir) "/build.hxml")))
(let ((buildFile (concat (hxc-prj-dir) "build.hxml")))
(if (file-exists-p buildFile)
(with-temp-buffer
(insert-file buildFile)
(delete-non-matching-lines "^-cp\\|^-lib")
(setq hxc-hxml
(concat " "
(mapconcat 'identity
(delete-dups
(split-string (buffer-string) "\n")) " "))))
(mapconcat 'identity
(delete-dups
(split-string (buffer-string) hxc-eol)) hxc-eol)))

(hxc-msg (format "Build file does not exist:%s" buildFile)))))
hxc-hxml)

(defun hxc-prj-dir ()
(cadr prj-current))

(defun hxc-chomp (str)
"Chomp leading and tailing whitespace from STR."
(while (string-match "\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'"
str)
(setq str (replace-match "" t t str)))
str)

(provide 'hxc-complete)

0 comments on commit dcfb187

Please sign in to comment.