Created
February 13, 2013 22:20
-
-
Save xhanin/4948901 to your computer and use it in GitHub Desktop.
ISO DateTime / Epoch timestamp converter plugin for IntellijEval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.intellij.openapi.actionSystem.AnActionEvent | |
import com.intellij.openapi.editor.SelectionModel | |
import org.joda.time.* | |
import static intellijeval.PluginUtil.* | |
// add-to-classpath $HOME/.m2/repository/joda-time/joda-time/2.1/joda-time-2.1.jar | |
registerAction("TransformDateISOEpoch", "ctrl alt shift T") { AnActionEvent event -> | |
def editor = currentEditorIn(event.project) | |
runDocumentWriteAction(event.project) { | |
SelectionModel selectionModel = editor.selectionModel | |
String selectedText = selectionModel.selectedText | |
if (selectedText == null) { | |
// try to auto expand selection to full iso date or epoch timestamp | |
def offset = editor.caretModel.offset | |
def start = offset | |
while (start > 0 && editor.document.text[start - 1] ==~ /[\dT\-\.\+:]/) { | |
start-- | |
} | |
def end = offset | |
while (end < editor.document.text.length() | |
&& editor.document.text[end] ==~ /[\dT\-\.\+:]/) { | |
end++ | |
} | |
selectionModel.setSelection(start, end) | |
} | |
transformSelectionIn(editor, { String s -> | |
try { | |
if (s ==~ /\d+/) { | |
// this is probably an epoch timestamp | |
return new DateTime(Long.valueOf(s)).toString() | |
} else { | |
// it's supposed to be an ISO date | |
return String.valueOf(DateTime.parse(s).millis) | |
} | |
} catch (Exception e) { | |
// if called with unsupported selection | |
show("unsupported selection $s : $e.message") | |
return s | |
} | |
} | |
) | |
} | |
} | |
show("Loaded 'TransformDateISOEpoch' | |
Select text in editor (either " | |
+ "an epoch timestamp or an ISO date) and press ctrl+shift+alt+T to run it") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment