changeset 298:3ac7d725ce7d

[add] Feature Request ID 1849297 : Added support for Indenters written in JavaScript. http://universalindent.sf.net/issue/1849297 git-svn-id: svn://svn.code.sf.net/p/universalindent/code/trunk@521 59b1889a-e5ac-428c-b0c7-476e01d41282
author thomas_-_s <thomas_-_s@59b1889a-e5ac-428c-b0c7-476e01d41282>
date Sun, 16 Dec 2007 15:49:52 +0000
parents 09961da1682e
children 0ef58a3ffd34
files UniversalIndentGUI.pro src/UniversalIndentGUI.vcproj src/indenthandler.cpp src/indenthandler.h
diffstat 4 files changed, 65 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/UniversalIndentGUI.pro	Sun Dec 16 15:31:08 2007 +0000
+++ b/UniversalIndentGUI.pro	Sun Dec 16 15:49:52 2007 +0000
@@ -5,6 +5,7 @@
 CONFIG += debug_and_release
 TEMPLATE = app
 QT += network
+QT += script 
 TARGET += 
 DEPENDPATH += resources \
               src \
--- a/src/UniversalIndentGUI.vcproj	Sun Dec 16 15:31:08 2007 +0000
+++ b/src/UniversalIndentGUI.vcproj	Sun Dec 16 15:49:52 2007 +0000
@@ -76,7 +76,7 @@
 			/>
 			<Tool
 				Name="VCLinkerTool"
-				AdditionalDependencies="qtmaind.lib QtGuid4.lib QtCored4.lib QtNetworkd4.lib qscintilla2.lib"
+				AdditionalDependencies="qtmaind.lib QtGuid4.lib QtCored4.lib QtNetworkd4.lib QtScriptd4.lib qscintilla2.lib"
 				LinkIncremental="2"
 				IgnoreAllDefaultLibraries="false"
 				GenerateDebugInformation="true"
@@ -164,7 +164,7 @@
 			/>
 			<Tool
 				Name="VCLinkerTool"
-				AdditionalDependencies="qtmain.lib QtGui4.lib QtCore4.lib QtNetwork4.lib"
+				AdditionalDependencies="qtmain.lib QtGui4.lib QtCore4.lib QtNetwork4.lib QtScript4.lib qscintilla2.lib"
 				LinkIncremental="1"
 				GenerateDebugInformation="true"
 				SubSystem="2"
--- a/src/indenthandler.cpp	Sun Dec 16 15:31:08 2007 +0000
+++ b/src/indenthandler.cpp	Sun Dec 16 15:49:52 2007 +0000
@@ -83,6 +83,7 @@
     errorMessageDialog = new UiGuiErrorMessage(mainWindow);
 
     indenterExecutableCallString = "";
+    indenterExecutableSuffix = "";
     createIndenterCallString();
 }
 
