Skip to content

Instantly share code, notes, and snippets.

@douo
Created August 19, 2016 15:59
Show Gist options
  • Save douo/997e0b89056522fbde86c2e4a4903960 to your computer and use it in GitHub Desktop.
Save douo/997e0b89056522fbde86c2e4a4903960 to your computer and use it in GitHub Desktop.
IntelliJ LivePlugin script:将当前单词或下一个单词转换为首字母大写
import com.intellij.openapi.actionSystem.AnActionEvent
import static liveplugin.PluginUtil.*
/**
* 将当前单词或下一个单词转换为首字母大写
*/
registerAction("Capitalize Word", "alt C") { AnActionEvent event ->
runDocumentWriteAction(event.project) {
currentEditorIn(event.project).with {
int offset = caretModel.offset
char[] text = document.getChars();
if (offset < text.length) {
int startOffset = offset;
int endOffset = offset;
if (Character.isLetter(text[offset])) {
while (startOffset > 0 && Character.isLetter(text[startOffset - 1])) {
startOffset--;
}
while(Character.isLetter(text[++endOffset ]) && endOffset < text.length);
} else {
while(!Character.isLetter(text[++startOffset ]) && startOffset < text.length - 1);
endOffset = startOffset;
while (endOffset < text.length - 1 && Character.isLetter(text[++endOffset]));
}
if(endOffset>startOffset) {
String word = Character.toUpperCase(text[startOffset]).toString();
for (int i = startOffset + 1; i < endOffset; i++) {
word += Character.toLowerCase(text[i]);
}
caretModel.moveToOffset(endOffset)
document.replaceString(startOffset, endOffset, word)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment