3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

サクラエディタ用スネークケース&キャメルケース変換マクロ

Last updated at Posted at 2016-02-08

はじめに

試しに、テストで投稿してみる。
※2016年4月8日 そもそも動かなかったため、他の対応含めて修正。
2016年7月27日 またも変な動きしてたのに気づかなかったのでとりあえず修正。。。

スネークケース&キャメルケースの変換マクロについてです。
※サクラエディタ用のマクロのため、他のエディタでは使用出来ません。

使い方

  1. 範囲外の場合は選択行の文字、範囲選択されていれば範囲の文字を取得する。
  2. _があればスネークケースに変換。_がなければキャメルケースに変換。→複数行の場合、_があればキャメルケース変換が優先となります。
  3. 変換後の値をエディタに返す。(選択されていれば選択範囲に。選択外の場合は選択行に返す。)
  4. 複数行ある場合でも対応可能とした。→その際は、改行コードは一律CRLFに変換される。
  5. キャメルケースに変換する際、デフォルトを先頭大文字変換とした。
    先頭小文字にしたいときは先頭をタブを入れるかスペース等で可能。
    ただし、Snakeケースに変換掛けた時に先頭にスペースまたはタブが含まれる場合は正しく変換はされないので注意。(先頭タブと文字の間に_が入ってしまう。)

ソース

SnakeAndCamelChange.js
var ParamStrChange = (function () 
{
    function ParamStrChange() 
    {
        this.resultStr = "";
        if (Editor.IsTextSelected() == 0) {
            this.lineStr = Editor.GetLineStr(0);
        }
        else {
            this.lineStr = Editor.GetSelectedString(0);
        }
        if (this.lineStr.length != 0) 
        {
            if (this.lineStr.indexOf("_") == - 1) {
                this.ChangeCamel();
            }
            else {
                this.ChangeToSnake();
            }
        }
        if (Editor.IsTextSelected() == 0) {
            Editor.SelectLine(0);
            Editor.InsText(this.resultStr);
        }
        else {
            Editor.InsText(this.resultStr);
        }
    }
    ParamStrChange.prototype.ChangeToSnake = function () 
    {
        var splitStr = this.lineStr.split("_");
        var strResult = "";
        for (var i = 0; i < splitStr.length; i++) 
        {
            if (splitStr[i] != "" && splitStr[i] != null && typeof splitStr[i] !== "undefined") 
            {
                var head = splitStr[i].substring(0, 1).toUpperCase();
                var inner = splitStr[i].substring(1, (splitStr[i].length)).toLowerCase();
                strResult += head + inner;
            }
        }
        //複数行の場合
        if(strResult.indexOf("\r\n") > -1){
            var list = strResult.split(/[\r\n]+/);
            for(var i=0;i < list.length;i++){
                this.resultStr += list[i].substring(0, 1).toUpperCase() + list[i].substring(1) + "\r\n";
            }
        }else{
            this.resultStr = strResult;
        }
    };
    ParamStrChange.prototype.ChangeCamel = function () 
    {
        var tmp = this.lineStr.substring(0, 1) + this.lineStr.substring(1).replace(/([A-Z])/g, "_$1");
        
        
       if(tmp.indexOf("\r\n") > -1){
            var list = tmp.split(/[\r\n]+/);
            tmp = "";
            //複数行の場合
            for(var i=0;i < list.length;i++){
                if(i==0 || list[i].indexOf("_") != 0){
                    tmp += list[i] + "\r\n";
                }else{
                    tmp += list[i].substring(1) + "\r\n";
                }
            }
       }
        
        this.resultStr = tmp.toUpperCase();
    };
    return ParamStrChange;
})();
new ParamStrChange();

最後に

サクラエディタでマクロ登録方法はとりあえず割愛します。
サクラエディタ上で、適当なショートカットキーに割り当てれば面倒が減ります。
(3年前に作って今日動かしてみて。若干気に入らない部分がそこそこありますがとりあえず放置。)→2016/4/8 気に入らない部分は修正済。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?