📜 ⬆️ ⬇️

Aptana - creating plugins

Recently, I have chosen Aptana Studio Community Edition as the main working editor. The editor is very good (a detailed overview of its features is a topic for a separate post) and pleases with its free.
So, Aptana supports Eclipse Monkey plug-ins, which you can create yourself! How to do it? Now I will tell on the example of creating a plugin for compressing CSS files.

First we need to create in any project (for scripts I created a separate project) the folder “Monkey” or “Scripts”. Then we create a new file in the JavaScript folder and open it.

At the very beginning we write the following:
/*
* Menu: CSS > Compact
* DOM: download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
* DOM: localhost/com.aptana.ide.scripting
*/


This comment describes how your plugin will be displayed on the panel and in the Scripts menu. In this case, the plugin will fall into a section of CSS called Compact.

We create the main function inside this function and all actions on the edited code will be described:
function main(){
}


First of all, we need to make sure that there is an active editor, for this we write the following to the main function:
')
var sourceEditor = editors.activeEditor;
if (sourceEditor === undefined)
{
alert(" !");
}


Create a sourceEditor variable, which we equate to editros.activeEditor (active editor). Then we check if there is an active editor, and if there is no one, we display a warning.
The next step we need to make sure that the file being edited is a CSS file. To do this, create the following function:

function getLanguage()
{
var result = "";

try
{
result = editors.activeEditor.textEditor.getFileContext().getDefaultLanguage();
}
catch(e)
{
}

return result;
}


This function will return the file type to us in the active editor. To call it, we add our if in the main function:

else if (getLanguage() != "text/css")
{
alert(" CSS-!");
}


Making sure that there is an active editor and the file in it is CSS, you can proceed directly to work on the code. To do this, add to our if:

else
{
var comString = sourceEditor.source; // comString

var reg = /\t/g; //
var comString = comString.replace(reg,''); //

var reg = /\/\*.*\*\//g; //
var comString = comString.replace(reg,''); //

var reg = /:\s*/g; //
var comString = comString.replace(reg,':'); // display: none;

var reg = /\r\n\r\n/g; //
var comString = comString.replace(reg,''); //

// ,
sourceEditor.applyEdit(0, sourceEditor.sourceLength, comString);
}


That's all. If you save this plugin and restart Aptana, you will be able to find the Compact entry in the Scripts> CSS menu, which you can compress your CSS file.

Thus, performing various actions on the comString variable, you can create a plugin that you need!

Final result:
/*
* Menu: CSS > Compact
* DOM: download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
* DOM: localhost/com.aptana.ide.scripting
*/

function main()
{
var sourceEditor = editors.activeEditor;

if (sourceEditor === undefined)
{
alert( " !" );
}
else if (getLanguage() != "text/css" )
{
alert( " CSS-!" );
}
else
{
var comString = sourceEditor.source;

var reg = /\t/g;
var comString = comString.replace(reg, '' );

var reg = /\/\*.*\*\ //g;
var comString = comString.replace(reg, '' );

var reg = /:\s*/g;
var comString = comString.replace(reg, ':' );

var reg = /{\s*/g;
var comString = comString.replace(reg, '{' );

var reg = /}\s*/g;
var comString = comString.replace(reg, '}' );

var reg = /\s*{/g;
var comString = comString.replace(reg, '{' );

var reg = /;\s*/g;
var comString = comString.replace(reg, ';' );

var reg = /\r\n/g;
var comString = comString.replace(reg, '' );

sourceEditor.applyEdit(0, sourceEditor.sourceLength, comString);
}
}

function getLanguage()
{
var result = "" ;

try
{
result = editors.activeEditor.textEditor.getFileContext().getDefaultLanguage();
}
catch (e)
{
}

return result;
}


* This source code was highlighted with Source Code Highlighter .


PS The example given in this article does not claim to be 100% compressor CSS files!

Source: https://habr.com/ru/post/50199/


All Articles