/* (Tab=4) [概要] カーソル位置の単語を Pascal形式(PascalCase) ⇔ アンダースコア区切りの大文字 に変換します。(トグル動作をします。) 変数名構成の単語(英字/"_" で始まり、英数字/"_" が続く)を対象とします。 例: "AbcDef" → "ABC_DEF" → "AbcDef" ... "_ABC_DEF_" → "_AbcDef_" → "_ABC_DEF_" (前後のアンダースコアは維持) "nWINDOW_WIDTH" → "nWindowWidth" (先頭の小文字は維持) → "N_WINDOW_WIDTH" → "NWindowWidth" "3AbcDef" → "3AbcDef" (変数名構成語ではないので対象外) 変換後、その単語を選択状態にします。 範囲選択には対応していません。範囲選択されている場合、範囲選択開始位置の 単語を対象とします。 [変更履歴] 2009/04/24 新規 */ //-------------------------------------- // 大文字("_" 区切り)への変換 // 大文字→大文字→小文字と続く場合、1つ目の大文字と2つ目の大文字の間に "_" を挿入するか。 // 例:元の文字列 | false | true // "getRGBValue" | "GET_RGBVALUE"| "GET_RGB_VALUE" // "EUCToSJIS" | "EUCTO_SJIS" | "EUC_TO_SJIS" // "EUCtoSJIS" | "EUCTO_SJIS" | "EU_CTO_SJIS" var bTO_UPCASE_InsertUnderscoreBetweenUpperAndUpperlower = true; // 数字→大文字の切り替わりで "_" を挿入するか。 // 例:元の文字列 | false | true // "UTF8ToSJIS" | "UTF8TO_SJIS" | "UTF8_TO_SJIS" // "D3Dcolor" | "D3DCOLOR" | "D3_DCOLOR" var bTO_UPCASE_InsertUnderscoreBetweenDigitAndUpper = false; //-------------------------------------- // Pascal形式への変換 // Pascal形式(Upper CamelCase)ではなくCamel形式(Lower CamelCase)にするか。 // 例:元の文字列 | false | true // "GET_RGB_VALUE" | "GetRgbValue" | "getRgbValue" var bTO_PASCALCASE_UseCamelCase = false; //-------------- // メイン Main(); function Main() { var selstr; // 選択範囲の文字列。 var newstr; // 変換後の文字列。 var re; var nLowerChars; var bToUpcase; // エディタが「読み取り専用」なら処理終了。 if( Editor.ExpandParameter("${R?読専$:$:$}") == "読専" ) return; // 範囲選択中の場合、範囲選択開始位置の単語を拾う。 if( Editor.IsTextSelected() > 0 ){ Editor.Left(); if( Editor.IsTextSelected() > 0 ){ // 範囲選択開始(F6) または 矩形範囲選択開始(Shift+F6) 中だった(とみなす)。 Editor.Right(); } } // カーソル位置の単語を選択。 Editor.SelectWord(); selstr = Editor.GetSelectedString(0); // 変数名構成の単語でなければ処理終了。 if( selstr.search( /^_*[A-Z][0-9A-Z_]*/ig ) == -1 ) return; // 大文字("_" 区切り)にするか、PascalCase にするか。 // 単語内で、小文字が文字数の10%以上なら大文字にする、とする。 re = selstr.match( /[a-z]/g ); nLowerChars = (re == null) ? 0 : re.length; bToUpcase = (nLowerChars >= 0.1 * selstr.length) ? true : false; newstr = ""; if( bToUpcase ){ // PascalCase → 大文字("_" 区切り) にする。 var DIGIT = 0x1; // 文字のタイプ:数字 var UPPER = 0x2; // 文字のタイプ:英大文字 var LOWER = 0x4; // 文字のタイプ:英小文字 var i; var c; var t; // 現在 の文字($0[i] )のタイプ var t_1 = 0; // 1文字前の文字($0[i-1])のタイプ var t_2 = 0; // 2文字前の文字($0[i-2])のタイプ for(i=0; i < selstr.length; i++){ c = selstr.charAt( i ); if( (c >= "0") && (c <= "9") ) t = DIGIT; else if( (c >= "A") && (c <= "Z") ) t = UPPER; else if( (c >= "a") && (c <= "z") ) t = LOWER; else t = 0; // 小文字→大文字か。 if( (t_1 & LOWER) && (t & UPPER) ){ newstr += "_"; } // 数字→大文字か。 else if( (bTO_UPCASE_InsertUnderscoreBetweenDigitAndUpper) && ((t_1 & DIGIT) && (t & UPPER)) ){ newstr += "_"; } // 大文字→大文字→小文字と続くか。 else if( (bTO_UPCASE_InsertUnderscoreBetweenUpperAndUpperlower) && ((t_2 & UPPER) && (t_1 & UPPER) && (t & LOWER)) ){ newstr = newstr.slice( 0, -1 ) + "_" + newstr.slice( -1 ); } newstr += c.toUpperCase(); t_2 = t_1; t_1 = t; } }else{ // 大文字("_" 区切り) → PascalCase または CamelCase にする。 var s; var bFirst = true; selstr.match( /^([a-z]*)(.*)/ ); newstr = RegExp.$1; // 先頭部分が小文字ならそのまま使用。 s = RegExp.$2; newstr += s.replace( /([A-Z]+)/ig, function($0,$1) { if( (bTO_PASCALCASE_UseCamelCase) && (bFirst) ){ bFirst = false; return $1.toLowerCase(); } return $1.substr( 0, 1 ).toUpperCase() + $1.substr( 1 ).toLowerCase(); } ); // アンダースコアを削除。(先頭と末尾のアンダースコアは残す。) newstr = newstr.replace( /(_*[A-Z])((?:[0-9A-Z]*_+)+)([0-9A-Z]+_*)/i, function($0,$1,$2,$3) { return $1 + $2.replace( /_/g, "" ) + $3; } ); } if( newstr != selstr ){ Editor.InsText( newstr ); // 再び単語を選択。 Editor.Left(); Editor.SelectWord(); } Editor.ReDraw(); // S_InsText() の後は再描画が必要。 }