From 089d9ccdce530ebd08dfc4681833cefeff4a7eb9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Morn=C3=A9=20Chamberlain?= <thefreeman.za@gmail.com>
Date: Sat, 24 Nov 2012 20:19:35 +0200
Subject: SublimeText 2 Gen: Set the sublimeclang_options_script property.

The sublimeclang_options_script property is now set in the project
file. It is set to execute a python script that reads the JSON options
file to get options per source file. Python must be installed and in the
path for this feature to work from Sublime Text.
---
 Source/cmExtraSublimeTextGenerator.cxx | 29 ++++++++++++++++++++++++++++-
 Source/cmExtraSublimeTextGenerator.h   |  1 +
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/Source/cmExtraSublimeTextGenerator.cxx b/Source/cmExtraSublimeTextGenerator.cxx
index 03e2e1d..b174125 100644
--- a/Source/cmExtraSublimeTextGenerator.cxx
+++ b/Source/cmExtraSublimeTextGenerator.cxx
@@ -161,7 +161,11 @@ void cmExtraSublimeTextGenerator
   std::string projectName = mf->GetProjectName();
   std::string sublimeClangOptionsFilename = outputDir+"/";
   sublimeClangOptionsFilename += projectName + ".sublimeclang-options";
-  fout << "\t\"sublimeclang_options_file\": \"" << sublimeClangOptionsFilename
+  std::string sublimeClangOptionsScriptFilename = outputDir + "/"
+    + projectName + "_sublimeclang_options_script.py";
+  fout << "\t\"sublimeclang_options_script\": \"python "
+       << sublimeClangOptionsScriptFilename << " "
+       << sublimeClangOptionsFilename
        << "\"\n\t";
   // End of the settings section
   fout << "}\n";
@@ -171,9 +175,31 @@ void cmExtraSublimeTextGenerator
 
   this->WriteSublimeClangOptionsFile(sourceFileFlags,
     sublimeClangOptionsFilename);
+  this->WriteSublimeClangOptionsScript(sublimeClangOptionsScriptFilename);
 }
 
 void cmExtraSublimeTextGenerator::
+  WriteSublimeClangOptionsScript(const std::string& filename)
+{
+  cmGeneratedFileStream fout(filename.c_str());
+  if(!fout)
+    {
+    return;
+    }
+  fout << "import json\n";
+  fout << "import sys\n\n\n";
+  fout << "if len(sys.argv) < 2:\n";
+  fout << "    sys.exit(1)\n";
+  fout << "data = None\n";
+  fout << "with open(sys.argv[1]) as f:\n";
+  fout << "    data = json.load(f)\n";
+  fout << "if data is not None:\n";
+  fout << "    for arg in data.get(sys.argv[2], []):\n";
+  fout << "        print arg\n";
+}
+
+
+void cmExtraSublimeTextGenerator::
   WriteSublimeClangOptionsFile(const MapSourceFileFlags& sourceFileFlags,
                                const std::string& filename)
 {
@@ -351,6 +377,7 @@ void cmExtraSublimeTextGenerator::
             this->ComputeDefines(*iter, lg, target, gtgt);
           flags.clear();
           cmsys::RegularExpression flagRegex;
+          // Regular expression to extract compiler flags from a string
           // https://gist.github.com/3944250
           const char* regexString =
             "(^|[ ])-[DIOUWfgs][^= ]+(=\\\"[^\"]+\\\"|=[^\"][^ ]+)?";
diff --git a/Source/cmExtraSublimeTextGenerator.h b/Source/cmExtraSublimeTextGenerator.h
index 26e09a6..8c0c1f3 100644
--- a/Source/cmExtraSublimeTextGenerator.h
+++ b/Source/cmExtraSublimeTextGenerator.h
@@ -48,6 +48,7 @@ private:
 
   void CreateNewProjectFile(const std::vector<cmLocalGenerator*>& lgs,
                                 const std::string& filename);
+  void WriteSublimeClangOptionsScript(const std::string& filename);
   void WriteSublimeClangOptionsFile(const MapSourceFileFlags& sourceFileFlags,
                                     const std::string& filename);
   /** Appends all targets as build systems to the project file and get all
-- 
cgit v0.12