akicho8 / string-inflection Goto Github PK
View Code? Open in Web Editor NEWunderscore -> UPCASE -> CamelCase conversion of names
underscore -> UPCASE -> CamelCase conversion of names
Hi:
I have seen the code and I wanted to simplify it a little bit the user customization. If you find it is useful you are free to modify it and use in the package's code:
(defcustom tests '(string-inflection-word-p
string-inflection-underscore-p
string-inflection-upcase-p
string-inflection-pascal-case-p
string-inflection-camelcase-p
string-inflection-kebab-case-p
))
(defcustom funcs [
string-inflection-upcase-function
string-inflection-upcase-function
string-inflection-pascal-case-function
string-inflection-camelcase-function
string-inflection-kebab-case-function
string-inflection-capital-underscore-function
string-inflection-underscore-function
])
(defun string-inflection-all-cycle-function (var)
(interactive)
(let ((temp tests)
(it 0))
(while temp
(if (funcall (car temp) var)
(progn
(funcall (aref funcs it) var)
(setq temp nil))
(setq it (1+ it))
(setq temp (cdr temp))
(unless temp
(funcall (aref funcs it) var))))))
This code has 2 main advantages:
This is kind of a feature request,
I have been using this package and I am very happy with it 👍
I would love to convert strings to kebab-case too
emacs_lisp => EMACS_LISP => EmacsLisp => emacsLisp => emacs_lisp => emacs-lisp
Would someone else also be interested in such a thing?
Would you well come a PR with such a feature?
All best & thanks
So far, there is no option to maintain the cursor position where it was before the inflection. You can use string-inflection-skip-backward-when-done
to determine if the cursor will end up at the end of the inflected string or jump to the beginning, but there is no option to make it remain where it was.
My proposal would be to replace string-inflection-skip-backward-when-done
with a config variable string-inflection-final-position
with the following semantics
'remain
→ cursor remains where it was initially'end
→ cursor jumps to the end of the inflected string'beginning
→ cursor jumps to the beginning of the inflected stringUsing (bounds-of-symbol-at-point)
that would also allow us to drop string-inflection-word-chars
and string-inflection-non-word-chars
.
Please drop a short comment whether you are in favor of such a change. If yes I will implement it, if not I will set up a solution in my local config.
To ease the installation, the discoverability of that lib and the management of our init file, it would be much appreciated to have a MELPA recipe.
Do you plan to do it ? Please do !
thanks in advance
their doc: https://github.com/milkypostman/melpa#recipe-format
it looks as simple as creating a one-line pull request.
Commit aac8f03 broke string-inflection-get-current-word
.
I'll submit a PR shortly to fix this.
first of all, awesome package. :)
repeatedly calling the string-inflection-all-cycle
function seems not to wrap around. instead it stays stuck on noInitialCamelCase
after cycling through a few variants first. i tried starting with foo_bar
. am i missing something?
Looking through the docs it seems camel case and lower camel case are mislabeled.
Pascal case is a subset of Camel Case where the first letter is capitalized.
That is, userAccount is a camel case and UserAccount is a Pascal case.
The conventions of using these are different. You use camel case for variables and Pascal case for Class names or Constructors.
It is easy to remember. Pascal is a proper noun so capitalize the first letter.
camel is a common noun, so you do not capitalize the first letter.
TL;DR
lower-camel-case
should be camel-case
camel-case
should be pascal-case
casting string-inflection-underscore on
objectName->method()
turns it into
object_name_>method()
Instead of doing your own forward scanning for words, you should be using the emacs concept of 'symbol'. What emacs calls a symbol basically corresponds to a variable name in all language modes. This has the advantage of behaving properly in all modes (for example string inflection does not work properly in emacs-lisp-mode) and even when using subword mode. It would also make string-inflection behave more consistently with other emacs commands. Currently if point is right after a symbol string-inflection will scan forward for the next word, but this is inconsistent with other emacs commands which would operate on the preceding word.
It would also simplify your code. Instead of trying to scan forward for a word, just call (bounds-of-thing-at-point 'symbol). If it can't find something just return an error.
Not so much of an issue than a comment, or maybe a simple feature request…
I really like the idea and approach of string-inflection
, it's really handy. Now, none of the proposed cycles (Ruby or Java) matches my own needs (which would be: foo_bar => fooBar => foo-bar => foo_bar). Following the source code for string-inflection-XXX-style-cycle
, I ended up writing my own custom cycle function, bound to C-c C-u
:
(defun string-inflection-custom-cycle ()
"foo_bar => fooBar => foo-bar => foo_bar"
(interactive)
(string-inflection-insert
(string-inflection-custom-cycle-function (string-inflection-get-current-word))))
(fset 'string-inflection-cycle 'string-inflection-custom-cycle)
(defun string-inflection-custom-cycle-function (str)
"foo_bar => fooBar => foo-bar => foo_bar"
(cond
((string-inflection-underscore-p str)
(string-inflection-lower-camelcase-function str))
((string-inflection-lower-camelcase-p str)
(string-inflection-kebab-case-function str))
(t
(string-inflection-underscore-function str))))
Would it be possible to have a simpler mechanism to set our own custom cycles?
When editing a python file like
fooBar-barFoo
^---- cursor position
and calling string-inflection-python-style-cycle
the result is
foo_bar_bar_foo
and not the expected
fooBar-bar_foo
Probably the mode aware (symbol-at-point)
function should be used to determine the current word.
Is this intentional?
(string-inflection-underscore-function "ASingleLine")
returns "asingle_line"
. I would have expected it to return "a_single_line"
.
Is this because of acronyms like PHP
? So in this example, it could assume AS
to be an acronym without human knowledge of the context.
Would it be a ridiculous idea to have a dictionary of common acronyms to cover cases like this?
Could you please upload the packade to https://marmalade-repo.org/, or somewhere else, so that it could be more easily installed from within Emacs?
(I love string-inflection, thanks a lot for it.)
To me it seems like there is an assumption baked into this package:
Words contain only ascii characters.
Therefore it breaks for words that have umlauts or similar in them:
string-inflection is mirrored on the Emacsmirror, which is a large up-to-date collection of Emacs packages.
As the maintainer of the mirror I am trying to resolve feature conflicts that result from one package bundling libraries from another package. I suspect in most cases these libraries were included so that users would not have to find, download and install each dependency manually.
Unfortunately bundling also has negative side-effects: if the bundled libraries are also installed separately, then it is undefined which version actually gets loaded when the respective feature is required.
Initially that isn't a big problem but in many cases upstream changes are not included or only after a long delay. This can be very confusing for users who are not aware that some of the installed packages bundle libraries which are also installed separately. In other cases bugs are fixed in the bundled versions but the fixes are never submitted to upstream.
Also now that Emacs contains the package.el package manager there is a better way to not require users to manually deal with dependencies: add the package (and when that hasn't been done yet the dependencies) to the Melpa package repository. If make
is required to install your make you might want to add it to the el-get (another popular package manager) package repository instead.
Alternatively if you want to keep bundling these libraries please move them to a directory only containing bundled libraries and add the file ".nosearch" to that directory. You can then load the library using something like this:
(or (require 'bundled nil t)
(let ((load-path
(cons (expand-file-name "fallback-libs"
(or load-file-name buffer-file-name)
load-path))))
(require 'bundled)))
Of course if your version differs from the upstream version this might not be enough in which case you should make an effort to get your changes merged upstream.
string-inflection bundles at least the following libraries:
Best regards,
Jonas
I used string-inflection-underscore
on some C code and it removed the forward slashes from the multi-line comments. Is this intentional?
Hi again!
If I M-x string-inflection-kebab-case
over:
:fooBar bar
I'll get:
-foo-bar bar
namely, I'll get an unexpected -
prefix.
Does this sound familiar?
Thanks - V
Thanks for the package! I was just thinking what would be the best way to do this when I saw the Emacs haiku in twitter.
I can see a couple of ways to make this package even more comprehensive while keeping it still focused on string infliction. At the moment the inflictions are all strictly related to changing variable names in various programming languages. There are at least two more general cases:
emacs lisp code -> emacs_lisp_code
emacs lisp-code -> emacs_lisp_code
It would be great if you could implement these features!
When point
is at the end of the word and I try to use string-inflection
I get an error code 2
message and the string doesn't change.
Is it possible to make it work when the point
is at the end of the word, this is useful when typing in mini buffer
Once I start typing some word, If I want to change the case I have to go to the beginning of the word. Ideally I want to stay at the current position(which is end here) and still change the case.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.