[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[ease-dev] Code example to comment out a line in an editor
|
Here is a proof-of-concept code example to comment out a line in an active editor. It does not check for error conditions. It relies on yesterday's changes by Christian to install itself and to get the active editor.
Notice that I evaluate the code on the UI thread to avoid a wrong thread exception. Probably only the last couple lines required running on the UI thread though. This works, but is there a better general way to run code on the UI thread so that it does not need to be passed in as a string (which makes it hard to maintain in a js file is JSHint won't check it and it won't be highlighted correctly)? I guess I could use the file commands to load the code from another file, but maybe there is some other approach?
// ********************************************************************************
// name : Comment Out Line EASE Test
// popup : enableFor(java.lang.Object)
// ********************************************************************************
// From: http://codeandme.blogspot.com/2014/08/ease-eclipse-advanced-scripting.html
loadModule('/System/UI');
// Modified from: http://stackoverflow.com/questions/1233102/replace-selected-code-from-eclipse-editor-thru-plugin-command
executeUI("" +
"var editor = getActiveEditor();\n" +
"var dp = editor.getDocumentProvider();\n" +
"var doc = dp.getDocument(editor.getEditorInput());\n" +
"var textSel = editor.getSelectionProvider().getSelection();\n" +
"var newText = '/*' + textSel.getText() + '*/';\n" +
"doc.replace(textSel.getOffset(), textSel.getLength(), newText);\n" +
"");
// End of code example
--Paul Fernhout
http://www.pdfernhout.net/
====
The biggest challenge of the 21st century is the irony of technologies of abundance in the hands of those still thinking in terms of scarcity.