We will write some code to generate the classname from filepath in 1 line of elisp. Then we will use it with yasnippet to autocomplete classname.
Let us try to write the elisp function.
First, we will need to grab hold of the filename.
buffer-file-nameis a permanent local variable which contains the name of the file being visited in the current buffer, or nil if it is not visiting a file.
For example, visiting this file
users_controller.rb in emacs and trying to get the
buffer-file-name variable will return the absolute filepath.
Once we have the filename, all we need to do is some simple text-processing and generate the class name.
- Get the base path.
- Convert the string to the upper camel case.
Now we have an elisp function to get the class name from the file.
(defun camelCaseFileName() (s-upper-camel-case (file-name-base buffer-file-name)))
Using this function we can create a snippet and autocomplete class name.
Full source code
(defun camelCaseFileName() "Get the camel case version of filename. Usefull when generating class name of ruby(or any other lang) file. You can use it with yasnippet." ;; # -*- mode: snippet -*- ;; # name: cls ;; # key: cls ;; # -- ;; class `(camelCaseFileName)` ;; $0 ;; end (s-upper-camel-case (file-name-base buffer-file-name)))