@@ -187,12 +188,52 @@
 
 
 /*!
-   \brief Format \a sourceCode by calling the indenter. 
+    \brief Format \a sourceCode by calling the indenter. 
    
-   The \a inputFileExtension has to be given as parameter so the called indenter 
-   can identify the programming language if needed.
+    The \a inputFileExtension has to be given as parameter so the called indenter 
+    can identify the programming language if needed.
  */
 QString IndentHandler::callIndenter(QString sourceCode, QString inputFileExtension) {
+    if ( indenterExecutableSuffix == ".js" ) {
+        return callJavaScriptIndenter(sourceCode, inputFileExtension);
+    } 
+    else {
+        return callExecutableIndenter(sourceCode, inputFileExtension);
+    }
+}
+
+
+/*!
+    \brief Format \a sourceCode by calling the interpreted JavaScript code of the indenter. 
+
+    The \a inputFileExtension has to be given as parameter so the called indenter 
+    can identify the programming language if needed.
+*/
+QString IndentHandler::callJavaScriptIndenter(QString sourceCode, QString inputFileExtension) {
+    QScriptEngine engine;
+
+    QScriptValue unformattedCode(&engine, sourceCode);
+    engine.globalObject().setProperty("unformattedCode", unformattedCode);
+
+    QFile jsDecoderFile( indenterExecutableCallString );
+    QString jsDecoderCode;
+    if (jsDecoderFile.open(QFile::ReadOnly)) {
+        jsDecoderCode = jsDecoderFile.readAll();
+    }
+    jsDecoderFile.close();
+
+    QScriptValue value = engine.evaluate(jsDecoderCode);
+    return value.toString();
+}
+
+
+/*!
+    \brief Format \a sourceCode by calling the binary exeutable of the indenter. 
+
+    The \a inputFileExtension has to be given as parameter so the called indenter 
+    can identify the programming language if needed.
+*/
+QString IndentHandler::callExecutableIndenter(QString sourceCode, QString inputFileExtension) {
     Q_ASSERT_X( !inputFileName.isEmpty(), "callIndenter", "inputFileName is empty" );
 //    Q_ASSERT_X( !outputFileName.isEmpty(), "callIndenter", "outputFileName is empty" );
     Q_ASSERT_X( !indenterFileName.isEmpty(), "callIndenter", "indenterFileName is empty" );
@@ -968,6 +1009,7 @@
     indentProcess.setWorkingDirectory( QFileInfo(dataDirctoryStr).absoluteFilePath() );
 
     foreach ( QString suffix, QStringList() << "" << ".exe" << ".bat" << ".com" << ".sh" ) {
+        indenterExecutableSuffix = suffix;
         indenterExecutableCallString = "\"" + QFileInfo(dataDirctoryStr).absoluteFilePath() + "/" + indenterFileName;
         indenterExecutableCallString += suffix + "\"";
         indentProcess.start( indenterExecutableCallString + " " + indenterShowHelpParameter );
@@ -980,9 +1022,20 @@
     }
 
 
+    // If unsuccessful try if the indenter executable is a JavaScript file
+    // -------------------------------------------------------------------
+    indenterExecutableSuffix = ".js";
+    indenterExecutableCallString = QFileInfo(dataDirctoryStr).absoluteFilePath() + "/" + indenterFileName;
+    indenterExecutableCallString += indenterExecutableSuffix;
+    if ( QFile::exists(indenterExecutableCallString) ) {
+        return true;
+    }
+
+
     // If unsuccessful try to call the indenter global, using some suffix
     // ------------------------------------------------------------------
     foreach ( QString suffix, QStringList() << "" << ".exe" << ".bat" << ".com" << ".sh" ) {
+        indenterExecutableSuffix = suffix;
         indenterExecutableCallString = indenterFileName + suffix;
         indentProcess.start( indenterExecutableCallString + " " + indenterShowHelpParameter );
         if ( indentProcess.waitForFinished(2000) ) {
@@ -999,6 +1052,7 @@
     indenterExecutableCallString = "\"" + QFileInfo(dataDirctoryStr).absoluteFilePath() + "/" + indenterFileName;
 
     foreach ( QString suffix, QStringList() << ".exe" << ".com" ) {
+        indenterExecutableSuffix = suffix;
         if ( QFile::exists(dataDirctoryStr + indenterFileName + suffix) ) {
             QProcess wineTestProcess;
             wineTestProcess.start("wine --version");
@@ -1019,5 +1073,6 @@
     }
 
     indenterExecutableCallString = "";
+    indenterExecutableSuffix = "";
     return false;
 }
--- a/src/indenthandler.h	Sun Dec 16 15:31:08 2007 +0000
+++ b/src/indenthandler.h	Sun Dec 16 15:49:52 2007 +0000
@@ -39,6 +39,7 @@
 #include <QMainWindow>
 #include <QTextStream>
 #include <QTextCodec>
+#include <QtScript\QtScript> 
 
 #include "uiguierrormessage.h"
 
@@ -60,6 +61,8 @@
     QString getIndenterCfgFile();
 
 private:
+    QString callExecutableIndenter(QString sourceCode, QString inputFileExtension);
+    QString callJavaScriptIndenter(QString sourceCode, QString inputFileExtension);
     void writeConfigFile(QString parameterString);
     void readIndentIniFile(QString iniFilePath);
     bool createIndenterCallString();
@@ -135,6 +138,7 @@
     QMainWindow *mainWindow;
     UiGuiErrorMessage *errorMessageDialog;
     QString indenterExecutableCallString;
+    QString indenterExecutableSuffix;
 
 public slots:
     void setIndenter(int indenterID);